Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TOC on Desktop & Mobile #1129

Merged
merged 27 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
a7bf5a2
update: hide toc on desktops.
ItzNotABug Jul 13, 2024
118731f
add: toggle class and bind value.
ItzNotABug Jul 13, 2024
992290c
Merge branch 'main' into fix-toc-showing-on-desktop
ItzNotABug Jul 16, 2024
68f001a
fix: `TOC` nav on mobile.
ItzNotABug Jul 17, 2024
9583384
fix: collapse `toc` on mobile.
ItzNotABug Jul 17, 2024
a930a13
Merge branch 'main' into fix-toc-showing-on-desktop
ItzNotABug Jul 17, 2024
5f2aecc
Merge branch 'main' into fix-toc-showing-on-desktop
ItzNotABug Jul 18, 2024
c1f04bc
push for review.
ItzNotABug Aug 1, 2024
3bb83da
Merge branch 'main' into fix-toc-showing-on-desktop
ItzNotABug Aug 1, 2024
4294f91
Merge branch 'main' into fix-toc-showing-on-desktop
ItzNotABug Aug 5, 2024
ae2c7ef
Merge branch 'main' into fix-toc-showing-on-desktop
ItzNotABug Aug 12, 2024
e7450fe
Merge branch 'main' into fix-toc-showing-on-desktop
ItzNotABug Aug 15, 2024
5fbcb01
Merge branch 'main' into fix-toc-showing-on-desktop
ItzNotABug Aug 23, 2024
a1bfc64
Merge branch 'main' into `fix-toc-showing-on-desktop`.
ItzNotABug Nov 10, 2024
e356c3a
fix: toc on pages for mobile and desktops.
ItzNotABug Nov 10, 2024
c777387
improve: animations, misc.
ItzNotABug Nov 11, 2024
1271c06
Merge branch 'main' into fix-toc-showing-on-desktop
ItzNotABug Nov 12, 2024
e5d84dd
Merge branch 'main' into fix-toc-showing-on-desktop
ItzNotABug Nov 25, 2024
ff7d0cf
update: address comments.
ItzNotABug Nov 25, 2024
bdf4a4d
remove: class def.
ItzNotABug Nov 25, 2024
9c2d7c0
Merge branch 'main' into fix-toc-showing-on-desktop
ItzNotABug Nov 25, 2024
3ed6b2b
Merge branch 'main' into fix-toc-showing-on-desktop
ItzNotABug Nov 26, 2024
c735369
Merge branch 'main' into fix-toc-showing-on-desktop
ItzNotABug Nov 26, 2024
a0f6351
fix: clicks and visibility across devices.
ItzNotABug Nov 27, 2024
8739130
ran: formatter.
ItzNotABug Nov 27, 2024
6679499
Merge branch 'main' into fix-toc-showing-on-desktop
ItzNotABug Nov 28, 2024
7007fb8
Merge branch 'main' into fix-toc-showing-on-desktop
ItzNotABug Nov 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions src/lib/components/Newsletter.svelte
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<script context="module" lang="ts">
import { PUBLIC_GROWTH_ENDPOINT } from '$env/static/public';
import { PUBLIC_GROWTH_ENDPOINT } from '$env/static/public';

export async function newsletter(name: string, email: string) {
const response = await fetch(`${PUBLIC_GROWTH_ENDPOINT}/newsletter/subscribe`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
name,
email
})
});
return response;
}
export async function newsletter(name: string, email: string) {
const response = await fetch(`${PUBLIC_GROWTH_ENDPOINT}/newsletter/subscribe`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
name,
email
})
});
return response;
}
</script>

<script lang="ts">
Expand Down
296 changes: 209 additions & 87 deletions src/lib/components/TocNav.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<script lang="ts">
import { getTocCtx } from './TocRoot.svelte';
import TocTree from './TocTree.svelte';
import { cubicOut } from 'svelte/easing';
import { getTocCtx } from './TocRoot.svelte';
import { onMount } from 'svelte';

export let showToc = true;

Expand All @@ -12,94 +14,214 @@
} = getTocCtx();

$: progress = Math.max(...$activeHeadingIdxs) / ($headingsTree.length - 1);

function slideFade(
node: HTMLElement,
{
delay = 0,
duration = 400,
easing = cubicOut
}: { delay?: number; duration?: number; easing?: (t: number) => number } = {}
) {
const initialHeight = node.offsetHeight;

return {
delay,
duration,
easing,
css: (t: number) => {
return `
opacity: ${t};
height: ${t * initialHeight}px;
overflow: hidden;
`;
}
};
}
Comment on lines +18 to +40
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also do this animation with CSS? I think that would better fit the direction we're moving in. Wdyt @thejessewinton?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if we probably can (due to t * initialHeight, but correct me if I am wrong), but I think I've seen a similar use-case somewhere around the repo. Lmk what we decide.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


// show toc by default on desktop
function handleResizeForTocTree() {
showToc = window.innerWidth >= 1024;
}

onMount(handleResizeForTocTree);
</script>

<aside class="web-grid-120-1fr-auto-side" class:web-is-mobile-closed={!showToc}>
<div class="web-page-steps">
<div
class="web-page-steps-location web-is-not-mobile"
style="--location:{progress * 100}%;"
<svelte:window on:resize={handleResizeForTocTree} />

<section class="web-mobile-header">
<div class="web-is-only-mobile">
<button
on:click={() => (showToc = !showToc)}
class="flex w-full items-center justify-between"
>
<span class="web-page-steps-location-button">
<svg
xmlns="http://www.w3.org/2000/svg"
width="16"
height="16"
viewBox="0 0 16 16"
fill="none"
>
<g clip-path="url(#clip0_1684_10747)">
<g filter="url(#filter0_b_1684_10747)">
<circle
cx="8"
cy="8"
r="8"
fill="url(#paint0_linear_1684_10747)"
fill-opacity="0.32"
/>
<circle
cx="8"
cy="8"
r="7.75"
stroke="url(#paint1_linear_1684_10747)"
stroke-width="0.5"
/>
</g>
<circle cx="8" cy="7.99219" r="3" fill="white" />
</g>
<defs>
<filter
id="filter0_b_1684_10747"
x="-200"
y="-200"
width="416"
height="416"
filterUnits="userSpaceOnUse"
color-interpolation-filters="sRGB"
>
<feFlood flood-opacity="0" result="BackgroundImageFix" />
<feGaussianBlur in="BackgroundImageFix" stdDeviation="100" />
<feComposite
in2="SourceAlpha"
operator="in"
result="effect1_backgroundBlur_1684_10747"
/>
<feBlend
mode="normal"
in="SourceGraphic"
in2="effect1_backgroundBlur_1684_10747"
result="shape"
/>
</filter>
<linearGradient
id="paint0_linear_1684_10747"
x1="2.02105"
y1="1.10843"
x2="16.3872"
y2="17.2901"
gradientUnits="userSpaceOnUse"
>
<stop stop-color="white" stop-opacity="0.4" />
<stop offset="1" stop-color="white" stop-opacity="0" />
</linearGradient>
<linearGradient
id="paint1_linear_1684_10747"
x1="7.45643"
y1="-1.10615"
x2="5.53812"
y2="17.9973"
gradientUnits="userSpaceOnUse"
>
<stop stop-color="white" stop-opacity="0.16" />
<stop offset="1" stop-color="white" stop-opacity="0" />
</linearGradient>
<clipPath id="clip0_1684_10747">
<rect width="16" height="16" fill="white" />
</clipPath>
</defs>
</svg>
<span class="flex w-full items-center justify-between">
<span class="text-description">Table of contents</span>
<span
aria-hidden="true"
class="toggle-icon {showToc ? 'web-icon-close' : 'icon-menu-alt-4'}"
></span>
</span>
</div>
<TocTree tree={$headingsTree} activeHeadingIdxs={$activeHeadingIdxs} {item} />
</button>
</div>
</aside>

{#if showToc}
<aside
class="web-grid-120-1fr-auto-side"
class:web-is-mobile-closed={!showToc}
transition:slideFade={{ duration: 300 }}
>
<div class="web-page-steps">
<div
class="web-page-steps-location web-is-not-mobile"
style="--location:{progress * 100}%;"
>
<span class="web-page-steps-location-button">
<svg
xmlns="http://www.w3.org/2000/svg"
width="16"
height="16"
viewBox="0 0 16 16"
fill="none"
>
<g clip-path="url(#clip0_1684_10747)">
<g filter="url(#filter0_b_1684_10747)">
<circle
cx="8"
cy="8"
r="8"
fill="url(#paint0_linear_1684_10747)"
fill-opacity="0.32"
/>
<circle
cx="8"
cy="8"
r="7.75"
stroke="url(#paint1_linear_1684_10747)"
stroke-width="0.5"
/>
</g>
<circle cx="8" cy="7.99219" r="3" fill="white" />
</g>
<defs>
<filter
id="filter0_b_1684_10747"
x="-200"
y="-200"
width="416"
height="416"
filterUnits="userSpaceOnUse"
color-interpolation-filters="sRGB"
>
<feFlood flood-opacity="0" result="BackgroundImageFix" />
<feGaussianBlur in="BackgroundImageFix" stdDeviation="100" />
<feComposite
in2="SourceAlpha"
operator="in"
result="effect1_backgroundBlur_1684_10747"
/>
<feBlend
mode="normal"
in="SourceGraphic"
in2="effect1_backgroundBlur_1684_10747"
result="shape"
/>
</filter>
<linearGradient
id="paint0_linear_1684_10747"
x1="2.02105"
y1="1.10843"
x2="16.3872"
y2="17.2901"
gradientUnits="userSpaceOnUse"
>
<stop stop-color="white" stop-opacity="0.4" />
<stop offset="1" stop-color="white" stop-opacity="0" />
</linearGradient>
<linearGradient
id="paint1_linear_1684_10747"
x1="7.45643"
y1="-1.10615"
x2="5.53812"
y2="17.9973"
gradientUnits="userSpaceOnUse"
>
<stop stop-color="white" stop-opacity="0.16" />
<stop offset="1" stop-color="white" stop-opacity="0" />
</linearGradient>
<clipPath id="clip0_1684_10747">
<rect width="16" height="16" fill="white" />
</clipPath>
</defs>
</svg>
</span>
</div>

<div class="toc-tree-holder">
<TocTree
tree={$headingsTree}
activeHeadingIdxs={$activeHeadingIdxs}
{item}
bind:showToc
/>
</div>
</div>
</aside>
{/if}
</section>

<style>
.web-mobile-header {
top: 5rem;
grid-area: side;
background: unset;
max-height: fit-content;
border-block-end: unset;
border-block-start: unset;
}

@media (max-width: 1024px) {
.web-mobile-header {
top: 0;
margin: 1rem 0;
display: block;
position: sticky;
padding: 1.375rem 0;
align-content: center;
/** 1.5rem covers main header completely so fragments of it are not shown during scroll */
padding-block: 1.5rem;
padding-inline: 1.25rem;
background: hsl(var(--p-body-bg-color));
border-block-end: solid 1px var(--p-mobile-header-border-color);
border-block-start: solid 1px var(--p-mobile-header-border-color);
}

.toc-tree-holder {
margin-top: 1.5rem;
margin-left: 0.25rem;
}

.web-icon-close {
max-width: 20px;
max-height: 24px;
}
}

@media (min-width: 1280px) {
.web-mobile-header {
top: 7rem;
display: block;
}
}

@media (min-width: 1024px) {
.web-mobile-header {
border-block-end: unset;
border-block-start: unset;
}

.toc-tree-holder {
margin-top: unset;
}
}
</style>
9 changes: 9 additions & 0 deletions src/lib/components/TocTree.svelte
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
<script lang="ts">
import { type TableOfContentsItem, type TableOfContentsElements, melt } from '@melt-ui/svelte';
import { getTocCtx } from './TocRoot.svelte';
import { browser } from '$app/environment';

export let tree: TableOfContentsItem[] = [];
export let activeHeadingIdxs: number[];
export let item: TableOfContentsElements['item'];
export let level = 1;

export let showToc = true;

const {
toc: {
helpers: { isActive }
}
} = getTocCtx();

function onItemClick() {
const isDesktop = browser ? window.innerWidth >= 1024 : false;
if (!isDesktop) showToc = !showToc;
}
</script>

<ul class="web-page-steps-list text-sub-body font-medium">
Expand All @@ -22,6 +30,7 @@
class:is-selected={$isActive(heading.id)}
href="#{heading.id}"
use:melt={$item(heading.id)}
on:click|preventDefault={onItemClick}
>
<!-- eslint-disable-next-line svelte/no-at-html-tags -->
{@html heading.node.innerHTML}
Expand Down
Loading