|
2 | 2 | import type { Snippet } from 'svelte' |
3 | 3 | import type { HTMLAttributes } from 'svelte/elements' |
4 | 4 |
|
5 | | - export type Item = string | [string, unknown] |
6 | | - type T = $$Generic<Item> |
| 5 | + export type Item = [string, unknown] |
7 | 6 |
|
8 | 7 | interface Props extends Omit<HTMLAttributes<HTMLElement>, `children` | `onkeyup`> { |
9 | | - items?: T[] |
| 8 | + items?: (string | Item)[] |
10 | 9 | node?: string |
11 | 10 | current?: string |
12 | 11 | log?: `verbose` | `errors` | `silent` |
13 | 12 | nav_options?: { replace_state: boolean; no_scroll: boolean } |
14 | 13 | titles?: { prev: string; next: string } |
15 | | - onkeyup?: ((obj: { prev: Item; next: Item }) => Record<string, string>) | null |
| 14 | + onkeyup?: |
| 15 | + | ((obj: { prev: Item; next: Item }) => Record<string, string | undefined>) |
| 16 | + | null |
16 | 17 | prev_snippet?: Snippet<[{ item: Item }]> |
17 | 18 | children?: Snippet<[{ kind: `prev` | `next`; item: Item }]> |
18 | 19 | between?: Snippet<[]> |
|
26 | 27 | log = `errors`, |
27 | 28 | nav_options = { replace_state: true, no_scroll: true }, |
28 | 29 | titles = { prev: `← Previous`, next: `Next →` }, |
29 | | - onkeyup = ({ prev, next }) => ({ |
30 | | - ArrowLeft: prev[0], |
31 | | - ArrowRight: next[0], |
32 | | - }), |
| 30 | + onkeyup = ({ prev, next }) => ({ ArrowLeft: prev[0], ArrowRight: next[0] }), |
33 | 31 | prev_snippet, |
34 | 32 | children, |
35 | 33 | between, |
|
40 | 38 |
|
41 | 39 | // Convert items to consistent [key, value] format |
42 | 40 | let items_arr = $derived( |
43 | | - (items ?? []).map(( |
44 | | - itm, |
45 | | - ) => (typeof itm === `string` ? [itm, itm] : itm)) as Item[], |
| 41 | + (items ?? []).map( |
| 42 | + (itm) => (typeof itm === `string` ? [itm, itm] : itm), |
| 43 | + ) as Item[], |
46 | 44 | ) |
47 | 45 |
|
48 | 46 | // Calculate prev/next items with wraparound |
|
71 | 69 | function handle_keyup(event: KeyboardEvent) { |
72 | 70 | if (!onkeyup) return |
73 | 71 | if ((items_arr?.length ?? 0) < min_items) return |
| 72 | + if (!prev || !next) return |
74 | 73 | const key_map = onkeyup({ prev, next }) |
75 | 74 | const to = key_map[event.key] |
76 | | - if (to) { |
| 75 | + if (to !== undefined) { |
77 | 76 | const { replace_state, no_scroll } = nav_options |
78 | 77 | const [scroll_x, scroll_y] = no_scroll |
79 | 78 | ? [window.scrollX, window.scrollY] |
|
0 commit comments