@spectral_labs/ui 1.6.1 → 1.7.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/command/__tests__/CommandInlineDirectTest.svelte +29 -0
- package/dist/components/command/__tests__/CommandInlineTest.svelte +89 -0
- package/dist/components/command/__tests__/command-inline.test.svelte.js +392 -0
- package/dist/components/command/command.svelte.d.ts +30 -0
- package/dist/components/command/command.svelte.d.ts.map +1 -1
- package/dist/components/command/command.svelte.js +85 -4
- package/dist/components/command/components/command-group.svelte +25 -15
- package/dist/components/command/components/command-group.svelte.d.ts.map +1 -1
- package/dist/components/command/components/command-input.svelte +54 -5
- package/dist/components/command/components/command-input.svelte.d.ts +1 -1
- package/dist/components/command/components/command-input.svelte.d.ts.map +1 -1
- package/dist/components/command/components/command-list.svelte +22 -3
- package/dist/components/command/components/command-list.svelte.d.ts.map +1 -1
- package/dist/components/command/components/command-root.svelte +43 -4
- package/dist/components/command/components/command-root.svelte.d.ts.map +1 -1
- package/dist/components/command/index.d.ts +1 -0
- package/dist/components/command/index.d.ts.map +1 -1
- package/dist/components/command/types.d.ts +59 -4
- package/dist/components/command/types.d.ts.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import CommandInput from '../components/command-input.svelte'
|
|
3
|
+
import CommandItem from '../components/command-item.svelte'
|
|
4
|
+
import CommandList from '../components/command-list.svelte'
|
|
5
|
+
import CommandRoot from '../components/command-root.svelte'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The same inline composition with NO `Command.Group`.
|
|
9
|
+
*
|
|
10
|
+
* The group and the item each gate rendering on their own predicate, and
|
|
11
|
+
* both read the registry. Reviewing #224 turned up two distinct failures
|
|
12
|
+
* behind one symptom, so the group must be removable from the picture for a
|
|
13
|
+
* test to say which mechanism it is exercising.
|
|
14
|
+
*/
|
|
15
|
+
let open = $state(false)
|
|
16
|
+
let value = $state('')
|
|
17
|
+
|
|
18
|
+
export function setOpen(next: boolean) {
|
|
19
|
+
open = next
|
|
20
|
+
}
|
|
21
|
+
</script>
|
|
22
|
+
|
|
23
|
+
<CommandRoot mode="inline" bind:open bind:value>
|
|
24
|
+
<CommandInput class="probe-input" />
|
|
25
|
+
<CommandList>
|
|
26
|
+
<CommandItem value="copy" keywords={['duplicate']}>Copy</CommandItem>
|
|
27
|
+
<CommandItem value="paste">Paste</CommandItem>
|
|
28
|
+
</CommandList>
|
|
29
|
+
</CommandRoot>
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { CommandMode } from '../command.svelte.js'
|
|
3
|
+
import CommandEmpty from '../components/command-empty.svelte'
|
|
4
|
+
import CommandGroup from '../components/command-group.svelte'
|
|
5
|
+
import CommandInput from '../components/command-input.svelte'
|
|
6
|
+
import CommandItem from '../components/command-item.svelte'
|
|
7
|
+
import CommandList from '../components/command-list.svelte'
|
|
8
|
+
import CommandRoot from '../components/command-root.svelte'
|
|
9
|
+
|
|
10
|
+
type Entry = { value: string; label: string; keywords?: string[] }
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Harness for the `inline` composition: a field that outlives its results.
|
|
14
|
+
*
|
|
15
|
+
* The harness OWNS `open` and `entries` as local state and exposes accessors,
|
|
16
|
+
* rather than taking them as props a test would try to reassign — Svelte 5
|
|
17
|
+
* props are not writable through the object `mount()` returns. Owning them
|
|
18
|
+
* here is also closer to real use: `bind:open` lets the Root push a close
|
|
19
|
+
* back (Escape, outside pointer) and the test observes the same value the
|
|
20
|
+
* consumer would.
|
|
21
|
+
*
|
|
22
|
+
* `entries` is mutable so a test can change the COLLECTION between two
|
|
23
|
+
* openings — the case where a stale selection, a dead ARIA reference or a
|
|
24
|
+
* lost first arrow would actually show up.
|
|
25
|
+
*/
|
|
26
|
+
const {
|
|
27
|
+
initialOpen = false,
|
|
28
|
+
initialEntries = [
|
|
29
|
+
{ value: 'copy', label: 'Copy', keywords: ['duplicate'] },
|
|
30
|
+
{ value: 'paste', label: 'Paste' },
|
|
31
|
+
{ value: 'cut', label: 'Cut' },
|
|
32
|
+
] as Entry[],
|
|
33
|
+
mode = 'inline' as CommandMode,
|
|
34
|
+
portalTo = undefined,
|
|
35
|
+
trapFocus = undefined,
|
|
36
|
+
onOpenChange = undefined,
|
|
37
|
+
onEscapeKeydown = undefined,
|
|
38
|
+
onSelect = undefined,
|
|
39
|
+
inputAttrs = undefined,
|
|
40
|
+
}: {
|
|
41
|
+
initialOpen?: boolean
|
|
42
|
+
initialEntries?: Entry[]
|
|
43
|
+
mode?: CommandMode
|
|
44
|
+
portalTo?: HTMLElement | string
|
|
45
|
+
trapFocus?: boolean
|
|
46
|
+
onOpenChange?: (open: boolean) => void
|
|
47
|
+
onEscapeKeydown?: (event: KeyboardEvent) => void
|
|
48
|
+
onSelect?: (value: string) => void
|
|
49
|
+
inputAttrs?: Record<string, unknown>
|
|
50
|
+
} = $props()
|
|
51
|
+
|
|
52
|
+
let open = $state(initialOpen)
|
|
53
|
+
let entries = $state(initialEntries)
|
|
54
|
+
let value = $state('')
|
|
55
|
+
let element = $state<HTMLInputElement | null>(null)
|
|
56
|
+
|
|
57
|
+
export function setOpen(next: boolean) {
|
|
58
|
+
open = next
|
|
59
|
+
}
|
|
60
|
+
export function getOpen() {
|
|
61
|
+
return open
|
|
62
|
+
}
|
|
63
|
+
export function setEntries(next: Entry[]) {
|
|
64
|
+
entries = next
|
|
65
|
+
}
|
|
66
|
+
export function getElement() {
|
|
67
|
+
return element
|
|
68
|
+
}
|
|
69
|
+
</script>
|
|
70
|
+
|
|
71
|
+
<CommandRoot bind:value bind:open {mode} {portalTo} {trapFocus} {onOpenChange} {onEscapeKeydown}>
|
|
72
|
+
<CommandInput
|
|
73
|
+
placeholder="Search..."
|
|
74
|
+
wrapperClass="probe-wrapper"
|
|
75
|
+
class="probe-input"
|
|
76
|
+
bind:element
|
|
77
|
+
{...inputAttrs ?? {}}
|
|
78
|
+
/>
|
|
79
|
+
<CommandList>
|
|
80
|
+
<CommandEmpty>No results found.</CommandEmpty>
|
|
81
|
+
<CommandGroup heading="Actions">
|
|
82
|
+
{#each entries as entry (entry.value)}
|
|
83
|
+
<CommandItem value={entry.value} keywords={entry.keywords} {onSelect}>
|
|
84
|
+
{entry.label}
|
|
85
|
+
</CommandItem>
|
|
86
|
+
{/each}
|
|
87
|
+
</CommandGroup>
|
|
88
|
+
</CommandList>
|
|
89
|
+
</CommandRoot>
|
|
@@ -0,0 +1,392 @@
|
|
|
1
|
+
import { flushSync, mount, unmount } from 'svelte';
|
|
2
|
+
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
3
|
+
import { EscapeLayerState, resetEscapeLayerStack } from '../../../internal/escape-layer/index.js';
|
|
4
|
+
import CommandInlineDirectTest from './CommandInlineDirectTest.svelte';
|
|
5
|
+
import CommandInlineTest from './CommandInlineTest.svelte';
|
|
6
|
+
/**
|
|
7
|
+
* `mode: 'inline'` — a persistent field with non-modal results.
|
|
8
|
+
*
|
|
9
|
+
* The contract these pins defend is not "the list is hidden": it is that a
|
|
10
|
+
* CLOSED palette is inert. It executes nothing, it claims no key, it holds no
|
|
11
|
+
* slot in the global layer stacks, and it names no option that is not on
|
|
12
|
+
* screen. `modal` is the default and must come through untouched.
|
|
13
|
+
*/
|
|
14
|
+
if (typeof globalThis.PointerEvent === 'undefined') {
|
|
15
|
+
// @ts-expect-error — minimal polyfill for testing
|
|
16
|
+
globalThis.PointerEvent = class PointerEvent extends MouseEvent {
|
|
17
|
+
constructor(type, init) {
|
|
18
|
+
super(type, init);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
describe('command — inline mode', () => {
|
|
23
|
+
let target;
|
|
24
|
+
let instance;
|
|
25
|
+
function setup(props = {}) {
|
|
26
|
+
target = document.createElement('div');
|
|
27
|
+
document.body.appendChild(target);
|
|
28
|
+
instance = mount(CommandInlineTest, { target, props });
|
|
29
|
+
flushSync();
|
|
30
|
+
return target;
|
|
31
|
+
}
|
|
32
|
+
afterEach(() => {
|
|
33
|
+
if (instance)
|
|
34
|
+
unmount(instance);
|
|
35
|
+
instance = undefined;
|
|
36
|
+
target?.remove();
|
|
37
|
+
resetEscapeLayerStack();
|
|
38
|
+
document.body.innerHTML = '';
|
|
39
|
+
});
|
|
40
|
+
const input = () => target.querySelector('input.probe-input');
|
|
41
|
+
const options = () => [...target.querySelectorAll('[data-command-item]')];
|
|
42
|
+
const activeId = () => input().getAttribute('aria-activedescendant');
|
|
43
|
+
function press(key, init = {}) {
|
|
44
|
+
const event = new KeyboardEvent('keydown', { key, bubbles: true, cancelable: true, ...init });
|
|
45
|
+
input().dispatchEvent(event);
|
|
46
|
+
flushSync();
|
|
47
|
+
return event;
|
|
48
|
+
}
|
|
49
|
+
function type(text) {
|
|
50
|
+
const el = input();
|
|
51
|
+
el.value = text;
|
|
52
|
+
el.dispatchEvent(new Event('input', { bubbles: true }));
|
|
53
|
+
flushSync();
|
|
54
|
+
}
|
|
55
|
+
// --- The field outlives the results ------------------------------------
|
|
56
|
+
it('closed: the field is rendered and NO option is', () => {
|
|
57
|
+
setup({ initialOpen: false });
|
|
58
|
+
expect(input()).toBeTruthy();
|
|
59
|
+
expect(options()).toHaveLength(0);
|
|
60
|
+
expect(target.querySelector('[role="listbox"]')).toBeNull();
|
|
61
|
+
});
|
|
62
|
+
it('closed: aria-expanded is false, and the relations that would dangle are absent', () => {
|
|
63
|
+
setup({ initialOpen: false });
|
|
64
|
+
expect(input().getAttribute('aria-expanded')).toBe('false');
|
|
65
|
+
expect(input().getAttribute('aria-controls')).toBeNull();
|
|
66
|
+
expect(activeId()).toBeNull();
|
|
67
|
+
});
|
|
68
|
+
it('the SAME input element survives a close and a reopen — never a second field', () => {
|
|
69
|
+
setup({ initialOpen: true });
|
|
70
|
+
const first = input();
|
|
71
|
+
instance.setOpen(false);
|
|
72
|
+
flushSync();
|
|
73
|
+
expect(options()).toHaveLength(0);
|
|
74
|
+
const whileClosed = input();
|
|
75
|
+
instance.setOpen(true);
|
|
76
|
+
flushSync();
|
|
77
|
+
// Node identity, not a lookalike: a remounted field would lose focus,
|
|
78
|
+
// selection and any consumer `bind:element` reference.
|
|
79
|
+
expect(whileClosed).toBe(first);
|
|
80
|
+
expect(input()).toBe(first);
|
|
81
|
+
});
|
|
82
|
+
it('exposes the real input element through `bind:element`, with no internal selector', () => {
|
|
83
|
+
setup({ initialOpen: false });
|
|
84
|
+
expect(instance.getElement()).toBe(input());
|
|
85
|
+
instance.getElement().focus();
|
|
86
|
+
expect(document.activeElement).toBe(input());
|
|
87
|
+
});
|
|
88
|
+
// --- A closed palette claims nothing -----------------------------------
|
|
89
|
+
it('closed: Enter executes nothing and is NOT prevented — it belongs to the page', () => {
|
|
90
|
+
const onSelect = vi.fn();
|
|
91
|
+
setup({ initialOpen: false, onSelect });
|
|
92
|
+
const event = press('Enter');
|
|
93
|
+
expect(onSelect).not.toHaveBeenCalled();
|
|
94
|
+
expect(event.defaultPrevented).toBe(false);
|
|
95
|
+
});
|
|
96
|
+
it('closed: a third-party Escape layer mounted FIRST still receives the key', () => {
|
|
97
|
+
const onEscapeKeydown = vi.fn();
|
|
98
|
+
// Mounted BEFORE the palette, so it sits at the BOTTOM of the stack. The
|
|
99
|
+
// stack offers the keystroke to the topmost 'close' layer and stops there:
|
|
100
|
+
// a palette that illicitly kept its layer while closed would be above this
|
|
101
|
+
// one and would swallow the key. Mounted after, it would win either way
|
|
102
|
+
// and the test would pass on a broken implementation.
|
|
103
|
+
const other = new EscapeLayerState({ enabled: true, onEscapeKeydown });
|
|
104
|
+
other.mount();
|
|
105
|
+
setup({ initialOpen: false });
|
|
106
|
+
const event = press('Escape');
|
|
107
|
+
expect(onEscapeKeydown).toHaveBeenCalledTimes(1);
|
|
108
|
+
expect(event.defaultPrevented).toBe(false);
|
|
109
|
+
other.unmount();
|
|
110
|
+
});
|
|
111
|
+
it('open: the palette DOES take the key from a layer mounted before it — the test above discriminates', () => {
|
|
112
|
+
const onEscapeKeydown = vi.fn();
|
|
113
|
+
const other = new EscapeLayerState({ enabled: true, onEscapeKeydown });
|
|
114
|
+
other.mount();
|
|
115
|
+
setup({ initialOpen: true });
|
|
116
|
+
press('Escape');
|
|
117
|
+
// Same order, results open: the palette is topmost and answers, so the
|
|
118
|
+
// previous test really observes the layer being absent, not the ordering.
|
|
119
|
+
expect(onEscapeKeydown).not.toHaveBeenCalled();
|
|
120
|
+
expect(instance.getOpen()).toBe(false);
|
|
121
|
+
other.unmount();
|
|
122
|
+
});
|
|
123
|
+
it('closed: Home and End stay with the caret — native editing is preserved', () => {
|
|
124
|
+
setup({ initialOpen: false });
|
|
125
|
+
expect(press('Home').defaultPrevented).toBe(false);
|
|
126
|
+
expect(press('End').defaultPrevented).toBe(false);
|
|
127
|
+
});
|
|
128
|
+
it('closed: an arrow OPENS the results instead of moving an unseen selection', () => {
|
|
129
|
+
const onOpenChange = vi.fn();
|
|
130
|
+
setup({ initialOpen: false, onOpenChange });
|
|
131
|
+
const event = press('ArrowDown');
|
|
132
|
+
expect(event.defaultPrevented).toBe(true);
|
|
133
|
+
expect(onOpenChange).toHaveBeenCalledWith(true);
|
|
134
|
+
expect(instance.getOpen()).toBe(true);
|
|
135
|
+
flushSync();
|
|
136
|
+
expect(options().length).toBeGreaterThan(0);
|
|
137
|
+
});
|
|
138
|
+
// --- Open: the engine is the one that shipped ---------------------------
|
|
139
|
+
it('open: keyword filtering still decides — a term absent from the label finds the option', () => {
|
|
140
|
+
setup({ initialOpen: true });
|
|
141
|
+
type('duplicate');
|
|
142
|
+
const values = options().map((el) => el.dataset.value);
|
|
143
|
+
expect(values).toEqual(['copy']);
|
|
144
|
+
});
|
|
145
|
+
it('open: arrow then Enter activates the option exactly ONCE', () => {
|
|
146
|
+
const onSelect = vi.fn();
|
|
147
|
+
setup({ initialOpen: true, onSelect });
|
|
148
|
+
press('ArrowDown');
|
|
149
|
+
const selected = options().find((el) => el.dataset.selected === 'true');
|
|
150
|
+
press('Enter');
|
|
151
|
+
expect(onSelect).toHaveBeenCalledTimes(1);
|
|
152
|
+
expect(onSelect).toHaveBeenCalledWith(selected.dataset.value);
|
|
153
|
+
});
|
|
154
|
+
it('open: aria-activedescendant names the rendered option’s DOM id, never its value', () => {
|
|
155
|
+
setup({ initialOpen: true });
|
|
156
|
+
const selected = options().find((el) => el.dataset.selected === 'true');
|
|
157
|
+
expect(activeId()).toBe(selected.id);
|
|
158
|
+
expect(activeId()).not.toBe(selected.dataset.value);
|
|
159
|
+
// And it resolves to a node that is really in the document.
|
|
160
|
+
expect(document.getElementById(activeId())).toBe(selected);
|
|
161
|
+
expect(input().getAttribute('aria-controls')).toBe(target.querySelector('[role="listbox"]').id);
|
|
162
|
+
});
|
|
163
|
+
// --- The reference cannot outlive what it names -------------------------
|
|
164
|
+
it('after filtering, the reference follows the surviving option', () => {
|
|
165
|
+
setup({ initialOpen: true });
|
|
166
|
+
type('duplicate');
|
|
167
|
+
const selected = options().find((el) => el.dataset.selected === 'true');
|
|
168
|
+
expect(selected.dataset.value).toBe('copy');
|
|
169
|
+
expect(activeId()).toBe(selected.id);
|
|
170
|
+
});
|
|
171
|
+
it('when nothing matches, there is no active option and no reference', () => {
|
|
172
|
+
setup({ initialOpen: true });
|
|
173
|
+
type('zzzzz');
|
|
174
|
+
expect(options()).toHaveLength(0);
|
|
175
|
+
expect(activeId()).toBeNull();
|
|
176
|
+
});
|
|
177
|
+
it('removing the active option leaves no dead reference', () => {
|
|
178
|
+
setup({
|
|
179
|
+
initialOpen: true,
|
|
180
|
+
initialEntries: [
|
|
181
|
+
{ value: 'copy', label: 'Copy' },
|
|
182
|
+
{ value: 'paste', label: 'Paste' },
|
|
183
|
+
],
|
|
184
|
+
});
|
|
185
|
+
expect(options().find((el) => el.dataset.selected === 'true').dataset.value).toBe('copy');
|
|
186
|
+
// The consumer drops the option that is currently active.
|
|
187
|
+
instance.setEntries([{ value: 'paste', label: 'Paste' }]);
|
|
188
|
+
flushSync();
|
|
189
|
+
const id = activeId();
|
|
190
|
+
expect(id).not.toBeNull();
|
|
191
|
+
const named = document.getElementById(id);
|
|
192
|
+
expect(named).not.toBeNull();
|
|
193
|
+
expect(named.dataset.value).toBe('paste');
|
|
194
|
+
});
|
|
195
|
+
it('reopening after the options changed: no stale selection, no dead reference, no lost first arrow', () => {
|
|
196
|
+
const onSelect = vi.fn();
|
|
197
|
+
setup({
|
|
198
|
+
initialOpen: true,
|
|
199
|
+
onSelect,
|
|
200
|
+
initialEntries: [
|
|
201
|
+
{ value: 'copy', label: 'Copy' },
|
|
202
|
+
{ value: 'paste', label: 'Paste' },
|
|
203
|
+
],
|
|
204
|
+
});
|
|
205
|
+
// Move the selection away from the first option, then close.
|
|
206
|
+
press('ArrowDown');
|
|
207
|
+
expect(options().find((el) => el.dataset.selected === 'true').dataset.value).toBe('paste');
|
|
208
|
+
instance.setOpen(false);
|
|
209
|
+
flushSync();
|
|
210
|
+
expect(activeId()).toBeNull();
|
|
211
|
+
// A completely different collection comes back.
|
|
212
|
+
instance.setEntries([
|
|
213
|
+
{ value: 'rename', label: 'Rename' },
|
|
214
|
+
{ value: 'delete', label: 'Delete' },
|
|
215
|
+
]);
|
|
216
|
+
instance.setOpen(true);
|
|
217
|
+
flushSync();
|
|
218
|
+
const selected = options().find((el) => el.dataset.selected === 'true');
|
|
219
|
+
// Not the stale `paste`, which no longer exists.
|
|
220
|
+
expect(selected.dataset.value).toBe('rename');
|
|
221
|
+
expect(activeId()).toBe(selected.id);
|
|
222
|
+
// The first arrow MOVES; it is not spent recovering from a stale value.
|
|
223
|
+
press('ArrowDown');
|
|
224
|
+
expect(options().find((el) => el.dataset.selected === 'true').dataset.value).toBe('delete');
|
|
225
|
+
press('Enter');
|
|
226
|
+
expect(onSelect).toHaveBeenCalledTimes(1);
|
|
227
|
+
expect(onSelect).toHaveBeenCalledWith('delete');
|
|
228
|
+
});
|
|
229
|
+
// --- A retained query must survive a remount (review of #224, [P1]) -----
|
|
230
|
+
/**
|
|
231
|
+
* One symptom — zero options after a close and a reopen with the query still
|
|
232
|
+
* in the field — hiding TWO mechanisms, so each gets its own pin.
|
|
233
|
+
*
|
|
234
|
+
* · `Command.Group` gated its children on `hasVisibleItems()`, a predicate
|
|
235
|
+
* over the very registry those children fill. Empty registry + retained
|
|
236
|
+
* query ⇒ children never mount ⇒ registry stays empty. A deadlock.
|
|
237
|
+
* · An item's `isVisible` reads `getScore()` from a plain Map during
|
|
238
|
+
* RENDER, while `registerItem` writes that score from an `$effect`, i.e.
|
|
239
|
+
* afterwards. Nothing woke the derived, so a matching item stayed hidden.
|
|
240
|
+
*
|
|
241
|
+
* Both make `aria-activedescendant` name a node that is not in the document,
|
|
242
|
+
* which is why every pin here resolves the id rather than trusting it.
|
|
243
|
+
*/
|
|
244
|
+
function activeElement() {
|
|
245
|
+
const id = activeId();
|
|
246
|
+
return id === null ? null : document.getElementById(id);
|
|
247
|
+
}
|
|
248
|
+
it('a query kept across a close and a reopen still renders its match, named and actionable', () => {
|
|
249
|
+
const onSelect = vi.fn();
|
|
250
|
+
setup({ initialOpen: true, onSelect });
|
|
251
|
+
type('duplicate');
|
|
252
|
+
expect(options().map((el) => el.dataset.value)).toEqual(['copy']);
|
|
253
|
+
instance.setOpen(false);
|
|
254
|
+
flushSync();
|
|
255
|
+
expect(options()).toHaveLength(0);
|
|
256
|
+
instance.setOpen(true);
|
|
257
|
+
flushSync();
|
|
258
|
+
expect(options().map((el) => el.dataset.value)).toEqual(['copy']);
|
|
259
|
+
// The reference resolves to a node that is really in the document.
|
|
260
|
+
expect(activeElement()).not.toBeNull();
|
|
261
|
+
expect(activeElement()).toBe(options()[0]);
|
|
262
|
+
press('Enter');
|
|
263
|
+
expect(onSelect).toHaveBeenCalledTimes(1);
|
|
264
|
+
expect(onSelect).toHaveBeenCalledWith('copy');
|
|
265
|
+
});
|
|
266
|
+
it('a query typed while CLOSED renders its match when an arrow opens the list', () => {
|
|
267
|
+
setup({ initialOpen: false });
|
|
268
|
+
type('duplicate');
|
|
269
|
+
expect(options()).toHaveLength(0);
|
|
270
|
+
press('ArrowDown');
|
|
271
|
+
expect(options().map((el) => el.dataset.value)).toEqual(['copy']);
|
|
272
|
+
expect(activeElement()).toBe(options()[0]);
|
|
273
|
+
});
|
|
274
|
+
it('same, with NO Command.Group: the item mechanism alone is enough to break it', () => {
|
|
275
|
+
target = document.createElement('div');
|
|
276
|
+
document.body.appendChild(target);
|
|
277
|
+
instance = mount(CommandInlineDirectTest, { target });
|
|
278
|
+
flushSync();
|
|
279
|
+
type('duplicate');
|
|
280
|
+
press('ArrowDown');
|
|
281
|
+
expect(options().map((el) => el.dataset.value)).toEqual(['copy']);
|
|
282
|
+
expect(activeElement()).not.toBeNull();
|
|
283
|
+
expect(activeElement()).toBe(options()[0]);
|
|
284
|
+
});
|
|
285
|
+
// --- Event composition --------------------------------------------------
|
|
286
|
+
it('a consumer onkeydown runs first and its preventDefault cancels the local handling', () => {
|
|
287
|
+
const onOpenChange = vi.fn();
|
|
288
|
+
setup({
|
|
289
|
+
initialOpen: false,
|
|
290
|
+
onOpenChange,
|
|
291
|
+
inputAttrs: { onkeydown: (e) => e.preventDefault() },
|
|
292
|
+
});
|
|
293
|
+
press('ArrowDown');
|
|
294
|
+
expect(onOpenChange).not.toHaveBeenCalled();
|
|
295
|
+
expect(instance.getOpen()).toBe(false);
|
|
296
|
+
});
|
|
297
|
+
it('a consumer onkeydown cannot retract a close already performed in the capture phase', () => {
|
|
298
|
+
const onEscapeKeydown = vi.fn();
|
|
299
|
+
setup({
|
|
300
|
+
initialOpen: true,
|
|
301
|
+
onEscapeKeydown,
|
|
302
|
+
inputAttrs: { onkeydown: (e) => e.preventDefault() },
|
|
303
|
+
});
|
|
304
|
+
press('Escape');
|
|
305
|
+
// The layer listens on `document` in capture — it read `defaultPrevented`
|
|
306
|
+
// before the input's handler ever ran. `onEscapeKeydown` is the hook.
|
|
307
|
+
expect(onEscapeKeydown).toHaveBeenCalledTimes(1);
|
|
308
|
+
expect(instance.getOpen()).toBe(false);
|
|
309
|
+
});
|
|
310
|
+
// --- Anchorable, and no focus trap --------------------------------------
|
|
311
|
+
it('renders IN PLACE with the inline chrome released, so results can be anchored', () => {
|
|
312
|
+
setup({ initialOpen: true });
|
|
313
|
+
const root = target.querySelector('.sp-command');
|
|
314
|
+
// In place — not portalled to document.body, which would break both the
|
|
315
|
+
// consumer's scoped CSS and any anchoring to the field.
|
|
316
|
+
expect(root.parentElement).not.toBe(document.body);
|
|
317
|
+
expect(target.contains(root)).toBe(true);
|
|
318
|
+
expect(root.dataset.mode).toBe('inline');
|
|
319
|
+
// The field and the results share one owner, which is what lets a consumer
|
|
320
|
+
// anchor the second to the first.
|
|
321
|
+
const field = root.querySelector('.sp-command-input-wrapper');
|
|
322
|
+
const list = root.querySelector('[role="listbox"]');
|
|
323
|
+
expect(field.parentElement).toBe(root);
|
|
324
|
+
expect(list.parentElement).toBe(root);
|
|
325
|
+
// Whether the released `overflow` really keeps the list unclipped is a
|
|
326
|
+
// LAYOUT fact, and this environment has no CSS at all — vite does not load
|
|
327
|
+
// the components' stylesheets into jsdom. It is proven in a real Chromium
|
|
328
|
+
// by `tests/e2e/command-inline.spec.ts`, on the docs page's live demo.
|
|
329
|
+
});
|
|
330
|
+
it('does not trap focus: an outside control keeps it', () => {
|
|
331
|
+
setup({ initialOpen: true });
|
|
332
|
+
const outside = document.createElement('button');
|
|
333
|
+
document.body.appendChild(outside);
|
|
334
|
+
outside.focus();
|
|
335
|
+
flushSync();
|
|
336
|
+
expect(document.activeElement).toBe(outside);
|
|
337
|
+
outside.remove();
|
|
338
|
+
});
|
|
339
|
+
it('the consumer names the field; the component keeps its structure', () => {
|
|
340
|
+
setup({
|
|
341
|
+
initialOpen: true,
|
|
342
|
+
inputAttrs: {
|
|
343
|
+
'aria-label': 'Site search',
|
|
344
|
+
'aria-describedby': 'hint-id',
|
|
345
|
+
title: 'Search',
|
|
346
|
+
'data-testid': 'field',
|
|
347
|
+
// Refused by the type; spread anyway to prove the markup order wins.
|
|
348
|
+
role: 'textbox',
|
|
349
|
+
id: 'consumer-id',
|
|
350
|
+
'aria-expanded': 'false',
|
|
351
|
+
'aria-activedescendant': 'nope',
|
|
352
|
+
},
|
|
353
|
+
});
|
|
354
|
+
const el = input();
|
|
355
|
+
// The consumer's naming and description reach the real combobox…
|
|
356
|
+
expect(el.getAttribute('aria-label')).toBe('Site search');
|
|
357
|
+
expect(el.getAttribute('aria-describedby')).toBe('hint-id');
|
|
358
|
+
expect(el.getAttribute('title')).toBe('Search');
|
|
359
|
+
expect(el.getAttribute('data-testid')).toBe('field');
|
|
360
|
+
// …while the structure stays the component's, whatever is spread.
|
|
361
|
+
expect(el.getAttribute('role')).toBe('combobox');
|
|
362
|
+
expect(el.id).not.toBe('consumer-id');
|
|
363
|
+
expect(el.getAttribute('aria-expanded')).toBe('true');
|
|
364
|
+
expect(el.getAttribute('aria-activedescendant')).not.toBe('nope');
|
|
365
|
+
expect(el.getAttribute('aria-activedescendant')).toBe(options().find((o) => o.dataset.selected === 'true').id);
|
|
366
|
+
});
|
|
367
|
+
it('exposes the wrapper through `wrapperClass` while `class` stays on the input', () => {
|
|
368
|
+
setup({ initialOpen: false });
|
|
369
|
+
const wrapper = target.querySelector('.sp-command-input-wrapper');
|
|
370
|
+
expect(wrapper.classList.contains('probe-wrapper')).toBe(true);
|
|
371
|
+
expect(input().classList.contains('probe-input')).toBe(true);
|
|
372
|
+
expect(input().classList.contains('probe-wrapper')).toBe(false);
|
|
373
|
+
});
|
|
374
|
+
// --- The default mode is untouched --------------------------------------
|
|
375
|
+
it('modal stays the default: closing unmounts the field itself', () => {
|
|
376
|
+
// `portalTo` keeps the modal content inside `target` for querying — the
|
|
377
|
+
// modal default is `document.body`, which is itself part of the contract.
|
|
378
|
+
target = document.createElement('div');
|
|
379
|
+
document.body.appendChild(target);
|
|
380
|
+
instance = mount(CommandInlineTest, {
|
|
381
|
+
target,
|
|
382
|
+
props: { mode: 'modal', initialOpen: true, portalTo: target },
|
|
383
|
+
});
|
|
384
|
+
flushSync();
|
|
385
|
+
expect(input()).toBeTruthy();
|
|
386
|
+
expect(input().getAttribute('aria-expanded')).toBe('true');
|
|
387
|
+
instance.setOpen(false);
|
|
388
|
+
flushSync();
|
|
389
|
+
expect(target.querySelector('input')).toBeNull();
|
|
390
|
+
expect(target.querySelector('.sp-command')).toBeNull();
|
|
391
|
+
});
|
|
392
|
+
});
|
|
@@ -4,6 +4,14 @@
|
|
|
4
4
|
* Manages search filtering, item scoring, keyboard navigation,
|
|
5
5
|
* and selection for a command palette interface.
|
|
6
6
|
*/
|
|
7
|
+
/**
|
|
8
|
+
* `'modal'` — the shipped behaviour: the Root exists only while open, and
|
|
9
|
+
* closing unmounts everything, layers included.
|
|
10
|
+
* `'inline'` — the Root and its field stay mounted; `open` then governs only
|
|
11
|
+
* whether the RESULTS are rendered, and the Escape / outside-pointer layers
|
|
12
|
+
* follow the results, never the field.
|
|
13
|
+
*/
|
|
14
|
+
export type CommandMode = 'modal' | 'inline';
|
|
7
15
|
export interface CommandItem {
|
|
8
16
|
id: string;
|
|
9
17
|
value: string;
|
|
@@ -18,6 +26,25 @@ export interface CommandState {
|
|
|
18
26
|
listId: string;
|
|
19
27
|
shouldFilter: boolean;
|
|
20
28
|
loop: boolean;
|
|
29
|
+
/** Composition mode of the owning Root. */
|
|
30
|
+
mode: CommandMode;
|
|
31
|
+
/** Whether the Root currently considers the palette open. */
|
|
32
|
+
open: boolean;
|
|
33
|
+
/** Whether a `Command.List` is actually rendered right now. */
|
|
34
|
+
listMounted: boolean;
|
|
35
|
+
/** Called by `Command.List` to report its own mount/unmount. */
|
|
36
|
+
setListMounted: (mounted: boolean) => void;
|
|
37
|
+
/**
|
|
38
|
+
* DOM id of the option that is BOTH active and rendered, or `null`.
|
|
39
|
+
*
|
|
40
|
+
* Resolved against the live registry — never from the stored selection
|
|
41
|
+
* alone — so it cannot outlive the option it names: filtering it out,
|
|
42
|
+
* unmounting the list, or removing the option all make it `null` on the
|
|
43
|
+
* spot. This is what `aria-activedescendant` reads.
|
|
44
|
+
*/
|
|
45
|
+
getActiveOptionId: () => string | null;
|
|
46
|
+
/** Asks the Root to open. No-op when it is already open. */
|
|
47
|
+
requestOpen: () => void;
|
|
21
48
|
setSearch: (query: string) => void;
|
|
22
49
|
getScore: (value: string) => number;
|
|
23
50
|
filter: (value: string, search: string, keywords?: string[]) => number;
|
|
@@ -47,7 +74,10 @@ export declare function createCommandState(options: {
|
|
|
47
74
|
filter: (value: string, search: string, keywords?: string[]) => number;
|
|
48
75
|
shouldFilter: boolean;
|
|
49
76
|
loop: boolean;
|
|
77
|
+
mode: CommandMode;
|
|
78
|
+
open: boolean;
|
|
50
79
|
onSearchChange?: (search: string) => void;
|
|
80
|
+
onRequestOpen?: () => void;
|
|
51
81
|
}): CommandState;
|
|
52
82
|
export declare function setCommandContext(state: CommandState): void;
|
|
53
83
|
export declare function getCommandContext(): CommandState;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"command.svelte.d.ts","sourceRoot":"","sources":["../../../src/lib/components/command/command.svelte.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAOH,MAAM,WAAW,WAAW;IAC1B,EAAE,EAAE,MAAM,CAAA;IACV,KAAK,EAAE,MAAM,CAAA;IACb,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAA;IACnB,QAAQ,CAAC,EAAE,OAAO,CAAA;IAClB,OAAO,CAAC,EAAE,MAAM,CAAA;CACjB;AAED,MAAM,WAAW,YAAY;IAC3B,MAAM,EAAE,MAAM,CAAA;IACd,aAAa,EAAE,MAAM,CAAA;IACrB,OAAO,EAAE,MAAM,CAAA;IACf,MAAM,EAAE,MAAM,CAAA;IACd,YAAY,EAAE,OAAO,CAAA;IACrB,IAAI,EAAE,OAAO,CAAA;IACb,SAAS,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAA;IAClC,QAAQ,EAAE,CAAC,KAAK,EAAE,MAAM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,CAAA;IAC9B,aAAa,EAAE,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,MAAM,KAAK,IAAI,CAAA;IACtE,aAAa,EAAE,MAAM,IAAI,CAAA;IACzB,QAAQ,EAAE,MAAM,WAAW,EAAE,CAAA;IAC7B,eAAe,EAAE,MAAM,WAAW,EAAE,CAAA;IACpC,eAAe,EAAE,MAAM,OAAO,CAAA;CAC/B;AAED;;;;;;;;;GASG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,EAAE,GAAG,MAAM,CAYxF;AA0CD,wBAAgB,kBAAkB,CAAC,OAAO,EAAE;IAC1C,MAAM,EAAE,MAAM,CAAA;IACd,MAAM,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,EAAE,KAAK,MAAM,CAAA;IACtE,YAAY,EAAE,OAAO,CAAA;IACrB,IAAI,EAAE,OAAO,CAAA;IACb,IAAI,EAAE,WAAW,CAAA;IACjB,IAAI,EAAE,OAAO,CAAA;IACb,cAAc,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,IAAI,CAAA;IACzC,aAAa,CAAC,EAAE,MAAM,IAAI,CAAA;CAC3B,GAAG,YAAY,CA6Mf;AAED,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,YAAY,GAAG,IAAI,CAE3D;AAED,wBAAgB,iBAAiB,IAAI,YAAY,CAEhD"}
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* Manages search filtering, item scoring, keyboard navigation,
|
|
5
5
|
* and selection for a command palette interface.
|
|
6
6
|
*/
|
|
7
|
-
import { getContext, setContext } from 'svelte';
|
|
7
|
+
import { getContext, setContext, untrack } from 'svelte';
|
|
8
8
|
import { useId } from '../../internal/helpers/index.js';
|
|
9
9
|
const KEY = Symbol('sp-command');
|
|
10
10
|
/**
|
|
@@ -76,7 +76,45 @@ export function createCommandState(options) {
|
|
|
76
76
|
// eslint-disable-next-line svelte/prefer-svelte-reactivity -- internal non-reactive scoring cache
|
|
77
77
|
const scores = new Map();
|
|
78
78
|
let selectedValue = $state('');
|
|
79
|
+
let listMounted = $state(false);
|
|
80
|
+
/**
|
|
81
|
+
* `items` and `scores` are deliberately PLAIN (a hot scoring cache). Anything
|
|
82
|
+
* derived from the COLLECTION therefore needs an explicit reactive beat, or
|
|
83
|
+
* removing an option would leave `aria-activedescendant` naming a node that
|
|
84
|
+
* no longer exists — silently, because no signal changed.
|
|
85
|
+
*/
|
|
86
|
+
let collectionVersion = $state(0);
|
|
87
|
+
/**
|
|
88
|
+
* `registerItem` / `unregisterItem` run from an item's `$effect`. A plain
|
|
89
|
+
* `collectionVersion += 1` there is a read-modify-WRITE of reactive state, so
|
|
90
|
+
* the effect would take a dependency on the very signal it bumps and loop
|
|
91
|
+
* forever. The read is untracked; only the write is observable.
|
|
92
|
+
*/
|
|
93
|
+
function bumpCollection() {
|
|
94
|
+
collectionVersion = untrack(() => collectionVersion) + 1;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* The selection as the user actually sees it.
|
|
98
|
+
*
|
|
99
|
+
* The stored `selectedValue` survives unregistration (an option removed by
|
|
100
|
+
* the consumer, or the whole list unmounted while an inline field stays put).
|
|
101
|
+
* Reading it raw would hand out a selection that no rendered option carries —
|
|
102
|
+
* a stale highlight, a dead ARIA reference, and a first arrow press spent
|
|
103
|
+
* recovering instead of moving. Resolving against the live enabled+visible
|
|
104
|
+
* set makes all three impossible by construction, whatever order mounts and
|
|
105
|
+
* unmounts happen to interleave in during a remount.
|
|
106
|
+
*/
|
|
107
|
+
function resolveSelected() {
|
|
108
|
+
void collectionVersion;
|
|
109
|
+
const enabled = getEnabledVisibleItems();
|
|
110
|
+
if (enabled.length === 0)
|
|
111
|
+
return '';
|
|
112
|
+
if (enabled.some((item) => item.value === selectedValue))
|
|
113
|
+
return selectedValue;
|
|
114
|
+
return enabled[0].value;
|
|
115
|
+
}
|
|
79
116
|
function updateScores(search) {
|
|
117
|
+
bumpCollection();
|
|
80
118
|
scores.clear();
|
|
81
119
|
if (!options.shouldFilter || search === '') {
|
|
82
120
|
for (const item of items)
|
|
@@ -89,6 +127,7 @@ export function createCommandState(options) {
|
|
|
89
127
|
}
|
|
90
128
|
}
|
|
91
129
|
function getVisibleItems() {
|
|
130
|
+
void collectionVersion;
|
|
92
131
|
return items.filter((item) => (scores.get(item.value) ?? 0) > 0);
|
|
93
132
|
}
|
|
94
133
|
function getEnabledVisibleItems() {
|
|
@@ -106,7 +145,11 @@ export function createCommandState(options) {
|
|
|
106
145
|
selectedValue = enabled[enabled.length - 1].value;
|
|
107
146
|
return;
|
|
108
147
|
}
|
|
109
|
-
|
|
148
|
+
// Résolue, pas brute : après un remontage la valeur stockée peut ne
|
|
149
|
+
// correspondre à aucune option rendue, et la navigation repartirait alors
|
|
150
|
+
// du début au lieu de continuer depuis ce qui est mis en évidence.
|
|
151
|
+
const current = resolveSelected();
|
|
152
|
+
const currentIndex = enabled.findIndex((item) => item.value === current);
|
|
110
153
|
if (direction === 'next') {
|
|
111
154
|
if (currentIndex === -1 || currentIndex >= enabled.length - 1) {
|
|
112
155
|
selectedValue = options.loop ? enabled[0].value : enabled[enabled.length - 1].value;
|
|
@@ -131,10 +174,39 @@ export function createCommandState(options) {
|
|
|
131
174
|
return options.search;
|
|
132
175
|
},
|
|
133
176
|
get selectedValue() {
|
|
134
|
-
return
|
|
177
|
+
return resolveSelected();
|
|
135
178
|
},
|
|
136
179
|
inputId,
|
|
137
180
|
listId,
|
|
181
|
+
get mode() {
|
|
182
|
+
return options.mode;
|
|
183
|
+
},
|
|
184
|
+
get open() {
|
|
185
|
+
return options.open;
|
|
186
|
+
},
|
|
187
|
+
get listMounted() {
|
|
188
|
+
return listMounted;
|
|
189
|
+
},
|
|
190
|
+
setListMounted(mounted) {
|
|
191
|
+
listMounted = mounted;
|
|
192
|
+
},
|
|
193
|
+
getActiveOptionId() {
|
|
194
|
+
void collectionVersion;
|
|
195
|
+
// No list rendered ⇒ no option rendered ⇒ nothing to point at.
|
|
196
|
+
if (!listMounted)
|
|
197
|
+
return null;
|
|
198
|
+
const active = resolveSelected();
|
|
199
|
+
if (active === '')
|
|
200
|
+
return null;
|
|
201
|
+
const item = getEnabledVisibleItems().find((candidate) => candidate.value === active);
|
|
202
|
+
// The DOM id of the rendered option, never its business `value`.
|
|
203
|
+
return item ? item.id : null;
|
|
204
|
+
},
|
|
205
|
+
requestOpen() {
|
|
206
|
+
if (options.open)
|
|
207
|
+
return;
|
|
208
|
+
options.onRequestOpen?.();
|
|
209
|
+
},
|
|
138
210
|
get shouldFilter() {
|
|
139
211
|
return options.shouldFilter;
|
|
140
212
|
},
|
|
@@ -151,11 +223,19 @@ export function createCommandState(options) {
|
|
|
151
223
|
}
|
|
152
224
|
},
|
|
153
225
|
getScore(value) {
|
|
226
|
+
// `scores` is a plain Map, so a reader that did not touch the collection
|
|
227
|
+
// beat would never see a score written AFTER it first ran. That is the
|
|
228
|
+
// case on every mount: an item's `isVisible` derived evaluates during
|
|
229
|
+
// render, while `registerItem` runs in its `$effect`, i.e. later. With a
|
|
230
|
+
// retained query the item scored 0 at render, matched at registration,
|
|
231
|
+
// and stayed hidden forever — no signal had changed.
|
|
232
|
+
void collectionVersion;
|
|
154
233
|
return scores.get(value) ?? 0;
|
|
155
234
|
},
|
|
156
235
|
filter: options.filter,
|
|
157
236
|
registerItem(item) {
|
|
158
237
|
items.push(item);
|
|
238
|
+
bumpCollection();
|
|
159
239
|
const score = options.shouldFilter && options.search
|
|
160
240
|
? options.filter(item.value, options.search, item.keywords)
|
|
161
241
|
: 1;
|
|
@@ -171,13 +251,14 @@ export function createCommandState(options) {
|
|
|
171
251
|
const item = items[index];
|
|
172
252
|
scores.delete(item.value);
|
|
173
253
|
items.splice(index, 1);
|
|
254
|
+
bumpCollection();
|
|
174
255
|
}
|
|
175
256
|
},
|
|
176
257
|
select(value) {
|
|
177
258
|
selectedValue = value;
|
|
178
259
|
},
|
|
179
260
|
getSelectedValue() {
|
|
180
|
-
return
|
|
261
|
+
return resolveSelected();
|
|
181
262
|
},
|
|
182
263
|
navigateItems,
|
|
183
264
|
selectCurrent() {
|
|
@@ -7,26 +7,36 @@
|
|
|
7
7
|
const ctx = getCommandContext()
|
|
8
8
|
const headingId = useId('sp-command-group')
|
|
9
9
|
|
|
10
|
-
// In filtered mode,
|
|
11
|
-
//
|
|
10
|
+
// In filtered mode, a group with no visible item is hidden — its heading
|
|
11
|
+
// alone would be a promise of results that are not there.
|
|
12
12
|
const isHidden = $derived(
|
|
13
13
|
!forceMount && ctx.shouldFilter && ctx.search !== '' && !ctx.hasVisibleItems(),
|
|
14
14
|
)
|
|
15
15
|
</script>
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
{
|
|
17
|
+
<!--
|
|
18
|
+
The group is HIDDEN, never unmounted. Items register from their own
|
|
19
|
+
`$effect`, so they must exist for the registry to learn about them: gating
|
|
20
|
+
the children on `hasVisibleItems()` — a predicate over that same registry —
|
|
21
|
+
deadlocked whenever the registry was empty while a query was retained. On
|
|
22
|
+
remount (an inline field whose list reopens, a list rebuilt after its options
|
|
23
|
+
changed) nothing ever registered again and the palette showed zero options,
|
|
24
|
+
permanently. Items already gate their OWN rendering on their score, so a
|
|
25
|
+
hidden group is an empty box: `hidden` takes it out of layout and out of the
|
|
26
|
+
accessibility tree, which is all the rule was ever asking for.
|
|
27
|
+
-->
|
|
28
|
+
<div
|
|
29
|
+
class="sp-command-group {className ?? ''}"
|
|
30
|
+
role="group"
|
|
31
|
+
aria-labelledby={heading ? headingId : undefined}
|
|
32
|
+
data-command-group
|
|
33
|
+
hidden={isHidden}
|
|
34
|
+
>
|
|
35
|
+
{#if heading}
|
|
36
|
+
<div class="sp-command-group-heading" id={headingId}>{heading}</div>
|
|
37
|
+
{/if}
|
|
38
|
+
{#if children}{@render children()}{/if}
|
|
39
|
+
</div>
|
|
30
40
|
|
|
31
41
|
<style>
|
|
32
42
|
.sp-command-group {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"command-group.svelte.d.ts","sourceRoot":"","sources":["../../../../src/lib/components/command/components/command-group.svelte.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAA;
|
|
1
|
+
{"version":3,"file":"command-group.svelte.d.ts","sourceRoot":"","sources":["../../../../src/lib/components/command/components/command-group.svelte.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAA;AAgCpD,QAAA,MAAM,YAAY,uDAAwC,CAAC;AAC3D,KAAK,YAAY,GAAG,UAAU,CAAC,OAAO,YAAY,CAAC,CAAC;AACpD,eAAe,YAAY,CAAC"}
|
|
@@ -3,12 +3,55 @@
|
|
|
3
3
|
import { kbd } from '../../../internal/helpers/index.js'
|
|
4
4
|
import { getCommandContext } from '../command.svelte.js'
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
let {
|
|
7
|
+
placeholder = 'Search...',
|
|
8
|
+
class: className,
|
|
9
|
+
wrapperClass,
|
|
10
|
+
element = $bindable(null),
|
|
11
|
+
// Pulled out of `rest` so the built-in behaviour can COMPOSE with it
|
|
12
|
+
// instead of being clobbered by the spread.
|
|
13
|
+
onkeydown: consumerKeydown,
|
|
14
|
+
...rest
|
|
15
|
+
}: CommandInputProps = $props()
|
|
7
16
|
const ctx = getCommandContext()
|
|
8
17
|
|
|
9
|
-
|
|
18
|
+
/**
|
|
19
|
+
* Every keyboard behaviour below is gated on the list being REALLY rendered,
|
|
20
|
+
* never on finding a node in the document. In `modal` the list is always
|
|
21
|
+
* rendered alongside the input, so this is constantly true and nothing about
|
|
22
|
+
* the shipped behaviour moves. In `inline` the field outlives the results,
|
|
23
|
+
* and that is where the distinction earns its keep.
|
|
24
|
+
*/
|
|
25
|
+
const open = $derived(ctx.listMounted)
|
|
26
|
+
|
|
27
|
+
/** The rendered option's DOM id, or nothing at all. Resolved through the live
|
|
28
|
+
* registry, so it cannot name an option that filtering, removal or a closed
|
|
29
|
+
* list has taken away. */
|
|
30
|
+
const activeOptionId = $derived(ctx.getActiveOptionId() ?? undefined)
|
|
31
|
+
|
|
32
|
+
function onkeydown(e: KeyboardEvent & { currentTarget: EventTarget & HTMLInputElement }) {
|
|
10
33
|
if (e.isComposing) return
|
|
11
34
|
|
|
35
|
+
// The consumer runs first and may cancel Spectral's local handling for this
|
|
36
|
+
// key. It cannot retract a close the Escape layer has already performed:
|
|
37
|
+
// that one runs earlier, on `document`, in the capture phase —
|
|
38
|
+
// `onEscapeKeydown` on `Command.Root` is the hook for it.
|
|
39
|
+
consumerKeydown?.(e)
|
|
40
|
+
if (e.defaultPrevented) return
|
|
41
|
+
|
|
42
|
+
if (!open) {
|
|
43
|
+
// Results closed. The field is an ordinary text input: Enter belongs to
|
|
44
|
+
// the surrounding form, Escape to whatever surface is actually listening,
|
|
45
|
+
// Home/End to the caret. Claiming them here would make a closed palette
|
|
46
|
+
// interfere with the page — and Enter, in particular, could activate an
|
|
47
|
+
// option nobody can see.
|
|
48
|
+
if (e.key === kbd.ARROW_DOWN || e.key === kbd.ARROW_UP) {
|
|
49
|
+
e.preventDefault()
|
|
50
|
+
ctx.requestOpen()
|
|
51
|
+
}
|
|
52
|
+
return
|
|
53
|
+
}
|
|
54
|
+
|
|
12
55
|
switch (e.key) {
|
|
13
56
|
case kbd.ARROW_DOWN: {
|
|
14
57
|
e.preventDefault()
|
|
@@ -54,7 +97,7 @@
|
|
|
54
97
|
}
|
|
55
98
|
</script>
|
|
56
99
|
|
|
57
|
-
<div class="sp-command-input-wrapper">
|
|
100
|
+
<div class="sp-command-input-wrapper {wrapperClass ?? ''}">
|
|
58
101
|
<svg
|
|
59
102
|
class="sp-command-input-icon"
|
|
60
103
|
xmlns="http://www.w3.org/2000/svg"
|
|
@@ -71,13 +114,19 @@
|
|
|
71
114
|
<circle cx="11" cy="11" r="8" />
|
|
72
115
|
<path d="m21 21-4.3-4.3" />
|
|
73
116
|
</svg>
|
|
117
|
+
<!-- `{...rest}` FIRST: a consumer may add attributes, never override the
|
|
118
|
+
field's identity (role, id, type) or the three relations computed from
|
|
119
|
+
the palette's own state — those come after and win. -->
|
|
74
120
|
<input
|
|
121
|
+
{...rest}
|
|
122
|
+
bind:this={element}
|
|
75
123
|
class="sp-command-input {className ?? ''}"
|
|
76
124
|
id={ctx.inputId}
|
|
77
125
|
type="text"
|
|
78
126
|
role="combobox"
|
|
79
|
-
aria-expanded=
|
|
80
|
-
aria-controls={ctx.listId}
|
|
127
|
+
aria-expanded={ctx.open}
|
|
128
|
+
aria-controls={open ? ctx.listId : undefined}
|
|
129
|
+
aria-activedescendant={activeOptionId}
|
|
81
130
|
aria-autocomplete="list"
|
|
82
131
|
autocomplete="off"
|
|
83
132
|
{placeholder}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { CommandInputProps } from '../types.js';
|
|
2
|
-
declare const CommandInput: import("svelte").Component<CommandInputProps, {}, "">;
|
|
2
|
+
declare const CommandInput: import("svelte").Component<CommandInputProps, {}, "element">;
|
|
3
3
|
type CommandInput = ReturnType<typeof CommandInput>;
|
|
4
4
|
export default CommandInput;
|
|
5
5
|
//# sourceMappingURL=command-input.svelte.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"command-input.svelte.d.ts","sourceRoot":"","sources":["../../../../src/lib/components/command/components/command-input.svelte.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAA;
|
|
1
|
+
{"version":3,"file":"command-input.svelte.d.ts","sourceRoot":"","sources":["../../../../src/lib/components/command/components/command-input.svelte.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAA;AAoHpD,QAAA,MAAM,YAAY,8DAAwC,CAAC;AAC3D,KAAK,YAAY,GAAG,UAAU,CAAC,OAAO,YAAY,CAAC,CAAC;AACpD,eAAe,YAAY,CAAC"}
|
|
@@ -4,11 +4,30 @@
|
|
|
4
4
|
|
|
5
5
|
const { label = 'Suggestions', class: className, children }: CommandListProps = $props()
|
|
6
6
|
const ctx = getCommandContext()
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* In `modal` the Root only exists while open, so this is always true and
|
|
10
|
+
* nothing changes. In `inline` the field outlives the results: the list must
|
|
11
|
+
* be ABSENT when closed, not merely hidden. Hiding it in CSS would leave
|
|
12
|
+
* every option in the document, where the Enter path could still reach one —
|
|
13
|
+
* an invisible command, executed.
|
|
14
|
+
*/
|
|
15
|
+
const rendered = $derived(ctx.mode !== 'inline' || ctx.open)
|
|
16
|
+
|
|
17
|
+
// The Root gates behaviour on the list being REALLY rendered, so the list is
|
|
18
|
+
// what reports it rather than anyone inferring it.
|
|
19
|
+
$effect(() => {
|
|
20
|
+
if (!rendered) return
|
|
21
|
+
ctx.setListMounted(true)
|
|
22
|
+
return () => ctx.setListMounted(false)
|
|
23
|
+
})
|
|
7
24
|
</script>
|
|
8
25
|
|
|
9
|
-
|
|
10
|
-
{
|
|
11
|
-
|
|
26
|
+
{#if rendered}
|
|
27
|
+
<div class="sp-command-list {className ?? ''}" id={ctx.listId} role="listbox" aria-label={label}>
|
|
28
|
+
{#if children}{@render children()}{/if}
|
|
29
|
+
</div>
|
|
30
|
+
{/if}
|
|
12
31
|
|
|
13
32
|
<style>
|
|
14
33
|
.sp-command-list {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"command-list.svelte.d.ts","sourceRoot":"","sources":["../../../../src/lib/components/command/components/command-list.svelte.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAA;
|
|
1
|
+
{"version":3,"file":"command-list.svelte.d.ts","sourceRoot":"","sources":["../../../../src/lib/components/command/components/command-list.svelte.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAA;AAsCnD,QAAA,MAAM,WAAW,sDAAwC,CAAC;AAC1D,KAAK,WAAW,GAAG,UAAU,CAAC,OAAO,WAAW,CAAC,CAAC;AAClD,eAAe,WAAW,CAAC"}
|
|
@@ -15,7 +15,8 @@
|
|
|
15
15
|
shouldFilter = true,
|
|
16
16
|
loop = true,
|
|
17
17
|
portalTo,
|
|
18
|
-
|
|
18
|
+
mode = 'modal',
|
|
19
|
+
trapFocus,
|
|
19
20
|
closeOnEscape = true,
|
|
20
21
|
closeOnOutsideClick = true,
|
|
21
22
|
onEscapeKeydown,
|
|
@@ -26,10 +27,27 @@
|
|
|
26
27
|
children,
|
|
27
28
|
}: CommandProps = $props()
|
|
28
29
|
|
|
30
|
+
/** A field living in an app bar must stay in the page's natural tab order,
|
|
31
|
+
* so `inline` flips the default — an explicit value still wins. */
|
|
32
|
+
const trapsFocus = $derived(trapFocus ?? mode !== 'inline')
|
|
33
|
+
/** Inline renders IN PLACE unless a target is given: the consumer's scoped
|
|
34
|
+
* CSS then applies, and the results can be anchored to the field. */
|
|
35
|
+
const portalDisabled = $derived(mode === 'inline' && portalTo === undefined)
|
|
36
|
+
|
|
29
37
|
const commandState = createCommandState({
|
|
30
38
|
get search() {
|
|
31
39
|
return value
|
|
32
40
|
},
|
|
41
|
+
get mode() {
|
|
42
|
+
return mode
|
|
43
|
+
},
|
|
44
|
+
get open() {
|
|
45
|
+
return open
|
|
46
|
+
},
|
|
47
|
+
onRequestOpen() {
|
|
48
|
+
open = true
|
|
49
|
+
onOpenChange?.(true)
|
|
50
|
+
},
|
|
33
51
|
get filter() {
|
|
34
52
|
return filter
|
|
35
53
|
},
|
|
@@ -83,11 +101,15 @@
|
|
|
83
101
|
},
|
|
84
102
|
})
|
|
85
103
|
|
|
104
|
+
// The two layers follow `open` in BOTH modes — which is exactly what makes a
|
|
105
|
+
// closed inline palette inert: no `document` listener, and no slot held in
|
|
106
|
+
// the global Escape / dismiss stacks that a surface below would have to
|
|
107
|
+
// yield to.
|
|
86
108
|
$effect(() => {
|
|
87
109
|
if (open && isBrowser) {
|
|
88
110
|
escapeLayer.mount()
|
|
89
111
|
dismissLayer.mount()
|
|
90
|
-
if (
|
|
112
|
+
if (trapsFocus && contentEl) {
|
|
91
113
|
focusScope.setElement(contentEl)
|
|
92
114
|
focusScope.mount()
|
|
93
115
|
}
|
|
@@ -109,12 +131,14 @@
|
|
|
109
131
|
})
|
|
110
132
|
</script>
|
|
111
133
|
|
|
112
|
-
|
|
113
|
-
|
|
134
|
+
<!-- Inline keeps the Root mounted whatever `open` is: the field is the point. -->
|
|
135
|
+
{#if mode === 'inline' || presence.isPresent}
|
|
136
|
+
<Portal to={portalTo} disabled={portalDisabled}>
|
|
114
137
|
<div
|
|
115
138
|
bind:this={contentEl}
|
|
116
139
|
class="sp-command {className ?? ''}"
|
|
117
140
|
data-command-root
|
|
141
|
+
data-mode={mode}
|
|
118
142
|
data-state={open ? 'open' : 'closed'}
|
|
119
143
|
>
|
|
120
144
|
{#if children}{@render children()}{/if}
|
|
@@ -132,4 +156,19 @@
|
|
|
132
156
|
background-color: var(--sp-sys-color-surface-container);
|
|
133
157
|
box-shadow: var(--sp-sys-elevation-3);
|
|
134
158
|
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Inline is not a panel: it is a field with results hanging off it. The
|
|
162
|
+
* panel chrome is dropped so the consumer can dress the field, and
|
|
163
|
+
* `overflow` is released so results positioned below are not clipped by the
|
|
164
|
+
* very element that owns them. `position: relative` makes the Root the
|
|
165
|
+
* containing block, which is what an anchored list needs.
|
|
166
|
+
*/
|
|
167
|
+
.sp-command[data-mode='inline'] {
|
|
168
|
+
position: relative;
|
|
169
|
+
overflow: visible;
|
|
170
|
+
border-radius: 0;
|
|
171
|
+
background-color: transparent;
|
|
172
|
+
box-shadow: none;
|
|
173
|
+
}
|
|
135
174
|
</style>
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"command-root.svelte.d.ts","sourceRoot":"","sources":["../../../../src/lib/components/command/components/command-root.svelte.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;
|
|
1
|
+
{"version":3,"file":"command-root.svelte.d.ts","sourceRoot":"","sources":["../../../../src/lib/components/command/components/command-root.svelte.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AA2J/C,QAAA,MAAM,WAAW,gEAAwC,CAAC;AAC1D,KAAK,WAAW,GAAG,UAAU,CAAC,OAAO,WAAW,CAAC,CAAC;AAClD,eAAe,WAAW,CAAC"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export { defaultFilter } from './command.svelte.js';
|
|
2
|
+
export type { CommandMode } from './command.svelte.js';
|
|
2
3
|
export { default as Empty } from './components/command-empty.svelte';
|
|
3
4
|
export { default as Group } from './components/command-group.svelte';
|
|
4
5
|
export { default as Input } from './components/command-input.svelte';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/lib/components/command/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AACnD,OAAO,EAAE,OAAO,IAAI,KAAK,EAAE,MAAM,mCAAmC,CAAA;AACpE,OAAO,EAAE,OAAO,IAAI,KAAK,EAAE,MAAM,mCAAmC,CAAA;AACpE,OAAO,EAAE,OAAO,IAAI,KAAK,EAAE,MAAM,mCAAmC,CAAA;AACpE,OAAO,EAAE,OAAO,IAAI,IAAI,EAAE,MAAM,kCAAkC,CAAA;AAClE,OAAO,EAAE,OAAO,IAAI,IAAI,EAAE,MAAM,kCAAkC,CAAA;AAClE,OAAO,EAAE,OAAO,IAAI,IAAI,EAAE,MAAM,kCAAkC,CAAA;AAClE,OAAO,EAAE,OAAO,IAAI,SAAS,EAAE,MAAM,uCAAuC,CAAA;AAC5E,YAAY,EACV,iBAAiB,EACjB,iBAAiB,EACjB,iBAAiB,EACjB,gBAAgB,EAChB,gBAAgB,EAChB,YAAY,EACZ,qBAAqB,GACtB,MAAM,YAAY,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/lib/components/command/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AACnD,YAAY,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAA;AACtD,OAAO,EAAE,OAAO,IAAI,KAAK,EAAE,MAAM,mCAAmC,CAAA;AACpE,OAAO,EAAE,OAAO,IAAI,KAAK,EAAE,MAAM,mCAAmC,CAAA;AACpE,OAAO,EAAE,OAAO,IAAI,KAAK,EAAE,MAAM,mCAAmC,CAAA;AACpE,OAAO,EAAE,OAAO,IAAI,IAAI,EAAE,MAAM,kCAAkC,CAAA;AAClE,OAAO,EAAE,OAAO,IAAI,IAAI,EAAE,MAAM,kCAAkC,CAAA;AAClE,OAAO,EAAE,OAAO,IAAI,IAAI,EAAE,MAAM,kCAAkC,CAAA;AAClE,OAAO,EAAE,OAAO,IAAI,SAAS,EAAE,MAAM,uCAAuC,CAAA;AAC5E,YAAY,EACV,iBAAiB,EACjB,iBAAiB,EACjB,iBAAiB,EACjB,gBAAgB,EAChB,gBAAgB,EAChB,YAAY,EACZ,qBAAqB,GACtB,MAAM,YAAY,CAAA"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Snippet } from 'svelte';
|
|
2
|
-
import type { HTMLAttributes } from 'svelte/elements';
|
|
2
|
+
import type { HTMLAttributes, HTMLInputAttributes } from 'svelte/elements';
|
|
3
|
+
import type { CommandMode } from './command.svelte.js';
|
|
3
4
|
export interface CommandProps {
|
|
4
5
|
/** Current search query (bindable). */
|
|
5
6
|
value?: string;
|
|
@@ -9,13 +10,39 @@ export interface CommandProps {
|
|
|
9
10
|
shouldFilter?: boolean;
|
|
10
11
|
/** Whether keyboard navigation loops at edges. @default true */
|
|
11
12
|
loop?: boolean;
|
|
13
|
+
/**
|
|
14
|
+
* Composition mode.
|
|
15
|
+
*
|
|
16
|
+
* - `'modal'` (default, unchanged behaviour): `open` controls whether the
|
|
17
|
+
* palette is MOUNTED — closing unmounts the content, the field included,
|
|
18
|
+
* together with the Escape and outside-pointer layers.
|
|
19
|
+
* - `'inline'`: the Root and its `Command.Input` stay mounted whatever `open`
|
|
20
|
+
* is, so a search field can live permanently in a bar; `open` then governs
|
|
21
|
+
* only whether `Command.List` is RENDERED, and the two layers follow the
|
|
22
|
+
* results. A closed inline palette listens to nothing: no document
|
|
23
|
+
* listener, no slot held in the global layer stacks.
|
|
24
|
+
*
|
|
25
|
+
* @default 'modal'
|
|
26
|
+
*/
|
|
27
|
+
mode?: CommandMode;
|
|
12
28
|
/**
|
|
13
29
|
* Controls whether the palette is mounted. When true, render content. Default: true.
|
|
30
|
+
* In `'inline'` mode this controls the RESULTS only — see `mode`.
|
|
14
31
|
*/
|
|
15
32
|
open?: boolean;
|
|
16
|
-
/**
|
|
33
|
+
/**
|
|
34
|
+
* Portal target. Default `document.body` in `'modal'` mode; in `'inline'`
|
|
35
|
+
* mode the content is rendered IN PLACE by default, so the consumer's scoped
|
|
36
|
+
* CSS applies and the results can be anchored to the field. Passing a target
|
|
37
|
+
* portals in either mode.
|
|
38
|
+
*/
|
|
17
39
|
portalTo?: HTMLElement | string;
|
|
18
|
-
/**
|
|
40
|
+
/**
|
|
41
|
+
* Trap focus within the palette.
|
|
42
|
+
* @default `true` in `'modal'` mode, `false` in `'inline'` mode — a field
|
|
43
|
+
* sitting in a bar must stay part of the page's natural tab order. An
|
|
44
|
+
* explicit value is honoured in both modes.
|
|
45
|
+
*/
|
|
19
46
|
trapFocus?: boolean;
|
|
20
47
|
/** Close on Escape keydown. Default: true. */
|
|
21
48
|
closeOnEscape?: boolean;
|
|
@@ -35,10 +62,38 @@ export interface CommandProps {
|
|
|
35
62
|
class?: string;
|
|
36
63
|
children?: Snippet;
|
|
37
64
|
}
|
|
38
|
-
|
|
65
|
+
/**
|
|
66
|
+
* Extends the native `input` attributes. A consumer's `title`, `name`,
|
|
67
|
+
* `aria-label` / `aria-labelledby` / `aria-describedby`, `data-testid`,
|
|
68
|
+
* `style` and native event handlers reach the real `role="combobox"` node.
|
|
69
|
+
*
|
|
70
|
+
* The field's IDENTITY stays with the component: `role`, `id`, `type` and the
|
|
71
|
+
* three computed relations (`aria-expanded`, `aria-controls`,
|
|
72
|
+
* `aria-activedescendant`) are rejected by the type — they are derived from
|
|
73
|
+
* the palette's own state and a consumer value could only contradict it.
|
|
74
|
+
*
|
|
75
|
+
* Consumer handlers COMPOSE with the built-in ones: yours runs first, and
|
|
76
|
+
* calling `preventDefault()` cancels Spectral's local keyboard handling for
|
|
77
|
+
* that key. It does NOT retract a close already performed by the Escape layer,
|
|
78
|
+
* which runs earlier, on `document`, in the capture phase — `onEscapeKeydown`
|
|
79
|
+
* on `Command.Root` is the public hook for that.
|
|
80
|
+
*/
|
|
81
|
+
export interface CommandInputProps extends Omit<HTMLInputAttributes, 'class' | 'children' | 'value' | 'type' | 'role' | 'id' | 'autocomplete' | 'oninput' | 'aria-expanded' | 'aria-controls' | 'aria-activedescendant' | 'aria-autocomplete'> {
|
|
39
82
|
/** Placeholder text for the search input. */
|
|
40
83
|
placeholder?: string;
|
|
84
|
+
/** Class placed on the `<input>` itself. */
|
|
41
85
|
class?: string;
|
|
86
|
+
/**
|
|
87
|
+
* Class placed on the wrapper that holds the icon and the input — the box
|
|
88
|
+
* that carries the padding and the bottom border. This is the hook for
|
|
89
|
+
* restyling the field as, say, a pill inside an app bar.
|
|
90
|
+
*/
|
|
91
|
+
wrapperClass?: string;
|
|
92
|
+
/**
|
|
93
|
+
* The real input element, bindable. Lets the consumer focus the field
|
|
94
|
+
* (a `Cmd/Ctrl+K` shortcut, say) without reaching for an internal selector.
|
|
95
|
+
*/
|
|
96
|
+
element?: HTMLInputElement | null;
|
|
42
97
|
}
|
|
43
98
|
export interface CommandListProps {
|
|
44
99
|
/** Accessible label for the listbox. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/lib/components/command/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,QAAQ,CAAA;AACrC,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAA;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/lib/components/command/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,QAAQ,CAAA;AACrC,OAAO,KAAK,EAAE,cAAc,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAA;AAC1E,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAA;AAEtD,MAAM,WAAW,YAAY;IAC3B,uCAAuC;IACvC,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,gGAAgG;IAChG,MAAM,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,EAAE,KAAK,MAAM,CAAA;IACvE,0EAA0E;IAC1E,YAAY,CAAC,EAAE,OAAO,CAAA;IACtB,gEAAgE;IAChE,IAAI,CAAC,EAAE,OAAO,CAAA;IACd;;;;;;;;;;;;;OAaG;IACH,IAAI,CAAC,EAAE,WAAW,CAAA;IAClB;;;OAGG;IACH,IAAI,CAAC,EAAE,OAAO,CAAA;IACd;;;;;OAKG;IACH,QAAQ,CAAC,EAAE,WAAW,GAAG,MAAM,CAAA;IAC/B;;;;;OAKG;IACH,SAAS,CAAC,EAAE,OAAO,CAAA;IACn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