@uifn/svelte 0.0.1 → 0.0.2
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/CHANGELOG.md +8 -0
- package/README.md +43 -50
- package/dist/index.js +3394 -2249
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +3392 -2249
- package/dist/index.mjs.map +1 -1
- package/package.json +27 -13
- package/src/Accordion.svelte +62 -30
- package/src/AccordionItem.svelte +11 -5
- package/src/AlertDialog.svelte +17 -4
- package/src/AlertDialogAction.svelte +6 -2
- package/src/AlertDialogCancel.svelte +6 -2
- package/src/AlertDialogContent.svelte +25 -2
- package/src/AlertDialogDescription.svelte +6 -2
- package/src/AlertDialogOverlay.svelte +6 -2
- package/src/AlertDialogPortal.svelte +6 -2
- package/src/AlertDialogTitle.svelte +6 -2
- package/src/AlertDialogTrigger.svelte +6 -2
- package/src/AvatarFallback.svelte +14 -3
- package/src/Checkbox.svelte +16 -5
- package/src/Collapsible.svelte +2 -1
- package/src/Combobox.svelte +147 -12
- package/src/ComboboxContent.svelte +58 -21
- package/src/ComboboxInput.svelte +34 -13
- package/src/ComboboxItem.svelte +63 -17
- package/src/ComboboxTrigger.svelte +17 -3
- package/src/ContextMenu.svelte +22 -3
- package/src/ContextMenuContent.svelte +71 -3
- package/src/ContextMenuItem.svelte +10 -2
- package/src/ContextMenuTrigger.svelte +21 -2
- package/src/Dialog.svelte +36 -10
- package/src/DialogClose.svelte +6 -2
- package/src/DialogContent.svelte +28 -2
- package/src/DialogDescription.svelte +6 -2
- package/src/DialogOverlay.svelte +6 -2
- package/src/DialogPortal.svelte +6 -2
- package/src/DialogTitle.svelte +6 -2
- package/src/DialogTrigger.svelte +23 -2
- package/src/DropdownMenu.svelte +44 -4
- package/src/DropdownMenuContent.svelte +88 -8
- package/src/DropdownMenuItem.svelte +10 -2
- package/src/DropdownMenuTrigger.svelte +17 -2
- package/src/HoverCard.svelte +4 -2
- package/src/HoverCardContent.svelte +1 -1
- package/src/MenuBar.svelte +79 -5
- package/src/MenuBarContent.svelte +113 -6
- package/src/MenuBarMenu.svelte +27 -4
- package/src/MenuBarTrigger.svelte +59 -4
- package/src/Popover.svelte +40 -6
- package/src/PopoverContent.svelte +69 -9
- package/src/PopoverTrigger.svelte +15 -2
- package/src/Progress.svelte +3 -1
- package/src/RadioGroup.svelte +18 -6
- package/src/RadioGroupItem.svelte +18 -8
- package/src/ScrollArea.svelte +85 -2
- package/src/ScrollAreaScrollbar.svelte +36 -11
- package/src/ScrollAreaThumb.svelte +83 -1
- package/src/ScrollAreaViewport.svelte +22 -3
- package/src/Select.svelte +138 -10
- package/src/SelectContent.svelte +78 -29
- package/src/SelectItem.svelte +47 -6
- package/src/SelectTrigger.svelte +43 -3
- package/src/SelectValue.svelte +6 -2
- package/src/Slider.svelte +22 -8
- package/src/SliderRange.svelte +19 -10
- package/src/Switch.svelte +16 -5
- package/src/Tabs.svelte +63 -9
- package/src/TabsContent.svelte +10 -5
- package/src/TabsList.svelte +6 -2
- package/src/TabsTrigger.svelte +37 -11
- package/src/Toast.svelte +14 -5
- package/src/ToastViewport.svelte +2 -2
- package/src/Toggle.svelte +14 -4
- package/src/ToggleGroup.svelte +10 -5
- package/src/Tooltip.svelte +12 -3
- package/src/TooltipContent.svelte +3 -2
- package/src/VirtualizedList.svelte +1 -0
- package/src/index.d.mts +109 -0
- package/src/index.ts +5 -1
- package/src/internal/body-scroll-lock.ts +22 -0
- package/src/internal/collection.ts +64 -0
- package/src/internal/context.ts +14 -0
- package/src/internal/dismissible.ts +10 -0
- package/src/internal/focus-trap.ts +10 -57
- package/src/internal/id.ts +20 -0
- package/src/internal/portal.ts +7 -14
- package/src/internal/position.ts +39 -93
- package/src/accordion.test.ts +0 -33
- package/src/alert-dialog.test.ts +0 -24
- package/src/checkbox.test.ts +0 -30
- package/src/combobox.test.ts +0 -24
- package/src/context-menu.test.ts +0 -20
- package/src/dialog.test.ts +0 -26
- package/src/dropdown-menu.test.ts +0 -20
- package/src/popover.test.ts +0 -20
- package/src/progress.test.ts +0 -17
- package/src/radio-group.test.ts +0 -22
- package/src/scroll-area.test.ts +0 -13
- package/src/select.test.ts +0 -23
- package/src/slider.test.ts +0 -16
- package/src/svelte-shims.d.ts +0 -4
- package/src/switch.test.ts +0 -32
- package/src/tabs.test.ts +0 -28
- package/src/test/setup.ts +0 -11
- package/src/testing/AccordionTest.svelte +0 -19
- package/src/testing/AlertDialogTest.svelte +0 -26
- package/src/testing/ComboboxTest.svelte +0 -21
- package/src/testing/ContextMenuTest.svelte +0 -16
- package/src/testing/DialogTest.svelte +0 -22
- package/src/testing/DropdownMenuTest.svelte +0 -20
- package/src/testing/PopoverTest.svelte +0 -12
- package/src/testing/RadioGroupTest.svelte +0 -12
- package/src/testing/ScrollAreaTest.svelte +0 -17
- package/src/testing/SelectTest.svelte +0 -29
- package/src/testing/SliderTest.svelte +0 -15
- package/src/testing/TabsTest.svelte +0 -17
- package/src/testing/ToastTest.svelte +0 -19
- package/src/testing/ToolbarTest.svelte +0 -12
- package/src/testing/TooltipTest.svelte +0 -12
- package/src/toast.test.ts +0 -40
- package/src/toggle.test.ts +0 -20
- package/src/toolbar.test.ts +0 -16
- package/src/tooltip.test.ts +0 -32
package/src/Combobox.svelte
CHANGED
|
@@ -1,12 +1,40 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
2
|
import { createCombobox } from '@uifn/core/primitives/combobox';
|
|
3
|
+
import type { ComboboxItem } from '@uifn/core/primitives/combobox';
|
|
3
4
|
import { setContext } from 'svelte';
|
|
5
|
+
import { createSveltePublicId } from './internal/id';
|
|
6
|
+
|
|
7
|
+
interface ComboboxContext {
|
|
8
|
+
value: string;
|
|
9
|
+
inputValue: string;
|
|
10
|
+
open: boolean;
|
|
11
|
+
disabled: boolean;
|
|
12
|
+
contentId: string;
|
|
13
|
+
highlightedItem: string | null;
|
|
14
|
+
filteredItems: string[];
|
|
15
|
+
trigger: HTMLElement | null;
|
|
16
|
+
input: HTMLInputElement | null;
|
|
17
|
+
setValue: (value: string) => void;
|
|
18
|
+
setInputValue: (value: string) => void;
|
|
19
|
+
setOpen: (value: boolean) => void;
|
|
20
|
+
setHighlightedItem: (value: string | null) => void;
|
|
21
|
+
registerTrigger: (el: HTMLElement) => void;
|
|
22
|
+
registerInput: (el: HTMLInputElement) => void;
|
|
23
|
+
registerItem: (item: ComboboxItem) => void;
|
|
24
|
+
unregisterItem: (value: string) => void;
|
|
25
|
+
registerItemElement: (value: string, element: HTMLElement) => void;
|
|
26
|
+
unregisterItemElement: (value: string) => void;
|
|
27
|
+
getItemId: (value: string) => string | undefined;
|
|
28
|
+
scrollItemIntoView: (value: string | null | undefined) => void;
|
|
29
|
+
handleKeyDown: (key: string, currentItem?: string | null) => string | null;
|
|
30
|
+
}
|
|
4
31
|
|
|
5
32
|
interface Props {
|
|
6
33
|
value?: string;
|
|
7
34
|
defaultValue?: string;
|
|
8
35
|
inputValue?: string;
|
|
9
36
|
defaultInputValue?: string;
|
|
37
|
+
items?: ComboboxItem[];
|
|
10
38
|
onValueChange?: (value: string) => void;
|
|
11
39
|
onInputValueChange?: (inputValue: string) => void;
|
|
12
40
|
open?: boolean;
|
|
@@ -22,6 +50,7 @@
|
|
|
22
50
|
defaultValue,
|
|
23
51
|
inputValue: inputValueProp,
|
|
24
52
|
defaultInputValue,
|
|
53
|
+
items = [],
|
|
25
54
|
onValueChange,
|
|
26
55
|
onInputValueChange,
|
|
27
56
|
open: openProp,
|
|
@@ -31,23 +60,42 @@
|
|
|
31
60
|
name,
|
|
32
61
|
children
|
|
33
62
|
}: Props = $props();
|
|
63
|
+
const initialValue = (() => valueProp)();
|
|
64
|
+
const initialDefaultValue = (() => defaultValue)();
|
|
65
|
+
const initialInputValue = (() => inputValueProp)();
|
|
66
|
+
const initialDefaultInputValue = (() => defaultInputValue)();
|
|
67
|
+
const initialItems = (() => items)();
|
|
68
|
+
const initialOpen = (() => openProp)();
|
|
69
|
+
const initialDefaultOpen = (() => defaultOpen)();
|
|
70
|
+
const initialDisabled = (() => disabled)();
|
|
71
|
+
const notifyValueChange = (value: string) => onValueChange?.(value);
|
|
72
|
+
const notifyInputValueChange = (inputValue: string) => onInputValueChange?.(inputValue);
|
|
73
|
+
const notifyOpenChange = (open: boolean) => onOpenChange?.(open);
|
|
74
|
+
const contentId = createSveltePublicId(undefined, {
|
|
75
|
+
prefix: 'combobox',
|
|
76
|
+
slot: 'content',
|
|
77
|
+
});
|
|
34
78
|
|
|
35
79
|
const machine = createCombobox({
|
|
36
|
-
value:
|
|
37
|
-
defaultValue,
|
|
38
|
-
inputValue:
|
|
39
|
-
defaultInputValue,
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
80
|
+
value: initialValue,
|
|
81
|
+
defaultValue: initialDefaultValue,
|
|
82
|
+
inputValue: initialInputValue,
|
|
83
|
+
defaultInputValue: initialDefaultInputValue,
|
|
84
|
+
items: initialItems,
|
|
85
|
+
onValueChange: notifyValueChange,
|
|
86
|
+
onInputValueChange: notifyInputValueChange,
|
|
87
|
+
open: initialOpen,
|
|
88
|
+
defaultOpen: initialDefaultOpen,
|
|
89
|
+
onOpenChange: notifyOpenChange,
|
|
90
|
+
disabled: initialDisabled
|
|
46
91
|
});
|
|
47
92
|
|
|
48
93
|
let state = $state(machine.state);
|
|
49
94
|
let triggerElement = $state<HTMLElement | null>(null);
|
|
50
95
|
let inputElement = $state<HTMLInputElement | null>(null);
|
|
96
|
+
let registeredItems = $state<ComboboxItem[]>(initialItems);
|
|
97
|
+
const itemElements = new Map<string, HTMLElement>();
|
|
98
|
+
const itemIds = new Map<string, string>();
|
|
51
99
|
|
|
52
100
|
$effect(() => {
|
|
53
101
|
return machine.subscribe((newState) => {
|
|
@@ -55,6 +103,29 @@
|
|
|
55
103
|
});
|
|
56
104
|
});
|
|
57
105
|
|
|
106
|
+
$effect(() => {
|
|
107
|
+
if (valueProp !== undefined && state.value !== valueProp) {
|
|
108
|
+
machine.actions.syncValue(valueProp);
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
$effect(() => {
|
|
113
|
+
if (inputValueProp !== undefined && state.inputValue !== inputValueProp) {
|
|
114
|
+
machine.actions.syncInputValue(inputValueProp);
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
$effect(() => {
|
|
119
|
+
if (openProp !== undefined && state.open !== openProp) {
|
|
120
|
+
machine.actions.syncOpen(openProp);
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
$effect(() => {
|
|
125
|
+
machine.actions.setItems(items);
|
|
126
|
+
registeredItems = items;
|
|
127
|
+
});
|
|
128
|
+
|
|
58
129
|
// Derived state
|
|
59
130
|
let value = $derived(valueProp !== undefined ? valueProp : state.value);
|
|
60
131
|
let inputValue = $derived(inputValueProp !== undefined ? inputValueProp : state.inputValue);
|
|
@@ -99,18 +170,82 @@
|
|
|
99
170
|
inputElement = el;
|
|
100
171
|
}
|
|
101
172
|
|
|
102
|
-
|
|
173
|
+
function registerItem(item: ComboboxItem) {
|
|
174
|
+
const existingIndex = registeredItems.findIndex((entry) => entry.value === item.value);
|
|
175
|
+
if (existingIndex === -1) {
|
|
176
|
+
registeredItems = [...registeredItems, item];
|
|
177
|
+
} else {
|
|
178
|
+
registeredItems = registeredItems.map((entry, index) => (index === existingIndex ? item : entry));
|
|
179
|
+
}
|
|
180
|
+
if (!itemIds.has(item.value)) {
|
|
181
|
+
itemIds.set(
|
|
182
|
+
item.value,
|
|
183
|
+
createSveltePublicId(undefined, {
|
|
184
|
+
prefix: 'combobox-option',
|
|
185
|
+
slot: item.value,
|
|
186
|
+
})
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
machine.actions.setItems(registeredItems);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function unregisterItem(value: string) {
|
|
193
|
+
registeredItems = registeredItems.filter((entry) => entry.value !== value);
|
|
194
|
+
itemIds.delete(value);
|
|
195
|
+
machine.actions.setItems(registeredItems);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function registerItemElement(value: string, element: HTMLElement) {
|
|
199
|
+
itemElements.set(value, element);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function unregisterItemElement(value: string) {
|
|
203
|
+
itemElements.delete(value);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function getItemId(itemValue: string) {
|
|
207
|
+
return itemIds.get(itemValue);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function scrollItemIntoView(itemValue: string | null | undefined) {
|
|
211
|
+
if (!itemValue) {
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
itemElements.get(itemValue)?.scrollIntoView({ block: 'nearest' });
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function handleKeyDown(key: string, currentItem?: string | null) {
|
|
219
|
+
return machine.actions.handleKeyDown(key, currentItem);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function setHighlightedItem(value: string | null) {
|
|
223
|
+
machine.actions.setHighlightedItem(value);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
setContext<ComboboxContext>('combobox', {
|
|
103
227
|
get value() { return value; },
|
|
104
228
|
get inputValue() { return inputValue; },
|
|
105
229
|
get open() { return isOpen; },
|
|
106
230
|
get disabled() { return disabled; },
|
|
231
|
+
get contentId() { return contentId; },
|
|
232
|
+
get highlightedItem() { return state.highlightedItem; },
|
|
233
|
+
get filteredItems() { return state.filteredItems; },
|
|
107
234
|
get trigger() { return triggerElement; },
|
|
108
235
|
get input() { return inputElement; },
|
|
109
236
|
setValue,
|
|
110
237
|
setInputValue,
|
|
111
238
|
setOpen,
|
|
239
|
+
setHighlightedItem,
|
|
112
240
|
registerTrigger,
|
|
113
|
-
registerInput
|
|
241
|
+
registerInput,
|
|
242
|
+
registerItem,
|
|
243
|
+
unregisterItem,
|
|
244
|
+
registerItemElement,
|
|
245
|
+
unregisterItemElement,
|
|
246
|
+
getItemId,
|
|
247
|
+
scrollItemIntoView,
|
|
248
|
+
handleKeyDown
|
|
114
249
|
});
|
|
115
250
|
</script>
|
|
116
251
|
|
|
@@ -1,7 +1,16 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
2
|
+
import { isOutsideEventTarget } from './internal/dismissible';
|
|
3
|
+
import { getRequiredContext } from './internal/context';
|
|
4
|
+
import { portal as portalAction } from './internal/portal';
|
|
5
|
+
import { position as positionAction, type Side, type Align } from './internal/position';
|
|
6
|
+
|
|
7
|
+
interface ComboboxContext {
|
|
8
|
+
open: boolean;
|
|
9
|
+
contentId: string;
|
|
10
|
+
trigger: HTMLElement | null;
|
|
11
|
+
input: HTMLInputElement | null;
|
|
12
|
+
setOpen: (value: boolean) => void;
|
|
13
|
+
}
|
|
5
14
|
|
|
6
15
|
interface Props {
|
|
7
16
|
children?: import('svelte').Snippet;
|
|
@@ -9,6 +18,7 @@
|
|
|
9
18
|
align?: Align;
|
|
10
19
|
sideOffset?: number;
|
|
11
20
|
alignOffset?: number;
|
|
21
|
+
class?: string;
|
|
12
22
|
[key: string]: any;
|
|
13
23
|
}
|
|
14
24
|
|
|
@@ -18,34 +28,61 @@
|
|
|
18
28
|
align = 'start',
|
|
19
29
|
sideOffset = 0,
|
|
20
30
|
alignOffset = 0,
|
|
31
|
+
class: className,
|
|
21
32
|
...rest
|
|
22
33
|
}: Props = $props();
|
|
23
34
|
|
|
24
|
-
const ctx =
|
|
35
|
+
const ctx = getRequiredContext<ComboboxContext>(
|
|
36
|
+
'combobox',
|
|
37
|
+
'ComboboxContent',
|
|
38
|
+
'Combobox components must be used within a Combobox'
|
|
39
|
+
);
|
|
25
40
|
|
|
26
41
|
// Combobox content usually matches width of input
|
|
27
42
|
// Position hook handles basic positioning.
|
|
28
43
|
// We need to attach to input or trigger. Input is usually primary anchor.
|
|
29
44
|
let anchor = $derived(ctx.input || ctx.trigger);
|
|
45
|
+
let contentEl: HTMLDivElement;
|
|
46
|
+
|
|
47
|
+
$effect(() => {
|
|
48
|
+
if (!ctx.open) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const handlePointerDown = (event: PointerEvent) => {
|
|
53
|
+
const target = event.target as Node | null;
|
|
54
|
+
if (!isOutsideEventTarget(target, [contentEl, ctx.input, ctx.trigger])) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
ctx.setOpen(false);
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
document.addEventListener('pointerdown', handlePointerDown);
|
|
62
|
+
return () => {
|
|
63
|
+
document.removeEventListener('pointerdown', handlePointerDown);
|
|
64
|
+
};
|
|
65
|
+
});
|
|
30
66
|
</script>
|
|
31
67
|
|
|
32
68
|
{#if ctx.open && anchor}
|
|
33
|
-
<div
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
69
|
+
<div
|
|
70
|
+
bind:this={contentEl}
|
|
71
|
+
role="listbox"
|
|
72
|
+
id={ctx.contentId}
|
|
73
|
+
data-state="open"
|
|
74
|
+
use:portalAction={'body'}
|
|
75
|
+
use:positionAction={{
|
|
76
|
+
trigger: anchor,
|
|
77
|
+
side,
|
|
78
|
+
align,
|
|
79
|
+
sideOffset,
|
|
80
|
+
alignOffset
|
|
81
|
+
}}
|
|
82
|
+
class={className}
|
|
83
|
+
style="z-index: var(--uifn-floating-layer-z-index, 100); background-color: white; border: 1px solid #ccc; border-radius: 4px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); min-width: 100px; max-height: 300px; overflow-y: auto;"
|
|
84
|
+
{...rest}
|
|
85
|
+
>
|
|
86
|
+
{@render children?.()}
|
|
50
87
|
</div>
|
|
51
88
|
{/if}
|
package/src/ComboboxInput.svelte
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
|
-
import {
|
|
2
|
+
import { onMount } from 'svelte';
|
|
3
|
+
import { getRequiredContext } from './internal/context';
|
|
4
|
+
|
|
5
|
+
interface ComboboxContext {
|
|
6
|
+
open: boolean;
|
|
7
|
+
disabled: boolean;
|
|
8
|
+
contentId: string;
|
|
9
|
+
inputValue: string;
|
|
10
|
+
highlightedItem: string | null;
|
|
11
|
+
registerInput: (el: HTMLInputElement) => void;
|
|
12
|
+
setInputValue: (value: string) => void;
|
|
13
|
+
setOpen: (value: boolean) => void;
|
|
14
|
+
getItemId: (value: string) => string | undefined;
|
|
15
|
+
scrollItemIntoView: (value: string | null | undefined) => void;
|
|
16
|
+
handleKeyDown: (key: string, currentItem?: string | null) => string | null;
|
|
17
|
+
}
|
|
3
18
|
|
|
4
19
|
interface Props {
|
|
5
20
|
children?: import('svelte').Snippet;
|
|
@@ -7,7 +22,11 @@
|
|
|
7
22
|
}
|
|
8
23
|
|
|
9
24
|
let { children, ...rest }: Props = $props();
|
|
10
|
-
const ctx =
|
|
25
|
+
const ctx = getRequiredContext<ComboboxContext>(
|
|
26
|
+
'combobox',
|
|
27
|
+
'ComboboxInput',
|
|
28
|
+
'Combobox components must be used within a Combobox'
|
|
29
|
+
);
|
|
11
30
|
let el: HTMLInputElement;
|
|
12
31
|
|
|
13
32
|
onMount(() => {
|
|
@@ -20,16 +39,17 @@
|
|
|
20
39
|
}
|
|
21
40
|
|
|
22
41
|
function handleKeyDown(e: KeyboardEvent) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
42
|
+
if (!['ArrowDown', 'ArrowUp', 'Home', 'End', 'Enter', 'Escape'].includes(e.key)) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
e.preventDefault();
|
|
47
|
+
const nextItem = ctx.handleKeyDown(e.key, ctx.highlightedItem);
|
|
48
|
+
if (nextItem) {
|
|
49
|
+
queueMicrotask(() => {
|
|
50
|
+
ctx.scrollItemIntoView(nextItem);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
33
53
|
}
|
|
34
54
|
|
|
35
55
|
function handleClick() {
|
|
@@ -43,7 +63,8 @@
|
|
|
43
63
|
role="combobox"
|
|
44
64
|
aria-autocomplete="list"
|
|
45
65
|
aria-expanded={ctx.open}
|
|
46
|
-
aria-controls=
|
|
66
|
+
aria-controls={ctx.open ? ctx.contentId : undefined}
|
|
67
|
+
aria-activedescendant={ctx.open && ctx.highlightedItem ? ctx.getItemId(ctx.highlightedItem) : undefined}
|
|
47
68
|
aria-disabled={ctx.disabled}
|
|
48
69
|
data-state={ctx.open ? 'open' : 'closed'}
|
|
49
70
|
disabled={ctx.disabled}
|
package/src/ComboboxItem.svelte
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
|
-
import {
|
|
2
|
+
import { onMount } from 'svelte';
|
|
3
|
+
import { setContext } from 'svelte';
|
|
4
|
+
import { getRequiredContext } from './internal/context';
|
|
5
|
+
|
|
6
|
+
interface ComboboxContext {
|
|
7
|
+
value: string;
|
|
8
|
+
highlightedItem: string | null;
|
|
9
|
+
filteredItems: string[];
|
|
10
|
+
registerItem: (item: { value: string; label?: string; disabled?: boolean }) => void;
|
|
11
|
+
unregisterItem: (value: string) => void;
|
|
12
|
+
registerItemElement: (value: string, element: HTMLElement) => void;
|
|
13
|
+
unregisterItemElement: (value: string) => void;
|
|
14
|
+
getItemId: (value: string) => string | undefined;
|
|
15
|
+
setValue: (value: string) => void;
|
|
16
|
+
setHighlightedItem: (value: string | null) => void;
|
|
17
|
+
}
|
|
3
18
|
|
|
4
19
|
interface Props {
|
|
5
20
|
value: string;
|
|
@@ -9,15 +24,40 @@
|
|
|
9
24
|
}
|
|
10
25
|
|
|
11
26
|
let { value, disabled = false, children, ...rest }: Props = $props();
|
|
12
|
-
const
|
|
27
|
+
const getValue = () => value;
|
|
28
|
+
const ctx = getRequiredContext<ComboboxContext>(
|
|
29
|
+
'combobox',
|
|
30
|
+
'ComboboxItem',
|
|
31
|
+
'Combobox components must be used within a Combobox'
|
|
32
|
+
);
|
|
33
|
+
let el: HTMLDivElement;
|
|
34
|
+
|
|
35
|
+
let isSelected = $derived(ctx.value === getValue());
|
|
36
|
+
let isHighlighted = $derived(ctx.highlightedItem === getValue());
|
|
37
|
+
let isVisible = $derived(ctx.filteredItems.includes(getValue()));
|
|
13
38
|
|
|
14
|
-
|
|
39
|
+
onMount(() => {
|
|
40
|
+
ctx.registerItem({
|
|
41
|
+
value: getValue(),
|
|
42
|
+
label: el?.textContent?.trim() || getValue(),
|
|
43
|
+
disabled
|
|
44
|
+
});
|
|
45
|
+
return () => {
|
|
46
|
+
ctx.unregisterItemElement(getValue());
|
|
47
|
+
};
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
$effect(() => {
|
|
51
|
+
if (el) {
|
|
52
|
+
ctx.registerItemElement(getValue(), el);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
15
55
|
|
|
16
56
|
function handleClick(e: MouseEvent) {
|
|
17
57
|
if (disabled) return;
|
|
18
58
|
if (rest.onclick) rest.onclick(e);
|
|
19
59
|
if (!e.defaultPrevented) {
|
|
20
|
-
ctx.setValue(
|
|
60
|
+
ctx.setValue(getValue());
|
|
21
61
|
}
|
|
22
62
|
}
|
|
23
63
|
|
|
@@ -26,16 +66,22 @@
|
|
|
26
66
|
});
|
|
27
67
|
</script>
|
|
28
68
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
69
|
+
{#if isVisible}
|
|
70
|
+
<div
|
|
71
|
+
bind:this={el}
|
|
72
|
+
role="option"
|
|
73
|
+
id={ctx.getItemId(getValue())}
|
|
74
|
+
aria-selected={isSelected}
|
|
75
|
+
aria-disabled={disabled}
|
|
76
|
+
data-state={isSelected ? 'checked' : 'unchecked'}
|
|
77
|
+
data-highlighted={isHighlighted ? '' : undefined}
|
|
78
|
+
data-disabled={disabled ? '' : undefined}
|
|
79
|
+
style:cursor={disabled ? 'not-allowed' : 'pointer'}
|
|
80
|
+
style:padding="5px 10px"
|
|
81
|
+
{...rest}
|
|
82
|
+
onclick={handleClick}
|
|
83
|
+
onmouseenter={() => ctx.setHighlightedItem(getValue())}
|
|
84
|
+
>
|
|
85
|
+
{@render children?.()}
|
|
86
|
+
</div>
|
|
87
|
+
{/if}
|
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
|
-
import {
|
|
2
|
+
import { onMount } from 'svelte';
|
|
3
|
+
import { getRequiredContext } from './internal/context';
|
|
4
|
+
|
|
5
|
+
interface ComboboxContext {
|
|
6
|
+
open: boolean;
|
|
7
|
+
disabled: boolean;
|
|
8
|
+
contentId: string;
|
|
9
|
+
input: HTMLInputElement | null;
|
|
10
|
+
registerTrigger: (el: HTMLElement) => void;
|
|
11
|
+
setOpen: (value: boolean) => void;
|
|
12
|
+
}
|
|
3
13
|
|
|
4
14
|
interface Props {
|
|
5
15
|
children?: import('svelte').Snippet;
|
|
@@ -7,7 +17,11 @@
|
|
|
7
17
|
}
|
|
8
18
|
|
|
9
19
|
let { children, ...rest }: Props = $props();
|
|
10
|
-
const ctx =
|
|
20
|
+
const ctx = getRequiredContext<ComboboxContext>(
|
|
21
|
+
'combobox',
|
|
22
|
+
'ComboboxTrigger',
|
|
23
|
+
'Combobox components must be used within a Combobox'
|
|
24
|
+
);
|
|
11
25
|
let el: HTMLElement;
|
|
12
26
|
|
|
13
27
|
onMount(() => {
|
|
@@ -31,7 +45,7 @@
|
|
|
31
45
|
type="button"
|
|
32
46
|
aria-haspopup="listbox"
|
|
33
47
|
aria-expanded={ctx.open}
|
|
34
|
-
aria-controls=
|
|
48
|
+
aria-controls={ctx.open ? ctx.contentId : undefined}
|
|
35
49
|
data-state={ctx.open ? 'open' : 'closed'}
|
|
36
50
|
data-disabled={ctx.disabled ? '' : undefined}
|
|
37
51
|
disabled={ctx.disabled}
|
package/src/ContextMenu.svelte
CHANGED
|
@@ -1,6 +1,17 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
2
|
import { createContextMenu } from '@uifn/core/primitives/context-menu';
|
|
3
3
|
import { setContext } from 'svelte';
|
|
4
|
+
import { createSveltePublicId } from './internal/id';
|
|
5
|
+
|
|
6
|
+
interface ContextMenuContext {
|
|
7
|
+
open: boolean;
|
|
8
|
+
contentId: string;
|
|
9
|
+
point: { x: number; y: number };
|
|
10
|
+
triggerElement: HTMLElement | undefined;
|
|
11
|
+
setOpen: (value: boolean) => void;
|
|
12
|
+
setPoint: (point: { x: number; y: number }) => void;
|
|
13
|
+
setTriggerElement: (el: HTMLElement) => void;
|
|
14
|
+
}
|
|
4
15
|
|
|
5
16
|
interface Props {
|
|
6
17
|
onOpenChange?: (open: boolean) => void;
|
|
@@ -8,9 +19,14 @@
|
|
|
8
19
|
}
|
|
9
20
|
|
|
10
21
|
let { onOpenChange, children }: Props = $props();
|
|
22
|
+
const notifyOpenChange = (open: boolean) => onOpenChange?.(open);
|
|
23
|
+
const contentId = createSveltePublicId(undefined, {
|
|
24
|
+
prefix: 'context-menu',
|
|
25
|
+
slot: 'content',
|
|
26
|
+
});
|
|
11
27
|
|
|
12
28
|
const machine = createContextMenu({
|
|
13
|
-
onOpenChange
|
|
29
|
+
onOpenChange: notifyOpenChange
|
|
14
30
|
});
|
|
15
31
|
|
|
16
32
|
let state = $state(machine.state);
|
|
@@ -27,11 +43,14 @@
|
|
|
27
43
|
machine.actions.setOpen(val);
|
|
28
44
|
}
|
|
29
45
|
|
|
30
|
-
setContext('context-menu', {
|
|
46
|
+
setContext<ContextMenuContext>('context-menu', {
|
|
31
47
|
get open() { return state.open; },
|
|
48
|
+
get contentId() { return contentId; },
|
|
32
49
|
get point() { return point; },
|
|
50
|
+
get triggerElement() { return triggerElement; },
|
|
33
51
|
setOpen,
|
|
34
|
-
setPoint: (p: { x: number, y: number }) => { point = p; }
|
|
52
|
+
setPoint: (p: { x: number, y: number }) => { point = p; },
|
|
53
|
+
setTriggerElement: (el: HTMLElement) => { triggerElement = el; }
|
|
35
54
|
});
|
|
36
55
|
</script>
|
|
37
56
|
|
|
@@ -1,31 +1,99 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
|
-
import {
|
|
2
|
+
import { focusCollectionItemByDirection } from './internal/collection';
|
|
3
|
+
import { isOutsideEventTarget } from './internal/dismissible';
|
|
4
|
+
import { getRequiredContext } from './internal/context';
|
|
3
5
|
import { portal as portalAction } from './internal/portal';
|
|
4
6
|
|
|
7
|
+
interface ContextMenuContext {
|
|
8
|
+
open: boolean;
|
|
9
|
+
contentId: string;
|
|
10
|
+
point: { x: number; y: number };
|
|
11
|
+
triggerElement: HTMLElement | undefined;
|
|
12
|
+
setOpen: (value: boolean) => void;
|
|
13
|
+
}
|
|
14
|
+
|
|
5
15
|
interface Props {
|
|
6
16
|
children?: import('svelte').Snippet;
|
|
7
17
|
class?: string;
|
|
8
18
|
}
|
|
9
19
|
|
|
10
20
|
let { children, class: className, ...rest }: Props = $props();
|
|
11
|
-
const context =
|
|
21
|
+
const context = getRequiredContext<ContextMenuContext>(
|
|
22
|
+
'context-menu',
|
|
23
|
+
'ContextMenuContent',
|
|
24
|
+
'ContextMenu components must be used within a ContextMenu'
|
|
25
|
+
);
|
|
26
|
+
let contentEl: HTMLDivElement;
|
|
27
|
+
const itemSelector = '[role="menuitem"]:not([aria-disabled="true"])';
|
|
12
28
|
|
|
13
29
|
function handleKeyDown(e: KeyboardEvent) {
|
|
14
30
|
if (e.key === 'Escape') {
|
|
31
|
+
e.preventDefault();
|
|
15
32
|
context.setOpen(false);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (e.key === 'ArrowDown') {
|
|
37
|
+
e.preventDefault();
|
|
38
|
+
focusCollectionItemByDirection(contentEl, itemSelector, document.activeElement as HTMLElement | null, 'next');
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (e.key === 'ArrowUp') {
|
|
43
|
+
e.preventDefault();
|
|
44
|
+
focusCollectionItemByDirection(contentEl, itemSelector, document.activeElement as HTMLElement | null, 'prev');
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (e.key === 'Home') {
|
|
49
|
+
e.preventDefault();
|
|
50
|
+
focusCollectionItemByDirection(contentEl, itemSelector, null, 'first');
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (e.key === 'End') {
|
|
55
|
+
e.preventDefault();
|
|
56
|
+
focusCollectionItemByDirection(contentEl, itemSelector, null, 'last');
|
|
16
57
|
}
|
|
17
58
|
}
|
|
59
|
+
|
|
60
|
+
$effect(() => {
|
|
61
|
+
if (!context.open) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
queueMicrotask(() => {
|
|
66
|
+
focusCollectionItemByDirection(contentEl, itemSelector, null, 'first');
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
const handlePointerDown = (event: PointerEvent) => {
|
|
70
|
+
const target = event.target as Node | null;
|
|
71
|
+
if (!isOutsideEventTarget(target, [contentEl, context.triggerElement])) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
context.setOpen(false);
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
document.addEventListener('pointerdown', handlePointerDown);
|
|
79
|
+
return () => {
|
|
80
|
+
document.removeEventListener('pointerdown', handlePointerDown);
|
|
81
|
+
};
|
|
82
|
+
});
|
|
18
83
|
</script>
|
|
19
84
|
|
|
20
85
|
{#if context.open}
|
|
21
86
|
<!-- svelte-ignore a11y_no_static_element_interactions -->
|
|
22
87
|
<div
|
|
88
|
+
bind:this={contentEl}
|
|
23
89
|
use:portalAction={'body'}
|
|
90
|
+
id={context.contentId}
|
|
24
91
|
role="menu"
|
|
25
92
|
data-state="open"
|
|
26
93
|
onkeydown={handleKeyDown}
|
|
94
|
+
tabindex="-1"
|
|
27
95
|
class={className}
|
|
28
|
-
style="z-index:
|
|
96
|
+
style="z-index: var(--uifn-floating-layer-z-index, 100); position: fixed; left: {context.point.x}px; top: {context.point.y}px; background-color: white; border: 1px solid #ccc; border-radius: 4px; padding: 5px 0; min-width: 150px;"
|
|
29
97
|
{...rest}
|
|
30
98
|
>
|
|
31
99
|
{@render children?.()}
|