Skip to content

Commit 4d39a87

Browse files
committed
replace page.subscribe() + onMount() with new afterNavigate() lifecycle hook sveltejs/kit#3293
1 parent d9d302b commit 4d39a87

File tree

8 files changed

+226
-231
lines changed

8 files changed

+226
-231
lines changed

.pre-commit-config.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,18 @@ repos:
3131
- id: codespell
3232
stages: [commit, commit-msg]
3333
exclude: yarn.lock
34+
35+
- repo: https://github.com/pre-commit/mirrors-eslint
36+
rev: v8.6.0
37+
hooks:
38+
- id: eslint
39+
types: [file]
40+
files: \.(svelte|js)$
41+
args: [--plugin, eslint-plugin-svelte3]
42+
additional_dependencies:
43+
- eslint
44+
- svelte
45+
- typescript
46+
- eslint-plugin-svelte3
47+
- '@typescript-eslint/eslint-plugin'
48+
- '@typescript-eslint/parser'

package.json

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,25 @@
1818
"check": "svelte-check --ignore package"
1919
},
2020
"devDependencies": {
21-
"@sveltejs/adapter-static": "^1.0.0-next.23",
22-
"@sveltejs/kit": "^1.0.0-next.206",
23-
"@typescript-eslint/eslint-plugin": "^5.8.1",
24-
"@typescript-eslint/parser": "^5.8.1",
25-
"eslint": "^8.5.0",
26-
"eslint-plugin-svelte3": "^3.2.1",
21+
"@sveltejs/adapter-static": "^1.0.0-next.26",
22+
"@sveltejs/kit": "^1.0.0-next.240",
23+
"@typescript-eslint/eslint-plugin": "^5.10.0",
24+
"@typescript-eslint/parser": "^5.10.0",
25+
"eslint": "^8.7.0",
26+
"eslint-plugin-svelte3": "^3.4.0",
2727
"hastscript": "^7.0.2",
2828
"mdsvex": "^0.9.8",
2929
"prettier": "^2.5.1",
30-
"prettier-plugin-svelte": "^2.5.1",
31-
"rehype-autolink-headings": "^6.1.0",
32-
"rehype-slug": "^5.0.0",
33-
"svelte": "^3.44.3",
34-
"svelte-check": "^2.2.11",
30+
"prettier-plugin-svelte": "^2.6.0",
31+
"rehype-autolink-headings": "^6.1.1",
32+
"rehype-slug": "^5.0.1",
33+
"svelte": "^3.46.2",
34+
"svelte-check": "^2.3.0",
3535
"svelte-github-corner": "^0.1.0",
36-
"svelte-preprocess": "^4.10.1",
37-
"svelte2tsx": "^0.4.12",
38-
"typescript": "^4.5.4",
39-
"vite": "^2.7.10"
36+
"svelte-preprocess": "^4.10.2",
37+
"svelte2tsx": "^0.4.14",
38+
"typescript": "^4.5.5",
39+
"vite": "^2.7.13"
4040
},
4141
"keywords": [
4242
"svelte",

readme.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,19 @@ The HTML structure of this component is
9898
- `var(--toc-hover-color, cornflowerblue)`: Text color of hovered headings.
9999
- `var(--toc-active-color, orange)`: Text color of the currently active heading. The active heading is the one closest to current scroll position.
100100
- `var(--toc-mobile-btn-color, black)`: Color of the menu icon used as ToC opener button on mobile screen sizes.
101+
- `var(--toc-li-scroll-margin, 20pt 0)`: scroll margin of ToC list items (determines distance from window edge when keeping active ToC item scrolled in view as page scrolls)
101102
- `var(--toc-mobile-btn-bg-color, rgba(255, 255, 255, 0.2))`: Background color of the padding area around the menu icon button.
102103
- `var(--toc-mobile-bg-color, white)`: Background color of the `nav` element hovering in the lower-left screen corner when the ToC was opened on mobile screens.
103104
- `var(--toc-desktop-bg-color)`: Background color of the `nav` element on desktop screens.
104105
- `var(--toc-desktop-sticky-top, 2em)`: How far below the screen's top edge the ToC starts being sticky.
105-
- `var(--toc-desktop-margin, 0)`: Margin of the outer-most `aside.toc` element.
106+
- `var(--toc-desktop-aside-margin, 0)`: Margin of the outer-most `aside.toc` element on desktops.
107+
- `var(--toc-desktop-nav-margin, 0 2ex 0 0)`: Margin of the `aside.toc > nav` element on desktops.
106108

107109
Example:
108110

109111
```svelte
110112
<Toc
111-
--toc-desktop-margin="10em 0 0 0"
113+
--toc-desktop-aside-margin="10em 0 0 0"
112114
--toc-desktop-sticky-top="3em"
113115
--toc-desktop-width="15em"
114116
/>

src/lib/Toc.svelte

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<script lang="ts">
2-
import { page } from '$app/stores'
3-
import { onMount } from 'svelte'
2+
import { afterNavigate } from '$app/navigation'
43
import { blur } from 'svelte/transition'
54
import { onClickOutside } from './actions'
65
import MenuIcon from './MenuIcon.svelte'
@@ -26,20 +25,13 @@
2625
$: levels = headings.map(getHeadingLevels)
2726
$: minLevel = Math.min(...levels)
2827
29-
function handleRouteChange() {
28+
// (re-)query headings on mount and on route changes
29+
afterNavigate(() => {
3030
headings = [...document.querySelectorAll(headingSelector)] as HTMLHeadingElement[]
3131
3232
// set first heading as active if null on page load
3333
if (activeHeading === null) activeHeading = headings[0]
34-
}
35-
36-
// (re-)compute list of HTML headings on mount and on route changes
37-
if (typeof window !== `undefined`) {
38-
page.subscribe(handleRouteChange)
39-
}
4034
41-
onMount(() => {
42-
handleRouteChange()
4335
const observer = new IntersectionObserver(
4436
(entries) => {
4537
// callback receives only observed nodes whose intersection changed
@@ -148,6 +140,7 @@
148140
:where(nav > ul > li) {
149141
margin-top: 5pt;
150142
cursor: pointer;
143+
scroll-margin: var(--toc-li-scroll-margin, 20pt 0);
151144
}
152145
:where(nav > ul > li:hover) {
153146
color: var(--toc-hover-color, cornflowerblue);
@@ -192,12 +185,12 @@
192185
}
193186
194187
:where(aside.toc.desktop) {
195-
margin: var(--toc-desktop-margin, 0);
188+
margin: var(--toc-desktop-aside-margin, 0);
196189
}
197190
:where(aside.toc.desktop > nav) {
198191
position: sticky;
199192
padding: 12pt 14pt 0;
200-
margin: 0 2ex 0 0;
193+
margin: var(--toc-desktop-nav-margin, 0 2ex 0 0);
201194
top: var(--toc-desktop-sticky-top, 2em);
202195
background-color: var(--toc-desktop-bg-color);
203196
border-radius: 5pt;

src/routes/index.svx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<Toc
77
headingSelector="main :where(h2, h3, h4)"
8-
--toc-desktop-margin="14em 0 0 0;"
8+
--toc-desktop-aside-margin="14em 0 0 0"
99
/>
1010

1111
<main>

static/global.css

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ body {
88
font-family: -apple-system, BlinkMacSystemFont, Roboto, sans-serif;
99
margin: auto;
1010
color: #eee;
11-
line-height: 3ex;
1211
overflow-x: hidden;
1312
}
1413
* {

svelte.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@ const rehypePlugins = [
2424

2525
export default {
2626
extensions: [`.svelte`, `.svx`, `.md`],
27+
2728
preprocess: [
2829
preprocess(),
2930
mdsvex({ rehypePlugins, extensions: [`.svx`, `.md`] }),
3031
],
32+
3133
kit: {
3234
adapter: adapter(),
3335

0 commit comments

Comments
 (0)