|
6 | 6 | import Icon from '@sveltejs/site-kit/components/Icon.svelte';
|
7 | 7 | import { browser } from '$app/environment';
|
8 | 8 | import { afterNavigate } from '$app/navigation';
|
| 9 | + import { tick } from 'svelte'; |
9 | 10 |
|
10 | 11 | /** @type {import('$lib/types').PartStub[]}*/
|
11 | 12 | export let index;
|
12 | 13 |
|
13 | 14 | /** @type {import('$lib/types').Exercise} */
|
14 | 15 | export let current;
|
15 | 16 |
|
| 17 | + const duration = browser && matchMedia('(prefers-reduced-motion: reduce)').matches ? 0 : 200; |
| 18 | +
|
16 | 19 | let is_open = false;
|
17 | 20 | let search = '';
|
18 | 21 |
|
19 |
| - let expanded_part = ''; |
20 |
| - let expanded_chapter = ''; |
21 |
| - let duration = 0; |
| 22 | + /** @type {HTMLInputElement} */ |
| 23 | + let search_input; |
22 | 24 |
|
23 |
| - // The following statements ensure that the select animation is not run during opening the menu |
24 |
| - $: if (is_open || !is_open) { |
25 |
| - expanded_part = current.part.slug; |
26 |
| - expanded_chapter = current.chapter.slug; |
27 |
| - } |
28 |
| - $: if (is_open) { |
29 |
| - duration = 0; |
30 |
| - setTimeout(() => { |
31 |
| - duration = browser && matchMedia('(prefers-reduced-motion: reduce)').matches ? 0 : 200; |
32 |
| - }, 210); |
33 |
| - } |
| 25 | + $: expanded_part = current.part.slug; |
| 26 | + $: expanded_chapter = current.chapter.slug; |
34 | 27 |
|
35 | 28 | $: regex = new RegExp(`\\b${search.length >= 2 ? search : ''}`, 'i');
|
36 | 29 |
|
37 | 30 | $: filtered = index
|
38 |
| - .map((part, i) => { |
| 31 | + .map((part) => { |
39 | 32 | return {
|
40 | 33 | slug: part.slug,
|
41 | 34 | title: part.title,
|
|
92 | 85 | <input
|
93 | 86 | type="search"
|
94 | 87 | placeholder="Search"
|
| 88 | + bind:this={search_input} |
95 | 89 | bind:value={search}
|
96 | 90 | aria-hidden={!is_open ? 'true' : null}
|
97 | 91 | tabindex={!is_open ? -1 : null}
|
|
170 | 164 |
|
171 | 165 | <!-- we don't want this to be keyboard-navigable, because the menu button to the left does that job better -->
|
172 | 166 | <!-- svelte-ignore a11y-click-events-have-key-events -->
|
173 |
| - <h1 on:click={() => (is_open = true)}> |
| 167 | + <h1 |
| 168 | + on:click={async () => { |
| 169 | + is_open = true; |
| 170 | + await tick(); |
| 171 | + search_input.focus(); |
| 172 | + }} |
| 173 | + > |
174 | 174 | {current.part.title} <span class="separator">/</span>
|
175 | 175 | {current.chapter.title} <span class="separator">/</span>
|
176 | 176 | <strong>{current.title}</strong>
|
|
0 commit comments