@humanonlyweb/ui 0.0.0-stage → 0.2.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.
Files changed (168) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +45 -2
  3. package/dist/components/accordion/Accordion.js +60 -0
  4. package/dist/components/accordion/Accordion.vue.d.ts +28 -0
  5. package/dist/components/accordion/AccordionItem.js +102 -0
  6. package/dist/components/accordion/AccordionItem.vue.d.ts +33 -0
  7. package/dist/components/avatar/Avatar.js +52 -0
  8. package/dist/components/avatar/Avatar.vue.d.ts +26 -0
  9. package/dist/components/avatar/AvatarGroup.js +48 -0
  10. package/dist/components/avatar/AvatarGroup.vue.d.ts +16 -0
  11. package/dist/components/breadcrumbs/Breadcrumbs.js +59 -0
  12. package/dist/components/breadcrumbs/Breadcrumbs.vue.d.ts +14 -0
  13. package/dist/components/button/Button.js +95 -0
  14. package/dist/components/button/Button.vue.d.ts +74 -0
  15. package/dist/components/button/IconButton.js +47 -0
  16. package/dist/components/button/IconButton.vue.d.ts +115 -0
  17. package/dist/components/checkbox/Checkbox.js +93 -0
  18. package/dist/components/checkbox/Checkbox.vue.d.ts +43 -0
  19. package/dist/components/checkbox-group/CheckboxGroup.js +89 -0
  20. package/dist/components/checkbox-group/CheckboxGroup.vue.d.ts +43 -0
  21. package/dist/components/combobox/Combobox.js +294 -0
  22. package/dist/components/combobox/Combobox.vue.d.ts +80 -0
  23. package/dist/components/context-menu/ContextMenu.js +170 -0
  24. package/dist/components/context-menu/ContextMenu.vue.d.ts +38 -0
  25. package/dist/components/dialog/ConfirmDialog.js +103 -0
  26. package/dist/components/dialog/ConfirmDialog.vue.d.ts +46 -0
  27. package/dist/components/dialog/Dialog.js +160 -0
  28. package/dist/components/dialog/Dialog.vue.d.ts +54 -0
  29. package/dist/components/field/FieldMessage.js +31 -0
  30. package/dist/components/fieldset/Fieldset.js +59 -0
  31. package/dist/components/fieldset/Fieldset.vue.d.ts +29 -0
  32. package/dist/components/input/Input.js +111 -0
  33. package/dist/components/input/Input.vue.d.ts +50 -0
  34. package/dist/components/kbd/Kbd.js +48 -0
  35. package/dist/components/kbd/Kbd.vue.d.ts +24 -0
  36. package/dist/components/menu/Menu.js +124 -0
  37. package/dist/components/menu/Menu.vue.d.ts +41 -0
  38. package/dist/components/otp-input/OtpInput.js +179 -0
  39. package/dist/components/otp-input/OtpInput.vue.d.ts +48 -0
  40. package/dist/components/pagination/Pagination.js +128 -0
  41. package/dist/components/pagination/Pagination.vue.d.ts +24 -0
  42. package/dist/components/password-input/PasswordInput.js +151 -0
  43. package/dist/components/password-input/PasswordInput.vue.d.ts +32 -0
  44. package/dist/components/popover/Popover.js +83 -0
  45. package/dist/components/popover/Popover.vue.d.ts +37 -0
  46. package/dist/components/progress/Progress.js +52 -0
  47. package/dist/components/progress/Progress.vue.d.ts +17 -0
  48. package/dist/components/radio-group/RadioGroup.js +125 -0
  49. package/dist/components/radio-group/RadioGroup.vue.d.ts +45 -0
  50. package/dist/components/segmented/Segmented.js +110 -0
  51. package/dist/components/segmented/Segmented.vue.d.ts +41 -0
  52. package/dist/components/select/Select.js +231 -0
  53. package/dist/components/select/Select.vue.d.ts +59 -0
  54. package/dist/components/slider/Slider.js +196 -0
  55. package/dist/components/slider/Slider.vue.d.ts +49 -0
  56. package/dist/components/spin-button/SpinButton.js +231 -0
  57. package/dist/components/spin-button/SpinButton.vue.d.ts +48 -0
  58. package/dist/components/split-button/SplitButton.js +69 -0
  59. package/dist/components/split-button/SplitButton.vue.d.ts +38 -0
  60. package/dist/components/switch/Switch.js +95 -0
  61. package/dist/components/switch/Switch.vue.d.ts +42 -0
  62. package/dist/components/table/Table.js +173 -0
  63. package/dist/components/table/Table.vue.d.ts +45 -0
  64. package/dist/components/tabs/Tabs.js +103 -0
  65. package/dist/components/tabs/Tabs.vue.d.ts +39 -0
  66. package/dist/components/tag-input/TagInput.js +186 -0
  67. package/dist/components/tag-input/TagInput.vue.d.ts +36 -0
  68. package/dist/components/textarea/Textarea.js +118 -0
  69. package/dist/components/textarea/Textarea.vue.d.ts +34 -0
  70. package/dist/components/toast/ToastItem.js +91 -0
  71. package/dist/components/toast/ToastItem.vue.d.ts +29 -0
  72. package/dist/components/toast/ToastViewport.js +56 -0
  73. package/dist/components/toast/ToastViewport.vue.d.ts +25 -0
  74. package/dist/components/toolbar/Toolbar.js +91 -0
  75. package/dist/components/toolbar/Toolbar.vue.d.ts +21 -0
  76. package/dist/components/tooltip/Tooltip.js +85 -0
  77. package/dist/components/tooltip/Tooltip.vue.d.ts +25 -0
  78. package/dist/components/visually-hidden/VisuallyHidden.js +16 -0
  79. package/dist/components/visually-hidden/VisuallyHidden.vue.d.ts +19 -0
  80. package/dist/composables/internal/env.js +5 -0
  81. package/dist/composables/internal/is-equal.js +14 -0
  82. package/dist/composables/internal/on-click-outside.js +18 -0
  83. package/dist/composables/internal/standard-schema.d.ts +38 -0
  84. package/dist/composables/internal/use-document-visibility.js +16 -0
  85. package/dist/composables/internal/use-event-listener.js +24 -0
  86. package/dist/composables/internal/use-interval-fn.js +21 -0
  87. package/dist/composables/internal/use-resize-observer.js +21 -0
  88. package/dist/composables/internal/use-timeout-fn.js +25 -0
  89. package/dist/composables/use-accordion.d.ts +9 -0
  90. package/dist/composables/use-accordion.js +10 -0
  91. package/dist/composables/use-combobox.d.ts +44 -0
  92. package/dist/composables/use-combobox.js +185 -0
  93. package/dist/composables/use-field.d.ts +17 -0
  94. package/dist/composables/use-field.js +27 -0
  95. package/dist/composables/use-form.d.ts +44 -0
  96. package/dist/composables/use-form.js +146 -0
  97. package/dist/composables/use-menu.d.ts +26 -0
  98. package/dist/composables/use-menu.js +117 -0
  99. package/dist/composables/use-platform.d.ts +17 -0
  100. package/dist/composables/use-platform.js +126 -0
  101. package/dist/composables/use-popover.d.ts +35 -0
  102. package/dist/composables/use-popover.js +49 -0
  103. package/dist/composables/use-roving-selection.d.ts +22 -0
  104. package/dist/composables/use-roving-selection.js +65 -0
  105. package/dist/composables/use-select.d.ts +31 -0
  106. package/dist/composables/use-select.js +169 -0
  107. package/dist/composables/use-slide-indicator.d.ts +24 -0
  108. package/dist/composables/use-slide-indicator.js +52 -0
  109. package/dist/composables/use-toast.d.ts +23 -0
  110. package/dist/composables/use-toast.js +51 -0
  111. package/dist/config.d.ts +40 -0
  112. package/dist/config.js +35 -0
  113. package/dist/docs/README.md +52 -0
  114. package/dist/docs/changelog.md +21 -0
  115. package/dist/docs/components/accordion.md +100 -0
  116. package/dist/docs/components/avatar-group.md +58 -0
  117. package/dist/docs/components/avatar.md +40 -0
  118. package/dist/docs/components/breadcrumbs.md +81 -0
  119. package/dist/docs/components/button.md +109 -0
  120. package/dist/docs/components/checkbox-group.md +118 -0
  121. package/dist/docs/components/checkbox.md +123 -0
  122. package/dist/docs/components/combobox.md +207 -0
  123. package/dist/docs/components/confirm-dialog.md +107 -0
  124. package/dist/docs/components/context-menu.md +98 -0
  125. package/dist/docs/components/dialog.md +127 -0
  126. package/dist/docs/components/fieldset.md +65 -0
  127. package/dist/docs/components/icon-button.md +47 -0
  128. package/dist/docs/components/input.md +89 -0
  129. package/dist/docs/components/kbd.md +41 -0
  130. package/dist/docs/components/menu.md +112 -0
  131. package/dist/docs/components/otp-input.md +92 -0
  132. package/dist/docs/components/pagination.md +62 -0
  133. package/dist/docs/components/password-input.md +55 -0
  134. package/dist/docs/components/popover.md +72 -0
  135. package/dist/docs/components/progress.md +56 -0
  136. package/dist/docs/components/radio-group.md +114 -0
  137. package/dist/docs/components/segmented.md +96 -0
  138. package/dist/docs/components/select.md +129 -0
  139. package/dist/docs/components/slider.md +105 -0
  140. package/dist/docs/components/spin-button.md +73 -0
  141. package/dist/docs/components/split-button.md +64 -0
  142. package/dist/docs/components/switch.md +71 -0
  143. package/dist/docs/components/table.md +241 -0
  144. package/dist/docs/components/tabs.md +106 -0
  145. package/dist/docs/components/tag-input.md +88 -0
  146. package/dist/docs/components/textarea.md +46 -0
  147. package/dist/docs/components/toast.md +135 -0
  148. package/dist/docs/components/toolbar.md +71 -0
  149. package/dist/docs/components/tooltip.md +57 -0
  150. package/dist/docs/components/visually-hidden.md +53 -0
  151. package/dist/docs/forms.md +182 -0
  152. package/dist/docs/index.md +11 -0
  153. package/dist/docs/installation.md +71 -0
  154. package/dist/docs/localisation.md +42 -0
  155. package/dist/docs/ssr.md +24 -0
  156. package/dist/docs/styling.md +82 -0
  157. package/dist/index.d.ts +60 -0
  158. package/dist/index.js +56 -0
  159. package/dist/nuxt/module.d.ts +15 -0
  160. package/dist/nuxt/module.js +80 -0
  161. package/dist/nuxt/runtime/plugin.d.ts +4 -0
  162. package/dist/nuxt/runtime/plugin.js +24 -0
  163. package/dist/package.js +4 -0
  164. package/dist/styles/core.css +310 -0
  165. package/dist/translations.d.ts +85 -0
  166. package/dist/translations.js +62 -0
  167. package/dist/types.d.ts +69 -0
  168. package/package.json +93 -4
@@ -0,0 +1,117 @@
1
+ import { usePopoverState } from "./use-popover.js";
2
+ import { useTimeoutFn } from "./internal/use-timeout-fn.js";
3
+ import { nextTick, ref, watch } from "vue";
4
+ //#region src/composables/use-menu.ts
5
+ const TYPEAHEAD_RESET = 500;
6
+ const isMenuSeparator = (entry) => Boolean(entry && "separator" in entry && entry.separator);
7
+ /** APG menu button: arrow/Home/End navigation, typeahead, focus moves onto items. */
8
+ function useMenu({ trigger, panel, open, itemCount, isDisabled, labelAt, onActivate }) {
9
+ const activeIndex = ref(-1);
10
+ let openEdge = "first";
11
+ function enabledFrom(start, dir) {
12
+ for (let i = start; i >= 0 && i < itemCount(); i += dir) if (!isDisabled(i)) return i;
13
+ return -1;
14
+ }
15
+ const firstEnabled = () => enabledFrom(0, 1);
16
+ const lastEnabled = () => enabledFrom(itemCount() - 1, -1);
17
+ function focusActive() {
18
+ nextTick(() => {
19
+ panel.value?.querySelector(`[data-menu-index="${activeIndex.value}"]`)?.focus();
20
+ });
21
+ }
22
+ usePopoverState({
23
+ panel,
24
+ open,
25
+ onShow: () => {
26
+ activeIndex.value = openEdge === "last" ? lastEnabled() : firstEnabled();
27
+ openEdge = "first";
28
+ focusActive();
29
+ },
30
+ onHide: () => {
31
+ activeIndex.value = -1;
32
+ }
33
+ });
34
+ function show(edge = "first") {
35
+ if (!itemCount()) return;
36
+ openEdge = edge;
37
+ open.value = true;
38
+ }
39
+ function close(refocus = true) {
40
+ open.value = false;
41
+ if (refocus) trigger()?.focus();
42
+ }
43
+ function setActive(index) {
44
+ if (index < 0) return;
45
+ activeIndex.value = index;
46
+ focusActive();
47
+ }
48
+ function move(dir) {
49
+ const next = enabledFrom(activeIndex.value < 0 ? dir === 1 ? -1 : itemCount() : activeIndex.value + dir, dir);
50
+ setActive(next >= 0 ? next : dir === 1 ? firstEnabled() : lastEnabled());
51
+ }
52
+ function activate(index) {
53
+ if (index < 0 || isDisabled(index)) return;
54
+ onActivate(index);
55
+ close();
56
+ }
57
+ function onTriggerKeydown(event) {
58
+ if (event.key !== "ArrowDown" && event.key !== "ArrowUp") return;
59
+ event.preventDefault();
60
+ const up = event.key === "ArrowUp";
61
+ if (open.value) move(up ? -1 : 1);
62
+ else show(up ? "last" : "first");
63
+ }
64
+ let typeahead = "";
65
+ const resetTypeahead = useTimeoutFn(() => typeahead = "", TYPEAHEAD_RESET);
66
+ function onType(char) {
67
+ typeahead += char.toLowerCase();
68
+ resetTypeahead.start();
69
+ const count = itemCount();
70
+ const single = typeahead.length > 1 && typeahead.replaceAll(typeahead[0], "") === "";
71
+ const needle = single ? typeahead[0] : typeahead;
72
+ const start = single ? activeIndex.value : activeIndex.value - 1;
73
+ for (let step = 1; step <= count; step++) {
74
+ const i = ((start + step) % count + count) % count;
75
+ if (!isDisabled(i) && labelAt(i)?.toLowerCase().startsWith(needle)) {
76
+ setActive(i);
77
+ return;
78
+ }
79
+ }
80
+ }
81
+ function onMenuKeydown(event) {
82
+ const actions = {
83
+ ArrowDown: () => move(1),
84
+ ArrowUp: () => move(-1),
85
+ Home: () => setActive(firstEnabled()),
86
+ End: () => setActive(lastEnabled()),
87
+ Escape: () => close()
88
+ };
89
+ if (event.key === "Tab") {
90
+ close(false);
91
+ return;
92
+ }
93
+ const action = actions[event.key];
94
+ if (action) {
95
+ event.preventDefault();
96
+ action();
97
+ return;
98
+ }
99
+ if (event.key.length === 1 && event.key !== " " && !event.metaKey && !event.ctrlKey && !event.altKey) {
100
+ event.preventDefault();
101
+ onType(event.key);
102
+ }
103
+ }
104
+ watch(itemCount, (len) => {
105
+ if (activeIndex.value >= len) activeIndex.value = firstEnabled();
106
+ });
107
+ return {
108
+ activeIndex,
109
+ setActive,
110
+ activate,
111
+ close,
112
+ onTriggerKeydown,
113
+ onMenuKeydown
114
+ };
115
+ }
116
+ //#endregion
117
+ export { isMenuSeparator, useMenu };
@@ -0,0 +1,17 @@
1
+ import { Platform } from "../types.js";
2
+ import { UiTranslations } from "../translations.js";
3
+ import { Ref } from "vue";
4
+ //#region src/composables/use-platform.d.ts
5
+ /** From a user agent or a `Sec-CH-UA-Platform` client hint. */
6
+ export declare function platformFrom(agentOrHint: string | null | undefined): Platform;
7
+ export declare function usePlatform(): Readonly<Ref<Platform>>;
8
+ export interface KeyCap {
9
+ glyph: string;
10
+ label: string;
11
+ }
12
+ /**
13
+ * "mod+shift+k" or "shift enter" → key caps, with `mod` as ⌘ on Apple and Ctrl
14
+ * elsewhere. `names` are the spoken key names, e.g. the app's `keys` translations.
15
+ */
16
+ export declare function parseChord(chord: string, platform: Platform, names?: UiTranslations["keys"]): KeyCap[];
17
+ //#endregion
@@ -0,0 +1,126 @@
1
+ import { defaultTranslations } from "../translations.js";
2
+ import { useUiConfig } from "../config.js";
3
+ import { computed, onMounted, shallowRef } from "vue";
4
+ //#region src/composables/use-platform.ts
5
+ /** From a user agent or a `Sec-CH-UA-Platform` client hint. */
6
+ function platformFrom(agentOrHint) {
7
+ return /mac|iphone|ipad|ipod/i.test(agentOrHint ?? "") ? "apple" : "pc";
8
+ }
9
+ /**
10
+ * The platform for key caps. Comes from `createUi({ platform })` when set — the Nuxt
11
+ * module fills it from request headers — so server and client render the same caps.
12
+ * Otherwise it starts as "pc" (hydration-safe) and corrects itself after mount.
13
+ */
14
+ let browserPlatform;
15
+ function usePlatform() {
16
+ const { platform } = useUiConfig();
17
+ const detected = shallowRef(browserPlatform ?? "pc");
18
+ onMounted(() => {
19
+ if (platform.value) return;
20
+ const nav = navigator;
21
+ browserPlatform ??= platformFrom(nav.userAgentData?.platform || nav.userAgent);
22
+ detected.value = browserPlatform;
23
+ });
24
+ return computed(() => platform.value ?? detected.value);
25
+ }
26
+ const KEYS = {
27
+ mod: {
28
+ glyph: "⌘",
29
+ name: "command",
30
+ pc: {
31
+ glyph: "Ctrl",
32
+ name: "control"
33
+ }
34
+ },
35
+ cmd: {
36
+ glyph: "⌘",
37
+ name: "command",
38
+ pc: {
39
+ glyph: "Ctrl",
40
+ name: "control"
41
+ }
42
+ },
43
+ ctrl: {
44
+ glyph: "⌃",
45
+ name: "control",
46
+ pc: { glyph: "Ctrl" }
47
+ },
48
+ alt: {
49
+ glyph: "⌥",
50
+ name: "option",
51
+ pc: {
52
+ glyph: "Alt",
53
+ name: "alt"
54
+ }
55
+ },
56
+ shift: {
57
+ glyph: "⇧",
58
+ name: "shift",
59
+ pc: { glyph: "Shift" }
60
+ },
61
+ enter: {
62
+ glyph: "↵",
63
+ name: "enter",
64
+ pc: { glyph: "Enter" }
65
+ },
66
+ backspace: {
67
+ glyph: "⌫",
68
+ name: "backspace",
69
+ pc: { glyph: "Backspace" }
70
+ },
71
+ tab: {
72
+ glyph: "⇥",
73
+ name: "tab",
74
+ pc: { glyph: "Tab" }
75
+ },
76
+ esc: {
77
+ glyph: "Esc",
78
+ name: "escape"
79
+ },
80
+ space: {
81
+ glyph: "Space",
82
+ name: "space"
83
+ },
84
+ up: {
85
+ glyph: "↑",
86
+ name: "up"
87
+ },
88
+ down: {
89
+ glyph: "↓",
90
+ name: "down"
91
+ },
92
+ left: {
93
+ glyph: "←",
94
+ name: "left"
95
+ },
96
+ right: {
97
+ glyph: "→",
98
+ name: "right"
99
+ }
100
+ };
101
+ /**
102
+ * "mod+shift+k" or "shift enter" → key caps, with `mod` as ⌘ on Apple and Ctrl
103
+ * elsewhere. `names` are the spoken key names, e.g. the app's `keys` translations.
104
+ */
105
+ function parseChord(chord, platform, names = defaultTranslations.keys) {
106
+ return chord.split(/[+\s]+/).filter(Boolean).map((token) => {
107
+ const key = KEYS[token.toLowerCase()];
108
+ if (!key) {
109
+ const glyph = token.length === 1 ? token.toUpperCase() : token;
110
+ return {
111
+ glyph,
112
+ label: glyph
113
+ };
114
+ }
115
+ if (platform === "apple" || !key.pc) return {
116
+ glyph: key.glyph,
117
+ label: names[key.name]
118
+ };
119
+ return {
120
+ glyph: key.pc.glyph,
121
+ label: names[key.pc.name ?? key.name]
122
+ };
123
+ });
124
+ }
125
+ //#endregion
126
+ export { parseChord, platformFrom, usePlatform };
@@ -0,0 +1,35 @@
1
+ import { Ref } from "vue";
2
+ //#region src/composables/use-popover.d.ts
3
+ /**
4
+ * Pairs a trigger and a panel through CSS anchor positioning. Bind `anchorStyle` on
5
+ * the element the panel should sit against and `panelStyle` on the panel; placement
6
+ * and viewport flipping are then done by the browser via `position-area` and
7
+ * `position-try-fallbacks` in the stylesheet.
8
+ */
9
+ export declare function useAnchor(): {
10
+ anchorName: string;
11
+ anchorStyle: {
12
+ "anchor-name": string;
13
+ };
14
+ panelStyle: {
15
+ "position-anchor": string;
16
+ };
17
+ };
18
+ interface PopoverStateOptions {
19
+ panel: Readonly<Ref<HTMLElement | null>>;
20
+ open: Ref<boolean>;
21
+ onShow?: () => void;
22
+ onHide?: () => void;
23
+ }
24
+ /**
25
+ * Two-way sync between a `v-model:open` boolean and a native `[popover]` element.
26
+ * The browser owns the top layer, light dismiss and Escape; the model follows its
27
+ * `toggle` events, and model changes drive `showPopover()` / `hidePopover()`.
28
+ */
29
+ export declare function usePopoverState({ panel, open, onShow, onHide }: PopoverStateOptions): {
30
+ isOpen: import("vue").ComputedRef<boolean>;
31
+ };
32
+ /** The `[autofocus]` descendant, else the first focusable one. */
33
+ export declare function firstFocusable(root: HTMLElement | null | undefined): HTMLElement | null | undefined;
34
+ //#endregion
35
+ export {};
@@ -0,0 +1,49 @@
1
+ import { useEventListener } from "./internal/use-event-listener.js";
2
+ import { computed, onMounted, useId, watch } from "vue";
3
+ //#region src/composables/use-popover.ts
4
+ /**
5
+ * Pairs a trigger and a panel through CSS anchor positioning. Bind `anchorStyle` on
6
+ * the element the panel should sit against and `panelStyle` on the panel; placement
7
+ * and viewport flipping are then done by the browser via `position-area` and
8
+ * `position-try-fallbacks` in the stylesheet.
9
+ */
10
+ function useAnchor() {
11
+ const name = `--ui-anchor-${useId().replace(/[^\w-]/g, "")}`;
12
+ return {
13
+ anchorName: name,
14
+ anchorStyle: { "anchor-name": name },
15
+ panelStyle: { "position-anchor": name }
16
+ };
17
+ }
18
+ const isShown = (el) => el.matches(":popover-open");
19
+ /**
20
+ * Two-way sync between a `v-model:open` boolean and a native `[popover]` element.
21
+ * The browser owns the top layer, light dismiss and Escape; the model follows its
22
+ * `toggle` events, and model changes drive `showPopover()` / `hidePopover()`.
23
+ */
24
+ function usePopoverState({ panel, open, onShow, onHide }) {
25
+ function sync() {
26
+ const el = panel.value;
27
+ if (!el?.isConnected) return;
28
+ if (open.value && !isShown(el)) el.showPopover();
29
+ else if (!open.value && isShown(el)) el.hidePopover();
30
+ }
31
+ watch(open, sync, { flush: "post" });
32
+ onMounted(sync);
33
+ useEventListener(panel, "toggle", (event) => {
34
+ const el = event.currentTarget;
35
+ if (!(el instanceof HTMLElement)) return;
36
+ const nowOpen = isShown(el);
37
+ if (open.value !== nowOpen) open.value = nowOpen;
38
+ if (event.newState === "open" && nowOpen) onShow?.();
39
+ else if (event.newState === "closed" && !nowOpen) onHide?.();
40
+ });
41
+ return { isOpen: computed(() => open.value) };
42
+ }
43
+ const FOCUSABLE = ":is(button, input, select, textarea, a[href], [tabindex]):not(:disabled, [tabindex='-1'], [inert] *)";
44
+ /** The `[autofocus]` descendant, else the first focusable one. */
45
+ function firstFocusable(root) {
46
+ return root?.querySelector("[autofocus]") ?? root?.querySelector(FOCUSABLE);
47
+ }
48
+ //#endregion
49
+ export { firstFocusable, useAnchor, usePopoverState };
@@ -0,0 +1,22 @@
1
+ import { SelectOption, SelectValue } from "../types.js";
2
+ import { Ref } from "vue";
3
+ //#region src/composables/use-roving-selection.d.ts
4
+ interface RovingSelectionOptions<T extends SelectValue> {
5
+ items: () => SelectOption<T>[];
6
+ elements: () => HTMLElement[] | null | undefined;
7
+ model: Ref<T | undefined>;
8
+ disabled?: () => boolean;
9
+ orientation?: () => "horizontal" | "vertical" | "both";
10
+ }
11
+ /**
12
+ * Roving tabindex over a single-select option list (radio group, tabs). Selection
13
+ * follows focus per the APG: arrows move to the next enabled option, select it and
14
+ * focus it, wrapping at both ends.
15
+ */
16
+ export declare function useRovingSelection<T extends SelectValue>({ items, elements, model, disabled, orientation }: RovingSelectionOptions<T>): {
17
+ isTabStop: (option: SelectOption<T>, index: number) => boolean;
18
+ select: (option: SelectOption<T>) => void;
19
+ onKeydown: (event: KeyboardEvent) => void;
20
+ };
21
+ //#endregion
22
+ export {};
@@ -0,0 +1,65 @@
1
+ import { computed } from "vue";
2
+ //#region src/composables/use-roving-selection.ts
3
+ /**
4
+ * Roving tabindex over a single-select option list (radio group, tabs). Selection
5
+ * follows focus per the APG: arrows move to the next enabled option, select it and
6
+ * focus it, wrapping at both ends.
7
+ */
8
+ function useRovingSelection({ items, elements, model, disabled = () => false, orientation = () => "both" }) {
9
+ const enabledIndexes = computed(() => items().reduce((acc, option, index) => {
10
+ if (!option.disabled) acc.push(index);
11
+ return acc;
12
+ }, []));
13
+ function isTabStop(option, index) {
14
+ if (model.value != null && items().some((o) => o.value === model.value)) return option.value === model.value;
15
+ return index === enabledIndexes.value[0];
16
+ }
17
+ function select(option) {
18
+ if (disabled() || option.disabled) return;
19
+ model.value = option.value;
20
+ }
21
+ function moveTo(index) {
22
+ if (index == null) return;
23
+ const option = items()[index];
24
+ if (!option) return;
25
+ model.value = option.value;
26
+ elements()?.[index]?.focus();
27
+ }
28
+ function step(direction) {
29
+ const enabled = enabledIndexes.value;
30
+ if (!enabled.length) return;
31
+ const current = model.value == null ? -1 : items().findIndex((o) => o.value === model.value);
32
+ const position = enabled.indexOf(current);
33
+ if (position < 0) {
34
+ moveTo(direction === 1 ? enabled[0] : enabled.at(-1));
35
+ return;
36
+ }
37
+ moveTo(enabled[(position + direction + enabled.length) % enabled.length]);
38
+ }
39
+ function onKeydown(event) {
40
+ if (disabled()) return;
41
+ const axis = orientation();
42
+ const horizontal = axis !== "vertical";
43
+ const vertical = axis !== "horizontal";
44
+ const enabled = enabledIndexes.value;
45
+ const forward = event.currentTarget instanceof Element && getComputedStyle(event.currentTarget).direction === "rtl" ? -1 : 1;
46
+ const action = {
47
+ ArrowRight: horizontal && (() => step(forward)),
48
+ ArrowLeft: horizontal && (() => step(-forward)),
49
+ ArrowDown: vertical && (() => step(1)),
50
+ ArrowUp: vertical && (() => step(-1)),
51
+ Home: () => moveTo(enabled[0]),
52
+ End: () => moveTo(enabled.at(-1))
53
+ }[event.key];
54
+ if (!action) return;
55
+ event.preventDefault();
56
+ action();
57
+ }
58
+ return {
59
+ isTabStop,
60
+ select,
61
+ onKeydown
62
+ };
63
+ }
64
+ //#endregion
65
+ export { useRovingSelection };
@@ -0,0 +1,31 @@
1
+ import { SelectOption, SelectValue } from "../types.js";
2
+ import { Ref } from "vue";
3
+ //#region src/composables/use-select.d.ts
4
+ interface UseSelectOptions<T extends SelectValue> {
5
+ options: () => SelectOption<T>[];
6
+ model: Ref<T | T[] | undefined>;
7
+ multiple: () => boolean;
8
+ disabled: () => boolean;
9
+ baseId: () => string;
10
+ trigger: Readonly<Ref<HTMLElement | null>>;
11
+ listbox: Readonly<Ref<HTMLElement | null>>;
12
+ label: Readonly<Ref<HTMLElement | null>>;
13
+ }
14
+ /**
15
+ * APG select-only combobox: focus stays on the trigger, the active option is
16
+ * conveyed with `aria-activedescendant`, and typing jumps by label.
17
+ */
18
+ export declare function useSelect<T extends SelectValue>({ model, baseId, options, multiple, disabled, trigger, listbox, label }: UseSelectOptions<T>): {
19
+ isOpen: Ref<boolean, boolean>;
20
+ activeIndex: Ref<number, number>;
21
+ activeId: import("vue").ComputedRef<string | undefined>;
22
+ optionId: (index: number) => string;
23
+ isSelected: (value: T) => boolean;
24
+ selectedOptions: import("vue").ComputedRef<SelectOption<T>[]>;
25
+ selectAt: (index: number) => void;
26
+ setActive: (index: number) => void;
27
+ onTriggerKeydown: (event: KeyboardEvent) => void;
28
+ onTriggerPointerdown: (event: PointerEvent) => void;
29
+ };
30
+ //#endregion
31
+ export {};
@@ -0,0 +1,169 @@
1
+ import { onClickOutside } from "./internal/on-click-outside.js";
2
+ import { useTimeoutFn } from "./internal/use-timeout-fn.js";
3
+ import { computed, nextTick, ref, watch } from "vue";
4
+ //#region src/composables/use-select.ts
5
+ const TYPEAHEAD_RESET = 500;
6
+ /**
7
+ * APG select-only combobox: focus stays on the trigger, the active option is
8
+ * conveyed with `aria-activedescendant`, and typing jumps by label.
9
+ */
10
+ function useSelect({ model, baseId, options, multiple, disabled, trigger, listbox, label }) {
11
+ const isOpen = ref(false);
12
+ const activeIndex = ref(-1);
13
+ const selectedValues = computed(() => {
14
+ const value = model.value;
15
+ if (multiple()) return Array.isArray(value) ? value : [];
16
+ if (value == null || Array.isArray(value)) return [];
17
+ return [value];
18
+ });
19
+ const isSelected = (value) => selectedValues.value.includes(value);
20
+ const selectedOptions = computed(() => options().filter((o) => isSelected(o.value)));
21
+ const optionId = (index) => `${baseId()}-opt-${index}`;
22
+ const activeId = computed(() => isOpen.value && activeIndex.value >= 0 && activeIndex.value < options().length ? optionId(activeIndex.value) : void 0);
23
+ function enabledFrom(start, dir) {
24
+ const list = options();
25
+ for (let i = start; i >= 0 && i < list.length; i += dir) if (!list[i]?.disabled) return i;
26
+ return -1;
27
+ }
28
+ async function scrollActiveIntoView() {
29
+ if (activeIndex.value < 0) return;
30
+ await nextTick();
31
+ listbox.value?.querySelector(`#${CSS.escape(optionId(activeIndex.value))}`)?.scrollIntoView({ block: "nearest" });
32
+ }
33
+ function open(intent) {
34
+ if (disabled() || !options().length) return;
35
+ isOpen.value = true;
36
+ const selected = intent === "selected" ? options().findIndex((o) => isSelected(o.value)) : -1;
37
+ activeIndex.value = selected >= 0 ? selected : intent === "last" ? enabledFrom(options().length - 1, -1) : enabledFrom(0, 1);
38
+ scrollActiveIntoView();
39
+ }
40
+ function close() {
41
+ if (!isOpen.value) return;
42
+ isOpen.value = false;
43
+ activeIndex.value = -1;
44
+ }
45
+ function selectAt(index) {
46
+ const option = options()[index];
47
+ if (!option || option.disabled) return;
48
+ if (multiple()) {
49
+ const current = selectedValues.value;
50
+ model.value = current.includes(option.value) ? current.filter((v) => v !== option.value) : [...current, option.value];
51
+ activeIndex.value = index;
52
+ return;
53
+ }
54
+ model.value = option.value;
55
+ close();
56
+ trigger.value?.focus();
57
+ }
58
+ function setActive(index) {
59
+ if (index < 0 || index === activeIndex.value) return;
60
+ activeIndex.value = index;
61
+ scrollActiveIntoView();
62
+ }
63
+ function move(dir) {
64
+ const next = enabledFrom(activeIndex.value < 0 ? dir === 1 ? -1 : options().length : activeIndex.value + dir, dir);
65
+ if (next >= 0) setActive(next);
66
+ }
67
+ watch(() => options().length, (len) => {
68
+ if (activeIndex.value >= len) activeIndex.value = enabledFrom(0, 1);
69
+ });
70
+ let typeahead = "";
71
+ const resetTypeahead = useTimeoutFn(() => typeahead = "", TYPEAHEAD_RESET);
72
+ function onType(char) {
73
+ typeahead += char.toLowerCase();
74
+ resetTypeahead.start();
75
+ const list = options();
76
+ const single = typeahead.length > 1 && typeahead.replaceAll(typeahead[0], "") === "";
77
+ const needle = single ? typeahead[0] : typeahead;
78
+ const start = single ? activeIndex.value : activeIndex.value - 1;
79
+ for (let step = 1; step <= list.length; step++) {
80
+ const i = ((start + step) % list.length + list.length) % list.length;
81
+ const opt = list[i];
82
+ if (opt && !opt.disabled && opt.label.toLowerCase().startsWith(needle)) {
83
+ setActive(i);
84
+ return;
85
+ }
86
+ }
87
+ }
88
+ function onTriggerKeydown(event) {
89
+ if (disabled()) return;
90
+ switch (event.key) {
91
+ case "ArrowDown":
92
+ event.preventDefault();
93
+ if (isOpen.value) move(1);
94
+ else open("first");
95
+ break;
96
+ case "ArrowUp":
97
+ event.preventDefault();
98
+ if (isOpen.value) move(-1);
99
+ else open("last");
100
+ break;
101
+ case "Home":
102
+ if (isOpen.value) {
103
+ event.preventDefault();
104
+ setActive(enabledFrom(0, 1));
105
+ }
106
+ break;
107
+ case "End":
108
+ if (isOpen.value) {
109
+ event.preventDefault();
110
+ setActive(enabledFrom(options().length - 1, -1));
111
+ }
112
+ break;
113
+ case "Enter":
114
+ case " ":
115
+ event.preventDefault();
116
+ if (isOpen.value) selectAt(activeIndex.value);
117
+ else open("selected");
118
+ break;
119
+ case "Escape":
120
+ if (isOpen.value) {
121
+ event.preventDefault();
122
+ close();
123
+ }
124
+ break;
125
+ case "Tab": {
126
+ const option = options()[activeIndex.value];
127
+ if (isOpen.value && !multiple() && option && !option.disabled) model.value = option.value;
128
+ close();
129
+ break;
130
+ }
131
+ default: if (event.key.length === 1 && !event.metaKey && !event.ctrlKey && !event.altKey) {
132
+ if (!isOpen.value) open("selected");
133
+ onType(event.key);
134
+ }
135
+ }
136
+ }
137
+ function onTriggerPointerdown(event) {
138
+ if (disabled() || event.button !== 0) return;
139
+ event.preventDefault();
140
+ trigger.value?.focus();
141
+ if (isOpen.value) close();
142
+ else open("selected");
143
+ }
144
+ watch(isOpen, (opened) => {
145
+ const el = listbox.value;
146
+ if (!el?.isConnected) return;
147
+ const shown = el.matches(":popover-open");
148
+ if (opened && !shown) el.showPopover();
149
+ else if (!opened && shown) el.hidePopover();
150
+ }, { flush: "post" });
151
+ onClickOutside(trigger, () => close(), {
152
+ ignore: [label, listbox],
153
+ active: () => isOpen.value
154
+ });
155
+ return {
156
+ isOpen,
157
+ activeIndex,
158
+ activeId,
159
+ optionId,
160
+ isSelected,
161
+ selectedOptions,
162
+ selectAt,
163
+ setActive,
164
+ onTriggerKeydown,
165
+ onTriggerPointerdown
166
+ };
167
+ }
168
+ //#endregion
169
+ export { useSelect };
@@ -0,0 +1,24 @@
1
+ import { Ref } from "vue";
2
+ //#region src/composables/use-slide-indicator.d.ts
3
+ interface SlideIndicatorOptions {
4
+ container: Readonly<Ref<HTMLElement | null>>;
5
+ items: () => HTMLElement[] | null | undefined;
6
+ activeIndex: () => number;
7
+ }
8
+ /**
9
+ * Measures the active item so an indicator can slide between items. The result is
10
+ * exposed as CSS custom properties, not positioning, so stylesheets decide how
11
+ * (and whether) to use each one: `--ui-indicator-x`, `-width`, `-top`, `-height`.
12
+ */
13
+ export declare function useSlideIndicator({ container, items, activeIndex }: SlideIndicatorOptions): {
14
+ style: import("vue").ComputedRef<{
15
+ "--ui-indicator-x": string;
16
+ "--ui-indicator-width": string;
17
+ "--ui-indicator-top": string;
18
+ "--ui-indicator-height": string;
19
+ }>;
20
+ isMeasured: Ref<boolean, boolean>;
21
+ canAnimate: Ref<boolean, boolean>;
22
+ };
23
+ //#endregion
24
+ export {};