From cadc1f637f2ca76d693696000455a889f3084447 Mon Sep 17 00:00:00 2001 From: Youwen Wu Date: Sat, 6 Apr 2024 03:05:16 -0700 Subject: [PATCH] feat: add sticky toc --- package.json | 1 + pnpm-lock.yaml | 11 ++++ src/lib/components/StickyToc.svelte | 72 ++++++++++++++++++++++ src/routes/blog/[year]/[slug]/+page.svelte | 37 ++++++++++- 4 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 src/lib/components/StickyToc.svelte diff --git a/package.json b/package.json index fbc3bda..4099957 100644 --- a/package.json +++ b/package.json @@ -47,6 +47,7 @@ "@fontsource/geist-sans": "^5.0.2", "@fontsource/merriweather": "^5.0.12", "@fontsource/zilla-slab": "^5.0.12", + "@svelte-put/toc": "^5.0.1", "@sveltejs/adapter-vercel": "^5.2.0", "bits-ui": "^0.21.2", "clsx": "^2.1.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index fbb1580..7d46234 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -17,6 +17,9 @@ dependencies: '@fontsource/zilla-slab': specifier: ^5.0.12 version: 5.0.12 + '@svelte-put/toc': + specifier: ^5.0.1 + version: 5.0.1(svelte@4.2.12) '@sveltejs/adapter-vercel': specifier: ^5.2.0 version: 5.2.0(@sveltejs/kit@2.5.5) @@ -890,6 +893,14 @@ packages: requiresBuild: true optional: true + /@svelte-put/toc@5.0.1(svelte@4.2.12): + resolution: {integrity: sha512-oxDFPM2Y0XmhhXoNDhcPltf8RxhIvtiotFL0PX9NLossTumuAPiLVaDeEsGfBOn+nZK2J1oo9XqJU3NWHDROcw==} + peerDependencies: + svelte: ^3.55.0 || ^4.0.0 + dependencies: + svelte: 4.2.12 + dev: false + /@sveltejs/adapter-vercel@5.2.0(@sveltejs/kit@2.5.5): resolution: {integrity: sha512-872y13DxKcOBxgnXc4C2YHRw1ow9N1CpUxMH34NYFqCn6PUO6f34qle8v/Byr8sHEC/d+PZIAI3MJs3c8f7TfA==} peerDependencies: diff --git a/src/lib/components/StickyToc.svelte b/src/lib/components/StickyToc.svelte new file mode 100644 index 0000000..93da107 --- /dev/null +++ b/src/lib/components/StickyToc.svelte @@ -0,0 +1,72 @@ + + + diff --git a/src/routes/blog/[year]/[slug]/+page.svelte b/src/routes/blog/[year]/[slug]/+page.svelte index 5e2bf64..ecec4dd 100644 --- a/src/routes/blog/[year]/[slug]/+page.svelte +++ b/src/routes/blog/[year]/[slug]/+page.svelte @@ -5,6 +5,26 @@ // import { config } from '$lib/stores/index.js'; import { cn } from '$lib/utils.js'; import Article from '$lib/components/Blog/Article.svelte'; + import { toc, createTocStore } from '@svelte-put/toc'; + import StickyToc from '$lib/components/StickyToc.svelte'; + import { onMount } from 'svelte'; + + let distanceFromRight = 0; + + let mainElement: HTMLElement; + + const handleResize = () => { + const screenWidth = window.innerWidth; + const mainWidth = mainElement.offsetWidth; + distanceFromRight = screenWidth - mainWidth - mainElement.offsetLeft; + console.log(distanceFromRight); + }; + + onMount(() => { + handleResize(); + }); + + const tocStore = createTocStore(); export let data: PageData; @@ -28,6 +48,21 @@ -
+ + +
+ +