Skip to content

Commit e5c501d

Browse files
authored
focus search input when opening menu from header (sveltejs#232)
* include all exercises in chapter that matches search - closes sveltejs#226 * this code looks wholly unnecessary * focus search input when opening menu from header - closes sveltejs#225
1 parent ee1e920 commit e5c501d

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

src/routes/tutorial/[slug]/Menu.svelte

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,29 @@
66
import Icon from '@sveltejs/site-kit/components/Icon.svelte';
77
import { browser } from '$app/environment';
88
import { afterNavigate } from '$app/navigation';
9+
import { tick } from 'svelte';
910
1011
/** @type {import('$lib/types').PartStub[]}*/
1112
export let index;
1213
1314
/** @type {import('$lib/types').Exercise} */
1415
export let current;
1516
17+
const duration = browser && matchMedia('(prefers-reduced-motion: reduce)').matches ? 0 : 200;
18+
1619
let is_open = false;
1720
let search = '';
1821
19-
let expanded_part = '';
20-
let expanded_chapter = '';
21-
let duration = 0;
22+
/** @type {HTMLInputElement} */
23+
let search_input;
2224
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;
3427
3528
$: regex = new RegExp(`\\b${search.length >= 2 ? search : ''}`, 'i');
3629
3730
$: filtered = index
38-
.map((part, i) => {
31+
.map((part) => {
3932
return {
4033
slug: part.slug,
4134
title: part.title,
@@ -92,6 +85,7 @@
9285
<input
9386
type="search"
9487
placeholder="Search"
88+
bind:this={search_input}
9589
bind:value={search}
9690
aria-hidden={!is_open ? 'true' : null}
9791
tabindex={!is_open ? -1 : null}
@@ -170,7 +164,13 @@
170164

171165
<!-- we don't want this to be keyboard-navigable, because the menu button to the left does that job better -->
172166
<!-- 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+
>
174174
{current.part.title} <span class="separator">/</span>
175175
{current.chapter.title} <span class="separator">/</span>
176176
<strong>{current.title}</strong>

0 commit comments

Comments
 (0)