polymorph-ui-components 0.1.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/README.md +354 -0
- package/dist/Accordion/Accordion.svelte +28 -0
- package/dist/Accordion/Accordion.svelte.d.ts +4 -0
- package/dist/Accordion/properties.d.ts +7 -0
- package/dist/Accordion/properties.js +1 -0
- package/dist/Animations/ModalAnimation.svelte +55 -0
- package/dist/Animations/ModalAnimation.svelte.d.ts +12 -0
- package/dist/Animations/OverlayAnimation.svelte +14 -0
- package/dist/Animations/OverlayAnimation.svelte.d.ts +7 -0
- package/dist/Avatar/Avatar.svelte +122 -0
- package/dist/Avatar/Avatar.svelte.d.ts +4 -0
- package/dist/Avatar/properties.d.ts +15 -0
- package/dist/Avatar/properties.js +1 -0
- package/dist/Badge/Badge.svelte +53 -0
- package/dist/Badge/Badge.svelte.d.ts +4 -0
- package/dist/Badge/properties.d.ts +6 -0
- package/dist/Badge/properties.js +1 -0
- package/dist/Banner/Banner.svelte +158 -0
- package/dist/Banner/Banner.svelte.d.ts +4 -0
- package/dist/Banner/properties.d.ts +19 -0
- package/dist/Banner/properties.js +1 -0
- package/dist/Book/Book.svelte +281 -0
- package/dist/Book/Book.svelte.d.ts +4 -0
- package/dist/Book/properties.d.ts +24 -0
- package/dist/Book/properties.js +1 -0
- package/dist/Browser/Browser.svelte +176 -0
- package/dist/Browser/Browser.svelte.d.ts +3 -0
- package/dist/Browser/properties.d.ts +14 -0
- package/dist/Browser/properties.js +1 -0
- package/dist/Button/Button.svelte +153 -0
- package/dist/Button/Button.svelte.d.ts +4 -0
- package/dist/Button/properties.d.ts +24 -0
- package/dist/Button/properties.js +1 -0
- package/dist/Calendar/Calendar.svelte +477 -0
- package/dist/Calendar/Calendar.svelte.d.ts +4 -0
- package/dist/Calendar/properties.d.ts +30 -0
- package/dist/Calendar/properties.js +1 -0
- package/dist/Carousel/Carousel.svelte +258 -0
- package/dist/Carousel/Carousel.svelte.d.ts +4 -0
- package/dist/Carousel/properties.d.ts +20 -0
- package/dist/Carousel/properties.js +1 -0
- package/dist/CheckListItem/CheckListItem.svelte +65 -0
- package/dist/CheckListItem/CheckListItem.svelte.d.ts +4 -0
- package/dist/CheckListItem/properties.d.ts +15 -0
- package/dist/CheckListItem/properties.js +1 -0
- package/dist/Checkbox/Checkbox.svelte +157 -0
- package/dist/Checkbox/Checkbox.svelte.d.ts +4 -0
- package/dist/Checkbox/properties.d.ts +17 -0
- package/dist/Checkbox/properties.js +1 -0
- package/dist/Choicebox/Choicebox.svelte +85 -0
- package/dist/Choicebox/Choicebox.svelte.d.ts +4 -0
- package/dist/Choicebox/properties.d.ts +14 -0
- package/dist/Choicebox/properties.js +1 -0
- package/dist/ColorPicker/ColorPicker.svelte +583 -0
- package/dist/ColorPicker/ColorPicker.svelte.d.ts +4 -0
- package/dist/ColorPicker/properties.d.ts +15 -0
- package/dist/ColorPicker/properties.js +1 -0
- package/dist/Combobox/Combobox.svelte +432 -0
- package/dist/Combobox/Combobox.svelte.d.ts +6 -0
- package/dist/Combobox/properties.d.ts +42 -0
- package/dist/Combobox/properties.js +1 -0
- package/dist/CommandMenu/CommandMenu.svelte +452 -0
- package/dist/CommandMenu/CommandMenu.svelte.d.ts +4 -0
- package/dist/CommandMenu/properties.d.ts +26 -0
- package/dist/CommandMenu/properties.js +1 -0
- package/dist/ContextMenu/ContextMenu.svelte +308 -0
- package/dist/ContextMenu/ContextMenu.svelte.d.ts +4 -0
- package/dist/ContextMenu/properties.d.ts +26 -0
- package/dist/ContextMenu/properties.js +1 -0
- package/dist/Gauge/Gauge.svelte +70 -0
- package/dist/Gauge/Gauge.svelte.d.ts +4 -0
- package/dist/Gauge/properties.d.ts +9 -0
- package/dist/Gauge/properties.js +1 -0
- package/dist/GridItem/GridItem.svelte +145 -0
- package/dist/GridItem/GridItem.svelte.d.ts +4 -0
- package/dist/GridItem/properties.d.ts +15 -0
- package/dist/GridItem/properties.js +1 -0
- package/dist/Icon/Icon.svelte +61 -0
- package/dist/Icon/Icon.svelte.d.ts +4 -0
- package/dist/Icon/properties.d.ts +12 -0
- package/dist/Icon/properties.js +1 -0
- package/dist/IconStack/IconStack.svelte +55 -0
- package/dist/IconStack/IconStack.svelte.d.ts +4 -0
- package/dist/IconStack/properties.d.ts +9 -0
- package/dist/IconStack/properties.js +1 -0
- package/dist/Img/Img.svelte +37 -0
- package/dist/Img/Img.svelte.d.ts +4 -0
- package/dist/Img/properties.d.ts +13 -0
- package/dist/Img/properties.js +1 -0
- package/dist/Input/Input.svelte +349 -0
- package/dist/Input/Input.svelte.d.ts +8 -0
- package/dist/Input/properties.d.ts +45 -0
- package/dist/Input/properties.js +1 -0
- package/dist/InputButton/InputButton.svelte +252 -0
- package/dist/InputButton/InputButton.svelte.d.ts +7 -0
- package/dist/InputButton/properties.d.ts +22 -0
- package/dist/InputButton/properties.js +1 -0
- package/dist/KeyboardInput/KeyboardInput.svelte +93 -0
- package/dist/KeyboardInput/KeyboardInput.svelte.d.ts +4 -0
- package/dist/KeyboardInput/properties.d.ts +12 -0
- package/dist/KeyboardInput/properties.js +1 -0
- package/dist/ListItem/ListItem.svelte +309 -0
- package/dist/ListItem/ListItem.svelte.d.ts +4 -0
- package/dist/ListItem/properties.d.ts +34 -0
- package/dist/ListItem/properties.js +1 -0
- package/dist/Loader/Loader.svelte +90 -0
- package/dist/Loader/Loader.svelte.d.ts +4 -0
- package/dist/Loader/properties.d.ts +4 -0
- package/dist/Loader/properties.js +1 -0
- package/dist/LoadingDots/LoadingDots.svelte +59 -0
- package/dist/LoadingDots/LoadingDots.svelte.d.ts +3 -0
- package/dist/LoadingDots/properties.d.ts +8 -0
- package/dist/LoadingDots/properties.js +1 -0
- package/dist/Menu/Menu.svelte +356 -0
- package/dist/Menu/Menu.svelte.d.ts +4 -0
- package/dist/Menu/properties.d.ts +28 -0
- package/dist/Menu/properties.js +1 -0
- package/dist/Modal/Modal.svelte +357 -0
- package/dist/Modal/Modal.svelte.d.ts +4 -0
- package/dist/Modal/properties.d.ts +39 -0
- package/dist/Modal/properties.js +1 -0
- package/dist/Pagination/Pagination.svelte +148 -0
- package/dist/Pagination/Pagination.svelte.d.ts +4 -0
- package/dist/Pagination/properties.d.ts +14 -0
- package/dist/Pagination/properties.js +1 -0
- package/dist/Phone/Phone.svelte +234 -0
- package/dist/Phone/Phone.svelte.d.ts +3 -0
- package/dist/Phone/properties.d.ts +11 -0
- package/dist/Phone/properties.js +1 -0
- package/dist/Pill/Pill.svelte +130 -0
- package/dist/Pill/Pill.svelte.d.ts +4 -0
- package/dist/Pill/properties.d.ts +16 -0
- package/dist/Pill/properties.js +1 -0
- package/dist/Progress/Progress.svelte +68 -0
- package/dist/Progress/Progress.svelte.d.ts +4 -0
- package/dist/Progress/properties.d.ts +10 -0
- package/dist/Progress/properties.js +1 -0
- package/dist/Radio/Radio.svelte +124 -0
- package/dist/Radio/Radio.svelte.d.ts +4 -0
- package/dist/Radio/properties.d.ts +15 -0
- package/dist/Radio/properties.js +1 -0
- package/dist/RelativeTime/RelativeTime.svelte +109 -0
- package/dist/RelativeTime/RelativeTime.svelte.d.ts +4 -0
- package/dist/RelativeTime/properties.d.ts +13 -0
- package/dist/RelativeTime/properties.js +1 -0
- package/dist/Scroller/Scroller.svelte +390 -0
- package/dist/Scroller/Scroller.svelte.d.ts +4 -0
- package/dist/Scroller/properties.d.ts +30 -0
- package/dist/Scroller/properties.js +1 -0
- package/dist/Select/Select.svelte +472 -0
- package/dist/Select/Select.svelte.d.ts +4 -0
- package/dist/Select/properties.d.ts +20 -0
- package/dist/Select/properties.js +1 -0
- package/dist/Sheet/Sheet.svelte +264 -0
- package/dist/Sheet/Sheet.svelte.d.ts +4 -0
- package/dist/Sheet/properties.d.ts +19 -0
- package/dist/Sheet/properties.js +1 -0
- package/dist/Shimmer/Shimmer.svelte +44 -0
- package/dist/Shimmer/Shimmer.svelte.d.ts +4 -0
- package/dist/Shimmer/properties.d.ts +4 -0
- package/dist/Shimmer/properties.js +1 -0
- package/dist/Slider/Slider.svelte +147 -0
- package/dist/Slider/Slider.svelte.d.ts +4 -0
- package/dist/Slider/properties.d.ts +17 -0
- package/dist/Slider/properties.js +1 -0
- package/dist/Snippet/Snippet.svelte +123 -0
- package/dist/Snippet/Snippet.svelte.d.ts +4 -0
- package/dist/Snippet/properties.d.ts +15 -0
- package/dist/Snippet/properties.js +1 -0
- package/dist/SplitButton/SplitButton.svelte +145 -0
- package/dist/SplitButton/SplitButton.svelte.d.ts +4 -0
- package/dist/SplitButton/properties.d.ts +17 -0
- package/dist/SplitButton/properties.js +1 -0
- package/dist/SplitInput/SplitInput.svelte +225 -0
- package/dist/SplitInput/SplitInput.svelte.d.ts +7 -0
- package/dist/SplitInput/properties.d.ts +20 -0
- package/dist/SplitInput/properties.js +1 -0
- package/dist/Stepper/Step.svelte +88 -0
- package/dist/Stepper/Step.svelte.d.ts +4 -0
- package/dist/Stepper/Stepper.svelte +64 -0
- package/dist/Stepper/Stepper.svelte.d.ts +4 -0
- package/dist/Stepper/properties.d.ts +27 -0
- package/dist/Stepper/properties.js +1 -0
- package/dist/Table/Table.svelte +357 -0
- package/dist/Table/Table.svelte.d.ts +4 -0
- package/dist/Table/properties.d.ts +26 -0
- package/dist/Table/properties.js +1 -0
- package/dist/Tabs/Tabs.svelte +303 -0
- package/dist/Tabs/Tabs.svelte.d.ts +4 -0
- package/dist/Tabs/properties.d.ts +30 -0
- package/dist/Tabs/properties.js +1 -0
- package/dist/ThemeSwitcher/ThemeSwitcher.svelte +249 -0
- package/dist/ThemeSwitcher/ThemeSwitcher.svelte.d.ts +4 -0
- package/dist/ThemeSwitcher/properties.d.ts +19 -0
- package/dist/ThemeSwitcher/properties.js +1 -0
- package/dist/Toast/Toast.svelte +220 -0
- package/dist/Toast/Toast.svelte.d.ts +4 -0
- package/dist/Toast/properties.d.ts +24 -0
- package/dist/Toast/properties.js +1 -0
- package/dist/Toggle/Toggle.svelte +99 -0
- package/dist/Toggle/Toggle.svelte.d.ts +4 -0
- package/dist/Toggle/properties.d.ts +9 -0
- package/dist/Toggle/properties.js +1 -0
- package/dist/Toolbar/Toolbar.svelte +142 -0
- package/dist/Toolbar/Toolbar.svelte.d.ts +4 -0
- package/dist/Toolbar/properties.d.ts +16 -0
- package/dist/Toolbar/properties.js +1 -0
- package/dist/Tooltip/Tooltip.svelte +153 -0
- package/dist/Tooltip/Tooltip.svelte.d.ts +4 -0
- package/dist/Tooltip/properties.d.ts +13 -0
- package/dist/Tooltip/properties.js +1 -0
- package/dist/assets/back.svg +3 -0
- package/dist/assets/battery.svg +5 -0
- package/dist/assets/checkmark.svg +3 -0
- package/dist/assets/chevron-down-sm.svg +3 -0
- package/dist/assets/chevron-down.svg +3 -0
- package/dist/assets/chevron-left-lg.svg +3 -0
- package/dist/assets/chevron-left.svg +3 -0
- package/dist/assets/chevron-right-lg.svg +3 -0
- package/dist/assets/chevron-right.svg +3 -0
- package/dist/assets/chevron-up.svg +3 -0
- package/dist/assets/close.svg +4 -0
- package/dist/assets/copy.svg +4 -0
- package/dist/assets/error-circle.svg +5 -0
- package/dist/assets/lock.svg +3 -0
- package/dist/assets/minus.svg +3 -0
- package/dist/assets/monitor.svg +5 -0
- package/dist/assets/moon.svg +3 -0
- package/dist/assets/palette.svg +7 -0
- package/dist/assets/search.svg +4 -0
- package/dist/assets/signal.svg +6 -0
- package/dist/assets/sort-default.svg +4 -0
- package/dist/assets/sun.svg +11 -0
- package/dist/assets/swap-vertical.svg +6 -0
- package/dist/assets/wifi.svg +3 -0
- package/dist/index.d.ts +103 -0
- package/dist/index.js +55 -0
- package/dist/types.d.ts +42 -0
- package/dist/types.js +1 -0
- package/dist/utils.d.ts +28 -0
- package/dist/utils.js +294 -0
- package/package.json +91 -0
|
@@ -0,0 +1,432 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { onMount, tick } from 'svelte';
|
|
3
|
+
import Input from '../Input/Input.svelte';
|
|
4
|
+
import type { ComboboxItem, ComboboxProperties } from './properties';
|
|
5
|
+
|
|
6
|
+
function defaultFilter(item: ComboboxItem, query: string): boolean {
|
|
7
|
+
return item.label.toLowerCase().includes(query.toLowerCase());
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
let {
|
|
11
|
+
items,
|
|
12
|
+
value = $bindable(''),
|
|
13
|
+
inputValue = $bindable(''),
|
|
14
|
+
open = $bindable(false),
|
|
15
|
+
highlightedIndex = $bindable(-1),
|
|
16
|
+
placeholder = '',
|
|
17
|
+
disabled = false,
|
|
18
|
+
name,
|
|
19
|
+
testId,
|
|
20
|
+
classes,
|
|
21
|
+
noResultsText = 'No results',
|
|
22
|
+
ariaLabel,
|
|
23
|
+
filterFn = defaultFilter,
|
|
24
|
+
inputProperties,
|
|
25
|
+
inputEventProperties,
|
|
26
|
+
itemSnippet,
|
|
27
|
+
emptySnippet,
|
|
28
|
+
inputPrefix,
|
|
29
|
+
inputSuffix,
|
|
30
|
+
dropdownHeader,
|
|
31
|
+
dropdownFooter,
|
|
32
|
+
onselect,
|
|
33
|
+
oninput,
|
|
34
|
+
onopen,
|
|
35
|
+
onclose,
|
|
36
|
+
onkeydown,
|
|
37
|
+
onfocus,
|
|
38
|
+
onblur
|
|
39
|
+
}: ComboboxProperties = $props();
|
|
40
|
+
|
|
41
|
+
let containerEl: HTMLDivElement | null = $state(null);
|
|
42
|
+
let inputRef: ReturnType<typeof Input> | null = $state(null);
|
|
43
|
+
|
|
44
|
+
export function getInputRef(): HTMLInputElement | HTMLTextAreaElement | null {
|
|
45
|
+
return inputRef?.getInputRef() ?? null;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const listboxId = `combobox-listbox-${Math.random().toString(36).slice(2, 9)}`;
|
|
49
|
+
|
|
50
|
+
let filteredItems: ComboboxItem[] = $derived(
|
|
51
|
+
inputValue.length > 0 ? items.filter((item) => filterFn(item, inputValue)) : items
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
let selectableItems: ComboboxItem[] = $derived(
|
|
55
|
+
filteredItems.filter((item) => item.disabled !== true)
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
let highlightedOptionId: string | null = $derived(
|
|
59
|
+
highlightedIndex >= 0 ? `${listboxId}-option-${highlightedIndex}` : null
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
function openDropdown() {
|
|
63
|
+
if (disabled || open) {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
open = true;
|
|
67
|
+
highlightedIndex = -1;
|
|
68
|
+
onopen?.();
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function closeDropdown() {
|
|
72
|
+
if (!open) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
open = false;
|
|
76
|
+
highlightedIndex = -1;
|
|
77
|
+
onclose?.();
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function selectItem(item: ComboboxItem) {
|
|
81
|
+
if (item.disabled === true) {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
value = item.id;
|
|
85
|
+
inputValue = item.label;
|
|
86
|
+
onselect?.(item);
|
|
87
|
+
closeDropdown();
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function getFilteredSelectableIndex(item: ComboboxItem): number {
|
|
91
|
+
let selectableIdx = 0;
|
|
92
|
+
for (let i = 0; i < filteredItems.length; i++) {
|
|
93
|
+
if (filteredItems[i] === item) {
|
|
94
|
+
return filteredItems[i].disabled === true ? -1 : selectableIdx;
|
|
95
|
+
}
|
|
96
|
+
if (filteredItems[i].disabled !== true) {
|
|
97
|
+
selectableIdx++;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return -1;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
async function moveHighlight(delta: number): Promise<void> {
|
|
104
|
+
if (selectableItems.length === 0) {
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
let next = highlightedIndex + delta;
|
|
108
|
+
if (next < 0) {
|
|
109
|
+
next = selectableItems.length - 1;
|
|
110
|
+
} else if (next >= selectableItems.length) {
|
|
111
|
+
next = 0;
|
|
112
|
+
}
|
|
113
|
+
highlightedIndex = next;
|
|
114
|
+
await tick();
|
|
115
|
+
if (containerEl !== null) {
|
|
116
|
+
const el = containerEl.querySelector('.combobox-option.highlighted');
|
|
117
|
+
if (el instanceof HTMLElement && typeof el.scrollIntoView === 'function') {
|
|
118
|
+
el.scrollIntoView({ block: 'nearest' });
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function selectHighlighted() {
|
|
124
|
+
if (highlightedIndex < 0 || highlightedIndex >= selectableItems.length) {
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
const item = selectableItems.at(highlightedIndex);
|
|
128
|
+
if (typeof item === 'object' && item !== null) {
|
|
129
|
+
selectItem(item);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function handleInput(val: string, _event: Event) {
|
|
134
|
+
inputValue = val;
|
|
135
|
+
oninput?.(val);
|
|
136
|
+
inputEventProperties?.oninput?.(val, _event);
|
|
137
|
+
if (!open) {
|
|
138
|
+
openDropdown();
|
|
139
|
+
}
|
|
140
|
+
highlightedIndex = -1;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function handleKeydown(event: KeyboardEvent) {
|
|
144
|
+
if (disabled) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
onkeydown?.(event);
|
|
148
|
+
if (event.defaultPrevented) {
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
switch (event.key) {
|
|
152
|
+
case 'ArrowDown':
|
|
153
|
+
event.preventDefault();
|
|
154
|
+
if (!open) {
|
|
155
|
+
openDropdown();
|
|
156
|
+
} else {
|
|
157
|
+
moveHighlight(1);
|
|
158
|
+
}
|
|
159
|
+
break;
|
|
160
|
+
case 'ArrowUp':
|
|
161
|
+
event.preventDefault();
|
|
162
|
+
if (open) {
|
|
163
|
+
moveHighlight(-1);
|
|
164
|
+
}
|
|
165
|
+
break;
|
|
166
|
+
case 'Enter':
|
|
167
|
+
if (open && highlightedIndex >= 0) {
|
|
168
|
+
event.preventDefault();
|
|
169
|
+
selectHighlighted();
|
|
170
|
+
}
|
|
171
|
+
break;
|
|
172
|
+
case 'Escape':
|
|
173
|
+
if (open) {
|
|
174
|
+
event.preventDefault();
|
|
175
|
+
closeDropdown();
|
|
176
|
+
}
|
|
177
|
+
break;
|
|
178
|
+
case 'Tab':
|
|
179
|
+
if (open) {
|
|
180
|
+
closeDropdown();
|
|
181
|
+
}
|
|
182
|
+
break;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function handleFocus(event: FocusEvent) {
|
|
187
|
+
openDropdown();
|
|
188
|
+
onfocus?.(event);
|
|
189
|
+
inputEventProperties?.onfocus?.(event);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function handleBlur(event: FocusEvent) {
|
|
193
|
+
onblur?.(event);
|
|
194
|
+
inputEventProperties?.onblur?.(event);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function handleClickOutside(event: Event) {
|
|
198
|
+
if (
|
|
199
|
+
event.target instanceof Node &&
|
|
200
|
+
containerEl !== null &&
|
|
201
|
+
!containerEl.contains(event.target)
|
|
202
|
+
) {
|
|
203
|
+
closeDropdown();
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
onMount(() => {
|
|
208
|
+
document.addEventListener('click', handleClickOutside);
|
|
209
|
+
return () => {
|
|
210
|
+
document.removeEventListener('click', handleClickOutside);
|
|
211
|
+
};
|
|
212
|
+
});
|
|
213
|
+
</script>
|
|
214
|
+
|
|
215
|
+
<div class="combobox {classes ?? ''}" class:disabled bind:this={containerEl} data-pw={testId}>
|
|
216
|
+
<div class="combobox-input-wrapper">
|
|
217
|
+
{#if typeof inputPrefix === 'function'}
|
|
218
|
+
<div class="combobox-input-prefix">{@render inputPrefix()}</div>
|
|
219
|
+
{/if}
|
|
220
|
+
<div class="combobox-input">
|
|
221
|
+
<Input
|
|
222
|
+
{...inputProperties}
|
|
223
|
+
bind:value={inputValue}
|
|
224
|
+
bind:this={inputRef}
|
|
225
|
+
{placeholder}
|
|
226
|
+
{name}
|
|
227
|
+
disable={disabled}
|
|
228
|
+
autoComplete="off"
|
|
229
|
+
actionInput={true}
|
|
230
|
+
testId={typeof testId === 'string' ? `${testId}-input` : ''}
|
|
231
|
+
role="combobox"
|
|
232
|
+
ariaExpanded={open}
|
|
233
|
+
ariaAutocomplete="list"
|
|
234
|
+
ariaControls={open ? listboxId : null}
|
|
235
|
+
ariaActivedescendant={highlightedOptionId ?? null}
|
|
236
|
+
oninput={handleInput}
|
|
237
|
+
onkeydown={handleKeydown}
|
|
238
|
+
onfocus={handleFocus}
|
|
239
|
+
onblur={handleBlur}
|
|
240
|
+
/>
|
|
241
|
+
</div>
|
|
242
|
+
{#if typeof inputSuffix === 'function'}
|
|
243
|
+
<div class="combobox-input-suffix">{@render inputSuffix()}</div>
|
|
244
|
+
{/if}
|
|
245
|
+
</div>
|
|
246
|
+
|
|
247
|
+
{#if open && !disabled}
|
|
248
|
+
<div class="combobox-dropdown" role="listbox" id={listboxId} aria-label={ariaLabel}>
|
|
249
|
+
{#if typeof dropdownHeader === 'function'}
|
|
250
|
+
<div class="combobox-dropdown-header">{@render dropdownHeader()}</div>
|
|
251
|
+
{/if}
|
|
252
|
+
{#if filteredItems.length === 0}
|
|
253
|
+
{#if typeof emptySnippet === 'function'}
|
|
254
|
+
{@render emptySnippet()}
|
|
255
|
+
{:else}
|
|
256
|
+
<div class="combobox-empty">{noResultsText}</div>
|
|
257
|
+
{/if}
|
|
258
|
+
{:else}
|
|
259
|
+
{#each filteredItems as item, _index (item.id)}
|
|
260
|
+
{@const selectableIndex = getFilteredSelectableIndex(item)}
|
|
261
|
+
{@const isHighlighted = item.disabled !== true && selectableIndex === highlightedIndex}
|
|
262
|
+
<!-- svelte-ignore a11y_click_events_have_key_events -->
|
|
263
|
+
<div
|
|
264
|
+
class="combobox-option"
|
|
265
|
+
class:highlighted={isHighlighted}
|
|
266
|
+
class:selected={item.id === value}
|
|
267
|
+
class:combobox-option-disabled={item.disabled === true}
|
|
268
|
+
role="option"
|
|
269
|
+
id={`${listboxId}-option-${selectableIndex}`}
|
|
270
|
+
aria-selected={item.id === value}
|
|
271
|
+
aria-disabled={item.disabled === true ? 'true' : null}
|
|
272
|
+
tabindex="-1"
|
|
273
|
+
onclick={() => selectItem(item)}
|
|
274
|
+
onmouseenter={() => {
|
|
275
|
+
if (item.disabled !== true) {
|
|
276
|
+
highlightedIndex = selectableIndex;
|
|
277
|
+
}
|
|
278
|
+
}}
|
|
279
|
+
data-pw={typeof testId === 'string' ? `${testId}-option-${item.id}` : null}
|
|
280
|
+
>
|
|
281
|
+
{#if typeof itemSnippet === 'function'}
|
|
282
|
+
{@render itemSnippet(item, isHighlighted)}
|
|
283
|
+
{:else}
|
|
284
|
+
{item.label}
|
|
285
|
+
{/if}
|
|
286
|
+
</div>
|
|
287
|
+
{/each}
|
|
288
|
+
{/if}
|
|
289
|
+
{#if typeof dropdownFooter === 'function'}
|
|
290
|
+
<div class="combobox-dropdown-footer">{@render dropdownFooter()}</div>
|
|
291
|
+
{/if}
|
|
292
|
+
</div>
|
|
293
|
+
{/if}
|
|
294
|
+
</div>
|
|
295
|
+
|
|
296
|
+
<style>
|
|
297
|
+
.combobox {
|
|
298
|
+
position: relative;
|
|
299
|
+
width: var(--combobox-width, 100%);
|
|
300
|
+
font-family: var(--combobox-font-family, inherit);
|
|
301
|
+
font-size: var(--combobox-font-size, 14px);
|
|
302
|
+
color: var(--combobox-color, currentColor);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
.combobox.disabled {
|
|
306
|
+
opacity: var(--combobox-disabled-opacity, 0.5);
|
|
307
|
+
cursor: var(--combobox-disabled-cursor, not-allowed);
|
|
308
|
+
pointer-events: none;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
.combobox-input-wrapper {
|
|
312
|
+
display: flex;
|
|
313
|
+
align-items: center;
|
|
314
|
+
background: var(--combobox-input-background, #ffffff);
|
|
315
|
+
border: var(--combobox-input-border, 1px solid currentColor);
|
|
316
|
+
border-radius: var(--combobox-input-border-radius, 6px);
|
|
317
|
+
transition: var(--combobox-input-transition, border-color 0.15s, box-shadow 0.15s);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
.combobox-input-wrapper:hover {
|
|
321
|
+
border-color: var(--combobox-input-hover-border-color, currentColor);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
.combobox-input-wrapper:focus-within {
|
|
325
|
+
border-color: var(--combobox-input-focus-border-color, currentColor);
|
|
326
|
+
box-shadow: var(--combobox-input-focus-shadow, 0 0 0 2px currentColor);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
.combobox-input-prefix {
|
|
330
|
+
display: flex;
|
|
331
|
+
align-items: center;
|
|
332
|
+
padding-left: var(--combobox-input-prefix-padding, 8px);
|
|
333
|
+
flex-shrink: 0;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
.combobox-input-suffix {
|
|
337
|
+
display: flex;
|
|
338
|
+
align-items: center;
|
|
339
|
+
padding-right: var(--combobox-input-suffix-padding, 8px);
|
|
340
|
+
flex-shrink: 0;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
.combobox-input {
|
|
344
|
+
flex: 1;
|
|
345
|
+
min-width: 0;
|
|
346
|
+
--input-border: none;
|
|
347
|
+
--input-focus-border: none;
|
|
348
|
+
--input-box-shadow: none;
|
|
349
|
+
--input-margin: 0;
|
|
350
|
+
--input-width: 100%;
|
|
351
|
+
--input-padding: var(--combobox-input-padding, 8px 12px);
|
|
352
|
+
--input-background: transparent;
|
|
353
|
+
--input-font-size: inherit;
|
|
354
|
+
--input-font-family: inherit;
|
|
355
|
+
--input-font-weight: inherit;
|
|
356
|
+
--input-text-color: inherit;
|
|
357
|
+
--input-radius: 0;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
.combobox-input::placeholder {
|
|
361
|
+
color: var(--combobox-placeholder-color, currentColor);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
.combobox-dropdown {
|
|
365
|
+
position: absolute;
|
|
366
|
+
top: var(--combobox-dropdown-top, 100%);
|
|
367
|
+
left: var(--combobox-dropdown-left, 0);
|
|
368
|
+
right: var(--combobox-dropdown-right, 0);
|
|
369
|
+
margin-top: var(--combobox-dropdown-gap, 4px);
|
|
370
|
+
background: var(--combobox-dropdown-background, #ffffff);
|
|
371
|
+
border: var(--combobox-dropdown-border, 1px solid currentColor);
|
|
372
|
+
border-radius: var(--combobox-dropdown-border-radius, 8px);
|
|
373
|
+
box-shadow: var(--combobox-dropdown-shadow, 0 4px 12px rgba(0, 0, 0, 0.1));
|
|
374
|
+
max-height: var(--combobox-dropdown-max-height, 200px);
|
|
375
|
+
overflow-y: auto;
|
|
376
|
+
z-index: var(--combobox-dropdown-z-index, 10);
|
|
377
|
+
padding: var(--combobox-dropdown-padding, 0);
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
.combobox-option {
|
|
381
|
+
padding: var(--combobox-option-padding, 8px 12px);
|
|
382
|
+
color: var(--combobox-option-color, currentColor);
|
|
383
|
+
font-size: var(--combobox-option-font-size, inherit);
|
|
384
|
+
font-weight: var(--combobox-option-font-weight, inherit);
|
|
385
|
+
cursor: pointer;
|
|
386
|
+
transition: background 0.1s;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
.combobox-option:hover,
|
|
390
|
+
.combobox-option.highlighted {
|
|
391
|
+
background: var(--combobox-option-hover-background, #f4f4f5);
|
|
392
|
+
color: var(--combobox-option-hover-color, var(--combobox-option-color, currentColor));
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
.combobox-option.selected {
|
|
396
|
+
background: var(--combobox-option-selected-background, transparent);
|
|
397
|
+
color: var(--combobox-option-selected-color, var(--combobox-option-color, currentColor));
|
|
398
|
+
font-weight: var(
|
|
399
|
+
--combobox-option-selected-font-weight,
|
|
400
|
+
var(--combobox-option-font-weight, inherit)
|
|
401
|
+
);
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
.combobox-option.selected.highlighted {
|
|
405
|
+
background: var(
|
|
406
|
+
--combobox-option-selected-hover-background,
|
|
407
|
+
var(--combobox-option-selected-background, #f4f4f5)
|
|
408
|
+
);
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
.combobox-option-disabled {
|
|
412
|
+
opacity: var(--combobox-option-disabled-opacity, 0.4);
|
|
413
|
+
cursor: var(--combobox-option-disabled-cursor, not-allowed);
|
|
414
|
+
pointer-events: none;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
.combobox-dropdown-header {
|
|
418
|
+
border-bottom: var(--combobox-dropdown-header-border, none);
|
|
419
|
+
padding: var(--combobox-dropdown-header-padding, 0);
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
.combobox-dropdown-footer {
|
|
423
|
+
border-top: var(--combobox-dropdown-footer-border, none);
|
|
424
|
+
padding: var(--combobox-dropdown-footer-padding, 0);
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
.combobox-empty {
|
|
428
|
+
padding: var(--combobox-empty-padding, 8px 12px);
|
|
429
|
+
color: var(--combobox-empty-color, currentColor);
|
|
430
|
+
font-style: var(--combobox-empty-font-style, italic);
|
|
431
|
+
}
|
|
432
|
+
</style>
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { ComboboxProperties } from './properties';
|
|
2
|
+
declare const Combobox: import("svelte").Component<ComboboxProperties, {
|
|
3
|
+
getInputRef: () => HTMLInputElement | HTMLTextAreaElement | null;
|
|
4
|
+
}, "value" | "open" | "inputValue" | "highlightedIndex">;
|
|
5
|
+
type Combobox = ReturnType<typeof Combobox>;
|
|
6
|
+
export default Combobox;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { Snippet } from 'svelte';
|
|
2
|
+
import type { OptionalInputProperties, InputEventProperties } from '../Input/properties';
|
|
3
|
+
export type ComboboxItem = {
|
|
4
|
+
id: string;
|
|
5
|
+
label: string;
|
|
6
|
+
disabled?: boolean;
|
|
7
|
+
};
|
|
8
|
+
export type ComboboxProperties = MandatoryComboboxProperties & OptionalComboboxProperties & ComboboxEventProperties;
|
|
9
|
+
export type MandatoryComboboxProperties = {
|
|
10
|
+
items: ComboboxItem[];
|
|
11
|
+
};
|
|
12
|
+
export type OptionalComboboxProperties = {
|
|
13
|
+
value?: string;
|
|
14
|
+
inputValue?: string;
|
|
15
|
+
open?: boolean;
|
|
16
|
+
highlightedIndex?: number;
|
|
17
|
+
placeholder?: string;
|
|
18
|
+
disabled?: boolean;
|
|
19
|
+
name?: string;
|
|
20
|
+
testId?: string;
|
|
21
|
+
classes?: string;
|
|
22
|
+
noResultsText?: string;
|
|
23
|
+
ariaLabel?: string;
|
|
24
|
+
filterFn?: (item: ComboboxItem, query: string) => boolean;
|
|
25
|
+
inputProperties?: OptionalInputProperties;
|
|
26
|
+
inputEventProperties?: InputEventProperties;
|
|
27
|
+
itemSnippet?: Snippet<[ComboboxItem, boolean]>;
|
|
28
|
+
emptySnippet?: Snippet;
|
|
29
|
+
inputPrefix?: Snippet;
|
|
30
|
+
inputSuffix?: Snippet;
|
|
31
|
+
dropdownHeader?: Snippet;
|
|
32
|
+
dropdownFooter?: Snippet;
|
|
33
|
+
};
|
|
34
|
+
export type ComboboxEventProperties = {
|
|
35
|
+
onselect?: (item: ComboboxItem) => void;
|
|
36
|
+
oninput?: (value: string) => void;
|
|
37
|
+
onopen?: () => void;
|
|
38
|
+
onclose?: () => void;
|
|
39
|
+
onkeydown?: (event: KeyboardEvent) => void;
|
|
40
|
+
onfocus?: (event: FocusEvent) => void;
|
|
41
|
+
onblur?: (event: FocusEvent) => void;
|
|
42
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|