-
-
Notifications
You must be signed in to change notification settings - Fork 231
Expand file tree
/
Copy pathMobileMenu.svelte
More file actions
345 lines (291 loc) · 7.6 KB
/
MobileMenu.svelte
File metadata and controls
345 lines (291 loc) · 7.6 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<script lang="ts">
import { afterNavigate } from '$app/navigation';
import { trap } from '../actions';
import { reduced_motion } from '../stores';
import { tick } from 'svelte';
import { expoOut, quintOut } from 'svelte/easing';
import type { TransitionConfig } from 'svelte/transition';
import Icon from '../components/Icon.svelte';
import MobileSubMenu from './MobileSubMenu.svelte';
import type { NavigationLink } from '../types';
import ModalOverlay from '../components/ModalOverlay.svelte';
interface Props {
links: NavigationLink[];
current: NavigationLink | undefined;
onclose: () => void;
}
let { links, current, onclose }: Props = $props();
// svelte-ignore state_referenced_locally
let show_context_menu = $state(!!current?.sections);
let nav_context_instance: ReturnType<typeof MobileSubMenu> | undefined = $state();
let menu_height = $state(0);
let universal_menu_inner_height = $state(0);
let ready = $state(false);
let universal_menu: HTMLElement | undefined = $state();
// svelte-ignore state_referenced_locally
afterNavigate(onclose);
$effect(() => {
// this is necessary to ensure that the menu-background height
// is applied without an animation
setTimeout(() => {
ready = true;
});
});
function popup(node: HTMLElement, { duration = 400, easing = expoOut } = {}): TransitionConfig {
const height = current ? node.clientHeight : universal_menu_inner_height;
return {
css: (t, u) =>
$reduced_motion
? `opacity: ${t}`
: `transform: translate3d(0, ${(height * u) / 0.9}px, 0) scale(${0.9 + 0.1 * t})`,
easing,
duration
};
}
</script>
<ModalOverlay {onclose} />
<div class="menu" use:trap={{ reset_focus: false }}>
<div class="mobile-main-menu" transition:popup={{ duration: 200, easing: quintOut }}>
<div
class="menu-background"
class:ready
style:height={show_context_menu ? '100%' : `${universal_menu_inner_height}px`}
></div>
<div
class="clip"
style:--height-difference="{menu_height - universal_menu_inner_height}px"
ontransitionstart={(e) => {
const target = e.target as HTMLElement;
if (!target?.classList.contains('viewport')) return;
if (e.propertyName !== 'transform') return;
// we need to apply a clip-path during the transition so that the contents
// are constrained to the menu background, but only while the transition
// is running, otherwise it prevents the contents from being scrolled
const a = 'calc(var(--height-difference) + 1px)';
const b = '1px';
const start = show_context_menu ? a : b;
const end = show_context_menu ? b : a;
const container = e.currentTarget;
container.style.clipPath = `polygon(0% ${start}, 100% ${start}, 100% 100%, 0% 100%)`;
setTimeout(() => {
container.style.clipPath = `polygon(0% ${end}, 100% ${end}, 100% 100%, 0% 100%)`;
}, 0);
}}
ontransitionend={(e) => {
const target = e.target as HTMLElement;
if (!target?.classList.contains('viewport')) return;
if (e.propertyName !== 'transform') return;
e.currentTarget.style.clipPath = '';
// whenever we transition from one menu to the other, we need to move focus to the first item in the new menu
if (!show_context_menu) {
universal_menu?.querySelector('a')?.focus();
}
}}
>
<div
class="viewport"
class:reduced-motion={$reduced_motion}
class:offset={show_context_menu}
bind:clientHeight={menu_height}
>
<div class="universal" inert={show_context_menu} bind:this={universal_menu}>
<div class="contents" bind:clientHeight={universal_menu_inner_height}>
<ul>
{#each links as link}
<li>
<a href="/{link.slug}">
{link.title}
</a>
{#if link.sections}
<button
class="raised icon"
onclick={async (event) => {
event.preventDefault();
current = link;
await tick();
show_context_menu = true;
await tick();
nav_context_instance?.scrollToActive();
}}
aria-label="Show {link.title} submenu"
>
<Icon name="arrow-right-chevron" size={18} />
</button>
{/if}
</li>
{/each}
</ul>
<hr />
<ul>
<li><a href="/chat">Discord</a></li>
<li><a href="https://bsky.app/profile/svelte.dev">Bluesky</a></li>
<li>
<a href="https://github.com/sveltejs">GitHub</a>
</li>
</ul>
</div>
</div>
<div class="context" inert={!show_context_menu}>
{#if current}
<MobileSubMenu
bind:this={nav_context_instance}
title={current.title}
contents={current.sections}
/>
{/if}
</div>
<label class="back-button">
<button
class="raised icon"
onclick={() => (show_context_menu = false)}
inert={!show_context_menu}
>
<Icon name="arrow-left" size={18} />
</button>
<span>Back to main menu</span>
</label>
</div>
</div>
</div>
</div>
<style>
.menu {
display: block;
position: fixed;
left: 0px;
bottom: var(--bottom, var(--sk-nav-height));
z-index: 100;
width: 100%;
height: 70vh;
border-radius: 1rem 1rem 0 0;
overflow-y: hidden;
overflow-x: hidden;
pointer-events: none;
transform: translate3d(0, 0, 0);
filter: var(--sk-shadow);
}
button {
display: flex;
align-items: center;
justify-content: center;
display: flex;
gap: 1.5rem;
}
.menu-background {
position: absolute;
width: 100%;
left: 0;
bottom: 0;
height: 99.5%;
border-radius: 1rem 1rem 0 0;
background: var(--background, var(--sk-bg-2));
will-change: height;
transition: 0.3s var(--quint-out);
transition-property: none;
&.ready {
transition-property: height;
}
:root.dark & {
border-top: solid 1px var(--sk-raised-highlight);
}
}
.mobile-main-menu {
height: 100%;
contain: layout paint;
transform: translateZ(0);
backface-visibility: hidden;
}
.clip {
width: 100%;
height: 100%;
transition: clip-path 0.3s cubic-bezier(0.23, 1, 0.32, 1);
will-change: clip-path;
}
.viewport {
position: relative;
bottom: -1px;
display: grid;
width: 200%;
height: 100%;
grid-template-columns: 50% 50%;
transition: transform 0.3s cubic-bezier(0.23, 1, 0.32, 1);
grid-auto-rows: 100%;
&.reduced-motion {
/* we still want the transition events to fire for focus management */
transition-duration: 0.01ms;
}
&.offset {
transform: translate3d(-50%, 0, 0);
}
& > * {
overflow-y: auto;
transition: inherit;
transition-property: transform, opacity;
}
& :global(a) {
position: relative;
padding: 0.3rem 0;
color: inherit;
font: var(--sk-font-ui-medium);
width: 100%;
height: 100%;
}
}
.universal .contents {
position: absolute;
width: 50%;
bottom: 0;
padding: 1rem var(--sk-page-padding-side);
max-height: 70vh;
overflow-y: scroll;
button {
/* width: 2.6rem; */
height: 2.6rem;
}
}
.context {
position: relative;
height: 100%;
bottom: -7px;
padding-bottom: 2rem;
}
.back-button {
position: absolute;
bottom: 0;
right: 0;
display: flex;
align-items: center;
justify-content: start;
gap: 1rem;
font: var(--sk-font-ui-medium);
color: var(--sk-fg-3);
background-color: var(--sk-bg-2);
width: 50%;
height: 4.8rem;
padding: 0 var(--sk-page-padding-side);
z-index: 10;
}
.universal .contents,
.context,
.back-button {
pointer-events: all;
}
.universal {
ul {
list-style: none;
margin: 0;
}
li {
display: flex;
a {
flex: 1;
}
}
hr {
margin: 0.5rem 0;
height: 1px;
background: var(--sk-border);
border: none;
}
}
</style>