dssoca 0.12.0 → 0.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/Kbd.svelte +120 -0
- package/dist/components/Kbd.svelte.d.ts +30 -0
- package/dist/components/SearchPalette.svelte +526 -0
- package/dist/components/SearchPalette.svelte.d.ts +82 -0
- package/dist/components/ShortcutsHelp.svelte +365 -0
- package/dist/components/ShortcutsHelp.svelte.d.ts +34 -0
- package/dist/components/Switch.svelte +32 -2
- package/dist/components/Switch.svelte.d.ts +5 -0
- package/dist/components/Topbar.svelte +39 -24
- package/dist/components/Topbar.svelte.d.ts +5 -2
- package/dist/dssoca.config.d.ts +1 -1
- package/dist/dssoca.config.js +2 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +7 -0
- package/dist/presets.d.ts +44 -0
- package/dist/presets.js +295 -0
- package/dist/shortcuts.svelte.d.ts +192 -0
- package/dist/shortcuts.svelte.js +633 -0
- package/package.json +2 -2
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
3
|
+
// Kbd (DS-0137). Display-only key-cap chip: renders a combo from the
|
|
4
|
+
// shortcut grammar (DS-0136) in house style. Registers nothing — pair the
|
|
5
|
+
// real binding with `shortcuts.add()` / `shortcut()` and put
|
|
6
|
+
// `ariaKeyshortcuts()` on the owning control.
|
|
7
|
+
//
|
|
8
|
+
// Markup follows MDN's nested-<kbd> convention: one outer <kbd> for the
|
|
9
|
+
// whole combo, one inner <kbd> per key, aria-hidden '+' separators between
|
|
10
|
+
// keys and a muted "or" between comma-alternatives.
|
|
11
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
12
|
+
import type { Snippet } from 'svelte'
|
|
13
|
+
import { resolveComponentSize, type Size } from '../config.js'
|
|
14
|
+
import {
|
|
15
|
+
detectPlatform,
|
|
16
|
+
formatShortcutParts,
|
|
17
|
+
type ShortcutFormat,
|
|
18
|
+
type ShortcutPlatform,
|
|
19
|
+
} from '../shortcuts.svelte.js'
|
|
20
|
+
|
|
21
|
+
interface Props {
|
|
22
|
+
/**
|
|
23
|
+
* Combo string in the shortcut grammar (`'mod+k'`, `'?, mod+/'`).
|
|
24
|
+
* `mod` renders as ⌘/Command on Apple platforms and Ctrl elsewhere.
|
|
25
|
+
* Malformed input throws (same parser as registration).
|
|
26
|
+
*/
|
|
27
|
+
keys?: string
|
|
28
|
+
/**
|
|
29
|
+
* `'glyph'` (default): ⌘⇧⌥ + arrow/enter glyphs on Apple, words elsewhere.
|
|
30
|
+
* `'label'`: always full words. Glyph roots carry a full-word `aria-label`
|
|
31
|
+
* (⌘ and ↵ read poorly in assistive tech).
|
|
32
|
+
*/
|
|
33
|
+
format?: ShortcutFormat
|
|
34
|
+
/**
|
|
35
|
+
* Override platform auto-detection. Defaults to `'other'` on the server
|
|
36
|
+
* and the first client render, corrected in an effect (no hydration
|
|
37
|
+
* mismatch); SSR apps can pass it explicitly for a stable first paint.
|
|
38
|
+
*/
|
|
39
|
+
platform?: ShortcutPlatform
|
|
40
|
+
/** Token size (sm|md|lg); inherits the global size when unset. */
|
|
41
|
+
size?: Size
|
|
42
|
+
/** Raw-content escape hatch for keys the grammar can't express (`<Kbd>F12</Kbd>` sequences, chords…). Ignored when `keys` is set. */
|
|
43
|
+
children?: Snippet
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
let { keys, format = 'glyph', platform, size, children }: Props = $props()
|
|
47
|
+
|
|
48
|
+
// Hydration posture: 'other' on the server AND the first client render,
|
|
49
|
+
// corrected in an effect so server and client first paints agree. An
|
|
50
|
+
// explicit `platform` prop wins and skips the correction.
|
|
51
|
+
let detected = $state<ShortcutPlatform>('other')
|
|
52
|
+
$effect(() => {
|
|
53
|
+
if (platform === undefined) detected = detectPlatform()
|
|
54
|
+
})
|
|
55
|
+
const plat = $derived(platform ?? detected)
|
|
56
|
+
|
|
57
|
+
// One keycap-string array per comma-alternative.
|
|
58
|
+
const groups = $derived(keys ? formatShortcutParts(keys, { format, platform: plat }) : [])
|
|
59
|
+
// Apple glyphs concatenate (⌘K, the macOS convention) — no '+' separators.
|
|
60
|
+
const plus = $derived(!(format === 'glyph' && plat === 'apple'))
|
|
61
|
+
// Glyph roots carry the full-word combo; role="img" makes the aria-label
|
|
62
|
+
// valid on an otherwise generic element (Badge's label-less precedent) and
|
|
63
|
+
// presents the chip as one named unit instead of glyph soup.
|
|
64
|
+
const ariaLabel = $derived(
|
|
65
|
+
keys && format === 'glyph'
|
|
66
|
+
? formatShortcutParts(keys, { format: 'label', platform: plat })
|
|
67
|
+
.map((group) => group.join(' '))
|
|
68
|
+
.join(' or ')
|
|
69
|
+
: undefined,
|
|
70
|
+
)
|
|
71
|
+
</script>
|
|
72
|
+
|
|
73
|
+
<kbd
|
|
74
|
+
class="ss-kbd"
|
|
75
|
+
data-size-variant={resolveComponentSize('Kbd', size)}
|
|
76
|
+
role={ariaLabel ? 'img' : undefined}
|
|
77
|
+
aria-label={ariaLabel}
|
|
78
|
+
>
|
|
79
|
+
{#if keys}
|
|
80
|
+
{#each groups as caps, g (g)}
|
|
81
|
+
{#if g > 0}<span class="or">or</span>{/if}
|
|
82
|
+
{#each caps as cap, i (i)}
|
|
83
|
+
{#if i > 0 && plus}<span class="sep" aria-hidden="true">+</span>{/if}
|
|
84
|
+
<kbd class="key">{cap}</kbd>
|
|
85
|
+
{/each}
|
|
86
|
+
{/each}
|
|
87
|
+
{:else if children}
|
|
88
|
+
<kbd class="key">{@render children()}</kbd>
|
|
89
|
+
{/if}
|
|
90
|
+
</kbd>
|
|
91
|
+
|
|
92
|
+
<style>.ss-kbd {
|
|
93
|
+
display: inline-flex;
|
|
94
|
+
align-items: center;
|
|
95
|
+
gap: calc(var(--ss-badge-gap) / 2);
|
|
96
|
+
font-family: var(--ss-font-mono);
|
|
97
|
+
font-size: var(--ss-size-xs);
|
|
98
|
+
}
|
|
99
|
+
.ss-kbd .key {
|
|
100
|
+
font-family: inherit;
|
|
101
|
+
font-size: inherit;
|
|
102
|
+
line-height: 1.6;
|
|
103
|
+
padding: 0 calc(var(--ss-badge-px) / 2);
|
|
104
|
+
min-width: 1.8em;
|
|
105
|
+
box-sizing: border-box;
|
|
106
|
+
text-align: center;
|
|
107
|
+
background: var(--ss-badge-neutral-bg);
|
|
108
|
+
color: var(--ss-badge-neutral-fg);
|
|
109
|
+
border: 1px solid var(--ss-badge-neutral-border);
|
|
110
|
+
border-bottom-color: var(--ss-line-strong);
|
|
111
|
+
}
|
|
112
|
+
.ss-kbd .sep,
|
|
113
|
+
.ss-kbd .or {
|
|
114
|
+
color: var(--ss-fg-muted);
|
|
115
|
+
flex: none;
|
|
116
|
+
}
|
|
117
|
+
.ss-kbd .or {
|
|
118
|
+
font-size: var(--ss-ui-xs);
|
|
119
|
+
letter-spacing: 0.05em;
|
|
120
|
+
}</style>
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Snippet } from 'svelte';
|
|
2
|
+
import { type Size } from '../config.js';
|
|
3
|
+
import { type ShortcutFormat, type ShortcutPlatform } from '../shortcuts.svelte.js';
|
|
4
|
+
interface Props {
|
|
5
|
+
/**
|
|
6
|
+
* Combo string in the shortcut grammar (`'mod+k'`, `'?, mod+/'`).
|
|
7
|
+
* `mod` renders as ⌘/Command on Apple platforms and Ctrl elsewhere.
|
|
8
|
+
* Malformed input throws (same parser as registration).
|
|
9
|
+
*/
|
|
10
|
+
keys?: string;
|
|
11
|
+
/**
|
|
12
|
+
* `'glyph'` (default): ⌘⇧⌥ + arrow/enter glyphs on Apple, words elsewhere.
|
|
13
|
+
* `'label'`: always full words. Glyph roots carry a full-word `aria-label`
|
|
14
|
+
* (⌘ and ↵ read poorly in assistive tech).
|
|
15
|
+
*/
|
|
16
|
+
format?: ShortcutFormat;
|
|
17
|
+
/**
|
|
18
|
+
* Override platform auto-detection. Defaults to `'other'` on the server
|
|
19
|
+
* and the first client render, corrected in an effect (no hydration
|
|
20
|
+
* mismatch); SSR apps can pass it explicitly for a stable first paint.
|
|
21
|
+
*/
|
|
22
|
+
platform?: ShortcutPlatform;
|
|
23
|
+
/** Token size (sm|md|lg); inherits the global size when unset. */
|
|
24
|
+
size?: Size;
|
|
25
|
+
/** Raw-content escape hatch for keys the grammar can't express (`<Kbd>F12</Kbd>` sequences, chords…). Ignored when `keys` is set. */
|
|
26
|
+
children?: Snippet;
|
|
27
|
+
}
|
|
28
|
+
declare const Kbd: import("svelte").Component<Props, {}, "">;
|
|
29
|
+
type Kbd = ReturnType<typeof Kbd>;
|
|
30
|
+
export default Kbd;
|
|
@@ -0,0 +1,526 @@
|
|
|
1
|
+
<script module lang="ts">
|
|
2
|
+
export interface SearchPaletteItem {
|
|
3
|
+
/** Stable unique id (keys the option and its DOM id). */
|
|
4
|
+
id: string
|
|
5
|
+
/** Primary text; also the default filter haystack. */
|
|
6
|
+
label: string
|
|
7
|
+
/** Secondary muted line under the label (default row rendering). */
|
|
8
|
+
hint?: string
|
|
9
|
+
/** When set, the option renders as a real <a>; Enter clicks it so routing stays native. */
|
|
10
|
+
href?: string
|
|
11
|
+
/** Group heading; groups render in first-appearance order, ungrouped items first. */
|
|
12
|
+
group?: string
|
|
13
|
+
/** Extra strings matched by the internal filter. */
|
|
14
|
+
keywords?: string[]
|
|
15
|
+
/** Rendered but not selectable; skipped by keyboard navigation. */
|
|
16
|
+
disabled?: boolean
|
|
17
|
+
}
|
|
18
|
+
</script>
|
|
19
|
+
|
|
20
|
+
<script lang="ts" generics="T extends SearchPaletteItem">
|
|
21
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
22
|
+
// SearchPalette (DS-0133). A Cmd/Ctrl+K search palette: modal combobox over
|
|
23
|
+
// a grouped listbox, generalized from passoca's app implementation.
|
|
24
|
+
//
|
|
25
|
+
// Built on the native <dialog> like Modal (DS-0094): showModal() gives us
|
|
26
|
+
// the focus trap, Esc-to-close (cancel → close), ::backdrop and top layer.
|
|
27
|
+
// Focus stays on the input the whole time (APG combobox pattern): options
|
|
28
|
+
// are referenced via aria-activedescendant, never DOM-focused.
|
|
29
|
+
//
|
|
30
|
+
// Enter on an item with `href` clicks the actually-rendered <a> instead of
|
|
31
|
+
// synthesizing navigation, so SvelteKit (or any router) intercepts it the
|
|
32
|
+
// same way as a pointer click.
|
|
33
|
+
//
|
|
34
|
+
// Component metrics are inline var(--ss-search-palette-*, fallback) so no
|
|
35
|
+
// global SCSS is touched (DS-0094 pattern).
|
|
36
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
37
|
+
import { tick, type Snippet } from 'svelte'
|
|
38
|
+
import { resolveComponentSize, type Size } from '../config.js'
|
|
39
|
+
import { shortcuts } from '../shortcuts.svelte.js'
|
|
40
|
+
|
|
41
|
+
interface Props {
|
|
42
|
+
/** Whether the palette is shown. Bindable — sync it from caller state. */
|
|
43
|
+
open?: boolean
|
|
44
|
+
/** Entries to show. With external filtering, pass the already-filtered list. */
|
|
45
|
+
items: T[]
|
|
46
|
+
/** Search text. Bindable — bind it when you own filtering (`filter={false}`). */
|
|
47
|
+
query?: string
|
|
48
|
+
/** Internal substring filter over label+keywords. Set false when the consumer filters. @default true */
|
|
49
|
+
filter?: boolean
|
|
50
|
+
/** Global toggle shortcut, registered through the shortcut registry as
|
|
51
|
+
* `ss:search-palette` (DS-0139) — listed in ShortcutsHelp, obeys
|
|
52
|
+
* `setEnabled`/`remap`. 'mod+k' = Cmd+K on mac, Ctrl+K elsewhere
|
|
53
|
+
* (platform modifier only); false disables. @default 'mod+k' */
|
|
54
|
+
shortcut?: 'mod+k' | false
|
|
55
|
+
/** Input placeholder. @default 'Search…' */
|
|
56
|
+
placeholder?: string
|
|
57
|
+
/** Accessible name for the dialog, input and listbox. @default 'Search' */
|
|
58
|
+
'aria-label'?: string
|
|
59
|
+
/** No-results text (used when the `empty` snippet is absent). @default 'No results' */
|
|
60
|
+
emptyText?: string
|
|
61
|
+
/** Footer hint row; false hides it. A `footer` snippet replaces it. @default '↑↓ navigate · ↵ open · esc close' */
|
|
62
|
+
footerText?: string | false
|
|
63
|
+
/** Clear the query and selection when the palette closes. @default true */
|
|
64
|
+
resetOnClose?: boolean
|
|
65
|
+
/** Token size (sm|md|lg); controls the panel width. Inherits the global size when unset. */
|
|
66
|
+
size?: Size
|
|
67
|
+
/** Called with the chosen item on Enter/click. Return false to keep the palette open. */
|
|
68
|
+
onselect?: (item: T) => void | boolean
|
|
69
|
+
/** Called after the palette opens. */
|
|
70
|
+
onopen?: () => void
|
|
71
|
+
/** Called after the palette closes (Esc, backdrop, selection). */
|
|
72
|
+
onclose?: () => void
|
|
73
|
+
/** Replaces the default option row body (label + hint). */
|
|
74
|
+
item?: Snippet<[T, { active: boolean }]>
|
|
75
|
+
/** Replaces the default no-results state; receives the current query. */
|
|
76
|
+
empty?: Snippet<[string]>
|
|
77
|
+
/** Replaces the footer text row. */
|
|
78
|
+
footer?: Snippet
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
let {
|
|
82
|
+
open = $bindable(false),
|
|
83
|
+
items,
|
|
84
|
+
query = $bindable(''),
|
|
85
|
+
filter = true,
|
|
86
|
+
shortcut = 'mod+k',
|
|
87
|
+
placeholder = 'Search…',
|
|
88
|
+
'aria-label': ariaLabel = 'Search',
|
|
89
|
+
emptyText = 'No results',
|
|
90
|
+
footerText = '↑↓ navigate · ↵ open · esc close',
|
|
91
|
+
resetOnClose = true,
|
|
92
|
+
size,
|
|
93
|
+
onselect,
|
|
94
|
+
onopen,
|
|
95
|
+
onclose,
|
|
96
|
+
item: itemSnippet,
|
|
97
|
+
empty,
|
|
98
|
+
footer,
|
|
99
|
+
}: Props = $props()
|
|
100
|
+
|
|
101
|
+
let dialog = $state<HTMLDialogElement | null>(null)
|
|
102
|
+
let input = $state<HTMLInputElement | null>(null)
|
|
103
|
+
let list = $state<HTMLElement | null>(null)
|
|
104
|
+
let active = $state(0)
|
|
105
|
+
|
|
106
|
+
const uid = $props.id()
|
|
107
|
+
const listId = `${uid}-listbox`
|
|
108
|
+
const optionId = (index: number) => `${uid}-option-${index}`
|
|
109
|
+
|
|
110
|
+
// Case- and diacritic-insensitive fold ("São" matches "sao").
|
|
111
|
+
const fold = (s: string) =>
|
|
112
|
+
s
|
|
113
|
+
.normalize('NFD')
|
|
114
|
+
.replace(/[\u0300-\u036f]/g, '')
|
|
115
|
+
.toLowerCase()
|
|
116
|
+
|
|
117
|
+
const filtered = $derived.by(() => {
|
|
118
|
+
if (!filter) return items
|
|
119
|
+
const q = fold(query.trim())
|
|
120
|
+
if (!q) return items
|
|
121
|
+
return items.filter((it) => fold([it.label, ...(it.keywords ?? [])].join(' ')).includes(q))
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
// Buckets in first-appearance order, ungrouped (label null) always first;
|
|
125
|
+
// options carry a flat index so keyboard order equals DOM order.
|
|
126
|
+
const groups = $derived.by(() => {
|
|
127
|
+
const buckets: { label: string | null; bucket: T[] }[] = []
|
|
128
|
+
for (const it of filtered) {
|
|
129
|
+
const label = it.group ?? null
|
|
130
|
+
let b = buckets.find((entry) => entry.label === label)
|
|
131
|
+
if (!b) {
|
|
132
|
+
b = { label, bucket: [] }
|
|
133
|
+
buckets.push(b)
|
|
134
|
+
}
|
|
135
|
+
b.bucket.push(it)
|
|
136
|
+
}
|
|
137
|
+
buckets.sort((a, b) => Number(b.label === null) - Number(a.label === null))
|
|
138
|
+
let index = 0
|
|
139
|
+
return buckets.map(({ label, bucket }) => ({
|
|
140
|
+
label,
|
|
141
|
+
entries: bucket.map((entry) => ({ item: entry, index: index++ })),
|
|
142
|
+
}))
|
|
143
|
+
})
|
|
144
|
+
const flat = $derived(groups.flatMap((g) => g.entries.map((e) => e.item)))
|
|
145
|
+
|
|
146
|
+
function firstSelectable(): number {
|
|
147
|
+
const i = flat.findIndex((it) => !it.disabled)
|
|
148
|
+
return i === -1 ? 0 : i
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Keep the active index valid as the list changes (typing, items swapped).
|
|
152
|
+
$effect(() => {
|
|
153
|
+
if (active >= flat.length || flat[active]?.disabled) active = firstSelectable()
|
|
154
|
+
})
|
|
155
|
+
|
|
156
|
+
// Mirror `open` onto the native dialog (same guard dance as Modal), then
|
|
157
|
+
// land focus on the input — the dialog's default focus target is the panel.
|
|
158
|
+
$effect(() => {
|
|
159
|
+
const el = dialog
|
|
160
|
+
if (!el) return
|
|
161
|
+
if (open && !el.open) {
|
|
162
|
+
el.showModal()
|
|
163
|
+
input?.focus()
|
|
164
|
+
onopen?.()
|
|
165
|
+
} else if (!open && el.open) {
|
|
166
|
+
el.close()
|
|
167
|
+
}
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
// Native `close` (Esc, .close()) → sync state, optionally reset, notify.
|
|
171
|
+
function onDialogClose() {
|
|
172
|
+
if (open) open = false
|
|
173
|
+
if (resetOnClose) {
|
|
174
|
+
query = ''
|
|
175
|
+
active = 0
|
|
176
|
+
}
|
|
177
|
+
onclose?.()
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// A pointerdown landing on the <dialog> itself (not the inner panel) is a
|
|
181
|
+
// backdrop hit (Modal's detection).
|
|
182
|
+
function onPointerDown(e: PointerEvent) {
|
|
183
|
+
if (e.target === dialog) open = false
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// DS-0139: the toggle chord goes through the shortcut registry (id
|
|
187
|
+
// `ss:search-palette`) instead of a hand-rolled window listener — listed in
|
|
188
|
+
// ShortcutsHelp, disable/remap-able (WCAG 2.1.4), deterministic on
|
|
189
|
+
// collision. While the palette is open, its own modal <dialog> suppresses
|
|
190
|
+
// global shortcuts (SC 2.1.2), so the binding re-registers focus-scoped on
|
|
191
|
+
// the dialog; same id, so overrides follow both directions of the toggle.
|
|
192
|
+
// `add()` is SSR-safe (no-op without `window`).
|
|
193
|
+
$effect(() => {
|
|
194
|
+
if (shortcut === false) return
|
|
195
|
+
const options = {
|
|
196
|
+
id: 'ss:search-palette',
|
|
197
|
+
label: 'Open search',
|
|
198
|
+
keys: shortcut,
|
|
199
|
+
allowInInputs: true, // chorded — must fire while typing (incl. the palette's own input)
|
|
200
|
+
onPress: () => {
|
|
201
|
+
open = !open
|
|
202
|
+
},
|
|
203
|
+
}
|
|
204
|
+
const el = dialog
|
|
205
|
+
return open && el ? shortcuts.add({ ...options, scope: 'focus' }, el) : shortcuts.add(options)
|
|
206
|
+
})
|
|
207
|
+
|
|
208
|
+
// Step to the next non-disabled option, wrapping; delta 0 re-validates.
|
|
209
|
+
function move(delta: number, from = active) {
|
|
210
|
+
const len = flat.length
|
|
211
|
+
if (!len) return
|
|
212
|
+
let i = from
|
|
213
|
+
for (let step = 0; step < len; step++) {
|
|
214
|
+
i = (i + delta + len) % len
|
|
215
|
+
if (!flat[i].disabled) {
|
|
216
|
+
active = i
|
|
217
|
+
break
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
void tick().then(() =>
|
|
221
|
+
list?.querySelector(`#${CSS.escape(optionId(active))}`)?.scrollIntoView({ block: 'nearest' }),
|
|
222
|
+
)
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function onInputKeydown(e: KeyboardEvent) {
|
|
226
|
+
if (e.isComposing) return
|
|
227
|
+
if (e.key === 'ArrowDown') {
|
|
228
|
+
e.preventDefault()
|
|
229
|
+
move(1)
|
|
230
|
+
} else if (e.key === 'ArrowUp') {
|
|
231
|
+
e.preventDefault()
|
|
232
|
+
move(-1)
|
|
233
|
+
} else if (e.key === 'PageUp') {
|
|
234
|
+
// Home/End stay native (caret editing in the textbox, per APG combobox);
|
|
235
|
+
// PageUp/PageDown jump to the first/last option instead.
|
|
236
|
+
e.preventDefault()
|
|
237
|
+
move(1, -1)
|
|
238
|
+
} else if (e.key === 'PageDown') {
|
|
239
|
+
e.preventDefault()
|
|
240
|
+
move(-1, flat.length)
|
|
241
|
+
} else if (e.key === 'Enter') {
|
|
242
|
+
e.preventDefault()
|
|
243
|
+
const it = flat[active]
|
|
244
|
+
if (!it || it.disabled) return
|
|
245
|
+
// href options: click the real anchor so the router/native semantics
|
|
246
|
+
// apply — its click handler runs choose() for us.
|
|
247
|
+
if (it.href) anchorAt(active)?.click()
|
|
248
|
+
else choose(it)
|
|
249
|
+
} else if (e.key === 'Tab') {
|
|
250
|
+
// Focus stays on the input while the palette is open.
|
|
251
|
+
e.preventDefault()
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function anchorAt(index: number): HTMLAnchorElement | null {
|
|
256
|
+
return list?.querySelector(`#${CSS.escape(optionId(index))}`) as HTMLAnchorElement | null
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// Shared select path for clicks and Enter; navigation (when any) has
|
|
260
|
+
// already been dispatched on the anchor itself.
|
|
261
|
+
function choose(it: T) {
|
|
262
|
+
if (it.disabled) return
|
|
263
|
+
const keep = onselect?.(it) === false
|
|
264
|
+
if (!keep) open = false
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// Focus must never leave the input (aria-activedescendant pattern):
|
|
268
|
+
// swallow the focusing pointerdown on option rows before it lands.
|
|
269
|
+
function onRowPointerDown(e: PointerEvent) {
|
|
270
|
+
e.preventDefault()
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// Reset the selection to the top whenever the query changes; after tick()
|
|
274
|
+
// the derived `flat` reflects the new query regardless of handler order.
|
|
275
|
+
async function onQueryInput() {
|
|
276
|
+
await tick()
|
|
277
|
+
active = firstSelectable()
|
|
278
|
+
}
|
|
279
|
+
</script>
|
|
280
|
+
|
|
281
|
+
<dialog
|
|
282
|
+
bind:this={dialog}
|
|
283
|
+
class="ss-search-palette"
|
|
284
|
+
data-size-variant={resolveComponentSize('SearchPalette', size)}
|
|
285
|
+
aria-label={ariaLabel}
|
|
286
|
+
onclose={onDialogClose}
|
|
287
|
+
onpointerdown={onPointerDown}
|
|
288
|
+
>
|
|
289
|
+
<div class="panel">
|
|
290
|
+
<input
|
|
291
|
+
bind:this={input}
|
|
292
|
+
bind:value={query}
|
|
293
|
+
oninput={onQueryInput}
|
|
294
|
+
onkeydown={onInputKeydown}
|
|
295
|
+
class="input"
|
|
296
|
+
type="text"
|
|
297
|
+
{placeholder}
|
|
298
|
+
spellcheck="false"
|
|
299
|
+
autocomplete="off"
|
|
300
|
+
role="combobox"
|
|
301
|
+
aria-label={ariaLabel}
|
|
302
|
+
aria-autocomplete="list"
|
|
303
|
+
aria-expanded={flat.length > 0}
|
|
304
|
+
aria-controls={listId}
|
|
305
|
+
aria-activedescendant={flat.length ? optionId(active) : undefined}
|
|
306
|
+
/>
|
|
307
|
+
|
|
308
|
+
<div class="results" bind:this={list}>
|
|
309
|
+
{#if flat.length === 0}
|
|
310
|
+
{#if empty}
|
|
311
|
+
{@render empty(query)}
|
|
312
|
+
{:else}
|
|
313
|
+
<p class="state">{emptyText}</p>
|
|
314
|
+
{/if}
|
|
315
|
+
{:else}
|
|
316
|
+
<ul id={listId} class="listbox" role="listbox" aria-label={ariaLabel}>
|
|
317
|
+
{#each groups as group (group.label)}
|
|
318
|
+
<li
|
|
319
|
+
class="group"
|
|
320
|
+
role={group.label ? 'group' : 'presentation'}
|
|
321
|
+
aria-label={group.label ?? undefined}
|
|
322
|
+
>
|
|
323
|
+
{#if group.label}
|
|
324
|
+
<span class="group-label" aria-hidden="true">{group.label}</span>
|
|
325
|
+
{/if}
|
|
326
|
+
<ul role="presentation">
|
|
327
|
+
{#each group.entries as { item: it, index } (it.id)}
|
|
328
|
+
<li role="presentation">
|
|
329
|
+
{#if it.href && !it.disabled}
|
|
330
|
+
<a
|
|
331
|
+
id={optionId(index)}
|
|
332
|
+
role="option"
|
|
333
|
+
aria-selected={index === active}
|
|
334
|
+
class="row"
|
|
335
|
+
class:active={index === active}
|
|
336
|
+
href={it.href}
|
|
337
|
+
tabindex="-1"
|
|
338
|
+
onpointerdown={onRowPointerDown}
|
|
339
|
+
onmousemove={() => (active = index)}
|
|
340
|
+
onclick={() => choose(it)}
|
|
341
|
+
>
|
|
342
|
+
{@render body(it, index)}
|
|
343
|
+
</a>
|
|
344
|
+
{:else}
|
|
345
|
+
<button
|
|
346
|
+
id={optionId(index)}
|
|
347
|
+
type="button"
|
|
348
|
+
role="option"
|
|
349
|
+
aria-selected={index === active}
|
|
350
|
+
aria-disabled={it.disabled || undefined}
|
|
351
|
+
class="row"
|
|
352
|
+
class:active={index === active}
|
|
353
|
+
class:disabled={it.disabled}
|
|
354
|
+
tabindex="-1"
|
|
355
|
+
onpointerdown={onRowPointerDown}
|
|
356
|
+
onmousemove={() => {
|
|
357
|
+
if (!it.disabled) active = index
|
|
358
|
+
}}
|
|
359
|
+
onclick={() => choose(it)}
|
|
360
|
+
>
|
|
361
|
+
{@render body(it, index)}
|
|
362
|
+
</button>
|
|
363
|
+
{/if}
|
|
364
|
+
</li>
|
|
365
|
+
{/each}
|
|
366
|
+
</ul>
|
|
367
|
+
</li>
|
|
368
|
+
{/each}
|
|
369
|
+
</ul>
|
|
370
|
+
{/if}
|
|
371
|
+
</div>
|
|
372
|
+
|
|
373
|
+
{#if footer}
|
|
374
|
+
<footer class="foot">{@render footer()}</footer>
|
|
375
|
+
{:else if footerText !== false && footerText !== ''}
|
|
376
|
+
<footer class="foot">{footerText}</footer>
|
|
377
|
+
{/if}
|
|
378
|
+
</div>
|
|
379
|
+
</dialog>
|
|
380
|
+
|
|
381
|
+
{#snippet body(it: T, index: number)}
|
|
382
|
+
{#if itemSnippet}
|
|
383
|
+
{@render itemSnippet(it, { active: index === active })}
|
|
384
|
+
{:else}
|
|
385
|
+
<span class="label">{it.label}</span>
|
|
386
|
+
{#if it.hint}
|
|
387
|
+
<span class="hint">{it.hint}</span>
|
|
388
|
+
{/if}
|
|
389
|
+
{/if}
|
|
390
|
+
{/snippet}
|
|
391
|
+
|
|
392
|
+
<style>.ss-search-palette {
|
|
393
|
+
padding: 0;
|
|
394
|
+
border: 1px solid var(--ss-line-strong);
|
|
395
|
+
background: var(--ss-bg-elev);
|
|
396
|
+
color: var(--ss-fg);
|
|
397
|
+
box-shadow: var(--ss-shadow-pop);
|
|
398
|
+
font-family: var(--ss-font-mono);
|
|
399
|
+
width: var(--ss-search-palette-w, 40rem);
|
|
400
|
+
max-width: var(--ss-search-palette-max, calc(100vw - var(--ss-s-8)));
|
|
401
|
+
margin: var(--ss-search-palette-top, 15vh) auto auto;
|
|
402
|
+
overflow: hidden;
|
|
403
|
+
}
|
|
404
|
+
.ss-search-palette::backdrop {
|
|
405
|
+
background: var(--ss-search-palette-backdrop, rgba(0, 0, 0, 0.62));
|
|
406
|
+
}
|
|
407
|
+
.ss-search-palette[data-size-variant=sm] {
|
|
408
|
+
--ss-search-palette-w: 32rem;
|
|
409
|
+
}
|
|
410
|
+
.ss-search-palette[data-size-variant=lg] {
|
|
411
|
+
--ss-search-palette-w: 48rem;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
.panel {
|
|
415
|
+
display: flex;
|
|
416
|
+
flex-direction: column;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
.input {
|
|
420
|
+
width: 100%;
|
|
421
|
+
padding: var(--ss-search-palette-input-pad, var(--ss-s-4)) var(--ss-s-4);
|
|
422
|
+
background: transparent;
|
|
423
|
+
border: none;
|
|
424
|
+
border-bottom: 1px solid var(--ss-line);
|
|
425
|
+
color: var(--ss-fg);
|
|
426
|
+
font-family: inherit;
|
|
427
|
+
font-size: var(--ss-search-palette-input-size, var(--ss-ui-lg, var(--ss-size-body)));
|
|
428
|
+
}
|
|
429
|
+
.input:focus {
|
|
430
|
+
outline: none;
|
|
431
|
+
border-bottom-color: var(--ss-primary);
|
|
432
|
+
}
|
|
433
|
+
.input::placeholder {
|
|
434
|
+
color: var(--ss-fg-muted);
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
.results {
|
|
438
|
+
overflow-y: auto;
|
|
439
|
+
max-height: var(--ss-search-palette-list-h, 60vh);
|
|
440
|
+
overscroll-behavior: contain;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
ul {
|
|
444
|
+
list-style: none;
|
|
445
|
+
margin: 0;
|
|
446
|
+
padding: 0;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
.state {
|
|
450
|
+
margin: 0;
|
|
451
|
+
padding: var(--ss-s-4);
|
|
452
|
+
font-size: var(--ss-ui-sm);
|
|
453
|
+
color: var(--ss-fg-muted);
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
.group-label {
|
|
457
|
+
display: block;
|
|
458
|
+
padding: var(--ss-s-3) var(--ss-s-4) var(--ss-s-1);
|
|
459
|
+
font-size: var(--ss-ui-xs);
|
|
460
|
+
text-transform: uppercase;
|
|
461
|
+
letter-spacing: 0.08em;
|
|
462
|
+
color: var(--ss-fg-muted);
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
.row {
|
|
466
|
+
display: block;
|
|
467
|
+
width: 100%;
|
|
468
|
+
padding: var(--ss-s-2) var(--ss-s-4);
|
|
469
|
+
border: none;
|
|
470
|
+
border-left: 2px solid transparent;
|
|
471
|
+
padding-left: calc(var(--ss-s-4) - 2px);
|
|
472
|
+
background: transparent;
|
|
473
|
+
color: var(--ss-fg);
|
|
474
|
+
font-family: inherit;
|
|
475
|
+
text-align: left;
|
|
476
|
+
text-decoration: none;
|
|
477
|
+
cursor: pointer;
|
|
478
|
+
}
|
|
479
|
+
.row.active {
|
|
480
|
+
background: var(--ss-primary-soft);
|
|
481
|
+
border-left-color: var(--ss-primary);
|
|
482
|
+
}
|
|
483
|
+
.row.disabled {
|
|
484
|
+
color: var(--ss-fg-faint, var(--ss-fg-muted));
|
|
485
|
+
cursor: default;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
.label {
|
|
489
|
+
display: block;
|
|
490
|
+
font-size: var(--ss-ui-md);
|
|
491
|
+
line-height: var(--ss-leading, 1.5);
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
.hint {
|
|
495
|
+
display: block;
|
|
496
|
+
margin-top: 2px;
|
|
497
|
+
font-size: var(--ss-ui-sm);
|
|
498
|
+
color: var(--ss-fg-muted);
|
|
499
|
+
white-space: nowrap;
|
|
500
|
+
overflow: hidden;
|
|
501
|
+
text-overflow: ellipsis;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
.foot {
|
|
505
|
+
padding: var(--ss-s-2) var(--ss-s-4);
|
|
506
|
+
border-top: 1px solid var(--ss-line);
|
|
507
|
+
font-size: var(--ss-ui-xs);
|
|
508
|
+
color: var(--ss-fg-muted);
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
@media (max-width: 767px) {
|
|
512
|
+
.ss-search-palette {
|
|
513
|
+
margin: 0;
|
|
514
|
+
width: 100vw;
|
|
515
|
+
max-width: 100vw;
|
|
516
|
+
height: 100dvh;
|
|
517
|
+
max-height: 100dvh;
|
|
518
|
+
}
|
|
519
|
+
.panel {
|
|
520
|
+
height: 100%;
|
|
521
|
+
}
|
|
522
|
+
.results {
|
|
523
|
+
flex: 1;
|
|
524
|
+
max-height: none;
|
|
525
|
+
}
|
|
526
|
+
}</style>
|