Skip to content

Commit

Permalink
fix: Misc responsive stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
kiosion committed Oct 17, 2023
1 parent 912fdbf commit cdf411a
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 53 deletions.
26 changes: 22 additions & 4 deletions svelte-app/src/components/document/content/common/header.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@
<div class="flex flex-col">
<svelte:component this={model === 'post' ? PostHeader : ProjectHeader}>
<svelte:fragment slot="title">
<h1
class="mb-4 mt-8 h-fit w-fit font-display text-4xl font-bold text-black transition-[color] dark:text-white lg:mt-10 lg:text-6xl lg:font-black"
bind:contentRect={titleRect}
>
<h1 bind:contentRect={titleRect}>
{data.title}
</h1>
</svelte:fragment>
Expand Down Expand Up @@ -75,3 +72,24 @@
</svelte:component>
</div>
</div>

<style lang="scss">
@import '@styles/mixins';
h1 {
@apply mb-4 mt-8 h-fit max-w-full font-display text-4xl font-bold text-black transition-[color];
overflow-wrap: break-word;
word-break: break-word;
@include media(lg) {
@apply mt-10 text-6xl font-black;
}
}
:global(.dark) {
h1 {
@apply text-white;
}
}
</style>
3 changes: 1 addition & 2 deletions svelte-app/src/components/layouts/scroll-container.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@
beforeNavigate(() => {
summaryVisible.set(false);
visibleHeadings.set(new Set());
});
afterNavigate(() => {
if (timeout) {
clearTimeout(timeout);
}
visibleHeadings.set(new Set());
timeout = setTimeout(() => {
element.scrollTo({
top: 0,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
<script lang="ts">
import { getContext, onMount } from 'svelte';
import { get } from 'svelte/store';
import { getContext, onDestroy, onMount } from 'svelte';
import { afterNavigate } from '$app/navigation';
import Logger from '$lib/logger';
import { visibleHeadings } from '$lib/summary';
import { headingElements, visibleHeadings } from '$lib/summary';
import Icon from '$components/icon.svelte';
Expand All @@ -17,22 +15,27 @@
const scrollContainer = (getContext('getScrollContainer') as () => HTMLDivElement)();
onMount(() => {
headingElements.update((state) => {
state.add(element);
return state;
});
observer = new IntersectionObserver(
(entries) => {
const key = value._key;
if (!key?.length) {
if (!value._key?.length) {
Logger.warn("Heading is missing '_key' property");
return;
}
visibleHeadings.update((state) => {
if (entries[0]?.isIntersecting) {
return state.add(key);
state.add(value._key!);
return state;
} else {
state.delete(key);
state.delete(value._key!);
return state;
}
});
get(visibleHeadings);
},
{
rootMargin: '-250px 0px 0px 0px',
Expand All @@ -43,8 +46,12 @@
observer.observe(element);
});
afterNavigate(() => {
onDestroy(() => {
observer?.disconnect();
headingElements.update((state) => {
state.delete(element);
return state;
});
});
$: ({ value, indexInParent } = portableText);
Expand Down
71 changes: 36 additions & 35 deletions svelte-app/src/lib/summary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { derived, writable } from 'svelte/store';
import { page } from '$app/stores';
import { isDesktop } from '$lib/helpers/responsive';

import type { Page } from '@sveltejs/kit';
// import type { Page } from '@sveltejs/kit';
import type { DocumentHeadings } from '$types';

const recursiveFlattenHeadings = (headings: DocumentHeadings[]): DocumentHeadings[] => {
Expand All @@ -16,45 +16,45 @@ const recursiveFlattenHeadings = (headings: DocumentHeadings[]): DocumentHeading

let previousFirstVisibleHeading: string | undefined;

const findFirstVisibleHeading = ([visibleHeadings, summaryContents, page]: [
Set<string>,
DocumentHeadings[] | undefined,
Page<Record<string, string>>
]) => {
if (!summaryContents?.length || !visibleHeadings?.size) {
return previousFirstVisibleHeading;
}

const flatSummaryContents = recursiveFlattenHeadings(summaryContents),
pageHash = page.url.hash,
pageHashHeading = flatSummaryContents.find((heading) => {
return heading.key === pageHash.slice(1);
});

let firstVisibleHeading = flatSummaryContents.find((heading) => {
return visibleHeadings.has(heading.key);
});

if (pageHashHeading && visibleHeadings.has(pageHashHeading?.key)) {
const pageHashHeadingIndex = flatSummaryContents.indexOf(pageHashHeading),
firstVisibleHeadingIndex = firstVisibleHeading
? flatSummaryContents.indexOf(firstVisibleHeading)
: -1;
if (pageHashHeadingIndex > firstVisibleHeadingIndex) {
firstVisibleHeading = pageHashHeading;
}
}

firstVisibleHeading?.key && (previousFirstVisibleHeading = firstVisibleHeading.key);
return firstVisibleHeading?.key ?? previousFirstVisibleHeading;
};

const summaryVisible = writable(false),
summaryContents = writable<DocumentHeadings[] | undefined>(undefined),
headingElements = writable<Set<HTMLElement>>(new Set()),
visibleHeadings = writable<Set<string>>(new Set()),
firstVisibleHeading = derived(
[visibleHeadings, summaryContents, page],
findFirstVisibleHeading
([_visibleHeadings, _summaryContents, _page]) => {
// console.log({
// _visibleHeadings,
// _summaryContents,
// _page
// });
if (!_summaryContents?.length || !_visibleHeadings?.size) {
return previousFirstVisibleHeading;
}

const flatSummaryContents = recursiveFlattenHeadings(_summaryContents),
pageHash = _page.url.hash,
pageHashHeading = flatSummaryContents.find((heading) => {
return heading.key === pageHash.slice(1);
});

let firstVisibleHeading = flatSummaryContents.find((heading) => {
return _visibleHeadings.has(heading.key);
});

if (pageHashHeading && _visibleHeadings.has(pageHashHeading?.key)) {
const pageHashHeadingIndex = flatSummaryContents.indexOf(pageHashHeading),
firstVisibleHeadingIndex = firstVisibleHeading
? flatSummaryContents.indexOf(firstVisibleHeading)
: -1;
if (pageHashHeadingIndex > firstVisibleHeadingIndex) {
firstVisibleHeading = pageHashHeading;
}
}

firstVisibleHeading?.key && (previousFirstVisibleHeading = firstVisibleHeading.key);
return firstVisibleHeading?.key ?? previousFirstVisibleHeading;
}
),
summaryOffset = writable(0),
shouldShowSummary = derived(
Expand All @@ -66,6 +66,7 @@ const summaryVisible = writable(false),

export {
firstVisibleHeading,
headingElements,
shouldShowSummary,
summaryContents,
summaryOffset,
Expand Down
6 changes: 4 additions & 2 deletions svelte-app/src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@
}
span:not(.shim) {
@apply absolute left-1/2 top-0 z-50 -mt-10 -translate-x-1/2 cursor-pointer rounded-md bg-light px-4 py-2 text-sm font-bold text-dark transition-[margin-top,background-color,color];
@apply absolute left-1/2 top-0 z-50 -mt-14 -translate-x-1/2 cursor-pointer rounded-md bg-light px-4 py-2 text-sm font-bold text-dark transition-[margin-top,background-color,color];
@include focus-state(sm);
Expand All @@ -200,7 +200,9 @@
}
div {
@apply relative mt-14 w-full;
@apply relative mt-14;
width: calc(100% - 2rem);
}
}
Expand Down

0 comments on commit cdf411a

Please sign in to comment.