@human-kit/ui 1.0.0-beta.7 → 1.0.0-beta.8
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/FOCUS_STATE_CONTRACT.md +15 -4
- package/dist/FOCUS_STATE_REVIEW_TEMPLATE.md +8 -8
- package/dist/avatar/README.md +52 -0
- package/dist/avatar/count/avatar-count.svelte +56 -0
- package/dist/avatar/count/avatar-count.svelte.d.ts +4 -0
- package/dist/avatar/fallback/avatar-fallback.svelte +67 -0
- package/dist/avatar/fallback/avatar-fallback.svelte.d.ts +4 -0
- package/dist/avatar/group/avatar-group.svelte +65 -0
- package/dist/avatar/group/avatar-group.svelte.d.ts +4 -0
- package/dist/avatar/group/context.d.ts +15 -0
- package/dist/avatar/group/context.js +15 -0
- package/dist/avatar/image/avatar-image.svelte +104 -0
- package/dist/avatar/image/avatar-image.svelte.d.ts +4 -0
- package/dist/avatar/index.d.ts +22 -0
- package/dist/avatar/index.js +10 -0
- package/dist/avatar/index.parts.d.ts +5 -0
- package/dist/avatar/index.parts.js +5 -0
- package/dist/avatar/root/avatar-root.svelte +79 -0
- package/dist/avatar/root/avatar-root.svelte.d.ts +4 -0
- package/dist/avatar/root/context.d.ts +14 -0
- package/dist/avatar/root/context.js +15 -0
- package/dist/avatar/types.d.ts +84 -0
- package/dist/avatar/types.js +1 -0
- package/dist/breadcrumbs/README.md +65 -0
- package/dist/breadcrumbs/ellipsis/breadcrumbs-ellipsis.svelte +62 -0
- package/dist/breadcrumbs/ellipsis/breadcrumbs-ellipsis.svelte.d.ts +4 -0
- package/dist/breadcrumbs/index.d.ts +23 -0
- package/dist/breadcrumbs/index.js +10 -0
- package/dist/breadcrumbs/index.parts.d.ts +6 -0
- package/dist/breadcrumbs/index.parts.js +6 -0
- package/dist/breadcrumbs/item/breadcrumbs-item.svelte +42 -0
- package/dist/breadcrumbs/item/breadcrumbs-item.svelte.d.ts +4 -0
- package/dist/breadcrumbs/link/breadcrumbs-link.svelte +47 -0
- package/dist/breadcrumbs/link/breadcrumbs-link.svelte.d.ts +4 -0
- package/dist/breadcrumbs/list/breadcrumbs-list.svelte +31 -0
- package/dist/breadcrumbs/list/breadcrumbs-list.svelte.d.ts +4 -0
- package/dist/breadcrumbs/root/breadcrumbs-root.svelte +112 -0
- package/dist/breadcrumbs/root/breadcrumbs-root.svelte.d.ts +4 -0
- package/dist/breadcrumbs/root/context.d.ts +19 -0
- package/dist/breadcrumbs/root/context.js +15 -0
- package/dist/breadcrumbs/separator/breadcrumbs-separator.svelte +35 -0
- package/dist/breadcrumbs/separator/breadcrumbs-separator.svelte.d.ts +4 -0
- package/dist/breadcrumbs/types.d.ts +80 -0
- package/dist/breadcrumbs/types.js +1 -0
- package/dist/combobox/popover/combobox-popover.svelte +4 -56
- package/dist/dialog/root/dialog-root.svelte +15 -2
- package/dist/drawer/root/drawer-root.svelte +15 -2
- package/dist/index.d.ts +14 -0
- package/dist/index.js +13 -0
- package/dist/internal/localized-strings.d.ts +48 -0
- package/dist/internal/localized-strings.js +48 -0
- package/dist/internal/number-precision.d.ts +9 -0
- package/dist/internal/number-precision.js +27 -0
- package/dist/menu/root/focus-state.d.ts +2 -2
- package/dist/menu/root/focus-state.js +8 -32
- package/dist/menu/root/menu-state.svelte.js +5 -1
- package/dist/numberfield/root/number-utils.js +1 -20
- package/dist/popover/root/focus-state.d.ts +2 -2
- package/dist/popover/root/focus-state.js +7 -34
- package/dist/primitives/contain-wheel.d.ts +9 -0
- package/dist/primitives/contain-wheel.js +41 -0
- package/dist/primitives/floating.d.ts +6 -0
- package/dist/primitives/floating.js +26 -5
- package/dist/primitives/keyboard-navigation.d.ts +6 -0
- package/dist/primitives/keyboard-navigation.js +2 -1
- package/dist/primitives/trigger-focus-state.d.ts +29 -0
- package/dist/primitives/trigger-focus-state.js +57 -0
- package/dist/progress/README.md +54 -0
- package/dist/progress/index.d.ts +21 -0
- package/dist/progress/index.js +9 -0
- package/dist/progress/index.parts.d.ts +5 -0
- package/dist/progress/index.parts.js +5 -0
- package/dist/progress/indicator/progress-indicator.svelte +37 -0
- package/dist/progress/indicator/progress-indicator.svelte.d.ts +4 -0
- package/dist/progress/label/progress-label.svelte +34 -0
- package/dist/progress/label/progress-label.svelte.d.ts +4 -0
- package/dist/progress/root/context.d.ts +26 -0
- package/dist/progress/root/context.js +15 -0
- package/dist/progress/root/progress-root.svelte +136 -0
- package/dist/progress/root/progress-root.svelte.d.ts +4 -0
- package/dist/progress/root/progress-utils.d.ts +12 -0
- package/dist/progress/root/progress-utils.js +41 -0
- package/dist/progress/track/progress-track.svelte +27 -0
- package/dist/progress/track/progress-track.svelte.d.ts +4 -0
- package/dist/progress/types.d.ts +81 -0
- package/dist/progress/types.js +1 -0
- package/dist/progress/value/progress-value.svelte +36 -0
- package/dist/progress/value/progress-value.svelte.d.ts +4 -0
- package/dist/select/README.md +129 -0
- package/dist/select/index.d.ts +30 -0
- package/dist/select/index.js +16 -0
- package/dist/select/index.parts.d.ts +8 -0
- package/dist/select/index.parts.js +9 -0
- package/dist/select/item/select-item.svelte +68 -0
- package/dist/select/item/select-item.svelte.d.ts +10 -0
- package/dist/select/item-indicator/select-item-indicator.svelte +55 -0
- package/dist/select/item-indicator/select-item-indicator.svelte.d.ts +4 -0
- package/dist/select/label/select-label.svelte +46 -0
- package/dist/select/label/select-label.svelte.d.ts +4 -0
- package/dist/select/list/select-list.svelte +241 -0
- package/dist/select/list/select-list.svelte.d.ts +62 -0
- package/dist/select/popover/select-popover.svelte +80 -0
- package/dist/select/popover/select-popover.svelte.d.ts +9 -0
- package/dist/select/root/context.d.ts +106 -0
- package/dist/select/root/context.js +22 -0
- package/dist/select/root/select-root.svelte +544 -0
- package/dist/select/root/select-root.svelte.d.ts +25 -0
- package/dist/select/trigger/select-trigger.svelte +192 -0
- package/dist/select/trigger/select-trigger.svelte.d.ts +4 -0
- package/dist/select/types.d.ts +133 -0
- package/dist/select/types.js +1 -0
- package/dist/select/value/select-value.svelte +61 -0
- package/dist/select/value/select-value.svelte.d.ts +4 -0
- package/dist/separator/README.md +35 -0
- package/dist/separator/index.d.ts +7 -0
- package/dist/separator/index.js +3 -0
- package/dist/separator/separator.svelte +254 -0
- package/dist/separator/separator.svelte.d.ts +45 -0
- package/dist/separator/types.d.ts +1 -0
- package/dist/separator/types.js +1 -0
- package/dist/slider/README.md +61 -0
- package/dist/slider/fill/slider-fill.svelte +40 -0
- package/dist/slider/fill/slider-fill.svelte.d.ts +4 -0
- package/dist/slider/index.d.ts +9 -0
- package/dist/slider/index.js +8 -0
- package/dist/slider/index.parts.d.ts +6 -0
- package/dist/slider/index.parts.js +6 -0
- package/dist/slider/label/slider-label.svelte +44 -0
- package/dist/slider/label/slider-label.svelte.d.ts +4 -0
- package/dist/slider/output/slider-output.svelte +46 -0
- package/dist/slider/output/slider-output.svelte.d.ts +4 -0
- package/dist/slider/root/context.d.ts +106 -0
- package/dist/slider/root/context.js +15 -0
- package/dist/slider/root/slider-root.svelte +545 -0
- package/dist/slider/root/slider-root.svelte.d.ts +4 -0
- package/dist/slider/root/slider-utils.d.ts +57 -0
- package/dist/slider/root/slider-utils.js +88 -0
- package/dist/slider/thumb/slider-thumb.svelte +276 -0
- package/dist/slider/thumb/slider-thumb.svelte.d.ts +4 -0
- package/dist/slider/track/slider-track.svelte +60 -0
- package/dist/slider/track/slider-track.svelte.d.ts +4 -0
- package/dist/slider/types.d.ts +144 -0
- package/dist/slider/types.js +1 -0
- package/dist/tooltip/README.md +53 -0
- package/dist/tooltip/arrow/tooltip-arrow.svelte +36 -0
- package/dist/tooltip/arrow/tooltip-arrow.svelte.d.ts +4 -0
- package/dist/tooltip/content/tooltip-content.svelte +148 -0
- package/dist/tooltip/content/tooltip-content.svelte.d.ts +4 -0
- package/dist/tooltip/index.d.ts +8 -0
- package/dist/tooltip/index.js +7 -0
- package/dist/tooltip/index.parts.d.ts +5 -0
- package/dist/tooltip/index.parts.js +5 -0
- package/dist/tooltip/provider/tooltip-provider.svelte +36 -0
- package/dist/tooltip/provider/tooltip-provider.svelte.d.ts +4 -0
- package/dist/tooltip/root/context.d.ts +46 -0
- package/dist/tooltip/root/context.js +22 -0
- package/dist/tooltip/root/safe-polygon.d.ts +26 -0
- package/dist/tooltip/root/safe-polygon.js +66 -0
- package/dist/tooltip/root/tooltip-group.d.ts +26 -0
- package/dist/tooltip/root/tooltip-group.js +41 -0
- package/dist/tooltip/root/tooltip-root.svelte +454 -0
- package/dist/tooltip/root/tooltip-root.svelte.d.ts +4 -0
- package/dist/tooltip/trigger/tooltip-trigger.svelte +52 -0
- package/dist/tooltip/trigger/tooltip-trigger.svelte.d.ts +4 -0
- package/dist/tooltip/types.d.ts +105 -0
- package/dist/tooltip/types.js +1 -0
- package/package.json +36 -1
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { getContext } from 'svelte';
|
|
3
|
+
import { SELECT_ITEM_CONTEXT_KEY, type SelectItemContext } from '../item/select-item.svelte';
|
|
4
|
+
import { useSelectContext } from '../root/context';
|
|
5
|
+
import type { SelectItemIndicatorProps } from '../types';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Select.ItemIndicator — the mark of a selected option. It is in the DOM only while its
|
|
9
|
+
* option is selected, unless `forceMount` keeps it.
|
|
10
|
+
*/
|
|
11
|
+
let {
|
|
12
|
+
children,
|
|
13
|
+
forceMount = false,
|
|
14
|
+
class: className = '',
|
|
15
|
+
...restProps
|
|
16
|
+
}: SelectItemIndicatorProps = $props();
|
|
17
|
+
|
|
18
|
+
const ctx = useSelectContext('Select.ItemIndicator');
|
|
19
|
+
const itemCtx = getContext<SelectItemContext | undefined>(SELECT_ITEM_CONTEXT_KEY);
|
|
20
|
+
|
|
21
|
+
if (!itemCtx) {
|
|
22
|
+
throw new Error('Select.ItemIndicator must be used inside a Select.Item');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const item = itemCtx;
|
|
26
|
+
const isSelected = $derived(ctx.selectedKeys.has(item.id));
|
|
27
|
+
const shouldRender = $derived(forceMount || isSelected);
|
|
28
|
+
</script>
|
|
29
|
+
|
|
30
|
+
{#if shouldRender}
|
|
31
|
+
<span
|
|
32
|
+
{...restProps}
|
|
33
|
+
aria-hidden="true"
|
|
34
|
+
class={className}
|
|
35
|
+
data-select-item-indicator="true"
|
|
36
|
+
data-state={isSelected ? 'checked' : 'unchecked'}
|
|
37
|
+
>
|
|
38
|
+
{#if children}
|
|
39
|
+
{@render children()}
|
|
40
|
+
{:else}
|
|
41
|
+
<svg
|
|
42
|
+
xmlns="http://www.w3.org/2000/svg"
|
|
43
|
+
viewBox="0 0 16 16"
|
|
44
|
+
fill="currentColor"
|
|
45
|
+
style="width:1rem;height:1rem"
|
|
46
|
+
>
|
|
47
|
+
<path
|
|
48
|
+
fill-rule="evenodd"
|
|
49
|
+
d="M12.416 3.376a.75.75 0 0 1 .208 1.04l-5 7.5a.75.75 0 0 1-1.154.114l-3-3a.75.75 0 0 1 1.06-1.06l2.353 2.353 4.493-6.74a.75.75 0 0 1 1.04-.207Z"
|
|
50
|
+
clip-rule="evenodd"
|
|
51
|
+
/>
|
|
52
|
+
</svg>
|
|
53
|
+
{/if}
|
|
54
|
+
</span>
|
|
55
|
+
{/if}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { useSelectContext } from '../root/context';
|
|
3
|
+
import type { SelectLabelProps } from '../types';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Select.Label — the accessible name of the field.
|
|
7
|
+
*
|
|
8
|
+
* It is a native `<label>` for the trigger: a button is a labelable element, thus a click
|
|
9
|
+
* on the text moves the focus to the trigger. It also registers its id with the root, and
|
|
10
|
+
* the trigger reads it into `aria-labelledby`, because the combobox role takes its name
|
|
11
|
+
* from that attribute and not from the content of the button.
|
|
12
|
+
*/
|
|
13
|
+
let {
|
|
14
|
+
children,
|
|
15
|
+
class: className = '',
|
|
16
|
+
id: idProp,
|
|
17
|
+
onclick: onClickExternal,
|
|
18
|
+
...restProps
|
|
19
|
+
}: SelectLabelProps = $props();
|
|
20
|
+
|
|
21
|
+
const ctx = useSelectContext('Select.Label');
|
|
22
|
+
const id = $derived(idProp ?? `select-label-${ctx.instanceId}`);
|
|
23
|
+
|
|
24
|
+
$effect(() => ctx.registerLabel(id));
|
|
25
|
+
|
|
26
|
+
// A click on a native label activates its button, and that would open the list. A click on
|
|
27
|
+
// the label of a native select only moves the focus, and this does the same.
|
|
28
|
+
function handleClick(event: MouseEvent & { currentTarget: EventTarget & HTMLLabelElement }) {
|
|
29
|
+
onClickExternal?.(event);
|
|
30
|
+
if (event.defaultPrevented) return;
|
|
31
|
+
event.preventDefault();
|
|
32
|
+
if (!ctx.isDisabled) ctx.triggerRef?.focus();
|
|
33
|
+
}
|
|
34
|
+
</script>
|
|
35
|
+
|
|
36
|
+
<label
|
|
37
|
+
{...restProps}
|
|
38
|
+
{id}
|
|
39
|
+
for={ctx.triggerId}
|
|
40
|
+
class={className}
|
|
41
|
+
onclick={handleClick}
|
|
42
|
+
data-select-label="true"
|
|
43
|
+
data-disabled={ctx.isDisabled || undefined}
|
|
44
|
+
>
|
|
45
|
+
{@render children?.()}
|
|
46
|
+
</label>
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
<script lang="ts" generics="T extends object = object">
|
|
2
|
+
import { tick, untrack } from 'svelte';
|
|
3
|
+
import { ListBoxRoot as ListBox, type ListBoxContext } from '../../listbox';
|
|
4
|
+
import { useSelectContext, type SelectKey } from '../root/context';
|
|
5
|
+
import type { ComponentProps, Snippet } from 'svelte';
|
|
6
|
+
|
|
7
|
+
type SelectListProps = Omit<
|
|
8
|
+
ComponentProps<typeof ListBox>,
|
|
9
|
+
| 'selectionMode'
|
|
10
|
+
| 'selectionBehavior'
|
|
11
|
+
| 'value'
|
|
12
|
+
| 'defaultValue'
|
|
13
|
+
| 'controlledValue'
|
|
14
|
+
| 'onChange'
|
|
15
|
+
| 'context'
|
|
16
|
+
| 'element'
|
|
17
|
+
| 'children'
|
|
18
|
+
| 'items'
|
|
19
|
+
| 'id'
|
|
20
|
+
| 'disabledKeys'
|
|
21
|
+
| 'disableFocusHandling'
|
|
22
|
+
| 'loop'
|
|
23
|
+
| 'typeahead'
|
|
24
|
+
| 'aria-label'
|
|
25
|
+
| 'aria-labelledby'
|
|
26
|
+
> & {
|
|
27
|
+
/**
|
|
28
|
+
* The items, for a list that the component makes from an array. Use it with a snippet that
|
|
29
|
+
* receives one item. Give the same array to the root, so the trigger knows the text of each
|
|
30
|
+
* key while the popover is closed.
|
|
31
|
+
*/
|
|
32
|
+
items?: Iterable<T>;
|
|
33
|
+
/** The content of the list. From an array of items, it receives one item. */
|
|
34
|
+
children?: Snippet<[T]> | Snippet;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Select.List — the listbox inside the popover.
|
|
39
|
+
*
|
|
40
|
+
* It is `ListBox.Root` with the selection of the select. The focus goes into it when the
|
|
41
|
+
* popover opens, on the selected option, thus the arrow keys, `Home`, `End` and the
|
|
42
|
+
* typeahead of the listbox operate the select. It adds `PageUp` and `PageDown`, which move
|
|
43
|
+
* the focus ten options, and it closes the popover on `Tab`.
|
|
44
|
+
*/
|
|
45
|
+
let {
|
|
46
|
+
children,
|
|
47
|
+
items,
|
|
48
|
+
onkeydowncapture: onKeyDownCaptureExternal,
|
|
49
|
+
onfocusin: onFocusInExternal,
|
|
50
|
+
onfocusout: onFocusOutExternal,
|
|
51
|
+
...props
|
|
52
|
+
}: SelectListProps = $props();
|
|
53
|
+
|
|
54
|
+
const ctx = useSelectContext('Select.List');
|
|
55
|
+
|
|
56
|
+
let listboxCtx: ListBoxContext | undefined = $state();
|
|
57
|
+
let listboxElement: HTMLElement | undefined = $state();
|
|
58
|
+
|
|
59
|
+
const listboxSelection = $derived(Array.from(ctx.selectedKeys));
|
|
60
|
+
const disabledKeys = $derived(Array.from(ctx.disabledKeys));
|
|
61
|
+
// A second press on the selected option must not clear it: the field always has an answer,
|
|
62
|
+
// the same as a native select. In the multiple mode a press toggles.
|
|
63
|
+
const selectionBehavior = $derived(ctx.selectionMode === 'single' ? 'replace' : 'toggle');
|
|
64
|
+
|
|
65
|
+
$effect(() => {
|
|
66
|
+
const current = listboxCtx ?? null;
|
|
67
|
+
ctx.setListboxCtx(current);
|
|
68
|
+
return () => {
|
|
69
|
+
if (ctx.listboxCtx === current) ctx.setListboxCtx(null);
|
|
70
|
+
};
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
$effect(() => {
|
|
74
|
+
const current = listboxElement ?? null;
|
|
75
|
+
ctx.setListboxRef(current);
|
|
76
|
+
return () => {
|
|
77
|
+
if (ctx.listboxRef === current) ctx.setListboxRef(null);
|
|
78
|
+
};
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
function handleSelectionChange(selection: Set<SelectKey>) {
|
|
82
|
+
ctx.commitSelection(selection);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// The root shows one focus state for the trigger and the list together: the list is in a
|
|
86
|
+
// portal, thus the root cannot read it from its own descendants.
|
|
87
|
+
$effect(() => {
|
|
88
|
+
if (!listboxCtx) return;
|
|
89
|
+
return listboxCtx.subscribeToFocusVisible((visible) => {
|
|
90
|
+
if (ctx.isFocusWithin) ctx.setFocusVisible(visible);
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
function handleFocusIn(event: FocusEvent & { currentTarget: EventTarget & HTMLDivElement }) {
|
|
95
|
+
ctx.setFocusWithin(true);
|
|
96
|
+
onFocusInExternal?.(event);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function handleFocusOut(event: FocusEvent & { currentTarget: EventTarget & HTMLDivElement }) {
|
|
100
|
+
queueMicrotask(() => {
|
|
101
|
+
const active = document.activeElement;
|
|
102
|
+
const trigger = ctx.triggerRef;
|
|
103
|
+
const inTrigger = !!trigger && (active === trigger || trigger.contains(active));
|
|
104
|
+
const inList =
|
|
105
|
+
!!listboxElement && (active === listboxElement || listboxElement.contains(active));
|
|
106
|
+
if (!inTrigger && !inList) {
|
|
107
|
+
ctx.setFocusWithin(false);
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
onFocusOutExternal?.(event);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function getEnabledOptions(): HTMLElement[] {
|
|
114
|
+
if (!listboxElement) return [];
|
|
115
|
+
return Array.from(
|
|
116
|
+
listboxElement.querySelectorAll<HTMLElement>('[data-navigation-item]:not([data-disabled])')
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function readOptionKey(option: HTMLElement | null): SelectKey | null {
|
|
121
|
+
if (!option || option.getAttribute('role') !== 'option') return null;
|
|
122
|
+
const raw = option.dataset.itemId;
|
|
123
|
+
if (raw === undefined) return null;
|
|
124
|
+
return option.dataset.itemIdType === 'number' ? Number(raw) : raw;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function focusOptionByKey(key: SelectKey) {
|
|
128
|
+
listboxCtx?.keyboardNav.focusById(key);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Puts the focus in the list when the popover opens.
|
|
133
|
+
*
|
|
134
|
+
* The selected option, when there is one and it is enabled; otherwise the first or the
|
|
135
|
+
* last option, as the key that opened the popover asked. A character typed on the closed
|
|
136
|
+
* trigger is searched afterwards, thus the focus lands on the option that starts with it.
|
|
137
|
+
*/
|
|
138
|
+
async function applyOpenFocus() {
|
|
139
|
+
await tick();
|
|
140
|
+
if (!ctx.isOpen || !listboxCtx || !listboxElement) return;
|
|
141
|
+
listboxCtx.keyboardNav.updateItems();
|
|
142
|
+
const intent = ctx.consumeOpenFocus();
|
|
143
|
+
const [selected] = ctx.selectedKeys;
|
|
144
|
+
const enabledKeys = new Set(
|
|
145
|
+
getEnabledOptions()
|
|
146
|
+
.map((option) => readOptionKey(option))
|
|
147
|
+
.filter((key): key is SelectKey => key !== null)
|
|
148
|
+
);
|
|
149
|
+
|
|
150
|
+
if (intent === 'last') {
|
|
151
|
+
listboxCtx.keyboardNav.focusLast();
|
|
152
|
+
} else if (intent === 'selected' && selected !== undefined && enabledKeys.has(selected)) {
|
|
153
|
+
focusOptionByKey(selected);
|
|
154
|
+
} else {
|
|
155
|
+
listboxCtx.keyboardNav.focusFirst();
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const char = ctx.consumePendingTypeahead();
|
|
159
|
+
if (char) {
|
|
160
|
+
listboxCtx.keyboardNav.typeahead(char);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
let wasOpen = false;
|
|
165
|
+
$effect(() => {
|
|
166
|
+
const open = ctx.isOpen;
|
|
167
|
+
const ready = Boolean(listboxCtx && listboxElement);
|
|
168
|
+
if (open && ready && !wasOpen) {
|
|
169
|
+
wasOpen = true;
|
|
170
|
+
untrack(() => void applyOpenFocus());
|
|
171
|
+
} else if (!open) {
|
|
172
|
+
wasOpen = false;
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
// The listbox itself has the arrows, `Home`, `End`, `Enter`, `Space` and the typeahead. This
|
|
177
|
+
// runs in the capture phase, before the listbox: its own handler takes `ArrowUp` for a move,
|
|
178
|
+
// and it would move the focus before `Alt+ArrowUp` can close.
|
|
179
|
+
function handleKeyDownCapture(
|
|
180
|
+
event: KeyboardEvent & { currentTarget: EventTarget & HTMLDivElement }
|
|
181
|
+
) {
|
|
182
|
+
onKeyDownCaptureExternal?.(event);
|
|
183
|
+
if (event.defaultPrevented) return;
|
|
184
|
+
|
|
185
|
+
if (event.key === 'Tab') {
|
|
186
|
+
// The focus goes to the trigger first, without `preventDefault`, so the browser
|
|
187
|
+
// continues from there: from the portal it would land at the end of the document.
|
|
188
|
+
ctx.triggerRef?.focus();
|
|
189
|
+
ctx.close('focus-out', event);
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (event.key === 'PageUp' || event.key === 'PageDown') {
|
|
194
|
+
event.preventDefault();
|
|
195
|
+
const options = getEnabledOptions();
|
|
196
|
+
if (options.length === 0) return;
|
|
197
|
+
const active = document.activeElement as HTMLElement | null;
|
|
198
|
+
const currentIndex = active ? options.indexOf(active) : -1;
|
|
199
|
+
const step = event.key === 'PageDown' ? 10 : -10;
|
|
200
|
+
const nextIndex = Math.min(options.length - 1, Math.max(0, currentIndex + step));
|
|
201
|
+
const key = readOptionKey(options[nextIndex]);
|
|
202
|
+
if (key !== null) focusOptionByKey(key);
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// Alt+ArrowUp selects the focused option and closes the list, as in the APG pattern. With
|
|
207
|
+
// nothing focused it only closes.
|
|
208
|
+
if (event.key === 'ArrowUp' && event.altKey) {
|
|
209
|
+
event.preventDefault();
|
|
210
|
+
event.stopPropagation();
|
|
211
|
+
const active = document.activeElement as HTMLElement | null;
|
|
212
|
+
const key = readOptionKey(active);
|
|
213
|
+
if (key !== null && listboxCtx && !listboxCtx.isDisabled(key)) {
|
|
214
|
+
listboxCtx.select(key);
|
|
215
|
+
if (!ctx.isOpen) return;
|
|
216
|
+
}
|
|
217
|
+
ctx.close('imperative-action', event);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
</script>
|
|
221
|
+
|
|
222
|
+
<ListBox
|
|
223
|
+
{...props}
|
|
224
|
+
bind:context={listboxCtx}
|
|
225
|
+
bind:element={listboxElement}
|
|
226
|
+
id={ctx.listboxId}
|
|
227
|
+
{items}
|
|
228
|
+
{children}
|
|
229
|
+
selectionMode={ctx.selectionMode}
|
|
230
|
+
{selectionBehavior}
|
|
231
|
+
{disabledKeys}
|
|
232
|
+
loop={ctx.loop}
|
|
233
|
+
typeahead={true}
|
|
234
|
+
value={listboxSelection}
|
|
235
|
+
onChange={handleSelectionChange}
|
|
236
|
+
aria-labelledby={ctx.triggerId}
|
|
237
|
+
data-select-list="true"
|
|
238
|
+
onkeydowncapture={handleKeyDownCapture}
|
|
239
|
+
onfocusin={handleFocusIn}
|
|
240
|
+
onfocusout={handleFocusOut}
|
|
241
|
+
/>
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { type ListBoxContext } from '../../listbox';
|
|
2
|
+
import type { Snippet } from 'svelte';
|
|
3
|
+
declare function $$render<T extends object = object>(): {
|
|
4
|
+
props: Omit<{
|
|
5
|
+
selectionBehavior?: "toggle" | "replace";
|
|
6
|
+
emptyPlaceholder?: string | Snippet;
|
|
7
|
+
items?: Iterable<object> | undefined;
|
|
8
|
+
getItemKey?: ((item: object) => string | number) | undefined;
|
|
9
|
+
disabledKeys?: Iterable<string | number>;
|
|
10
|
+
selectionMode?: "single" | "multiple";
|
|
11
|
+
value?: Iterable<string | number>;
|
|
12
|
+
defaultValue?: Iterable<string | number>;
|
|
13
|
+
controlledValue?: boolean;
|
|
14
|
+
children?: Snippet<[]> | Snippet<[object]> | undefined;
|
|
15
|
+
header?: Snippet;
|
|
16
|
+
class?: string;
|
|
17
|
+
id?: string;
|
|
18
|
+
'aria-label'?: string;
|
|
19
|
+
'aria-labelledby'?: string;
|
|
20
|
+
onChange?: ((value: Set<string | number>) => void) | undefined;
|
|
21
|
+
disableFocusHandling?: boolean;
|
|
22
|
+
loop?: boolean;
|
|
23
|
+
typeahead?: boolean;
|
|
24
|
+
virtualizer?: {
|
|
25
|
+
rowHeight?: number;
|
|
26
|
+
overscan?: number;
|
|
27
|
+
} | undefined;
|
|
28
|
+
} & Omit<import("svelte/elements").HTMLAttributes<HTMLDivElement>, "children" | "class" | "id" | "role" | "aria-label" | "aria-labelledby" | "onchange"> & {
|
|
29
|
+
context?: ListBoxContext;
|
|
30
|
+
element?: HTMLElement;
|
|
31
|
+
}, "children" | "id" | "aria-label" | "aria-labelledby" | "value" | "element" | "defaultValue" | "controlledValue" | "onChange" | "selectionMode" | "loop" | "context" | "typeahead" | "disableFocusHandling" | "selectionBehavior" | "items" | "disabledKeys"> & {
|
|
32
|
+
/**
|
|
33
|
+
* The items, for a list that the component makes from an array. Use it with a snippet that
|
|
34
|
+
* receives one item. Give the same array to the root, so the trigger knows the text of each
|
|
35
|
+
* key while the popover is closed.
|
|
36
|
+
*/
|
|
37
|
+
items?: Iterable<T>;
|
|
38
|
+
/** The content of the list. From an array of items, it receives one item. */
|
|
39
|
+
children?: Snippet<[T]> | Snippet;
|
|
40
|
+
};
|
|
41
|
+
exports: {};
|
|
42
|
+
bindings: "";
|
|
43
|
+
slots: {};
|
|
44
|
+
events: {};
|
|
45
|
+
};
|
|
46
|
+
declare class __sveltets_Render<T extends object = object> {
|
|
47
|
+
props(): ReturnType<typeof $$render<T>>['props'];
|
|
48
|
+
events(): ReturnType<typeof $$render<T>>['events'];
|
|
49
|
+
slots(): ReturnType<typeof $$render<T>>['slots'];
|
|
50
|
+
bindings(): "";
|
|
51
|
+
exports(): {};
|
|
52
|
+
}
|
|
53
|
+
interface $$IsomorphicComponent {
|
|
54
|
+
new <T extends object = object>(options: import('svelte').ComponentConstructorOptions<ReturnType<__sveltets_Render<T>['props']>>): import('svelte').SvelteComponent<ReturnType<__sveltets_Render<T>['props']>, ReturnType<__sveltets_Render<T>['events']>, ReturnType<__sveltets_Render<T>['slots']>> & {
|
|
55
|
+
$$bindings?: ReturnType<__sveltets_Render<T>['bindings']>;
|
|
56
|
+
} & ReturnType<__sveltets_Render<T>['exports']>;
|
|
57
|
+
<T extends object = object>(internal: unknown, props: ReturnType<__sveltets_Render<T>['props']> & {}): ReturnType<__sveltets_Render<T>['exports']>;
|
|
58
|
+
z_$$bindings?: ReturnType<__sveltets_Render<any>['bindings']>;
|
|
59
|
+
}
|
|
60
|
+
declare const SelectList: $$IsomorphicComponent;
|
|
61
|
+
type SelectList<T extends object = object> = InstanceType<typeof SelectList<T>>;
|
|
62
|
+
export default SelectList;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { Popover } from '../../popover';
|
|
3
|
+
import type { PopoverOpenChangeDetails } from '../../popover/root/context';
|
|
4
|
+
import { containWheel } from '../../primitives/contain-wheel';
|
|
5
|
+
import { useSelectContext, type SelectCloseReason } from '../root/context';
|
|
6
|
+
import type { ComponentProps, Snippet } from 'svelte';
|
|
7
|
+
|
|
8
|
+
type SelectPopoverProps = Omit<
|
|
9
|
+
ComponentProps<typeof Popover.Content>,
|
|
10
|
+
'open' | 'triggerRef' | 'onOpenChange' | 'children' | 'role' | 'initialFocus'
|
|
11
|
+
> & {
|
|
12
|
+
/** The content: the list. */
|
|
13
|
+
children?: Snippet;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Select.Popover — the floating panel that holds the list.
|
|
18
|
+
*
|
|
19
|
+
* It is `Popover.Content` with the state of the select. The panel is not a dialog: it has
|
|
20
|
+
* `role="presentation"`, thus the listbox inside it is what the trigger controls and what
|
|
21
|
+
* assistive technology lands on.
|
|
22
|
+
*/
|
|
23
|
+
let {
|
|
24
|
+
class: className = '',
|
|
25
|
+
children,
|
|
26
|
+
placement = 'bottom-start',
|
|
27
|
+
nonModal = true,
|
|
28
|
+
onwheel: onWheelExternal,
|
|
29
|
+
...contentProps
|
|
30
|
+
}: SelectPopoverProps = $props();
|
|
31
|
+
|
|
32
|
+
const ctx = useSelectContext('Select.Popover');
|
|
33
|
+
|
|
34
|
+
const POPOVER_TO_SELECT_REASON: Record<string, SelectCloseReason> = {
|
|
35
|
+
'escape-key': 'escape-key',
|
|
36
|
+
'outside-press': 'outside-press',
|
|
37
|
+
'focus-out': 'focus-out',
|
|
38
|
+
scroll: 'scroll',
|
|
39
|
+
'close-press': 'imperative-action',
|
|
40
|
+
'imperative-action': 'imperative-action',
|
|
41
|
+
none: 'none'
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
// The root holds the open state. A close that `Popover.Content` asks for (Escape, an
|
|
45
|
+
// outside press, a Tab out, a scroll) goes to the root, and a refusal of the consumer goes
|
|
46
|
+
// back to `Popover.Root` through the same details, so both agree. `Popover.Root` then
|
|
47
|
+
// applies the focus state of the trigger, as it does for every popover.
|
|
48
|
+
function handleOpenChange(open: boolean, details: PopoverOpenChangeDetails) {
|
|
49
|
+
if (open) {
|
|
50
|
+
ctx.open('trigger-press', details.event);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
const reason = POPOVER_TO_SELECT_REASON[details.reason] ?? 'imperative-action';
|
|
54
|
+
if (!ctx.close(reason, details.event)) {
|
|
55
|
+
details.cancel();
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function handleWheel(event: WheelEvent & { currentTarget: EventTarget & HTMLDivElement }) {
|
|
60
|
+
containWheel(event, event.currentTarget);
|
|
61
|
+
onWheelExternal?.(event);
|
|
62
|
+
}
|
|
63
|
+
</script>
|
|
64
|
+
|
|
65
|
+
<Popover.Root open={ctx.isOpen} triggerRef={ctx.triggerRef} onOpenChange={handleOpenChange}>
|
|
66
|
+
<Popover.Content
|
|
67
|
+
{nonModal}
|
|
68
|
+
{placement}
|
|
69
|
+
class={className}
|
|
70
|
+
role="presentation"
|
|
71
|
+
aria-modal={undefined}
|
|
72
|
+
data-select-popover="true"
|
|
73
|
+
onwheel={handleWheel}
|
|
74
|
+
{...contentProps}
|
|
75
|
+
>
|
|
76
|
+
{#if children}
|
|
77
|
+
{@render children()}
|
|
78
|
+
{/if}
|
|
79
|
+
</Popover.Content>
|
|
80
|
+
</Popover.Root>
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Popover } from '../../popover';
|
|
2
|
+
import type { ComponentProps, Snippet } from 'svelte';
|
|
3
|
+
type SelectPopoverProps = Omit<ComponentProps<typeof Popover.Content>, 'open' | 'triggerRef' | 'onOpenChange' | 'children' | 'role' | 'initialFocus'> & {
|
|
4
|
+
/** The content: the list. */
|
|
5
|
+
children?: Snippet;
|
|
6
|
+
};
|
|
7
|
+
declare const SelectPopover: import("svelte").Component<SelectPopoverProps, {}, "">;
|
|
8
|
+
type SelectPopover = ReturnType<typeof SelectPopover>;
|
|
9
|
+
export default SelectPopover;
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import type { ListBoxContext } from '../../listbox/root/context';
|
|
2
|
+
import type { PopoverCloseReason } from '../../popover/root/context';
|
|
3
|
+
export type SelectKey = string | number;
|
|
4
|
+
/**
|
|
5
|
+
* The reasons of Popover, less its close button, plus the two of a select: a press on the open
|
|
6
|
+
* trigger, and a selection.
|
|
7
|
+
*/
|
|
8
|
+
export type SelectCloseReason = Exclude<PopoverCloseReason, 'close-press'> | 'trigger-press' | 'item-select';
|
|
9
|
+
export type SelectOpenReason = 'trigger-press' | 'imperative-action' | 'none';
|
|
10
|
+
export type SelectChangeReason = SelectOpenReason | SelectCloseReason;
|
|
11
|
+
export type SelectOpenChangeDetails = {
|
|
12
|
+
reason: SelectChangeReason;
|
|
13
|
+
event?: Event;
|
|
14
|
+
cancel: () => void;
|
|
15
|
+
isCanceled: boolean;
|
|
16
|
+
};
|
|
17
|
+
/** Where the focus lands in the list when the popover opens. */
|
|
18
|
+
export type SelectOpenFocusIntent = 'selected' | 'first' | 'last';
|
|
19
|
+
/**
|
|
20
|
+
* Context shared between Select.Root and its parts.
|
|
21
|
+
*/
|
|
22
|
+
export type SelectContext = {
|
|
23
|
+
/** The instance id. Every ARIA id of the parts is made from it. */
|
|
24
|
+
instanceId: string;
|
|
25
|
+
/** The id of the trigger element. */
|
|
26
|
+
triggerId: string;
|
|
27
|
+
/** The id of the listbox element. */
|
|
28
|
+
listboxId: string;
|
|
29
|
+
/** The id of the `Select.Value` element, when one is in the DOM. */
|
|
30
|
+
valueId: string | null;
|
|
31
|
+
/** The id of the `Select.Label` element, when one is in the DOM. */
|
|
32
|
+
labelId: string | null;
|
|
33
|
+
/** Whether the popover is open. */
|
|
34
|
+
isOpen: boolean;
|
|
35
|
+
/** The selected keys. One key at most in the single mode. */
|
|
36
|
+
selectedKeys: Set<SelectKey>;
|
|
37
|
+
/** The selection mode. */
|
|
38
|
+
selectionMode: 'single' | 'multiple';
|
|
39
|
+
/** Whether the whole select is disabled. */
|
|
40
|
+
isDisabled: boolean;
|
|
41
|
+
/** Whether the select is read-only: it keeps the focus, and it refuses to open. */
|
|
42
|
+
isReadOnly: boolean;
|
|
43
|
+
/** Whether a form must have a value here. */
|
|
44
|
+
isRequired: boolean;
|
|
45
|
+
/** Whether the value failed a validation. */
|
|
46
|
+
isInvalid: boolean;
|
|
47
|
+
/** The `name` of the hidden form control. */
|
|
48
|
+
name: string | undefined;
|
|
49
|
+
/** The keys the user cannot select. */
|
|
50
|
+
disabledKeys: Set<SelectKey>;
|
|
51
|
+
/** Whether the arrow keys wrap at the ends of the list. */
|
|
52
|
+
loop: boolean;
|
|
53
|
+
/** The reference to the trigger element. */
|
|
54
|
+
triggerRef: HTMLElement | null;
|
|
55
|
+
/** The reference to the listbox element. */
|
|
56
|
+
listboxRef: HTMLElement | null;
|
|
57
|
+
/** The context of the inner ListBox, once it is in the DOM. */
|
|
58
|
+
listboxCtx: ListBoxContext | null;
|
|
59
|
+
/** The text of a key, from `items` or from an option that registered it. */
|
|
60
|
+
getLabel: (key: SelectKey) => string;
|
|
61
|
+
/** The keys in list order, when the root knows them from `items`. */
|
|
62
|
+
orderedKeys: SelectKey[];
|
|
63
|
+
/** The placeholder text, from the prop or from the locale. */
|
|
64
|
+
placeholder: string;
|
|
65
|
+
/** The `aria-label` given to the root. The trigger carries it. */
|
|
66
|
+
ariaLabel: string | undefined;
|
|
67
|
+
/** The `aria-labelledby` given to the root. The trigger carries it. */
|
|
68
|
+
ariaLabelledBy: string | undefined;
|
|
69
|
+
/** The `aria-describedby` given to the root. The trigger carries it. */
|
|
70
|
+
ariaDescribedBy: string | undefined;
|
|
71
|
+
setTriggerRef: (el: HTMLElement | null) => void;
|
|
72
|
+
setListboxRef: (el: HTMLElement | null) => void;
|
|
73
|
+
setListboxCtx: (ctx: ListBoxContext | null) => void;
|
|
74
|
+
/** Registers the id of `Select.Value`. Returns the unregister function. */
|
|
75
|
+
registerValue: (id: string) => () => void;
|
|
76
|
+
/** Registers the id of `Select.Label`. Returns the unregister function. */
|
|
77
|
+
registerLabel: (id: string) => () => void;
|
|
78
|
+
/** Registers the text of an option, so the trigger can show it after the option leaves the DOM. */
|
|
79
|
+
registerItemLabel: (key: SelectKey, label: string) => void;
|
|
80
|
+
open: (reason?: SelectOpenReason, event?: Event, focus?: SelectOpenFocusIntent) => void;
|
|
81
|
+
/** Closes the popover. Returns false when it was closed already, or when the consumer refused. */
|
|
82
|
+
close: (reason?: SelectCloseReason, event?: Event) => boolean;
|
|
83
|
+
toggle: (reason?: SelectOpenReason, event?: Event) => void;
|
|
84
|
+
/** Opens the popover with a character for the list to search with. */
|
|
85
|
+
openWithTypeahead: (char: string, event?: Event) => void;
|
|
86
|
+
/** Applies a selection that the list reports, and closes the popover when the mode asks for it. */
|
|
87
|
+
commitSelection: (keys: Set<SelectKey>) => void;
|
|
88
|
+
/** Reads and clears where the focus must land at the next open. */
|
|
89
|
+
consumeOpenFocus: () => SelectOpenFocusIntent;
|
|
90
|
+
/** Reads and clears a character typed on the closed trigger, for the list to search with. */
|
|
91
|
+
consumePendingTypeahead: () => string | null;
|
|
92
|
+
/** Whether the focus is on the trigger or in the list. */
|
|
93
|
+
isFocusWithin: boolean;
|
|
94
|
+
/** Whether the focus in the select must show as keyboard focus. */
|
|
95
|
+
isFocusVisible: boolean;
|
|
96
|
+
setFocusWithin: (within: boolean) => void;
|
|
97
|
+
setFocusVisible: (visible: boolean) => void;
|
|
98
|
+
};
|
|
99
|
+
export declare function setSelectContext(ctx: SelectContext): void;
|
|
100
|
+
export declare function getSelectContext(): SelectContext | undefined;
|
|
101
|
+
export declare function useSelectContext(part: string): SelectContext;
|
|
102
|
+
/**
|
|
103
|
+
* A deterministic DOM id for an option. A key can hold characters that are not safe in an id
|
|
104
|
+
* reference, so it is sanitized first.
|
|
105
|
+
*/
|
|
106
|
+
export declare function getSelectItemDomId(instanceId: string, key: SelectKey): string;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { setContext, getContext } from 'svelte';
|
|
2
|
+
const SELECT_KEY = Symbol('select');
|
|
3
|
+
export function setSelectContext(ctx) {
|
|
4
|
+
setContext(SELECT_KEY, ctx);
|
|
5
|
+
}
|
|
6
|
+
export function getSelectContext() {
|
|
7
|
+
return getContext(SELECT_KEY);
|
|
8
|
+
}
|
|
9
|
+
export function useSelectContext(part) {
|
|
10
|
+
const ctx = getSelectContext();
|
|
11
|
+
if (!ctx) {
|
|
12
|
+
throw new Error(`${part} must be used inside a Select.Root`);
|
|
13
|
+
}
|
|
14
|
+
return ctx;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* A deterministic DOM id for an option. A key can hold characters that are not safe in an id
|
|
18
|
+
* reference, so it is sanitized first.
|
|
19
|
+
*/
|
|
20
|
+
export function getSelectItemDomId(instanceId, key) {
|
|
21
|
+
return `select-item-${instanceId}-${String(key).replace(/[^a-zA-Z0-9_-]/g, '_')}`;
|
|
22
|
+
}
|