@humanonlyweb/ui 0.0.0-stage → 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 +45 -2
- package/dist/components/accordion/Accordion.js +60 -0
- package/dist/components/accordion/Accordion.vue.d.ts +28 -0
- package/dist/components/accordion/AccordionItem.js +102 -0
- package/dist/components/accordion/AccordionItem.vue.d.ts +33 -0
- package/dist/components/avatar/Avatar.js +52 -0
- package/dist/components/avatar/Avatar.vue.d.ts +26 -0
- package/dist/components/avatar/AvatarGroup.js +48 -0
- package/dist/components/avatar/AvatarGroup.vue.d.ts +16 -0
- package/dist/components/button/Button.js +95 -0
- package/dist/components/button/Button.vue.d.ts +74 -0
- package/dist/components/button/IconButton.js +47 -0
- package/dist/components/button/IconButton.vue.d.ts +115 -0
- package/dist/components/checkbox/Checkbox.js +93 -0
- package/dist/components/checkbox/Checkbox.vue.d.ts +43 -0
- package/dist/components/combobox/Combobox.js +294 -0
- package/dist/components/combobox/Combobox.vue.d.ts +80 -0
- package/dist/components/context-menu/ContextMenu.js +170 -0
- package/dist/components/context-menu/ContextMenu.vue.d.ts +38 -0
- package/dist/components/dialog/ConfirmDialog.js +103 -0
- package/dist/components/dialog/ConfirmDialog.vue.d.ts +46 -0
- package/dist/components/dialog/Dialog.js +160 -0
- package/dist/components/dialog/Dialog.vue.d.ts +54 -0
- package/dist/components/field/FieldMessage.js +31 -0
- package/dist/components/input/Input.js +111 -0
- package/dist/components/input/Input.vue.d.ts +50 -0
- package/dist/components/kbd/Kbd.js +48 -0
- package/dist/components/kbd/Kbd.vue.d.ts +24 -0
- package/dist/components/menu/Menu.js +124 -0
- package/dist/components/menu/Menu.vue.d.ts +41 -0
- package/dist/components/otp-input/OtpInput.js +179 -0
- package/dist/components/otp-input/OtpInput.vue.d.ts +48 -0
- package/dist/components/popover/Popover.js +83 -0
- package/dist/components/popover/Popover.vue.d.ts +37 -0
- package/dist/components/radio-group/RadioGroup.js +125 -0
- package/dist/components/radio-group/RadioGroup.vue.d.ts +45 -0
- package/dist/components/segmented/Segmented.js +110 -0
- package/dist/components/segmented/Segmented.vue.d.ts +41 -0
- package/dist/components/select/Select.js +231 -0
- package/dist/components/select/Select.vue.d.ts +59 -0
- package/dist/components/slider/Slider.js +196 -0
- package/dist/components/slider/Slider.vue.d.ts +49 -0
- package/dist/components/spin-button/SpinButton.js +231 -0
- package/dist/components/spin-button/SpinButton.vue.d.ts +48 -0
- package/dist/components/split-button/SplitButton.js +69 -0
- package/dist/components/split-button/SplitButton.vue.d.ts +38 -0
- package/dist/components/switch/Switch.js +95 -0
- package/dist/components/switch/Switch.vue.d.ts +42 -0
- package/dist/components/table/Table.js +173 -0
- package/dist/components/table/Table.vue.d.ts +45 -0
- package/dist/components/tabs/Tabs.js +99 -0
- package/dist/components/tabs/Tabs.vue.d.ts +39 -0
- package/dist/components/textarea/Textarea.js +118 -0
- package/dist/components/textarea/Textarea.vue.d.ts +34 -0
- package/dist/components/toast/ToastItem.js +91 -0
- package/dist/components/toast/ToastItem.vue.d.ts +29 -0
- package/dist/components/toast/ToastViewport.js +56 -0
- package/dist/components/toast/ToastViewport.vue.d.ts +25 -0
- package/dist/components/toolbar/Toolbar.js +91 -0
- package/dist/components/toolbar/Toolbar.vue.d.ts +21 -0
- package/dist/components/tooltip/Tooltip.js +85 -0
- package/dist/components/tooltip/Tooltip.vue.d.ts +25 -0
- package/dist/composables/internal/env.js +5 -0
- package/dist/composables/internal/is-equal.js +14 -0
- package/dist/composables/internal/on-click-outside.js +18 -0
- package/dist/composables/internal/standard-schema.d.ts +38 -0
- package/dist/composables/internal/use-document-visibility.js +16 -0
- package/dist/composables/internal/use-event-listener.js +24 -0
- package/dist/composables/internal/use-interval-fn.js +21 -0
- package/dist/composables/internal/use-resize-observer.js +21 -0
- package/dist/composables/internal/use-timeout-fn.js +25 -0
- package/dist/composables/use-accordion.d.ts +9 -0
- package/dist/composables/use-accordion.js +10 -0
- package/dist/composables/use-combobox.d.ts +44 -0
- package/dist/composables/use-combobox.js +185 -0
- package/dist/composables/use-field.d.ts +17 -0
- package/dist/composables/use-field.js +27 -0
- package/dist/composables/use-form.d.ts +44 -0
- package/dist/composables/use-form.js +146 -0
- package/dist/composables/use-menu.d.ts +26 -0
- package/dist/composables/use-menu.js +117 -0
- package/dist/composables/use-platform.d.ts +17 -0
- package/dist/composables/use-platform.js +126 -0
- package/dist/composables/use-popover.d.ts +35 -0
- package/dist/composables/use-popover.js +49 -0
- package/dist/composables/use-roving-selection.d.ts +22 -0
- package/dist/composables/use-roving-selection.js +65 -0
- package/dist/composables/use-select.d.ts +31 -0
- package/dist/composables/use-select.js +169 -0
- package/dist/composables/use-slide-indicator.d.ts +24 -0
- package/dist/composables/use-slide-indicator.js +52 -0
- package/dist/composables/use-toast.d.ts +23 -0
- package/dist/composables/use-toast.js +51 -0
- package/dist/config.d.ts +40 -0
- package/dist/config.js +35 -0
- package/dist/docs/README.md +44 -0
- package/dist/docs/changelog.md +14 -0
- package/dist/docs/components/accordion.md +100 -0
- package/dist/docs/components/avatar-group.md +58 -0
- package/dist/docs/components/avatar.md +40 -0
- package/dist/docs/components/button.md +109 -0
- package/dist/docs/components/checkbox.md +123 -0
- package/dist/docs/components/combobox.md +207 -0
- package/dist/docs/components/confirm-dialog.md +107 -0
- package/dist/docs/components/context-menu.md +98 -0
- package/dist/docs/components/dialog.md +127 -0
- package/dist/docs/components/icon-button.md +47 -0
- package/dist/docs/components/input.md +89 -0
- package/dist/docs/components/kbd.md +41 -0
- package/dist/docs/components/menu.md +112 -0
- package/dist/docs/components/otp-input.md +92 -0
- package/dist/docs/components/popover.md +72 -0
- package/dist/docs/components/radio-group.md +114 -0
- package/dist/docs/components/segmented.md +96 -0
- package/dist/docs/components/select.md +129 -0
- package/dist/docs/components/slider.md +105 -0
- package/dist/docs/components/spin-button.md +73 -0
- package/dist/docs/components/split-button.md +64 -0
- package/dist/docs/components/switch.md +71 -0
- package/dist/docs/components/table.md +241 -0
- package/dist/docs/components/tabs.md +106 -0
- package/dist/docs/components/textarea.md +46 -0
- package/dist/docs/components/toast.md +135 -0
- package/dist/docs/components/toolbar.md +71 -0
- package/dist/docs/components/tooltip.md +57 -0
- package/dist/docs/forms.md +182 -0
- package/dist/docs/index.md +11 -0
- package/dist/docs/installation.md +73 -0
- package/dist/docs/localisation.md +40 -0
- package/dist/docs/ssr.md +24 -0
- package/dist/docs/styling.md +66 -0
- package/dist/index.d.ts +46 -0
- package/dist/index.js +44 -0
- package/dist/nuxt/module.d.ts +15 -0
- package/dist/nuxt/module.js +72 -0
- package/dist/nuxt/runtime/plugin.d.ts +4 -0
- package/dist/nuxt/runtime/plugin.js +24 -0
- package/dist/package.js +4 -0
- package/dist/styles/core.css +284 -0
- package/dist/translations.d.ts +71 -0
- package/dist/translations.js +55 -0
- package/dist/types.d.ts +61 -0
- package/package.json +93 -4
|
@@ -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 {};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { useResizeObserver } from "./internal/use-resize-observer.js";
|
|
2
|
+
import { computed, onMounted, ref, watch } from "vue";
|
|
3
|
+
//#region src/composables/use-slide-indicator.ts
|
|
4
|
+
/**
|
|
5
|
+
* Measures the active item so an indicator can slide between items. The result is
|
|
6
|
+
* exposed as CSS custom properties, not positioning, so stylesheets decide how
|
|
7
|
+
* (and whether) to use each one: `--ui-indicator-x`, `-width`, `-top`, `-height`.
|
|
8
|
+
*/
|
|
9
|
+
function useSlideIndicator({ container, items, activeIndex }) {
|
|
10
|
+
const rect = ref({
|
|
11
|
+
x: 0,
|
|
12
|
+
width: 0,
|
|
13
|
+
top: 0,
|
|
14
|
+
height: 0
|
|
15
|
+
});
|
|
16
|
+
const isMeasured = ref(false);
|
|
17
|
+
const canAnimate = ref(false);
|
|
18
|
+
function measure() {
|
|
19
|
+
const el = items()?.[activeIndex()];
|
|
20
|
+
if (!el) {
|
|
21
|
+
isMeasured.value = false;
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
rect.value = {
|
|
25
|
+
x: el.offsetLeft,
|
|
26
|
+
width: el.offsetWidth,
|
|
27
|
+
top: el.offsetTop,
|
|
28
|
+
height: el.offsetHeight
|
|
29
|
+
};
|
|
30
|
+
isMeasured.value = true;
|
|
31
|
+
}
|
|
32
|
+
watch([activeIndex, () => items()?.length], measure, { flush: "post" });
|
|
33
|
+
useResizeObserver(container, measure);
|
|
34
|
+
onMounted(() => {
|
|
35
|
+
measure();
|
|
36
|
+
requestAnimationFrame(() => {
|
|
37
|
+
canAnimate.value = true;
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
return {
|
|
41
|
+
style: computed(() => ({
|
|
42
|
+
"--ui-indicator-x": `${rect.value.x}px`,
|
|
43
|
+
"--ui-indicator-width": `${rect.value.width}px`,
|
|
44
|
+
"--ui-indicator-top": `${rect.value.top}px`,
|
|
45
|
+
"--ui-indicator-height": `${rect.value.height}px`
|
|
46
|
+
})),
|
|
47
|
+
isMeasured,
|
|
48
|
+
canAnimate
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
//#endregion
|
|
52
|
+
export { useSlideIndicator };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Toast, ToastAction, ToastTone } from "../types.js";
|
|
2
|
+
import { InjectionKey, Ref } from "vue";
|
|
3
|
+
//#region src/composables/use-toast.d.ts
|
|
4
|
+
export interface ToastOptions {
|
|
5
|
+
tone?: ToastTone;
|
|
6
|
+
duration?: number;
|
|
7
|
+
/** A button in the toast, e.g. `{ label: "Undo", onClick: restore }`. */
|
|
8
|
+
action?: ToastAction;
|
|
9
|
+
}
|
|
10
|
+
export interface Toaster {
|
|
11
|
+
items: Readonly<Ref<readonly Toast[]>>;
|
|
12
|
+
push: (message: string, options?: ToastOptions) => number;
|
|
13
|
+
dismiss: (id: number) => void;
|
|
14
|
+
clear: () => void;
|
|
15
|
+
success: (message: string, options?: Omit<ToastOptions, "tone">) => number;
|
|
16
|
+
error: (message: string, options?: Omit<ToastOptions, "tone">) => number;
|
|
17
|
+
info: (message: string, options?: Omit<ToastOptions, "tone">) => number;
|
|
18
|
+
}
|
|
19
|
+
export declare const TOASTER_KEY: InjectionKey<Toaster>;
|
|
20
|
+
/** One queue per app instance — never module-level, or SSR requests would share it. */
|
|
21
|
+
export declare function createToaster(): Toaster;
|
|
22
|
+
export declare function useToast(): Toaster;
|
|
23
|
+
//#endregion
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { isClient } from "./internal/env.js";
|
|
2
|
+
import { inject, readonly, shallowRef } from "vue";
|
|
3
|
+
//#region src/composables/use-toast.ts
|
|
4
|
+
const DEFAULT_DURATION = 4500;
|
|
5
|
+
const TOASTER_KEY = Symbol("humanonlyweb-ui-toaster");
|
|
6
|
+
/** One queue per app instance — never module-level, or SSR requests would share it. */
|
|
7
|
+
function createToaster() {
|
|
8
|
+
const items = shallowRef([]);
|
|
9
|
+
let nextId = 0;
|
|
10
|
+
function push(message, { tone = "info", duration = DEFAULT_DURATION, action } = {}) {
|
|
11
|
+
if (!isClient()) return -1;
|
|
12
|
+
const id = ++nextId;
|
|
13
|
+
items.value = [...items.value, {
|
|
14
|
+
id,
|
|
15
|
+
message,
|
|
16
|
+
tone,
|
|
17
|
+
duration,
|
|
18
|
+
action
|
|
19
|
+
}].slice(-5);
|
|
20
|
+
return id;
|
|
21
|
+
}
|
|
22
|
+
return {
|
|
23
|
+
items: readonly(items),
|
|
24
|
+
push,
|
|
25
|
+
dismiss: (id) => {
|
|
26
|
+
items.value = items.value.filter((toast) => toast.id !== id);
|
|
27
|
+
},
|
|
28
|
+
clear: () => {
|
|
29
|
+
items.value = [];
|
|
30
|
+
},
|
|
31
|
+
success: (message, options) => push(message, {
|
|
32
|
+
...options,
|
|
33
|
+
tone: "success"
|
|
34
|
+
}),
|
|
35
|
+
error: (message, options) => push(message, {
|
|
36
|
+
...options,
|
|
37
|
+
tone: "danger"
|
|
38
|
+
}),
|
|
39
|
+
info: (message, options) => push(message, {
|
|
40
|
+
...options,
|
|
41
|
+
tone: "info"
|
|
42
|
+
})
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
function useToast() {
|
|
46
|
+
const toaster = inject(TOASTER_KEY, null);
|
|
47
|
+
if (!toaster) throw new Error("[@humanonlyweb/ui] useToast() needs app.use(createUi()) or the Nuxt module.");
|
|
48
|
+
return toaster;
|
|
49
|
+
}
|
|
50
|
+
//#endregion
|
|
51
|
+
export { TOASTER_KEY, createToaster, useToast };
|