@astronox/core 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/LICENSE +21 -0
- package/README.md +66 -0
- package/package.json +77 -0
- package/src/client/clipboard.ts +7 -0
- package/src/client/focus.ts +17 -0
- package/src/client/index.ts +3 -0
- package/src/client/scroll.ts +15 -0
- package/src/components/actions/ActionListItem/ActionListItem.astro +78 -0
- package/src/components/actions/ActionListItem/ActionListItem.css +43 -0
- package/src/components/actions/Button/Button.astro +100 -0
- package/src/components/actions/Button/Button.css +165 -0
- package/src/components/actions/CardAction/CardAction.astro +97 -0
- package/src/components/actions/CardAction/CardAction.css +188 -0
- package/src/components/actions/MediaTrigger/MediaTrigger.astro +55 -0
- package/src/components/actions/MediaTrigger/MediaTrigger.css +175 -0
- package/src/components/actions/ScrollToTop/ScrollToTop.astro +36 -0
- package/src/components/actions/ScrollToTop/ScrollToTop.css +19 -0
- package/src/components/actions/ScrollToTop/scrollToTopController.ts +32 -0
- package/src/components/content/CardBody/CardBody.astro +31 -0
- package/src/components/content/CardBody/CardBody.css +74 -0
- package/src/components/content/CardFooter/CardFooter.astro +58 -0
- package/src/components/content/CardFooter/CardFooter.css +102 -0
- package/src/components/content/CardHeader/CardHeader.astro +96 -0
- package/src/components/content/CardHeader/CardHeader.css +164 -0
- package/src/components/content/CardLink/CardLink.astro +138 -0
- package/src/components/content/CardLink/CardLink.css +362 -0
- package/src/components/content/CodeBlock/CodeBlock.astro +102 -0
- package/src/components/content/CodeBlock/CodeBlock.css +85 -0
- package/src/components/content/CodeBlock/codeBlockController.ts +60 -0
- package/src/components/content/CodeBlock/contracts.ts +8 -0
- package/src/components/content/InlineDefinition/InlineDefinition.astro +31 -0
- package/src/components/content/InlineDefinition/InlineDefinition.css +9 -0
- package/src/components/content/MarkdownContent/MarkdownContent.astro +32 -0
- package/src/components/content/MarkdownContent/MarkdownContent.css +358 -0
- package/src/components/content/MarkdownContent/MarkdownInline.astro +44 -0
- package/src/components/content/MarkdownContent/MarkdownNode.astro +280 -0
- package/src/components/content/MarkdownContent/MarkdownSymbol.astro +47 -0
- package/src/components/content/MarkdownContent/contracts.ts +181 -0
- package/src/components/data-display/BadgeList/BadgeList.astro +54 -0
- package/src/components/data-display/BadgeList/BadgeList.css +29 -0
- package/src/components/data-display/IconList/IconList.astro +64 -0
- package/src/components/data-display/IconList/IconList.css +55 -0
- package/src/components/data-display/MetricTile/MetricTile.astro +68 -0
- package/src/components/data-display/MetricTile/MetricTile.css +103 -0
- package/src/components/data-display/MetricTile/MetricTileContent.astro +70 -0
- package/src/components/data-display/MetricTile/metricTileController.ts +9 -0
- package/src/components/data-display/Tag/Tag.astro +67 -0
- package/src/components/data-display/Tag/Tag.css +134 -0
- package/src/components/data-display/Tag/TagAffix.astro +33 -0
- package/src/components/data-display/TextPair/TextPair.astro +63 -0
- package/src/components/data-display/TextPair/TextPair.css +99 -0
- package/src/components/feedback/Spinner/Spinner.astro +32 -0
- package/src/components/feedback/Spinner/Spinner.css +37 -0
- package/src/components/feedback/Tooltip/Tooltip.astro +66 -0
- package/src/components/feedback/Tooltip/Tooltip.css +67 -0
- package/src/components/feedback/Tooltip/tooltipController.ts +371 -0
- package/src/components/forms/FieldError/FieldError.astro +31 -0
- package/src/components/forms/FieldError/FieldError.css +12 -0
- package/src/components/forms/FieldError/fieldErrorState.ts +21 -0
- package/src/components/forms/FieldLabel/FieldLabel.astro +32 -0
- package/src/components/forms/FieldLabel/FieldLabel.css +17 -0
- package/src/components/forms/TextInput/TextInput.astro +79 -0
- package/src/components/forms/TextInput/TextInput.css +121 -0
- package/src/components/forms/TextInput/textInputController.ts +53 -0
- package/src/components/icons/ArrowDownIcon.astro +26 -0
- package/src/components/icons/ArrowLeftIcon.astro +26 -0
- package/src/components/icons/ArrowRightIcon.astro +26 -0
- package/src/components/icons/ArrowUpDownIcon.astro +26 -0
- package/src/components/icons/ArrowUpIcon.astro +26 -0
- package/src/components/icons/BulletIcon.astro +21 -0
- package/src/components/icons/BurnIcon.astro +24 -0
- package/src/components/icons/CircleSymbolIcon.css +66 -0
- package/src/components/icons/ClearIcon.astro +28 -0
- package/src/components/icons/CloseIcon.astro +21 -0
- package/src/components/icons/CopyIcon.astro +30 -0
- package/src/components/icons/CurrencyIcon.css +9 -0
- package/src/components/icons/DayIcon.astro +33 -0
- package/src/components/icons/DollarIcon.astro +28 -0
- package/src/components/icons/DownloadIcon.astro +22 -0
- package/src/components/icons/EnterFullscreenIcon.astro +22 -0
- package/src/components/icons/EqualIcon.astro +25 -0
- package/src/components/icons/ExitFullscreenIcon.astro +22 -0
- package/src/components/icons/ExternalLinkIcon.astro +22 -0
- package/src/components/icons/FieldIcon.astro +28 -0
- package/src/components/icons/GrassIcon.astro +29 -0
- package/src/components/icons/InfoIcon.astro +24 -0
- package/src/components/icons/MinusIcon.astro +21 -0
- package/src/components/icons/MountainIcon.astro +31 -0
- package/src/components/icons/NightIcon.astro +32 -0
- package/src/components/icons/PlayIcon.astro +22 -0
- package/src/components/icons/PlusIcon.astro +21 -0
- package/src/components/icons/PoisonIcon.astro +24 -0
- package/src/components/icons/RibbonIcon.astro +25 -0
- package/src/components/icons/SlashIcon.astro +22 -0
- package/src/components/icons/SlowIcon.astro +25 -0
- package/src/components/icons/StarIcon.astro +28 -0
- package/src/components/icons/StunIcon.astro +22 -0
- package/src/components/icons/TableSortIcon.astro +34 -0
- package/src/components/icons/WaterIcon.astro +30 -0
- package/src/components/index.ts +83 -0
- package/src/components/media/CanvasFrame/CanvasFrame.astro +130 -0
- package/src/components/media/CanvasFrame/CanvasFrame.css +59 -0
- package/src/components/media/CanvasFrame/canvasFrameController.ts +104 -0
- package/src/components/media/EmbedFrame/EmbedFrame.astro +147 -0
- package/src/components/media/EmbedFrame/EmbedFrame.css +51 -0
- package/src/components/media/EmbedFrame/embedFrameController.ts +66 -0
- package/src/components/media/MediaFrame/MediaFrame.astro +34 -0
- package/src/components/media/MediaFrame/MediaFrame.css +66 -0
- package/src/components/navigation/Breadcrumbs/Breadcrumbs.astro +55 -0
- package/src/components/navigation/Breadcrumbs/Breadcrumbs.css +89 -0
- package/src/components/navigation/DesktopNavigation.astro +92 -0
- package/src/components/navigation/DesktopNavigation.css +99 -0
- package/src/components/navigation/Header.astro +73 -0
- package/src/components/navigation/Header.css +20 -0
- package/src/components/navigation/MobileNavigation.astro +124 -0
- package/src/components/navigation/MobileNavigation.css +177 -0
- package/src/components/navigation/NavigationBrand.astro +63 -0
- package/src/components/navigation/NavigationBrand.css +52 -0
- package/src/components/navigation/NavigationControl.css +166 -0
- package/src/components/navigation/NavigationItemContent.astro +24 -0
- package/src/components/navigation/NavigationLink.astro +121 -0
- package/src/components/navigation/NavigationLinks.astro +27 -0
- package/src/components/navigation/NavigationMenuList.astro +37 -0
- package/src/components/navigation/NavigationMenuList.css +6 -0
- package/src/components/navigation/SkipLink/SkipLink.astro +18 -0
- package/src/components/navigation/SkipLink/SkipLink.css +26 -0
- package/src/components/navigation/SubNavigation/SubNavigation.astro +124 -0
- package/src/components/navigation/SubNavigation/SubNavigation.css +211 -0
- package/src/components/navigation/mobileNavigationController.ts +122 -0
- package/src/components/navigation/navigationState.ts +35 -0
- package/src/components/navigation/navigationTypes.ts +18 -0
- package/src/components/seo/SeoHead.astro +194 -0
- package/src/components/widgetTypes.ts +3 -0
- package/src/composition/Grid/Grid.astro +82 -0
- package/src/composition/Grid/Grid.css +314 -0
- package/src/composition/GridItem/GridItem.astro +50 -0
- package/src/composition/GridItem/GridItem.css +95 -0
- package/src/composition/Inline/Inline.astro +48 -0
- package/src/composition/Inline/Inline.css +65 -0
- package/src/composition/Stack/Stack.astro +37 -0
- package/src/composition/Stack/Stack.css +31 -0
- package/src/composition/SurfaceGroup/SurfaceGroup.astro +44 -0
- package/src/composition/SurfaceGroup/SurfaceGroup.css +76 -0
- package/src/composition/SurfaceGroup/stickySurfaceGroupController.ts +89 -0
- package/src/composition/index.ts +6 -0
- package/src/config/index.ts +2 -0
- package/src/config/pageMeta.ts +55 -0
- package/src/config/siteConfig.ts +90 -0
- package/src/data/collectionValidation.ts +61 -0
- package/src/data/index.ts +1 -0
- package/src/display/contracts.ts +284 -0
- package/src/display/index.ts +1 -0
- package/src/index.ts +6 -0
- package/src/layouts/BaseLayout.astro +74 -0
- package/src/layouts/PageSurface.astro +15 -0
- package/src/layouts/PageSurface.css +24 -0
- package/src/layouts/index.ts +2 -0
- package/src/patterns/CircuitPuzzle/CircuitPuzzle.astro +93 -0
- package/src/patterns/CircuitPuzzle/CircuitPuzzle.css +118 -0
- package/src/patterns/CircuitPuzzle/circuitPuzzleController.ts +205 -0
- package/src/patterns/CircuitPuzzle/contracts.ts +42 -0
- package/src/patterns/Combobox/Combobox.astro +462 -0
- package/src/patterns/Combobox/Combobox.css +403 -0
- package/src/patterns/Combobox/comboboxController.ts +788 -0
- package/src/patterns/Combobox/contracts.ts +18 -0
- package/src/patterns/Dropdown/Dropdown.astro +127 -0
- package/src/patterns/Dropdown/Dropdown.css +100 -0
- package/src/patterns/Dropdown/dropdownController.ts +157 -0
- package/src/patterns/Fullscreen/FullscreenControl.astro +103 -0
- package/src/patterns/Fullscreen/FullscreenControl.css +49 -0
- package/src/patterns/Fullscreen/fullscreenController.ts +73 -0
- package/src/patterns/GlitchOverlay/GlitchOverlay.astro +50 -0
- package/src/patterns/GlitchOverlay/GlitchOverlay.css +234 -0
- package/src/patterns/GlitchOverlay/glitchOverlayController.ts +152 -0
- package/src/patterns/GuidedFlow/GuidedFlow.astro +60 -0
- package/src/patterns/GuidedFlow/GuidedFlow.css +82 -0
- package/src/patterns/GuidedFlow/contracts.ts +3 -0
- package/src/patterns/GuidedFlow/guidedFlowController.ts +49 -0
- package/src/patterns/MagneticFieldPuzzle/MagneticFieldPuzzle.astro +217 -0
- package/src/patterns/MagneticFieldPuzzle/MagneticFieldPuzzle.css +383 -0
- package/src/patterns/MagneticFieldPuzzle/contracts.ts +81 -0
- package/src/patterns/MagneticFieldPuzzle/magneticFieldPuzzleController.ts +802 -0
- package/src/patterns/MagneticFieldPuzzle/simulation.ts +244 -0
- package/src/patterns/MapViewport/MapViewport.astro +296 -0
- package/src/patterns/MapViewport/MapViewport.css +257 -0
- package/src/patterns/MapViewport/contracts.ts +79 -0
- package/src/patterns/MapViewport/geometry.ts +80 -0
- package/src/patterns/MapViewport/mapViewportController.ts +636 -0
- package/src/patterns/Modal/Modal.astro +64 -0
- package/src/patterns/Modal/Modal.css +87 -0
- package/src/patterns/Modal/ModalClose.astro +37 -0
- package/src/patterns/Modal/ModalOpen.astro +36 -0
- package/src/patterns/Modal/modalController.ts +118 -0
- package/src/patterns/PuzzleGrid/PuzzleGrid.astro +22 -0
- package/src/patterns/PuzzleGrid/PuzzleGrid.css +149 -0
- package/src/patterns/PuzzleGrid/contracts.ts +2 -0
- package/src/patterns/PuzzleGrid/selectionDismissal.ts +27 -0
- package/src/patterns/PuzzleGrid/state.ts +25 -0
- package/src/patterns/SpriteProjectilePreview/SpriteProjectilePreview.astro +65 -0
- package/src/patterns/SpriteProjectilePreview/SpriteProjectilePreview.css +35 -0
- package/src/patterns/SpriteProjectilePreview/contracts.ts +33 -0
- package/src/patterns/SpriteProjectilePreview/spriteProjectilePreviewController.ts +586 -0
- package/src/patterns/StageFlow/StageFlow.astro +57 -0
- package/src/patterns/StageFlow/StageFlow.css +54 -0
- package/src/patterns/StageFlow/StageFlowAlternative.astro +23 -0
- package/src/patterns/StageFlow/StageFlowArrow.astro +22 -0
- package/src/patterns/StageFlow/StageFlowConnector.astro +7 -0
- package/src/patterns/StageFlow/StageFlowConnector.css +4 -0
- package/src/patterns/StageFlow/StageFlowStage.astro +51 -0
- package/src/patterns/StageFlow/StageFlowStage.css +106 -0
- package/src/patterns/StageFlow/StageFlowStep.astro +24 -0
- package/src/patterns/StageFlow/StageFlowStep.css +19 -0
- package/src/patterns/StageFlow/StageFlowSymbol.css +28 -0
- package/src/patterns/Table/Table.astro +176 -0
- package/src/patterns/Table/Table.css +70 -0
- package/src/patterns/Table/TableBody.astro +185 -0
- package/src/patterns/Table/TableBody.css +262 -0
- package/src/patterns/Table/TableCell.astro +59 -0
- package/src/patterns/Table/TableCell.css +99 -0
- package/src/patterns/Table/TableCellValue.astro +86 -0
- package/src/patterns/Table/TableFooter.astro +32 -0
- package/src/patterns/Table/TableFooter.css +31 -0
- package/src/patterns/Table/TableHeader.astro +118 -0
- package/src/patterns/Table/TableHeader.css +209 -0
- package/src/patterns/Table/TablePagination.astro +70 -0
- package/src/patterns/Table/TablePagination.css +133 -0
- package/src/patterns/Table/TableSkeleton.astro +36 -0
- package/src/patterns/Table/TableSkeleton.css +81 -0
- package/src/patterns/Table/tableCellController.ts +11 -0
- package/src/patterns/Table/tableFilterController.ts +206 -0
- package/src/patterns/Table/tableHeaderOverflowController.ts +105 -0
- package/src/patterns/Table/tableLayout.ts +113 -0
- package/src/patterns/Table/tableProgressiveController.ts +319 -0
- package/src/patterns/Table/tableRowNavigationController.ts +91 -0
- package/src/patterns/Table/tableScrollController.ts +192 -0
- package/src/patterns/Table/tableSortController.ts +272 -0
- package/src/patterns/index.ts +46 -0
- package/src/primitives/index.ts +4 -0
- package/src/primitives/layout/Separator/Separator.astro +29 -0
- package/src/primitives/layout/Separator/Separator.css +20 -0
- package/src/primitives/media/AmbientAudio/AmbientAudio.astro +28 -0
- package/src/primitives/media/Media/Media.astro +122 -0
- package/src/primitives/media/Media/Media.css +161 -0
- package/src/primitives/text/TextBlock/TextBlock.astro +46 -0
- package/src/primitives/text/TextBlock/TextBlock.css +21 -0
- package/src/seo/StaticRedirect.astro +32 -0
- package/src/seo/index.ts +6 -0
- package/src/seo/pageSeo.ts +99 -0
- package/src/seo/robots.ts +16 -0
- package/src/seo/sitemap.ts +38 -0
- package/src/seo/structuredData.ts +133 -0
- package/src/seo/url.ts +64 -0
- package/src/styles/base.css +148 -0
- package/src/styles/interaction.css +108 -0
- package/src/styles/style.css +93 -0
- package/src/styles/tokens.css +256 -0
- package/src/theme/contracts.ts +7 -0
- package/src/theme/index.ts +1 -0
- package/tsconfig.json +20 -0
|
@@ -0,0 +1,788 @@
|
|
|
1
|
+
export type ComboboxChangeDetail =
|
|
2
|
+
| {
|
|
3
|
+
mode: 'options';
|
|
4
|
+
values: readonly string[];
|
|
5
|
+
}
|
|
6
|
+
| {
|
|
7
|
+
mode: 'number';
|
|
8
|
+
value: number;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
type SetComboboxValueOptions = {
|
|
12
|
+
emit?: boolean;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
type NumberBounds = {
|
|
16
|
+
min: number;
|
|
17
|
+
max: number;
|
|
18
|
+
step: number;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const initializedAttribute = 'data-combobox-initialized';
|
|
22
|
+
const compactViewportQuery =
|
|
23
|
+
'(width < 48rem), (hover: none) and (pointer: coarse)';
|
|
24
|
+
const noop = (): void => undefined;
|
|
25
|
+
|
|
26
|
+
const normalizeSearchValue = (value: string): string =>
|
|
27
|
+
value
|
|
28
|
+
.normalize('NFD')
|
|
29
|
+
.replace(/\p{Diacritic}/gu, '')
|
|
30
|
+
.toLocaleLowerCase(document.documentElement.lang || undefined);
|
|
31
|
+
|
|
32
|
+
const getOptions = (root: HTMLElement): HTMLButtonElement[] =>
|
|
33
|
+
Array.from(
|
|
34
|
+
root.querySelectorAll<HTMLButtonElement>('[data-combobox-option]'),
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
export function getComboboxValues(root: HTMLElement): string[] {
|
|
38
|
+
return getOptions(root)
|
|
39
|
+
.filter((option) => option.getAttribute('aria-selected') === 'true')
|
|
40
|
+
.map((option) => option.dataset.comboboxOption)
|
|
41
|
+
.filter((value): value is string => value !== undefined);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const syncSelectedTags = (
|
|
45
|
+
root: HTMLElement,
|
|
46
|
+
selectedValues: readonly string[],
|
|
47
|
+
): void => {
|
|
48
|
+
const valuesRoot = root.querySelector<HTMLElement>('[data-combobox-values]');
|
|
49
|
+
const placeholder = root.querySelector<HTMLElement>(
|
|
50
|
+
'[data-combobox-placeholder]',
|
|
51
|
+
);
|
|
52
|
+
if (!valuesRoot || !placeholder) return;
|
|
53
|
+
|
|
54
|
+
for (const tag of valuesRoot.querySelectorAll('[data-combobox-tag]')) {
|
|
55
|
+
tag.remove();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const templates = Array.from(
|
|
59
|
+
root.querySelectorAll<HTMLTemplateElement>(
|
|
60
|
+
'template[data-combobox-tag-template]',
|
|
61
|
+
),
|
|
62
|
+
);
|
|
63
|
+
for (const value of selectedValues) {
|
|
64
|
+
const template = templates.find(
|
|
65
|
+
(candidate) => candidate.dataset.comboboxTagTemplate === value,
|
|
66
|
+
);
|
|
67
|
+
const tag = template?.content.firstElementChild?.cloneNode(true);
|
|
68
|
+
if (tag) valuesRoot.insertBefore(tag, placeholder);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
placeholder.hidden = selectedValues.length > 0;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
function getNumberBounds(root: HTMLElement): NumberBounds | null {
|
|
75
|
+
const min = Number(root.dataset.comboboxNumberMin);
|
|
76
|
+
const max = Number(root.dataset.comboboxNumberMax);
|
|
77
|
+
const step = Number(root.dataset.comboboxNumberStep);
|
|
78
|
+
|
|
79
|
+
return Number.isInteger(min) &&
|
|
80
|
+
Number.isInteger(max) &&
|
|
81
|
+
Number.isInteger(step) &&
|
|
82
|
+
min <= max &&
|
|
83
|
+
step > 0
|
|
84
|
+
? { min, max, step }
|
|
85
|
+
: null;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function normalizeNumber(value: number, bounds: NumberBounds): number {
|
|
89
|
+
const bounded = Math.min(bounds.max, Math.max(bounds.min, value));
|
|
90
|
+
const stepped =
|
|
91
|
+
bounds.min + Math.round((bounded - bounds.min) / bounds.step) * bounds.step;
|
|
92
|
+
|
|
93
|
+
return Math.min(bounds.max, Math.max(bounds.min, stepped));
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function setComboboxValues(
|
|
97
|
+
root: HTMLElement,
|
|
98
|
+
requestedValues: readonly string[],
|
|
99
|
+
{ emit = false }: SetComboboxValueOptions = {},
|
|
100
|
+
): void {
|
|
101
|
+
if (root.dataset.comboboxMode === 'number') return;
|
|
102
|
+
|
|
103
|
+
const options = getOptions(root);
|
|
104
|
+
const selectionMode =
|
|
105
|
+
root.dataset.comboboxSelectionMode === 'multiple' ? 'multiple' : 'single';
|
|
106
|
+
const validValues = new Set(
|
|
107
|
+
options
|
|
108
|
+
.filter((option) => !option.disabled)
|
|
109
|
+
.map((option) => option.dataset.comboboxOption)
|
|
110
|
+
.filter((value): value is string => value !== undefined),
|
|
111
|
+
);
|
|
112
|
+
const selectedValues = [
|
|
113
|
+
...new Set(requestedValues.filter((value) => validValues.has(value))),
|
|
114
|
+
];
|
|
115
|
+
if (selectionMode === 'single') selectedValues.splice(1);
|
|
116
|
+
|
|
117
|
+
const selectedSet = new Set(selectedValues);
|
|
118
|
+
for (const option of options) {
|
|
119
|
+
option.setAttribute(
|
|
120
|
+
'aria-selected',
|
|
121
|
+
String(selectedSet.has(option.dataset.comboboxOption ?? '')),
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
syncSelectedTags(root, selectedValues);
|
|
125
|
+
|
|
126
|
+
if (emit) {
|
|
127
|
+
root.dispatchEvent(
|
|
128
|
+
new CustomEvent<ComboboxChangeDetail>('combobox:change', {
|
|
129
|
+
bubbles: true,
|
|
130
|
+
detail: { mode: 'options', values: selectedValues },
|
|
131
|
+
}),
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export function clearCombobox(
|
|
137
|
+
root: HTMLElement,
|
|
138
|
+
options: SetComboboxValueOptions = {},
|
|
139
|
+
): void {
|
|
140
|
+
if (root.dataset.comboboxMode === 'number') return;
|
|
141
|
+
|
|
142
|
+
setComboboxValues(root, [], options);
|
|
143
|
+
root.dispatchEvent(new Event('combobox:clear'));
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export function setComboboxNumberValue(
|
|
147
|
+
root: HTMLElement,
|
|
148
|
+
requestedValue: number,
|
|
149
|
+
{ emit = false }: SetComboboxValueOptions = {},
|
|
150
|
+
): void {
|
|
151
|
+
if (root.dataset.comboboxMode !== 'number') return;
|
|
152
|
+
|
|
153
|
+
const bounds = getNumberBounds(root);
|
|
154
|
+
const input = root.querySelector<HTMLInputElement>(
|
|
155
|
+
'[data-combobox-number-input]',
|
|
156
|
+
);
|
|
157
|
+
if (!bounds || !input || !Number.isFinite(requestedValue)) return;
|
|
158
|
+
|
|
159
|
+
const value = normalizeNumber(Math.round(requestedValue), bounds);
|
|
160
|
+
const displayValue = String(value);
|
|
161
|
+
if (input.value !== displayValue) input.value = displayValue;
|
|
162
|
+
|
|
163
|
+
for (const option of getOptions(root)) {
|
|
164
|
+
option.setAttribute(
|
|
165
|
+
'aria-selected',
|
|
166
|
+
String(option.dataset.comboboxOption === displayValue),
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const decrement = root.querySelector<HTMLButtonElement>(
|
|
171
|
+
'[data-combobox-number-step="decrement"]',
|
|
172
|
+
);
|
|
173
|
+
const increment = root.querySelector<HTMLButtonElement>(
|
|
174
|
+
'[data-combobox-number-step="increment"]',
|
|
175
|
+
);
|
|
176
|
+
if (decrement) decrement.disabled = value <= bounds.min;
|
|
177
|
+
if (increment) increment.disabled = value >= bounds.max;
|
|
178
|
+
|
|
179
|
+
if (emit) {
|
|
180
|
+
root.dispatchEvent(
|
|
181
|
+
new CustomEvent<ComboboxChangeDetail>('combobox:change', {
|
|
182
|
+
bubbles: true,
|
|
183
|
+
detail: { mode: 'number', value },
|
|
184
|
+
}),
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export function initCombobox(root: HTMLElement): () => void {
|
|
190
|
+
if (root.getAttribute(initializedAttribute) === 'true') return noop;
|
|
191
|
+
|
|
192
|
+
const mode = root.dataset.comboboxMode === 'number' ? 'number' : 'options';
|
|
193
|
+
const control = root.querySelector<HTMLElement>('[data-combobox-control]');
|
|
194
|
+
const trigger = root.querySelector<HTMLButtonElement>(
|
|
195
|
+
'[data-combobox-trigger]',
|
|
196
|
+
);
|
|
197
|
+
const numberInput = root.querySelector<HTMLInputElement>(
|
|
198
|
+
'[data-combobox-number-input]',
|
|
199
|
+
);
|
|
200
|
+
const panel = root.querySelector<HTMLElement>('[data-combobox-panel]');
|
|
201
|
+
const search = root.querySelector<HTMLInputElement>('[data-combobox-search]');
|
|
202
|
+
const optionsRoot = root.querySelector<HTMLElement>(
|
|
203
|
+
'[data-combobox-options]',
|
|
204
|
+
);
|
|
205
|
+
const empty = root.querySelector<HTMLElement>('[data-combobox-empty]');
|
|
206
|
+
const owner = mode === 'number' ? numberInput : trigger;
|
|
207
|
+
|
|
208
|
+
if (
|
|
209
|
+
!control ||
|
|
210
|
+
!owner ||
|
|
211
|
+
!panel ||
|
|
212
|
+
!optionsRoot ||
|
|
213
|
+
(mode === 'options' && (!trigger || !search || !empty)) ||
|
|
214
|
+
(mode === 'number' && (!numberInput || !getNumberBounds(root)))
|
|
215
|
+
) {
|
|
216
|
+
return noop;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
root.setAttribute(initializedAttribute, 'true');
|
|
220
|
+
const clearable = root.dataset.comboboxClearable === 'true';
|
|
221
|
+
const closeOnSelect = root.dataset.comboboxCloseOnSelect === 'true';
|
|
222
|
+
const selectionMode =
|
|
223
|
+
root.dataset.comboboxSelectionMode === 'multiple' ? 'multiple' : 'single';
|
|
224
|
+
let activeOption: HTMLButtonElement | null = null;
|
|
225
|
+
let positionFrame: number | null = null;
|
|
226
|
+
let viewportListenersAttached = false;
|
|
227
|
+
|
|
228
|
+
const isOpen = (): boolean => root.hasAttribute('data-combobox-open');
|
|
229
|
+
|
|
230
|
+
const getVisibleOptions = (): HTMLButtonElement[] =>
|
|
231
|
+
getOptions(root).filter((option) => !option.hidden && !option.disabled);
|
|
232
|
+
|
|
233
|
+
const toPixels = (value: string): number => Number.parseFloat(value) || 0;
|
|
234
|
+
|
|
235
|
+
const getPanelContentBlockSize = (): number => {
|
|
236
|
+
const panelStyle = window.getComputedStyle(panel);
|
|
237
|
+
const searchRegion = search?.closest<HTMLElement>('.combobox__search');
|
|
238
|
+
|
|
239
|
+
return (
|
|
240
|
+
toPixels(panelStyle.borderBlockStartWidth) +
|
|
241
|
+
toPixels(panelStyle.borderBlockEndWidth) +
|
|
242
|
+
(searchRegion?.offsetHeight ?? 0) +
|
|
243
|
+
optionsRoot.scrollHeight
|
|
244
|
+
);
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
const setActiveOption = (option: HTMLButtonElement | null): void => {
|
|
248
|
+
for (const candidate of getOptions(root)) {
|
|
249
|
+
candidate.toggleAttribute(
|
|
250
|
+
'data-interaction-accent-hover',
|
|
251
|
+
candidate === option,
|
|
252
|
+
);
|
|
253
|
+
}
|
|
254
|
+
activeOption = option;
|
|
255
|
+
|
|
256
|
+
for (const ariaOwner of [owner, search]) {
|
|
257
|
+
if (!ariaOwner) continue;
|
|
258
|
+
if (option) {
|
|
259
|
+
ariaOwner.setAttribute('aria-activedescendant', option.id);
|
|
260
|
+
} else {
|
|
261
|
+
ariaOwner.removeAttribute('aria-activedescendant');
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
const positionPanel = (): void => {
|
|
267
|
+
if (!isOpen()) return;
|
|
268
|
+
|
|
269
|
+
const viewportPadding = 8;
|
|
270
|
+
const visualViewport = window.visualViewport;
|
|
271
|
+
const viewportLeft = visualViewport?.offsetLeft ?? 0;
|
|
272
|
+
const viewportTop = visualViewport?.offsetTop ?? 0;
|
|
273
|
+
const viewportWidth = visualViewport?.width ?? window.innerWidth;
|
|
274
|
+
const viewportHeight = visualViewport?.height ?? window.innerHeight;
|
|
275
|
+
const viewportRight = viewportLeft + viewportWidth;
|
|
276
|
+
const viewportBottom = viewportTop + viewportHeight;
|
|
277
|
+
const controlRect = control.getBoundingClientRect();
|
|
278
|
+
const overlayBoundary = root.closest<HTMLElement>(
|
|
279
|
+
'[data-overlay-boundary]',
|
|
280
|
+
);
|
|
281
|
+
const upperBoundary = Math.max(
|
|
282
|
+
viewportTop + viewportPadding,
|
|
283
|
+
overlayBoundary?.getBoundingClientRect().top ??
|
|
284
|
+
viewportTop + viewportPadding,
|
|
285
|
+
);
|
|
286
|
+
const availableWidth = Math.max(0, viewportWidth - viewportPadding * 2);
|
|
287
|
+
const panelWidth = Math.min(controlRect.width, availableWidth);
|
|
288
|
+
const left = Math.min(
|
|
289
|
+
Math.max(viewportLeft + viewportPadding, controlRect.left),
|
|
290
|
+
Math.max(
|
|
291
|
+
viewportLeft + viewportPadding,
|
|
292
|
+
viewportRight - panelWidth - viewportPadding,
|
|
293
|
+
),
|
|
294
|
+
);
|
|
295
|
+
const availableBelow = Math.max(
|
|
296
|
+
0,
|
|
297
|
+
viewportBottom - controlRect.bottom - viewportPadding,
|
|
298
|
+
);
|
|
299
|
+
const availableAbove = Math.max(0, controlRect.top - upperBoundary);
|
|
300
|
+
const contentBlockSize = getPanelContentBlockSize();
|
|
301
|
+
const placement =
|
|
302
|
+
contentBlockSize <= availableBelow || availableBelow >= availableAbove
|
|
303
|
+
? 'below'
|
|
304
|
+
: 'above';
|
|
305
|
+
const availableBlockSize =
|
|
306
|
+
placement === 'above' ? availableAbove : availableBelow;
|
|
307
|
+
|
|
308
|
+
panel.style.setProperty(
|
|
309
|
+
'--combobox-panel-inline-start',
|
|
310
|
+
`${Math.round(left)}px`,
|
|
311
|
+
);
|
|
312
|
+
panel.style.setProperty(
|
|
313
|
+
'--combobox-panel-inline-size',
|
|
314
|
+
`${Math.round(panelWidth)}px`,
|
|
315
|
+
);
|
|
316
|
+
panel.style.setProperty(
|
|
317
|
+
'--combobox-panel-available-block-size',
|
|
318
|
+
`${Math.floor(availableBlockSize)}px`,
|
|
319
|
+
);
|
|
320
|
+
root.dataset.comboboxPlacement = placement;
|
|
321
|
+
const top =
|
|
322
|
+
placement === 'above'
|
|
323
|
+
? Math.max(upperBoundary, controlRect.top - panel.offsetHeight)
|
|
324
|
+
: controlRect.bottom;
|
|
325
|
+
|
|
326
|
+
panel.style.setProperty(
|
|
327
|
+
'--combobox-panel-block-start',
|
|
328
|
+
`${Math.round(top)}px`,
|
|
329
|
+
);
|
|
330
|
+
};
|
|
331
|
+
|
|
332
|
+
const schedulePanelPosition = (): void => {
|
|
333
|
+
if (!isOpen() || positionFrame !== null) return;
|
|
334
|
+
|
|
335
|
+
positionFrame = window.requestAnimationFrame(() => {
|
|
336
|
+
positionFrame = null;
|
|
337
|
+
positionPanel();
|
|
338
|
+
});
|
|
339
|
+
};
|
|
340
|
+
const controlResizeObserver =
|
|
341
|
+
'ResizeObserver' in window
|
|
342
|
+
? new ResizeObserver(schedulePanelPosition)
|
|
343
|
+
: null;
|
|
344
|
+
|
|
345
|
+
const resetFilter = (): void => {
|
|
346
|
+
if (search) search.value = '';
|
|
347
|
+
for (const option of getOptions(root)) option.hidden = false;
|
|
348
|
+
if (empty) empty.hidden = getOptions(root).length > 0;
|
|
349
|
+
};
|
|
350
|
+
|
|
351
|
+
const handleViewportChange = (): void => {
|
|
352
|
+
schedulePanelPosition();
|
|
353
|
+
};
|
|
354
|
+
|
|
355
|
+
const handleViewportScroll = (event: Event): void => {
|
|
356
|
+
const target = event.target;
|
|
357
|
+
if (target instanceof Node && panel.contains(target)) return;
|
|
358
|
+
|
|
359
|
+
if (window.matchMedia(compactViewportQuery).matches) {
|
|
360
|
+
close();
|
|
361
|
+
blurFocusedDescendant();
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
schedulePanelPosition();
|
|
366
|
+
};
|
|
367
|
+
|
|
368
|
+
const attachViewportListeners = (): void => {
|
|
369
|
+
if (viewportListenersAttached) return;
|
|
370
|
+
|
|
371
|
+
viewportListenersAttached = true;
|
|
372
|
+
document.addEventListener('click', handleDocumentClick);
|
|
373
|
+
document.addEventListener('focusin', handleDocumentFocusin);
|
|
374
|
+
window.addEventListener('resize', handleViewportChange, { passive: true });
|
|
375
|
+
window.addEventListener('scroll', handleViewportScroll, {
|
|
376
|
+
capture: true,
|
|
377
|
+
passive: true,
|
|
378
|
+
});
|
|
379
|
+
window.visualViewport?.addEventListener('resize', handleViewportChange, {
|
|
380
|
+
passive: true,
|
|
381
|
+
});
|
|
382
|
+
window.visualViewport?.addEventListener('scroll', handleViewportChange, {
|
|
383
|
+
passive: true,
|
|
384
|
+
});
|
|
385
|
+
controlResizeObserver?.observe(control);
|
|
386
|
+
};
|
|
387
|
+
|
|
388
|
+
const detachViewportListeners = (): void => {
|
|
389
|
+
if (!viewportListenersAttached) return;
|
|
390
|
+
|
|
391
|
+
viewportListenersAttached = false;
|
|
392
|
+
document.removeEventListener('click', handleDocumentClick);
|
|
393
|
+
document.removeEventListener('focusin', handleDocumentFocusin);
|
|
394
|
+
window.removeEventListener('resize', handleViewportChange);
|
|
395
|
+
window.removeEventListener('scroll', handleViewportScroll, true);
|
|
396
|
+
window.visualViewport?.removeEventListener('resize', handleViewportChange);
|
|
397
|
+
window.visualViewport?.removeEventListener('scroll', handleViewportChange);
|
|
398
|
+
controlResizeObserver?.unobserve(control);
|
|
399
|
+
};
|
|
400
|
+
|
|
401
|
+
const close = (restoreFocus = false): void => {
|
|
402
|
+
if (!isOpen()) return;
|
|
403
|
+
|
|
404
|
+
detachViewportListeners();
|
|
405
|
+
if (positionFrame !== null) {
|
|
406
|
+
window.cancelAnimationFrame(positionFrame);
|
|
407
|
+
positionFrame = null;
|
|
408
|
+
}
|
|
409
|
+
owner.setAttribute('aria-expanded', 'false');
|
|
410
|
+
root.removeAttribute('data-combobox-open');
|
|
411
|
+
root.removeAttribute('data-combobox-placement');
|
|
412
|
+
setActiveOption(null);
|
|
413
|
+
resetFilter();
|
|
414
|
+
if (panel.matches(':popover-open')) panel.hidePopover();
|
|
415
|
+
panel.hidden = true;
|
|
416
|
+
|
|
417
|
+
if (restoreFocus) owner.focus();
|
|
418
|
+
};
|
|
419
|
+
|
|
420
|
+
const blurFocusedDescendant = (): void => {
|
|
421
|
+
const focused = document.activeElement;
|
|
422
|
+
if (focused instanceof HTMLElement && root.contains(focused)) {
|
|
423
|
+
focused.blur();
|
|
424
|
+
}
|
|
425
|
+
};
|
|
426
|
+
|
|
427
|
+
const open = (focusSearch = true): void => {
|
|
428
|
+
if (isOpen() || owner.matches(':disabled')) return;
|
|
429
|
+
|
|
430
|
+
panel.hidden = false;
|
|
431
|
+
if (!panel.matches(':popover-open')) panel.showPopover();
|
|
432
|
+
owner.setAttribute('aria-expanded', 'true');
|
|
433
|
+
root.setAttribute('data-combobox-open', '');
|
|
434
|
+
attachViewportListeners();
|
|
435
|
+
schedulePanelPosition();
|
|
436
|
+
|
|
437
|
+
const selectedOption =
|
|
438
|
+
getVisibleOptions().find(
|
|
439
|
+
(option) => option.getAttribute('aria-selected') === 'true',
|
|
440
|
+
) ?? null;
|
|
441
|
+
setActiveOption(null);
|
|
442
|
+
selectedOption?.scrollIntoView({ block: 'nearest' });
|
|
443
|
+
|
|
444
|
+
if (focusSearch && search) {
|
|
445
|
+
window.requestAnimationFrame(() => {
|
|
446
|
+
if (isOpen()) search.focus();
|
|
447
|
+
});
|
|
448
|
+
}
|
|
449
|
+
};
|
|
450
|
+
|
|
451
|
+
const moveActiveOption = (direction: 1 | -1): void => {
|
|
452
|
+
const visibleOptions = getVisibleOptions();
|
|
453
|
+
if (visibleOptions.length === 0) {
|
|
454
|
+
setActiveOption(null);
|
|
455
|
+
return;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
const currentIndex = activeOption
|
|
459
|
+
? visibleOptions.indexOf(activeOption)
|
|
460
|
+
: -1;
|
|
461
|
+
const nextIndex =
|
|
462
|
+
currentIndex === -1
|
|
463
|
+
? direction === 1
|
|
464
|
+
? 0
|
|
465
|
+
: visibleOptions.length - 1
|
|
466
|
+
: (currentIndex + direction + visibleOptions.length) %
|
|
467
|
+
visibleOptions.length;
|
|
468
|
+
const nextOption = visibleOptions[nextIndex] ?? null;
|
|
469
|
+
setActiveOption(nextOption);
|
|
470
|
+
nextOption?.scrollIntoView({ block: 'nearest' });
|
|
471
|
+
};
|
|
472
|
+
|
|
473
|
+
const selectOption = (
|
|
474
|
+
option: HTMLButtonElement,
|
|
475
|
+
restoreFocus = true,
|
|
476
|
+
): void => {
|
|
477
|
+
const value = option.dataset.comboboxOption;
|
|
478
|
+
if (!value || option.disabled) return;
|
|
479
|
+
|
|
480
|
+
if (mode === 'number') {
|
|
481
|
+
setComboboxNumberValue(root, Number(value), { emit: true });
|
|
482
|
+
close(restoreFocus);
|
|
483
|
+
return;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
const selectedValues = getComboboxValues(root);
|
|
487
|
+
const isSelected = selectedValues.includes(value);
|
|
488
|
+
const nextValues = isSelected
|
|
489
|
+
? clearable
|
|
490
|
+
? selectedValues.filter((selectedValue) => selectedValue !== value)
|
|
491
|
+
: selectedValues
|
|
492
|
+
: selectionMode === 'multiple'
|
|
493
|
+
? [...selectedValues, value]
|
|
494
|
+
: [value];
|
|
495
|
+
|
|
496
|
+
if (!isSelected || clearable) {
|
|
497
|
+
setComboboxValues(root, nextValues, { emit: true });
|
|
498
|
+
}
|
|
499
|
+
if (closeOnSelect) close(restoreFocus);
|
|
500
|
+
};
|
|
501
|
+
|
|
502
|
+
const removeTag = (button: HTMLElement): void => {
|
|
503
|
+
if (!clearable) return;
|
|
504
|
+
const tag = button.closest<HTMLElement>('[data-combobox-tag]');
|
|
505
|
+
const value = tag?.dataset.comboboxTag;
|
|
506
|
+
if (!value) return;
|
|
507
|
+
|
|
508
|
+
setComboboxValues(
|
|
509
|
+
root,
|
|
510
|
+
getComboboxValues(root).filter(
|
|
511
|
+
(selectedValue) => selectedValue !== value,
|
|
512
|
+
),
|
|
513
|
+
{ emit: true },
|
|
514
|
+
);
|
|
515
|
+
if (isOpen() && search) {
|
|
516
|
+
search.focus();
|
|
517
|
+
} else {
|
|
518
|
+
owner.focus();
|
|
519
|
+
}
|
|
520
|
+
};
|
|
521
|
+
|
|
522
|
+
const filterOptions = (): void => {
|
|
523
|
+
if (!search || !empty) return;
|
|
524
|
+
|
|
525
|
+
const query = normalizeSearchValue(search.value.trim());
|
|
526
|
+
let visibleCount = 0;
|
|
527
|
+
|
|
528
|
+
for (const option of getOptions(root)) {
|
|
529
|
+
const searchText = normalizeSearchValue(
|
|
530
|
+
option.dataset.comboboxSearchText ?? '',
|
|
531
|
+
);
|
|
532
|
+
option.hidden = query.length > 0 && !searchText.includes(query);
|
|
533
|
+
if (!option.hidden) visibleCount += 1;
|
|
534
|
+
}
|
|
535
|
+
empty.hidden = visibleCount > 0;
|
|
536
|
+
setActiveOption(null);
|
|
537
|
+
schedulePanelPosition();
|
|
538
|
+
};
|
|
539
|
+
|
|
540
|
+
const handleTagRemoval = (event: Event): boolean => {
|
|
541
|
+
const target = event.target;
|
|
542
|
+
if (!(target instanceof Element)) return false;
|
|
543
|
+
|
|
544
|
+
const removeButton = target.closest<HTMLElement>('[data-tag-remove]');
|
|
545
|
+
if (removeButton && control.contains(removeButton)) {
|
|
546
|
+
event.preventDefault();
|
|
547
|
+
event.stopPropagation();
|
|
548
|
+
removeTag(removeButton);
|
|
549
|
+
return true;
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
return false;
|
|
553
|
+
};
|
|
554
|
+
|
|
555
|
+
const toggleControl = (restoreFocus = true): void => {
|
|
556
|
+
if (isOpen()) {
|
|
557
|
+
close(restoreFocus);
|
|
558
|
+
} else {
|
|
559
|
+
open();
|
|
560
|
+
}
|
|
561
|
+
};
|
|
562
|
+
|
|
563
|
+
const handleOptionsTriggerClick = (): void => {
|
|
564
|
+
toggleControl(false);
|
|
565
|
+
};
|
|
566
|
+
|
|
567
|
+
const handleOptionsControlClick = (event: MouseEvent): void => {
|
|
568
|
+
handleTagRemoval(event);
|
|
569
|
+
};
|
|
570
|
+
|
|
571
|
+
const handleOwnerKeydown = (event: KeyboardEvent): void => {
|
|
572
|
+
if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
|
|
573
|
+
event.preventDefault();
|
|
574
|
+
if (!isOpen()) {
|
|
575
|
+
open(false);
|
|
576
|
+
if (event.key === 'ArrowUp') {
|
|
577
|
+
const visibleOptions = getVisibleOptions();
|
|
578
|
+
setActiveOption(visibleOptions.at(-1) ?? null);
|
|
579
|
+
}
|
|
580
|
+
} else {
|
|
581
|
+
moveActiveOption(event.key === 'ArrowDown' ? 1 : -1);
|
|
582
|
+
}
|
|
583
|
+
} else if (event.key === 'Home' || event.key === 'End') {
|
|
584
|
+
event.preventDefault();
|
|
585
|
+
open(false);
|
|
586
|
+
const visibleOptions = getVisibleOptions();
|
|
587
|
+
setActiveOption(
|
|
588
|
+
event.key === 'Home'
|
|
589
|
+
? (visibleOptions[0] ?? null)
|
|
590
|
+
: (visibleOptions.at(-1) ?? null),
|
|
591
|
+
);
|
|
592
|
+
} else if (event.key === 'Enter') {
|
|
593
|
+
event.preventDefault();
|
|
594
|
+
if (mode === 'number' && numberInput) {
|
|
595
|
+
commitNumberInput();
|
|
596
|
+
close(true);
|
|
597
|
+
} else if (!isOpen()) {
|
|
598
|
+
open();
|
|
599
|
+
} else if (activeOption) {
|
|
600
|
+
selectOption(activeOption);
|
|
601
|
+
}
|
|
602
|
+
} else if (event.key === ' ' && mode === 'options') {
|
|
603
|
+
event.preventDefault();
|
|
604
|
+
if (!isOpen()) open();
|
|
605
|
+
else if (activeOption) selectOption(activeOption);
|
|
606
|
+
} else if (event.key === 'Escape') {
|
|
607
|
+
event.preventDefault();
|
|
608
|
+
if (mode === 'number' && numberInput) {
|
|
609
|
+
const selected = Number(getComboboxValues(root)[0]);
|
|
610
|
+
if (Number.isFinite(selected)) numberInput.value = String(selected);
|
|
611
|
+
}
|
|
612
|
+
close(true);
|
|
613
|
+
}
|
|
614
|
+
};
|
|
615
|
+
|
|
616
|
+
const handleSearchKeydown = (event: KeyboardEvent): void => {
|
|
617
|
+
if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
|
|
618
|
+
event.preventDefault();
|
|
619
|
+
moveActiveOption(event.key === 'ArrowDown' ? 1 : -1);
|
|
620
|
+
} else if (event.key === 'Home' || event.key === 'End') {
|
|
621
|
+
event.preventDefault();
|
|
622
|
+
const visibleOptions = getVisibleOptions();
|
|
623
|
+
setActiveOption(
|
|
624
|
+
event.key === 'Home'
|
|
625
|
+
? (visibleOptions[0] ?? null)
|
|
626
|
+
: (visibleOptions.at(-1) ?? null),
|
|
627
|
+
);
|
|
628
|
+
} else if (event.key === 'Enter') {
|
|
629
|
+
event.preventDefault();
|
|
630
|
+
if (activeOption) selectOption(activeOption);
|
|
631
|
+
} else if (event.key === 'Escape') {
|
|
632
|
+
event.preventDefault();
|
|
633
|
+
close(true);
|
|
634
|
+
} else if (event.key === 'Tab') {
|
|
635
|
+
close();
|
|
636
|
+
}
|
|
637
|
+
};
|
|
638
|
+
|
|
639
|
+
const handleOptionClick = (event: MouseEvent): void => {
|
|
640
|
+
const target = event.target;
|
|
641
|
+
if (!(target instanceof Element)) return;
|
|
642
|
+
const option = target.closest<HTMLButtonElement>('[data-combobox-option]');
|
|
643
|
+
if (option && optionsRoot.contains(option)) {
|
|
644
|
+
const closesPanel = mode === 'number' || closeOnSelect;
|
|
645
|
+
selectOption(option, false);
|
|
646
|
+
if (closesPanel) blurFocusedDescendant();
|
|
647
|
+
if (mode === 'options' && !closeOnSelect) search?.focus();
|
|
648
|
+
}
|
|
649
|
+
};
|
|
650
|
+
|
|
651
|
+
const commitNumberInput = (emit = true): void => {
|
|
652
|
+
if (!numberInput) return;
|
|
653
|
+
const selected = Number(getComboboxValues(root)[0]);
|
|
654
|
+
const requested = Number(numberInput.value);
|
|
655
|
+
|
|
656
|
+
if (!Number.isInteger(requested)) {
|
|
657
|
+
if (Number.isFinite(selected)) numberInput.value = String(selected);
|
|
658
|
+
return;
|
|
659
|
+
}
|
|
660
|
+
setComboboxNumberValue(root, requested, {
|
|
661
|
+
emit: emit && requested !== selected,
|
|
662
|
+
});
|
|
663
|
+
};
|
|
664
|
+
|
|
665
|
+
const handleNumberInputClick = (): void => {
|
|
666
|
+
if (!numberInput) return;
|
|
667
|
+
|
|
668
|
+
if (isOpen()) {
|
|
669
|
+
close();
|
|
670
|
+
} else {
|
|
671
|
+
open(false);
|
|
672
|
+
numberInput.select();
|
|
673
|
+
}
|
|
674
|
+
};
|
|
675
|
+
|
|
676
|
+
const handleNumberInput = (): void => {
|
|
677
|
+
if (!numberInput) return;
|
|
678
|
+
const bounds = getNumberBounds(root);
|
|
679
|
+
const allowNegative = Boolean(bounds && bounds.min < 0);
|
|
680
|
+
const sign = allowNegative && numberInput.value.startsWith('-') ? '-' : '';
|
|
681
|
+
const digits = numberInput.value.replace(/\D/g, '');
|
|
682
|
+
numberInput.value = `${sign}${digits}`;
|
|
683
|
+
const matchingOption = getOptions(root).find(
|
|
684
|
+
(option) => option.dataset.comboboxOption === numberInput.value,
|
|
685
|
+
);
|
|
686
|
+
if (matchingOption) {
|
|
687
|
+
const selected = Number(getComboboxValues(root)[0]);
|
|
688
|
+
const requested = Number(matchingOption.dataset.comboboxOption);
|
|
689
|
+
setComboboxNumberValue(root, requested, {
|
|
690
|
+
emit: requested !== selected,
|
|
691
|
+
});
|
|
692
|
+
}
|
|
693
|
+
setActiveOption(matchingOption ?? null);
|
|
694
|
+
matchingOption?.scrollIntoView({ block: 'nearest' });
|
|
695
|
+
};
|
|
696
|
+
|
|
697
|
+
const handleNumberInputChange = (): void => {
|
|
698
|
+
commitNumberInput();
|
|
699
|
+
};
|
|
700
|
+
|
|
701
|
+
const handleNumberStep = (event: MouseEvent): void => {
|
|
702
|
+
const target = event.currentTarget;
|
|
703
|
+
if (!(target instanceof HTMLButtonElement) || !numberInput) return;
|
|
704
|
+
const bounds = getNumberBounds(root);
|
|
705
|
+
if (!bounds) return;
|
|
706
|
+
|
|
707
|
+
commitNumberInput(false);
|
|
708
|
+
const current = Number(numberInput.value);
|
|
709
|
+
const direction =
|
|
710
|
+
target.dataset.comboboxNumberStep === 'decrement' ? -1 : 1;
|
|
711
|
+
setComboboxNumberValue(root, current + direction * bounds.step, {
|
|
712
|
+
emit: true,
|
|
713
|
+
});
|
|
714
|
+
if (isOpen()) {
|
|
715
|
+
const selectedOption =
|
|
716
|
+
getOptions(root).find(
|
|
717
|
+
(option) => option.getAttribute('aria-selected') === 'true',
|
|
718
|
+
) ?? null;
|
|
719
|
+
setActiveOption(selectedOption);
|
|
720
|
+
selectedOption?.scrollIntoView({ block: 'nearest' });
|
|
721
|
+
}
|
|
722
|
+
};
|
|
723
|
+
|
|
724
|
+
const handleDocumentClick = (event: MouseEvent): void => {
|
|
725
|
+
const target = event.target;
|
|
726
|
+
if (target instanceof Node && !root.contains(target)) close();
|
|
727
|
+
};
|
|
728
|
+
|
|
729
|
+
const handleDocumentFocusin = (event: FocusEvent): void => {
|
|
730
|
+
const target = event.target;
|
|
731
|
+
if (target instanceof Node && !root.contains(target)) close();
|
|
732
|
+
};
|
|
733
|
+
|
|
734
|
+
const handleClear = (): void => {
|
|
735
|
+
resetFilter();
|
|
736
|
+
close();
|
|
737
|
+
};
|
|
738
|
+
|
|
739
|
+
if (mode === 'options' && trigger && search) {
|
|
740
|
+
control.addEventListener('click', handleOptionsControlClick);
|
|
741
|
+
trigger.addEventListener('click', handleOptionsTriggerClick);
|
|
742
|
+
trigger.addEventListener('keydown', handleOwnerKeydown);
|
|
743
|
+
search.addEventListener('input', filterOptions);
|
|
744
|
+
search.addEventListener('keydown', handleSearchKeydown);
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
if (mode === 'number' && numberInput) {
|
|
748
|
+
numberInput.addEventListener('click', handleNumberInputClick);
|
|
749
|
+
numberInput.addEventListener('input', handleNumberInput);
|
|
750
|
+
numberInput.addEventListener('change', handleNumberInputChange);
|
|
751
|
+
numberInput.addEventListener('keydown', handleOwnerKeydown);
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
const stepButtons = Array.from(
|
|
755
|
+
root.querySelectorAll<HTMLButtonElement>('[data-combobox-number-step]'),
|
|
756
|
+
);
|
|
757
|
+
for (const button of stepButtons) {
|
|
758
|
+
button.addEventListener('click', handleNumberStep);
|
|
759
|
+
}
|
|
760
|
+
optionsRoot.addEventListener('click', handleOptionClick);
|
|
761
|
+
root.addEventListener('combobox:clear', handleClear);
|
|
762
|
+
|
|
763
|
+
return () => {
|
|
764
|
+
close();
|
|
765
|
+
detachViewportListeners();
|
|
766
|
+
controlResizeObserver?.disconnect();
|
|
767
|
+
control.removeEventListener('click', handleOptionsControlClick);
|
|
768
|
+
trigger?.removeEventListener('click', handleOptionsTriggerClick);
|
|
769
|
+
trigger?.removeEventListener('keydown', handleOwnerKeydown);
|
|
770
|
+
search?.removeEventListener('input', filterOptions);
|
|
771
|
+
search?.removeEventListener('keydown', handleSearchKeydown);
|
|
772
|
+
numberInput?.removeEventListener('click', handleNumberInputClick);
|
|
773
|
+
numberInput?.removeEventListener('input', handleNumberInput);
|
|
774
|
+
numberInput?.removeEventListener('change', handleNumberInputChange);
|
|
775
|
+
numberInput?.removeEventListener('keydown', handleOwnerKeydown);
|
|
776
|
+
for (const button of stepButtons) {
|
|
777
|
+
button.removeEventListener('click', handleNumberStep);
|
|
778
|
+
}
|
|
779
|
+
optionsRoot.removeEventListener('click', handleOptionClick);
|
|
780
|
+
root.removeEventListener('combobox:clear', handleClear);
|
|
781
|
+
panel.style.removeProperty('--combobox-panel-inline-start');
|
|
782
|
+
panel.style.removeProperty('--combobox-panel-block-start');
|
|
783
|
+
panel.style.removeProperty('--combobox-panel-inline-size');
|
|
784
|
+
panel.style.removeProperty('--combobox-panel-available-block-size');
|
|
785
|
+
root.removeAttribute('data-combobox-placement');
|
|
786
|
+
root.removeAttribute(initializedAttribute);
|
|
787
|
+
};
|
|
788
|
+
}
|