-
-
Notifications
You must be signed in to change notification settings - Fork 231
Expand file tree
/
Copy pathMobileSubMenu.svelte
More file actions
157 lines (130 loc) · 3.21 KB
/
MobileSubMenu.svelte
File metadata and controls
157 lines (130 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<script lang="ts">
import { page } from '$app/stores';
import type { NavigationLink } from '../types';
import { onMount } from 'svelte';
let { title, contents = [] }: { title: string; contents: NavigationLink['sections'] } = $props();
let nav = $state() as HTMLElement;
onMount(() => {
scrollToActive();
});
export async function scrollToActive() {
const active = nav.querySelector('[aria-current="page"]') as HTMLElement;
if (!active) {
nav.scrollTop = 0;
return;
}
const nav_center = nav.offsetHeight / 2;
const child_center = active.offsetHeight / 2;
const offset_top = active.offsetTop;
const scroll_position = offset_top - nav_center + child_center;
const update_scroll = () => (nav.scrollTop = scroll_position);
requestAnimationFrame(update_scroll);
}
</script>
<nav bind:this={nav}>
{#each contents as section, i}
<h2 style="--index: {i}; --reverse-index: {contents.length - i - 1}">
<a href="#{section.title}">{section.title} <span class="visually-hidden">{title}</span></a>
</h2>
{#if section.sections.length !== 0}
<ul id={section.title} style="--index: {i}">
{#each section.sections as { title, sections: subsections }}
<li>
{#if title}
<h3>
{title}
</h3>
{/if}
<ul>
{#each subsections as { path, title }}
<li>
<a href={path} aria-current={path === $page.url.pathname ? 'page' : undefined}>
{title}
</a>
</li>
{/each}
</ul>
</li>
{/each}
</ul>
{/if}
{/each}
</nav>
<style>
nav {
--header-padding: 1rem;
container-type: size;
font-family: var(--sk-font-family-ui);
overflow-y: auto;
height: 100%;
padding: 0 var(--sk-page-padding-side) 3rem;
}
ul {
--block-height: calc(1lh + var(--header-padding));
/*
* Necessary values to match `scroll-margin-top`
* with `h2` heights.
*/
font-size: 1.6rem;
line-height: 1.5;
list-style-type: none;
margin: 0;
margin-bottom: 2.5rem;
scroll-margin-top: calc((var(--index, 1) + 1) * var(--block-height));
}
li {
display: block;
}
h2 + ul > li {
/* h2 anchor padding - margin-left - border width */
padding-left: calc(1.75rem - 1px);
margin-left: 0.25rem;
border-left: 1px solid var(--sk-border);
}
h2,
h3 {
display: block;
padding-bottom: 0.8rem;
font: var(--sk-font-ui-medium);
text-transform: uppercase;
}
h2 {
position: sticky;
top: calc(var(--index, 0) * calc(1lh + var(--header-padding)));
bottom: calc(var(--reverse-index, 0) * calc(1lh + var(--header-padding)));
z-index: calc(var(--index, 1) + 1);
padding: calc(var(--header-padding) / 2) 0;
margin: calc(var(--header-padding) / 2) 0;
background-color: var(--sk-bg-2);
}
a {
display: flex;
align-items: center;
&[aria-current='page'] {
color: var(--sk-fg-accent) !important;
}
}
h2 a {
padding: 0 0 0 2rem !important;
&::before {
content: '#';
position: absolute;
left: 0;
color: var(--sk-fg-3);
}
&:hover::before,
&:focus::before {
color: var(--sk-fg-1);
}
}
/* Hide stacked heading experience on very small screens */
@container (height < 450px) {
ul {
scroll-margin-top: var(--block-height);
}
h2 {
top: 0;
bottom: initial;
}
}
</style>