@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.
- 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/breadcrumbs/Breadcrumbs.js +59 -0
- package/dist/components/breadcrumbs/Breadcrumbs.vue.d.ts +14 -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/checkbox-group/CheckboxGroup.js +89 -0
- package/dist/components/checkbox-group/CheckboxGroup.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/fieldset/Fieldset.js +59 -0
- package/dist/components/fieldset/Fieldset.vue.d.ts +29 -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/pagination/Pagination.js +128 -0
- package/dist/components/pagination/Pagination.vue.d.ts +24 -0
- package/dist/components/password-input/PasswordInput.js +151 -0
- package/dist/components/password-input/PasswordInput.vue.d.ts +32 -0
- package/dist/components/popover/Popover.js +83 -0
- package/dist/components/popover/Popover.vue.d.ts +37 -0
- package/dist/components/progress/Progress.js +52 -0
- package/dist/components/progress/Progress.vue.d.ts +17 -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 +103 -0
- package/dist/components/tabs/Tabs.vue.d.ts +39 -0
- package/dist/components/tag-input/TagInput.js +186 -0
- package/dist/components/tag-input/TagInput.vue.d.ts +36 -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/components/visually-hidden/VisuallyHidden.js +16 -0
- package/dist/components/visually-hidden/VisuallyHidden.vue.d.ts +19 -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 +52 -0
- package/dist/docs/changelog.md +21 -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/breadcrumbs.md +81 -0
- package/dist/docs/components/button.md +109 -0
- package/dist/docs/components/checkbox-group.md +118 -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/fieldset.md +65 -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/pagination.md +62 -0
- package/dist/docs/components/password-input.md +55 -0
- package/dist/docs/components/popover.md +72 -0
- package/dist/docs/components/progress.md +56 -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/tag-input.md +88 -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/components/visually-hidden.md +53 -0
- package/dist/docs/forms.md +182 -0
- package/dist/docs/index.md +11 -0
- package/dist/docs/installation.md +71 -0
- package/dist/docs/localisation.md +42 -0
- package/dist/docs/ssr.md +24 -0
- package/dist/docs/styling.md +82 -0
- package/dist/index.d.ts +60 -0
- package/dist/index.js +56 -0
- package/dist/nuxt/module.d.ts +15 -0
- package/dist/nuxt/module.js +80 -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 +310 -0
- package/dist/translations.d.ts +85 -0
- package/dist/translations.js +62 -0
- package/dist/types.d.ts +69 -0
- package/package.json +93 -4
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
//#region src/composables/internal/standard-schema.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* The Standard Schema interface (https://standardschema.dev), inlined as the spec
|
|
4
|
+
* recommends so validators like zod, valibot and arktype work without a dependency.
|
|
5
|
+
*/
|
|
6
|
+
export interface StandardSchemaV1<Input = unknown, Output = Input> {
|
|
7
|
+
readonly "~standard": StandardSchemaV1.Props<Input, Output>;
|
|
8
|
+
}
|
|
9
|
+
export declare namespace StandardSchemaV1 {
|
|
10
|
+
interface Props<Input = unknown, Output = Input> {
|
|
11
|
+
readonly version: 1;
|
|
12
|
+
readonly vendor: string;
|
|
13
|
+
readonly validate: (value: unknown) => Result<Output> | Promise<Result<Output>>;
|
|
14
|
+
readonly types?: Types<Input, Output> | undefined;
|
|
15
|
+
}
|
|
16
|
+
type Result<Output> = SuccessResult<Output> | FailureResult;
|
|
17
|
+
interface SuccessResult<Output> {
|
|
18
|
+
readonly value: Output;
|
|
19
|
+
readonly issues?: undefined;
|
|
20
|
+
}
|
|
21
|
+
interface FailureResult {
|
|
22
|
+
readonly issues: ReadonlyArray<Issue>;
|
|
23
|
+
}
|
|
24
|
+
interface Issue {
|
|
25
|
+
readonly message: string;
|
|
26
|
+
readonly path?: ReadonlyArray<PropertyKey | PathSegment> | undefined;
|
|
27
|
+
}
|
|
28
|
+
interface PathSegment {
|
|
29
|
+
readonly key: PropertyKey;
|
|
30
|
+
}
|
|
31
|
+
interface Types<Input = unknown, Output = Input> {
|
|
32
|
+
readonly input: Input;
|
|
33
|
+
readonly output: Output;
|
|
34
|
+
}
|
|
35
|
+
type InferInput<Schema extends StandardSchemaV1> = NonNullable<Schema["~standard"]["types"]>["input"];
|
|
36
|
+
type InferOutput<Schema extends StandardSchemaV1> = NonNullable<Schema["~standard"]["types"]>["output"];
|
|
37
|
+
}
|
|
38
|
+
//#endregion
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { isClient } from "./env.js";
|
|
2
|
+
import { useEventListener } from "./use-event-listener.js";
|
|
3
|
+
import { shallowRef } from "vue";
|
|
4
|
+
//#region src/composables/internal/use-document-visibility.ts
|
|
5
|
+
/** Starts as "visible" on the server so SSR and the first client render agree. */
|
|
6
|
+
function useDocumentVisibility() {
|
|
7
|
+
const visibility = shallowRef("visible");
|
|
8
|
+
if (!isClient()) return visibility;
|
|
9
|
+
visibility.value = document.visibilityState;
|
|
10
|
+
useEventListener(document, "visibilitychange", () => {
|
|
11
|
+
visibility.value = document.visibilityState;
|
|
12
|
+
});
|
|
13
|
+
return visibility;
|
|
14
|
+
}
|
|
15
|
+
//#endregion
|
|
16
|
+
export { useDocumentVisibility };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { isClient } from "./env.js";
|
|
2
|
+
import { onScopeDispose, toValue, watch } from "vue";
|
|
3
|
+
//#region src/composables/internal/use-event-listener.ts
|
|
4
|
+
/**
|
|
5
|
+
* Attaches a listener for as long as the target resolves to an element. A getter
|
|
6
|
+
* returning `null` detaches it, so listeners can live only while something is open.
|
|
7
|
+
* No-op during SSR.
|
|
8
|
+
*/
|
|
9
|
+
function useEventListener(target, type, handler, options) {
|
|
10
|
+
if (!isClient()) return () => {};
|
|
11
|
+
const listener = handler;
|
|
12
|
+
const stopWatch = watch(() => toValue(target), (el, _prev, onCleanup) => {
|
|
13
|
+
if (!el) return;
|
|
14
|
+
el.addEventListener(type, listener, options);
|
|
15
|
+
onCleanup(() => el.removeEventListener(type, listener, options));
|
|
16
|
+
}, {
|
|
17
|
+
immediate: true,
|
|
18
|
+
flush: "post"
|
|
19
|
+
});
|
|
20
|
+
onScopeDispose(stopWatch, true);
|
|
21
|
+
return stopWatch;
|
|
22
|
+
}
|
|
23
|
+
//#endregion
|
|
24
|
+
export { useEventListener };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { onScopeDispose } from "vue";
|
|
2
|
+
//#region src/composables/internal/use-interval-fn.ts
|
|
3
|
+
/** A pausable interval, cleared automatically when the owning scope is disposed. */
|
|
4
|
+
function useIntervalFn(callback, ms) {
|
|
5
|
+
let timer;
|
|
6
|
+
function pause() {
|
|
7
|
+
if (timer !== void 0) clearInterval(timer);
|
|
8
|
+
timer = void 0;
|
|
9
|
+
}
|
|
10
|
+
function resume() {
|
|
11
|
+
pause();
|
|
12
|
+
timer = setInterval(callback, ms);
|
|
13
|
+
}
|
|
14
|
+
onScopeDispose(pause, true);
|
|
15
|
+
return {
|
|
16
|
+
pause,
|
|
17
|
+
resume
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
//#endregion
|
|
21
|
+
export { useIntervalFn };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { isClient } from "./env.js";
|
|
2
|
+
import { onScopeDispose, toValue, watch } from "vue";
|
|
3
|
+
//#region src/composables/internal/use-resize-observer.ts
|
|
4
|
+
function useResizeObserver(target, callback) {
|
|
5
|
+
if (!isClient() || typeof ResizeObserver === "undefined") return;
|
|
6
|
+
const observer = new ResizeObserver(callback);
|
|
7
|
+
const stopWatch = watch(() => toValue(target), (el, _prev, onCleanup) => {
|
|
8
|
+
if (!el) return;
|
|
9
|
+
observer.observe(el);
|
|
10
|
+
onCleanup(() => observer.unobserve(el));
|
|
11
|
+
}, {
|
|
12
|
+
immediate: true,
|
|
13
|
+
flush: "post"
|
|
14
|
+
});
|
|
15
|
+
onScopeDispose(() => {
|
|
16
|
+
stopWatch();
|
|
17
|
+
observer.disconnect();
|
|
18
|
+
}, true);
|
|
19
|
+
}
|
|
20
|
+
//#endregion
|
|
21
|
+
export { useResizeObserver };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { onScopeDispose } from "vue";
|
|
2
|
+
//#region src/composables/internal/use-timeout-fn.ts
|
|
3
|
+
/** A restartable timer, cleared automatically when the owning scope is disposed. */
|
|
4
|
+
function useTimeoutFn(callback, ms) {
|
|
5
|
+
let timer;
|
|
6
|
+
function stop() {
|
|
7
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
8
|
+
timer = void 0;
|
|
9
|
+
}
|
|
10
|
+
/** Starts (or restarts) the timer, for `delay` ms if given, else the default. */
|
|
11
|
+
function start(delay = ms) {
|
|
12
|
+
stop();
|
|
13
|
+
timer = setTimeout(() => {
|
|
14
|
+
timer = void 0;
|
|
15
|
+
callback();
|
|
16
|
+
}, delay);
|
|
17
|
+
}
|
|
18
|
+
onScopeDispose(stop, true);
|
|
19
|
+
return {
|
|
20
|
+
start,
|
|
21
|
+
stop
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
//#endregion
|
|
25
|
+
export { useTimeoutFn };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { InjectionKey } from "vue";
|
|
2
|
+
//#region src/composables/use-accordion.d.ts
|
|
3
|
+
export interface AccordionContext {
|
|
4
|
+
isOpen: (value: string) => boolean;
|
|
5
|
+
toggle: (value: string) => void;
|
|
6
|
+
}
|
|
7
|
+
export declare const ACCORDION_KEY: InjectionKey<AccordionContext>;
|
|
8
|
+
export declare function useAccordionItem(): AccordionContext;
|
|
9
|
+
//#endregion
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { inject } from "vue";
|
|
2
|
+
//#region src/composables/use-accordion.ts
|
|
3
|
+
const ACCORDION_KEY = Symbol("humanonlyweb-ui-accordion");
|
|
4
|
+
function useAccordionItem() {
|
|
5
|
+
const context = inject(ACCORDION_KEY, null);
|
|
6
|
+
if (!context) throw new Error("[@humanonlyweb/ui] <AccordionItem> must be inside <Accordion>.");
|
|
7
|
+
return context;
|
|
8
|
+
}
|
|
9
|
+
//#endregion
|
|
10
|
+
export { ACCORDION_KEY, useAccordionItem };
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { SelectOption, SelectValue } from "../types.js";
|
|
2
|
+
import { Ref } from "vue";
|
|
3
|
+
//#region src/composables/use-combobox.d.ts
|
|
4
|
+
export type ComboboxFilter<T extends SelectValue> = (option: SelectOption<T>, query: string) => boolean;
|
|
5
|
+
interface UseComboboxOptions<T extends SelectValue> {
|
|
6
|
+
options: () => SelectOption<T>[];
|
|
7
|
+
model: Ref<T | T[] | undefined>;
|
|
8
|
+
query: Ref<string>;
|
|
9
|
+
multiple: () => boolean;
|
|
10
|
+
disabled: () => boolean;
|
|
11
|
+
/** Custom match; without one, labels are ranked exact, prefix, then substring. */
|
|
12
|
+
filter: () => ComboboxFilter<T> | undefined;
|
|
13
|
+
/** Show `options` as given, e.g. when they come from a search request. */
|
|
14
|
+
ignoreFilter: () => boolean;
|
|
15
|
+
creatable: () => boolean;
|
|
16
|
+
onCreate: (query: string) => void;
|
|
17
|
+
baseId: () => string;
|
|
18
|
+
input: Readonly<Ref<HTMLInputElement | null>>;
|
|
19
|
+
control: Readonly<Ref<HTMLElement | null>>;
|
|
20
|
+
listbox: Readonly<Ref<HTMLElement | null>>;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* APG editable combobox with list autocomplete: typing filters, focus stays in the
|
|
24
|
+
* input, and the active option is conveyed with `aria-activedescendant`.
|
|
25
|
+
*/
|
|
26
|
+
export declare function useCombobox<T extends SelectValue>({ options, model, query, multiple, disabled, filter, ignoreFilter, creatable, onCreate, baseId, input, control, listbox }: UseComboboxOptions<T>): {
|
|
27
|
+
isOpen: Ref<boolean, boolean>;
|
|
28
|
+
activeIndex: Ref<number, number>;
|
|
29
|
+
activeId: import("vue").ComputedRef<string | undefined>;
|
|
30
|
+
optionId: (index: number) => string;
|
|
31
|
+
results: import("vue").ComputedRef<SelectOption<T>[]>;
|
|
32
|
+
selectedOptions: import("vue").ComputedRef<SelectOption<T>[]>;
|
|
33
|
+
isSelected: (value: T) => boolean;
|
|
34
|
+
canCreate: import("vue").ComputedRef<boolean>;
|
|
35
|
+
open: () => void;
|
|
36
|
+
close: () => void;
|
|
37
|
+
setActive: (index: number) => void;
|
|
38
|
+
selectOption: (option: SelectOption<T>) => void;
|
|
39
|
+
remove: (value: T) => void;
|
|
40
|
+
onInput: () => void;
|
|
41
|
+
onKeydown: (event: KeyboardEvent) => void;
|
|
42
|
+
};
|
|
43
|
+
//#endregion
|
|
44
|
+
export {};
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { onClickOutside } from "./internal/on-click-outside.js";
|
|
2
|
+
import { computed, nextTick, ref, watch } from "vue";
|
|
3
|
+
//#region src/composables/use-combobox.ts
|
|
4
|
+
function rank(label, query) {
|
|
5
|
+
const text = label.toLowerCase();
|
|
6
|
+
if (text === query) return 0;
|
|
7
|
+
if (text.startsWith(query)) return 1;
|
|
8
|
+
if (text.includes(query)) return 2;
|
|
9
|
+
return -1;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* APG editable combobox with list autocomplete: typing filters, focus stays in the
|
|
13
|
+
* input, and the active option is conveyed with `aria-activedescendant`.
|
|
14
|
+
*/
|
|
15
|
+
function useCombobox({ options, model, query, multiple, disabled, filter, ignoreFilter, creatable, onCreate, baseId, input, control, listbox }) {
|
|
16
|
+
const isOpen = ref(false);
|
|
17
|
+
const activeIndex = ref(-1);
|
|
18
|
+
const selectedValues = computed(() => {
|
|
19
|
+
const value = model.value;
|
|
20
|
+
if (multiple()) return Array.isArray(value) ? value : [];
|
|
21
|
+
if (value == null || Array.isArray(value)) return [];
|
|
22
|
+
return [value];
|
|
23
|
+
});
|
|
24
|
+
const isSelected = (value) => selectedValues.value.includes(value);
|
|
25
|
+
const selectedOptions = computed(() => selectedValues.value.map((value) => options().find((o) => o.value === value)).filter((o) => Boolean(o)));
|
|
26
|
+
const selectedLabel = () => multiple() ? "" : selectedOptions.value[0]?.label ?? "";
|
|
27
|
+
const searchTerm = computed(() => {
|
|
28
|
+
const term = query.value.trim().toLowerCase();
|
|
29
|
+
return !multiple() && query.value === selectedLabel() ? "" : term;
|
|
30
|
+
});
|
|
31
|
+
const results = computed(() => {
|
|
32
|
+
const term = searchTerm.value;
|
|
33
|
+
const custom = filter();
|
|
34
|
+
if (!term || ignoreFilter()) return options();
|
|
35
|
+
if (custom) return options().filter((option) => custom(option, term));
|
|
36
|
+
return options().map((option) => ({
|
|
37
|
+
option,
|
|
38
|
+
score: rank(option.label, term)
|
|
39
|
+
})).filter((entry) => entry.score >= 0).toSorted((a, b) => a.score - b.score).map((entry) => entry.option);
|
|
40
|
+
});
|
|
41
|
+
const optionId = (index) => `${baseId()}-opt-${index}`;
|
|
42
|
+
const activeId = computed(() => isOpen.value && activeIndex.value >= 0 && activeIndex.value < results.value.length ? optionId(activeIndex.value) : void 0);
|
|
43
|
+
/** Whether Enter would offer to create the typed value. */
|
|
44
|
+
const canCreate = computed(() => {
|
|
45
|
+
const term = query.value.trim();
|
|
46
|
+
if (!creatable() || !term) return false;
|
|
47
|
+
return !options().some((o) => o.label.toLowerCase() === term.toLowerCase());
|
|
48
|
+
});
|
|
49
|
+
function enabledFrom(start, dir) {
|
|
50
|
+
const list = results.value;
|
|
51
|
+
for (let i = start; i >= 0 && i < list.length; i += dir) if (!list[i]?.disabled) return i;
|
|
52
|
+
return -1;
|
|
53
|
+
}
|
|
54
|
+
async function scrollActiveIntoView() {
|
|
55
|
+
if (activeIndex.value < 0) return;
|
|
56
|
+
await nextTick();
|
|
57
|
+
listbox.value?.querySelector(`#${CSS.escape(optionId(activeIndex.value))}`)?.scrollIntoView({ block: "nearest" });
|
|
58
|
+
}
|
|
59
|
+
function setActive(index) {
|
|
60
|
+
if (index === activeIndex.value) return;
|
|
61
|
+
activeIndex.value = index;
|
|
62
|
+
scrollActiveIntoView();
|
|
63
|
+
}
|
|
64
|
+
function open() {
|
|
65
|
+
if (disabled() || isOpen.value) return;
|
|
66
|
+
isOpen.value = true;
|
|
67
|
+
const selected = results.value.findIndex((o) => isSelected(o.value));
|
|
68
|
+
setActive(selected >= 0 ? selected : enabledFrom(0, 1));
|
|
69
|
+
}
|
|
70
|
+
function close() {
|
|
71
|
+
if (!isOpen.value) return;
|
|
72
|
+
isOpen.value = false;
|
|
73
|
+
activeIndex.value = -1;
|
|
74
|
+
if (!multiple()) query.value = selectedLabel();
|
|
75
|
+
}
|
|
76
|
+
function selectOption(option) {
|
|
77
|
+
if (option.disabled) return;
|
|
78
|
+
if (multiple()) {
|
|
79
|
+
const current = selectedValues.value;
|
|
80
|
+
model.value = current.includes(option.value) ? current.filter((v) => v !== option.value) : [...current, option.value];
|
|
81
|
+
query.value = "";
|
|
82
|
+
input.value?.focus();
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
model.value = option.value;
|
|
86
|
+
query.value = option.label;
|
|
87
|
+
isOpen.value = false;
|
|
88
|
+
activeIndex.value = -1;
|
|
89
|
+
}
|
|
90
|
+
function remove(value) {
|
|
91
|
+
if (!multiple() || disabled()) return;
|
|
92
|
+
model.value = selectedValues.value.filter((v) => v !== value);
|
|
93
|
+
input.value?.focus();
|
|
94
|
+
}
|
|
95
|
+
function move(dir) {
|
|
96
|
+
if (!isOpen.value) {
|
|
97
|
+
open();
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
const length = results.value.length;
|
|
101
|
+
const next = enabledFrom(activeIndex.value < 0 ? dir === 1 ? -1 : length : activeIndex.value + dir, dir);
|
|
102
|
+
setActive(next >= 0 ? next : enabledFrom(dir === 1 ? 0 : length - 1, dir));
|
|
103
|
+
}
|
|
104
|
+
function onInput() {
|
|
105
|
+
if (!isOpen.value) open();
|
|
106
|
+
nextTick(() => setActive(enabledFrom(0, 1)));
|
|
107
|
+
}
|
|
108
|
+
function onKeydown(event) {
|
|
109
|
+
if (disabled()) return;
|
|
110
|
+
switch (event.key) {
|
|
111
|
+
case "ArrowDown":
|
|
112
|
+
event.preventDefault();
|
|
113
|
+
if (event.altKey) open();
|
|
114
|
+
else move(1);
|
|
115
|
+
break;
|
|
116
|
+
case "ArrowUp":
|
|
117
|
+
event.preventDefault();
|
|
118
|
+
move(-1);
|
|
119
|
+
break;
|
|
120
|
+
case "Enter": {
|
|
121
|
+
const option = results.value[activeIndex.value];
|
|
122
|
+
if (isOpen.value && option) {
|
|
123
|
+
event.preventDefault();
|
|
124
|
+
selectOption(option);
|
|
125
|
+
} else if (canCreate.value) {
|
|
126
|
+
event.preventDefault();
|
|
127
|
+
onCreate(query.value.trim());
|
|
128
|
+
if (multiple()) query.value = "";
|
|
129
|
+
}
|
|
130
|
+
break;
|
|
131
|
+
}
|
|
132
|
+
case "Escape":
|
|
133
|
+
if (isOpen.value) {
|
|
134
|
+
event.preventDefault();
|
|
135
|
+
close();
|
|
136
|
+
} else if (query.value) {
|
|
137
|
+
event.preventDefault();
|
|
138
|
+
query.value = "";
|
|
139
|
+
if (!multiple()) model.value = void 0;
|
|
140
|
+
}
|
|
141
|
+
break;
|
|
142
|
+
case "Backspace":
|
|
143
|
+
if (multiple() && !query.value && selectedValues.value.length) remove(selectedValues.value.at(-1));
|
|
144
|
+
break;
|
|
145
|
+
case "Tab": close();
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
watch(() => results.value.length, (length) => {
|
|
149
|
+
if (activeIndex.value >= length) activeIndex.value = enabledFrom(0, 1);
|
|
150
|
+
});
|
|
151
|
+
if (!multiple() && !query.value) query.value = selectedLabel();
|
|
152
|
+
watch(selectedLabel, (label) => {
|
|
153
|
+
if (!multiple() && !isOpen.value) query.value = label;
|
|
154
|
+
});
|
|
155
|
+
watch(isOpen, (opened) => {
|
|
156
|
+
const el = listbox.value;
|
|
157
|
+
if (!el?.isConnected) return;
|
|
158
|
+
const shown = el.matches(":popover-open");
|
|
159
|
+
if (opened && !shown) el.showPopover();
|
|
160
|
+
else if (!opened && shown) el.hidePopover();
|
|
161
|
+
}, { flush: "post" });
|
|
162
|
+
onClickOutside(control, () => close(), {
|
|
163
|
+
ignore: [listbox],
|
|
164
|
+
active: () => isOpen.value
|
|
165
|
+
});
|
|
166
|
+
return {
|
|
167
|
+
isOpen,
|
|
168
|
+
activeIndex,
|
|
169
|
+
activeId,
|
|
170
|
+
optionId,
|
|
171
|
+
results,
|
|
172
|
+
selectedOptions,
|
|
173
|
+
isSelected,
|
|
174
|
+
canCreate,
|
|
175
|
+
open,
|
|
176
|
+
close,
|
|
177
|
+
setActive,
|
|
178
|
+
selectOption,
|
|
179
|
+
remove,
|
|
180
|
+
onInput,
|
|
181
|
+
onKeydown
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
//#endregion
|
|
185
|
+
export { useCombobox };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { MaybeRefOrGetter } from "vue";
|
|
2
|
+
//#region src/composables/use-field.d.ts
|
|
3
|
+
export interface FieldSource {
|
|
4
|
+
id: MaybeRefOrGetter<string | undefined>;
|
|
5
|
+
hint: MaybeRefOrGetter<string | undefined>;
|
|
6
|
+
error: MaybeRefOrGetter<string | boolean | undefined>;
|
|
7
|
+
}
|
|
8
|
+
/** Ids and ARIA wiring shared by every labelled form control. */
|
|
9
|
+
export declare function useField(source: FieldSource): {
|
|
10
|
+
id: import("vue").ComputedRef<string>;
|
|
11
|
+
hintId: import("vue").ComputedRef<string>;
|
|
12
|
+
errorId: import("vue").ComputedRef<string>;
|
|
13
|
+
isError: import("vue").ComputedRef<boolean>;
|
|
14
|
+
errorMessage: import("vue").ComputedRef<string | undefined>;
|
|
15
|
+
describedBy: import("vue").ComputedRef<string | undefined>;
|
|
16
|
+
};
|
|
17
|
+
//#endregion
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { computed, toValue, useId } from "vue";
|
|
2
|
+
//#region src/composables/use-field.ts
|
|
3
|
+
/** Ids and ARIA wiring shared by every labelled form control. */
|
|
4
|
+
function useField(source) {
|
|
5
|
+
const uid = useId();
|
|
6
|
+
const id = computed(() => toValue(source.id) ?? uid);
|
|
7
|
+
const hintId = computed(() => `${id.value}-hint`);
|
|
8
|
+
const errorId = computed(() => `${id.value}-error`);
|
|
9
|
+
const isError = computed(() => Boolean(toValue(source.error)));
|
|
10
|
+
const errorMessage = computed(() => {
|
|
11
|
+
const error = toValue(source.error);
|
|
12
|
+
return typeof error === "string" ? error : void 0;
|
|
13
|
+
});
|
|
14
|
+
return {
|
|
15
|
+
id,
|
|
16
|
+
hintId,
|
|
17
|
+
errorId,
|
|
18
|
+
isError,
|
|
19
|
+
errorMessage,
|
|
20
|
+
describedBy: computed(() => {
|
|
21
|
+
if (errorMessage.value) return errorId.value;
|
|
22
|
+
if (toValue(source.hint)) return hintId.value;
|
|
23
|
+
})
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
//#endregion
|
|
27
|
+
export { useField };
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { StandardSchemaV1 } from "./internal/standard-schema.js";
|
|
2
|
+
import { Ref } from "vue";
|
|
3
|
+
//#region src/composables/use-form.d.ts
|
|
4
|
+
/** Any object shape, including `interface`s (which aren't assignable to `Record`). */
|
|
5
|
+
export type FormValues = object;
|
|
6
|
+
/**
|
|
7
|
+
* Errors keyed by field path: top-level fields by name (`"email"`), nested ones
|
|
8
|
+
* dot-joined (`"address.city"`). Inputs are matched by their `name` attribute.
|
|
9
|
+
*/
|
|
10
|
+
export type FormErrors = Partial<Record<string, string>>;
|
|
11
|
+
type Input<S extends StandardSchemaV1> = StandardSchemaV1.InferInput<S> & FormValues;
|
|
12
|
+
type Output<S extends StandardSchemaV1> = StandardSchemaV1.InferOutput<S>;
|
|
13
|
+
export interface UseFormOptions<S extends StandardSchemaV1> {
|
|
14
|
+
/** Any Standard Schema validator: zod 4, valibot, arktype, … */
|
|
15
|
+
schema: S;
|
|
16
|
+
initialValues: Input<S>;
|
|
17
|
+
onSubmit: (values: Output<S>) => void | Promise<void>;
|
|
18
|
+
/**
|
|
19
|
+
* Turns a thrown submit error into field errors (e.g. a 422 from the API). Return
|
|
20
|
+
* `undefined` for errors that aren't about fields.
|
|
21
|
+
*/
|
|
22
|
+
mapError?: (error: unknown) => FormErrors | undefined;
|
|
23
|
+
/** Called with any error `onSubmit` throws. Without it, non-field errors rethrow. */
|
|
24
|
+
onError?: (error: unknown) => void;
|
|
25
|
+
/** The `<form>` element; enables blur/input tracking and focusing the first invalid field. */
|
|
26
|
+
formRef?: Readonly<Ref<HTMLElement | null>>;
|
|
27
|
+
/** When a field's error becomes visible. Every error shows after the first submit. */
|
|
28
|
+
validateOn?: "blur" | "input" | "submit";
|
|
29
|
+
}
|
|
30
|
+
export declare function useForm<S extends StandardSchemaV1>({ schema, initialValues, onSubmit, mapError, onError, formRef, validateOn }: UseFormOptions<S>): {
|
|
31
|
+
form: import("vue").Reactive<Input<S>>;
|
|
32
|
+
errors: import("vue").ComputedRef<Partial<Record<string, string>>>;
|
|
33
|
+
processing: Ref<boolean, boolean>;
|
|
34
|
+
validating: Ref<boolean, boolean>;
|
|
35
|
+
isValid: import("vue").ComputedRef<boolean>;
|
|
36
|
+
isDirty: import("vue").ComputedRef<boolean>;
|
|
37
|
+
submit: (event?: Event) => Promise<void>;
|
|
38
|
+
reset: () => void;
|
|
39
|
+
touch: (field: string) => void;
|
|
40
|
+
setErrors: (fields: FormErrors) => void;
|
|
41
|
+
setValues: (values: Partial<Input<S>>) => void;
|
|
42
|
+
};
|
|
43
|
+
//#endregion
|
|
44
|
+
export type { StandardSchemaV1 };
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { useEventListener } from "./internal/use-event-listener.js";
|
|
2
|
+
import { isEqual } from "./internal/is-equal.js";
|
|
3
|
+
import { computed, nextTick, reactive, ref, shallowRef, toRaw, watch } from "vue";
|
|
4
|
+
//#region src/composables/use-form.ts
|
|
5
|
+
const clone = (values) => structuredClone(toRaw(values));
|
|
6
|
+
function pathKey(path) {
|
|
7
|
+
if (!path?.length) return void 0;
|
|
8
|
+
return path.map((segment) => String(typeof segment === "object" ? segment.key : segment)).join(".");
|
|
9
|
+
}
|
|
10
|
+
function issuesToErrors(issues) {
|
|
11
|
+
const errors = {};
|
|
12
|
+
for (const issue of issues) {
|
|
13
|
+
const key = pathKey(issue.path);
|
|
14
|
+
if (key && !(key in errors)) errors[key] = issue.message;
|
|
15
|
+
}
|
|
16
|
+
return errors;
|
|
17
|
+
}
|
|
18
|
+
function fieldNameOf(event) {
|
|
19
|
+
const target = event.target;
|
|
20
|
+
if (!(target instanceof HTMLElement)) return void 0;
|
|
21
|
+
if ((target instanceof HTMLInputElement || target instanceof HTMLTextAreaElement || target instanceof HTMLSelectElement || target instanceof HTMLButtonElement) && target.name) return target.name;
|
|
22
|
+
return target.closest("[data-field-name]")?.dataset.fieldName || void 0;
|
|
23
|
+
}
|
|
24
|
+
function useForm({ schema, initialValues, onSubmit, mapError, onError, formRef, validateOn = "blur" }) {
|
|
25
|
+
const baseline = shallowRef(clone(initialValues));
|
|
26
|
+
const form = reactive(clone(initialValues));
|
|
27
|
+
const touched = reactive(/* @__PURE__ */ new Set());
|
|
28
|
+
const schemaErrors = shallowRef({});
|
|
29
|
+
const serverErrors = shallowRef({});
|
|
30
|
+
const processing = ref(false);
|
|
31
|
+
const submitted = ref(false);
|
|
32
|
+
const validating = ref(false);
|
|
33
|
+
let run = 0;
|
|
34
|
+
async function validate() {
|
|
35
|
+
const current = ++run;
|
|
36
|
+
validating.value = true;
|
|
37
|
+
try {
|
|
38
|
+
const result = await schema["~standard"].validate(clone(form));
|
|
39
|
+
if (current === run) schemaErrors.value = result.issues ? issuesToErrors(result.issues) : {};
|
|
40
|
+
return result;
|
|
41
|
+
} finally {
|
|
42
|
+
if (current === run) validating.value = false;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
watch(form, () => void validate(), {
|
|
46
|
+
deep: true,
|
|
47
|
+
immediate: true
|
|
48
|
+
});
|
|
49
|
+
const errors = computed(() => {
|
|
50
|
+
const visible = {};
|
|
51
|
+
for (const [key, message] of Object.entries(schemaErrors.value)) if (submitted.value || touched.has(key)) visible[key] = message;
|
|
52
|
+
return {
|
|
53
|
+
...visible,
|
|
54
|
+
...serverErrors.value
|
|
55
|
+
};
|
|
56
|
+
});
|
|
57
|
+
const isValid = computed(() => Object.keys(schemaErrors.value).length === 0);
|
|
58
|
+
const isDirty = computed(() => !isEqual(form, baseline.value));
|
|
59
|
+
function clearServerError(field) {
|
|
60
|
+
if (!(field in serverErrors.value)) return;
|
|
61
|
+
const { [field]: _removed, ...rest } = serverErrors.value;
|
|
62
|
+
serverErrors.value = rest;
|
|
63
|
+
}
|
|
64
|
+
if (formRef) {
|
|
65
|
+
useEventListener(formRef, "input", (event) => {
|
|
66
|
+
const field = fieldNameOf(event);
|
|
67
|
+
if (!field) return;
|
|
68
|
+
clearServerError(field);
|
|
69
|
+
if (validateOn === "input") touched.add(field);
|
|
70
|
+
});
|
|
71
|
+
if (validateOn === "blur") useEventListener(formRef, "focusout", (event) => {
|
|
72
|
+
const field = fieldNameOf(event);
|
|
73
|
+
if (field) touched.add(field);
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
async function focusFirstError() {
|
|
77
|
+
await nextTick();
|
|
78
|
+
formRef?.value?.querySelector("[aria-invalid=\"true\"]")?.focus();
|
|
79
|
+
}
|
|
80
|
+
function setErrors(fields) {
|
|
81
|
+
serverErrors.value = { ...fields };
|
|
82
|
+
submitted.value = true;
|
|
83
|
+
}
|
|
84
|
+
/** Marks a field touched so its error shows, e.g. for custom controls without a `name`. */
|
|
85
|
+
function touch(field) {
|
|
86
|
+
touched.add(field);
|
|
87
|
+
}
|
|
88
|
+
/** Replaces values and makes them the new baseline for `isDirty` and `reset()`. */
|
|
89
|
+
function setValues(values) {
|
|
90
|
+
Object.assign(form, clone(values));
|
|
91
|
+
baseline.value = clone(form);
|
|
92
|
+
touched.clear();
|
|
93
|
+
serverErrors.value = {};
|
|
94
|
+
submitted.value = false;
|
|
95
|
+
}
|
|
96
|
+
function reset() {
|
|
97
|
+
Object.assign(form, clone(baseline.value));
|
|
98
|
+
touched.clear();
|
|
99
|
+
serverErrors.value = {};
|
|
100
|
+
submitted.value = false;
|
|
101
|
+
processing.value = false;
|
|
102
|
+
}
|
|
103
|
+
let submitting = false;
|
|
104
|
+
async function submit(event) {
|
|
105
|
+
event?.preventDefault();
|
|
106
|
+
if (submitting) return;
|
|
107
|
+
submitting = true;
|
|
108
|
+
submitted.value = true;
|
|
109
|
+
serverErrors.value = {};
|
|
110
|
+
try {
|
|
111
|
+
const result = await validate();
|
|
112
|
+
if (result.issues) {
|
|
113
|
+
await focusFirstError();
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
processing.value = true;
|
|
117
|
+
await onSubmit(result.value);
|
|
118
|
+
} catch (error) {
|
|
119
|
+
const fields = mapError?.(error);
|
|
120
|
+
if (fields) {
|
|
121
|
+
setErrors(fields);
|
|
122
|
+
await focusFirstError();
|
|
123
|
+
}
|
|
124
|
+
if (onError) onError(error);
|
|
125
|
+
else if (!fields) throw error;
|
|
126
|
+
} finally {
|
|
127
|
+
processing.value = false;
|
|
128
|
+
submitting = false;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return {
|
|
132
|
+
form,
|
|
133
|
+
errors,
|
|
134
|
+
processing,
|
|
135
|
+
validating,
|
|
136
|
+
isValid,
|
|
137
|
+
isDirty,
|
|
138
|
+
submit,
|
|
139
|
+
reset,
|
|
140
|
+
touch,
|
|
141
|
+
setErrors,
|
|
142
|
+
setValues
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
//#endregion
|
|
146
|
+
export { useForm };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { MenuEntry, MenuSeparator } from "../types.js";
|
|
2
|
+
import { Ref } from "vue";
|
|
3
|
+
//#region src/composables/use-menu.d.ts
|
|
4
|
+
interface UseMenuOptions {
|
|
5
|
+
trigger: () => HTMLElement | null | undefined;
|
|
6
|
+
panel: Readonly<Ref<HTMLElement | null>>;
|
|
7
|
+
open: Ref<boolean>;
|
|
8
|
+
itemCount: () => number;
|
|
9
|
+
/** Disabled items and separators are skipped. */
|
|
10
|
+
isDisabled: (index: number) => boolean;
|
|
11
|
+
/** For typeahead; separators have none. */
|
|
12
|
+
labelAt: (index: number) => string | undefined;
|
|
13
|
+
onActivate: (index: number) => void;
|
|
14
|
+
}
|
|
15
|
+
export declare const isMenuSeparator: (entry: MenuEntry | undefined) => entry is MenuSeparator;
|
|
16
|
+
/** APG menu button: arrow/Home/End navigation, typeahead, focus moves onto items. */
|
|
17
|
+
export declare function useMenu({ trigger, panel, open, itemCount, isDisabled, labelAt, onActivate }: UseMenuOptions): {
|
|
18
|
+
activeIndex: Ref<number, number>;
|
|
19
|
+
setActive: (index: number) => void;
|
|
20
|
+
activate: (index: number) => void;
|
|
21
|
+
close: (refocus?: boolean) => void;
|
|
22
|
+
onTriggerKeydown: (event: KeyboardEvent) => void;
|
|
23
|
+
onMenuKeydown: (event: KeyboardEvent) => void;
|
|
24
|
+
};
|
|
25
|
+
//#endregion
|
|
26
|
+
export {};
|