@human-kit/ui 1.0.0-beta.8 → 1.0.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/README.md +27 -60
- package/dist/FOCUS_STATE_CONTRACT.md +1 -0
- package/dist/color-picker/README.md +78 -0
- package/dist/color-picker/area/color-picker-area.svelte +195 -0
- package/dist/color-picker/area/color-picker-area.svelte.d.ts +4 -0
- package/dist/color-picker/area-thumb/color-picker-area-thumb.svelte +231 -0
- package/dist/color-picker/area-thumb/color-picker-area-thumb.svelte.d.ts +4 -0
- package/dist/color-picker/channel-field/color-picker-channel-field.svelte +95 -0
- package/dist/color-picker/channel-field/color-picker-channel-field.svelte.d.ts +4 -0
- package/dist/color-picker/eye-dropper/color-picker-eye-dropper.svelte +79 -0
- package/dist/color-picker/eye-dropper/color-picker-eye-dropper.svelte.d.ts +4 -0
- package/dist/color-picker/hex-field/color-picker-hex-field.svelte +105 -0
- package/dist/color-picker/hex-field/color-picker-hex-field.svelte.d.ts +4 -0
- package/dist/color-picker/index.d.ts +16 -0
- package/dist/color-picker/index.js +15 -0
- package/dist/color-picker/index.parts.d.ts +12 -0
- package/dist/color-picker/index.parts.js +12 -0
- package/dist/color-picker/label/color-picker-label.svelte +34 -0
- package/dist/color-picker/label/color-picker-label.svelte.d.ts +4 -0
- package/dist/color-picker/preview/color-picker-preview.svelte +43 -0
- package/dist/color-picker/preview/color-picker-preview.svelte.d.ts +4 -0
- package/dist/color-picker/root/color-picker-root.svelte +365 -0
- package/dist/color-picker/root/color-picker-root.svelte.d.ts +4 -0
- package/dist/color-picker/root/color.d.ts +68 -0
- package/dist/color-picker/root/color.js +266 -0
- package/dist/color-picker/root/context.d.ts +127 -0
- package/dist/color-picker/root/context.js +44 -0
- package/dist/color-picker/slider/color-picker-slider.svelte +188 -0
- package/dist/color-picker/slider/color-picker-slider.svelte.d.ts +4 -0
- package/dist/color-picker/slider-thumb/color-picker-slider-thumb.svelte +208 -0
- package/dist/color-picker/slider-thumb/color-picker-slider-thumb.svelte.d.ts +4 -0
- package/dist/color-picker/swatch/color-picker-swatch.svelte +149 -0
- package/dist/color-picker/swatch/color-picker-swatch.svelte.d.ts +4 -0
- package/dist/color-picker/swatch-list/color-picker-swatch-list.svelte +133 -0
- package/dist/color-picker/swatch-list/color-picker-swatch-list.svelte.d.ts +4 -0
- package/dist/color-picker/types.d.ts +176 -0
- package/dist/color-picker/types.js +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +10 -0
- package/dist/internal/localized-strings.d.ts +176 -0
- package/dist/internal/localized-strings.js +176 -0
- package/dist/pin-input/README.md +55 -0
- package/dist/pin-input/cell/pin-input-cell.svelte +210 -0
- package/dist/pin-input/cell/pin-input-cell.svelte.d.ts +4 -0
- package/dist/pin-input/index.d.ts +6 -0
- package/dist/pin-input/index.js +5 -0
- package/dist/pin-input/index.parts.d.ts +3 -0
- package/dist/pin-input/index.parts.js +3 -0
- package/dist/pin-input/label/pin-input-label.svelte +43 -0
- package/dist/pin-input/label/pin-input-label.svelte.d.ts +4 -0
- package/dist/pin-input/root/context.d.ts +70 -0
- package/dist/pin-input/root/context.js +15 -0
- package/dist/pin-input/root/pin-input-root.svelte +337 -0
- package/dist/pin-input/root/pin-input-root.svelte.d.ts +4 -0
- package/dist/pin-input/root/pin-input-utils.d.ts +18 -0
- package/dist/pin-input/root/pin-input-utils.js +44 -0
- package/dist/pin-input/types.d.ts +84 -0
- package/dist/pin-input/types.js +1 -0
- package/dist/primitives/aria-hide-outside.d.ts +13 -0
- package/dist/primitives/aria-hide-outside.js +7 -1
- package/dist/primitives/focus-trap.d.ts +2 -0
- package/dist/primitives/focus-trap.js +7 -1
- package/dist/primitives/presence.svelte.js +6 -1
- package/dist/rating/README.md +58 -0
- package/dist/rating/index.d.ts +7 -0
- package/dist/rating/index.js +6 -0
- package/dist/rating/index.parts.d.ts +4 -0
- package/dist/rating/index.parts.js +4 -0
- package/dist/rating/item/rating-item.svelte +177 -0
- package/dist/rating/item/rating-item.svelte.d.ts +4 -0
- package/dist/rating/label/rating-label.svelte +42 -0
- package/dist/rating/label/rating-label.svelte.d.ts +4 -0
- package/dist/rating/output/rating-output.svelte +38 -0
- package/dist/rating/output/rating-output.svelte.d.ts +4 -0
- package/dist/rating/root/context.d.ts +87 -0
- package/dist/rating/root/context.js +15 -0
- package/dist/rating/root/rating-root.svelte +521 -0
- package/dist/rating/root/rating-root.svelte.d.ts +4 -0
- package/dist/rating/root/rating-utils.d.ts +28 -0
- package/dist/rating/root/rating-utils.js +53 -0
- package/dist/rating/types.d.ts +124 -0
- package/dist/rating/types.js +1 -0
- package/dist/search-field/README.md +29 -0
- package/dist/search-field/clear/README.md +5 -0
- package/dist/search-field/clear/search-field-clear.svelte +99 -0
- package/dist/search-field/clear/search-field-clear.svelte.d.ts +10 -0
- package/dist/search-field/index.d.ts +15 -0
- package/dist/search-field/index.js +6 -0
- package/dist/search-field/index.parts.d.ts +4 -0
- package/dist/search-field/index.parts.js +4 -0
- package/dist/search-field/input/README.md +5 -0
- package/dist/search-field/input/search-field-input.svelte +158 -0
- package/dist/search-field/input/search-field-input.svelte.d.ts +14 -0
- package/dist/search-field/label/README.md +3 -0
- package/dist/search-field/label/search-field-label.svelte +27 -0
- package/dist/search-field/label/search-field-label.svelte.d.ts +7 -0
- package/dist/search-field/root/README.md +5 -0
- package/dist/search-field/root/context.d.ts +28 -0
- package/dist/search-field/root/context.js +15 -0
- package/dist/search-field/root/search-field-root.svelte +230 -0
- package/dist/search-field/root/search-field-root.svelte.d.ts +33 -0
- package/dist/toast/README.md +75 -0
- package/dist/toast/action/toast-action.svelte +42 -0
- package/dist/toast/action/toast-action.svelte.d.ts +4 -0
- package/dist/toast/close/toast-close.svelte +41 -0
- package/dist/toast/close/toast-close.svelte.d.ts +4 -0
- package/dist/toast/content/toast-content.svelte +24 -0
- package/dist/toast/content/toast-content.svelte.d.ts +4 -0
- package/dist/toast/description/toast-description.svelte +41 -0
- package/dist/toast/description/toast-description.svelte.d.ts +4 -0
- package/dist/toast/index.d.ts +34 -0
- package/dist/toast/index.js +15 -0
- package/dist/toast/index.parts.d.ts +9 -0
- package/dist/toast/index.parts.js +9 -0
- package/dist/toast/positioner/toast-positioner.svelte +75 -0
- package/dist/toast/positioner/toast-positioner.svelte.d.ts +4 -0
- package/dist/toast/provider/context.d.ts +36 -0
- package/dist/toast/provider/context.js +22 -0
- package/dist/toast/provider/toast-manager.svelte.d.ts +113 -0
- package/dist/toast/provider/toast-manager.svelte.js +210 -0
- package/dist/toast/provider/toast-provider.svelte +98 -0
- package/dist/toast/provider/toast-provider.svelte.d.ts +4 -0
- package/dist/toast/root/context.d.ts +18 -0
- package/dist/toast/root/context.js +15 -0
- package/dist/toast/root/toast-root.svelte +296 -0
- package/dist/toast/root/toast-root.svelte.d.ts +4 -0
- package/dist/toast/title/toast-title.svelte +38 -0
- package/dist/toast/title/toast-title.svelte.d.ts +4 -0
- package/dist/toast/types.d.ts +100 -0
- package/dist/toast/types.js +1 -0
- package/dist/toast/viewport/toast-viewport.svelte +389 -0
- package/dist/toast/viewport/toast-viewport.svelte.d.ts +4 -0
- package/dist/tooltip/root/tooltip-root.svelte +16 -11
- package/package.json +26 -1
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { usePinInputContext } from '../root/context';
|
|
3
|
+
import type { PinInputLabelProps } from '../types';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* PinInput.Label — the accessible name of the group.
|
|
7
|
+
*
|
|
8
|
+
* It registers its id with the root, which gives it to the root element as `aria-labelledby`.
|
|
9
|
+
* It renders a `<span>` and not a `<label>`, because a `<label>` names one control and the
|
|
10
|
+
* group holds one input for each character. A press on it puts the focus on the first cell
|
|
11
|
+
* that is open, as a `<label>` would.
|
|
12
|
+
*/
|
|
13
|
+
let {
|
|
14
|
+
children,
|
|
15
|
+
class: className = '',
|
|
16
|
+
id: idProp,
|
|
17
|
+
onclick,
|
|
18
|
+
...restProps
|
|
19
|
+
}: PinInputLabelProps = $props();
|
|
20
|
+
|
|
21
|
+
const ctx = usePinInputContext('PinInput.Label');
|
|
22
|
+
const id = $derived(idProp ?? `${ctx.rootId}-label`);
|
|
23
|
+
|
|
24
|
+
// The returned unregister runs on destroy, and again whenever the id changes.
|
|
25
|
+
$effect(() => ctx.registerLabel(id));
|
|
26
|
+
|
|
27
|
+
function handleClick(event: MouseEvent & { currentTarget: EventTarget & HTMLSpanElement }) {
|
|
28
|
+
onclick?.(event);
|
|
29
|
+
if (event.defaultPrevented || ctx.isDisabled) return;
|
|
30
|
+
ctx.focusCell(ctx.value.length, 'pointer');
|
|
31
|
+
}
|
|
32
|
+
</script>
|
|
33
|
+
|
|
34
|
+
<span
|
|
35
|
+
{...restProps}
|
|
36
|
+
{id}
|
|
37
|
+
class={className}
|
|
38
|
+
data-pin-input-label="true"
|
|
39
|
+
data-disabled={ctx.isDisabled || undefined}
|
|
40
|
+
onclick={handleClick}
|
|
41
|
+
>
|
|
42
|
+
{@render children?.()}
|
|
43
|
+
</span>
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import type { PinInputType } from './pin-input-utils';
|
|
2
|
+
export type { PinInputType };
|
|
3
|
+
/** How a value changed. */
|
|
4
|
+
export type PinInputChangeReason = 'input' | 'paste' | 'delete' | 'clear' | 'form-reset';
|
|
5
|
+
export type PinInputChangeDetails = {
|
|
6
|
+
reason: PinInputChangeReason;
|
|
7
|
+
event?: Event;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Context shared between PinInput.Root and its parts.
|
|
11
|
+
*/
|
|
12
|
+
export type PinInputContext = {
|
|
13
|
+
/** The instance id. Every ARIA id of the parts is made from it. */
|
|
14
|
+
instanceId: string;
|
|
15
|
+
/** The id of the root element. */
|
|
16
|
+
rootId: string;
|
|
17
|
+
/** The id of the `PinInput.Label` element, when one is in the DOM. */
|
|
18
|
+
labelId: string | null;
|
|
19
|
+
/** The value. It is never longer than `length`, and it has no holes. */
|
|
20
|
+
value: string;
|
|
21
|
+
/** The count of cells. */
|
|
22
|
+
length: number;
|
|
23
|
+
/** Which characters each cell accepts. */
|
|
24
|
+
type: PinInputType;
|
|
25
|
+
/** Whether the cells hide the characters. */
|
|
26
|
+
mask: boolean;
|
|
27
|
+
/** Whether the cells accept a code from a message. */
|
|
28
|
+
otp: boolean;
|
|
29
|
+
/** The text of an empty cell. */
|
|
30
|
+
placeholder: string | undefined;
|
|
31
|
+
isDisabled: boolean;
|
|
32
|
+
isReadOnly: boolean;
|
|
33
|
+
isRequired: boolean;
|
|
34
|
+
isInvalid: boolean;
|
|
35
|
+
/** The index of the cell that has the focus, or null. */
|
|
36
|
+
focusedIndex: number | null;
|
|
37
|
+
/** Whether the focus in the group must show as keyboard focus. */
|
|
38
|
+
isFocusVisible: boolean;
|
|
39
|
+
/** The `aria-describedby` given to the root. Each cell carries it. */
|
|
40
|
+
ariaDescribedBy: string | undefined;
|
|
41
|
+
/** Registers the id of `PinInput.Label`. Returns the unregister function. */
|
|
42
|
+
registerLabel: (id: string) => () => void;
|
|
43
|
+
/** Registers a cell. Without an index, the cell takes the next free one, in mount order. */
|
|
44
|
+
registerCell: (options: {
|
|
45
|
+
index?: number;
|
|
46
|
+
inputRef: () => HTMLInputElement | null;
|
|
47
|
+
}) => {
|
|
48
|
+
index: number;
|
|
49
|
+
unregister: () => void;
|
|
50
|
+
};
|
|
51
|
+
/** The id of one cell. */
|
|
52
|
+
getCellId: (index: number) => string;
|
|
53
|
+
/** The character of one cell, or an empty text. */
|
|
54
|
+
getCellCharacter: (index: number) => string;
|
|
55
|
+
/** The accessible name of one cell, for example "Digit 2 of 6". */
|
|
56
|
+
getCellLabel: (index: number) => string;
|
|
57
|
+
/** Writes text at one cell. The focus goes to the cell after the last character it wrote. */
|
|
58
|
+
insertText: (index: number, text: string, details: PinInputChangeDetails) => void;
|
|
59
|
+
/** Removes the character of one cell. The characters after it move to the left. */
|
|
60
|
+
deleteCharacter: (index: number, details: PinInputChangeDetails) => void;
|
|
61
|
+
/** Empties the value and puts the focus on the first cell. */
|
|
62
|
+
clear: (event?: Event) => void;
|
|
63
|
+
/** Puts the focus on one cell. The index is clamped to the first empty cell. */
|
|
64
|
+
focusCell: (index: number, modality?: 'keyboard' | 'pointer') => void;
|
|
65
|
+
setCellFocus: (index: number, focused: boolean) => void;
|
|
66
|
+
setFocusVisible: (visible: boolean) => void;
|
|
67
|
+
};
|
|
68
|
+
export declare function setPinInputContext(ctx: PinInputContext): void;
|
|
69
|
+
export declare function getPinInputContext(): PinInputContext | undefined;
|
|
70
|
+
export declare function usePinInputContext(part: string): PinInputContext;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { getContext, setContext } from 'svelte';
|
|
2
|
+
const PIN_INPUT_KEY = Symbol('pin-input');
|
|
3
|
+
export function setPinInputContext(ctx) {
|
|
4
|
+
setContext(PIN_INPUT_KEY, ctx);
|
|
5
|
+
}
|
|
6
|
+
export function getPinInputContext() {
|
|
7
|
+
return getContext(PIN_INPUT_KEY);
|
|
8
|
+
}
|
|
9
|
+
export function usePinInputContext(part) {
|
|
10
|
+
const ctx = getPinInputContext();
|
|
11
|
+
if (!ctx) {
|
|
12
|
+
throw new Error(`${part} must be used inside a PinInput.Root`);
|
|
13
|
+
}
|
|
14
|
+
return ctx;
|
|
15
|
+
}
|
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { untrack } from 'svelte';
|
|
3
|
+
import { SvelteMap } from 'svelte/reactivity';
|
|
4
|
+
import { readable } from 'svelte/store';
|
|
5
|
+
import { dev } from '../../internal/environment';
|
|
6
|
+
import { resolveLocalizedString } from '../../internal/localized-strings';
|
|
7
|
+
import { getLocaleContext } from '../../locale-provider/context';
|
|
8
|
+
import { focusWithModality } from '../../primitives/input-modality';
|
|
9
|
+
import type { PinInputRootProps } from '../types';
|
|
10
|
+
import { setPinInputContext, type PinInputChangeDetails, type PinInputContext } from './context';
|
|
11
|
+
import {
|
|
12
|
+
clampPinInputIndex,
|
|
13
|
+
deletePinInputCharacter,
|
|
14
|
+
filterPinInputText,
|
|
15
|
+
getPinInputPattern,
|
|
16
|
+
insertPinInputText
|
|
17
|
+
} from './pin-input-utils';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* PinInput.Root — the value, the cells and the focus between them.
|
|
21
|
+
*
|
|
22
|
+
* The root is a `role="group"` around one `PinInput.Cell` for each character. Each cell is a
|
|
23
|
+
* real input, thus the telephone shows the correct keyboard, the password manager sees a
|
|
24
|
+
* field, and the code of a message fills the cells with one touch. The value has no holes:
|
|
25
|
+
* the characters fill the cells from the first one, and a character that goes away takes the
|
|
26
|
+
* ones after it one cell to the left.
|
|
27
|
+
*/
|
|
28
|
+
const generatedId = $props.id();
|
|
29
|
+
|
|
30
|
+
let {
|
|
31
|
+
id: idProp,
|
|
32
|
+
value = $bindable(),
|
|
33
|
+
defaultValue,
|
|
34
|
+
controlledValue = false,
|
|
35
|
+
onChange,
|
|
36
|
+
onComplete,
|
|
37
|
+
length = 6,
|
|
38
|
+
type = 'numeric',
|
|
39
|
+
pattern,
|
|
40
|
+
otp = false,
|
|
41
|
+
mask = false,
|
|
42
|
+
placeholder,
|
|
43
|
+
blurOnComplete = false,
|
|
44
|
+
disabled = false,
|
|
45
|
+
readonly = false,
|
|
46
|
+
required = false,
|
|
47
|
+
invalid = false,
|
|
48
|
+
name,
|
|
49
|
+
form,
|
|
50
|
+
'aria-label': ariaLabel,
|
|
51
|
+
'aria-labelledby': ariaLabelledBy,
|
|
52
|
+
'aria-describedby': ariaDescribedBy,
|
|
53
|
+
children,
|
|
54
|
+
class: className = '',
|
|
55
|
+
element = $bindable<HTMLDivElement | null>(null),
|
|
56
|
+
context = $bindable(),
|
|
57
|
+
...restProps
|
|
58
|
+
}: PinInputRootProps = $props();
|
|
59
|
+
|
|
60
|
+
const localeStore = getLocaleContext()?.locale ?? readable<string | undefined>(undefined);
|
|
61
|
+
|
|
62
|
+
const instanceId = untrack(() => idProp) ?? generatedId;
|
|
63
|
+
const rootId = `pin-input-${instanceId}`;
|
|
64
|
+
|
|
65
|
+
let rootRef: HTMLDivElement | null = $state(null);
|
|
66
|
+
let hiddenInputRef: HTMLInputElement | null = $state(null);
|
|
67
|
+
let labelId: string | null = $state(null);
|
|
68
|
+
let focusedIndex: number | null = $state(null);
|
|
69
|
+
let focusVisible = $state(false);
|
|
70
|
+
|
|
71
|
+
type CellRegistration = { inputRef: () => HTMLInputElement | null };
|
|
72
|
+
const cells = new SvelteMap<number, CellRegistration>();
|
|
73
|
+
|
|
74
|
+
const resolvedLength = $derived(Math.max(1, Math.floor(length)));
|
|
75
|
+
const resolvedPattern = $derived(getPinInputPattern(type, pattern));
|
|
76
|
+
const resolvedLocale = $derived($localeStore);
|
|
77
|
+
|
|
78
|
+
$effect(() => {
|
|
79
|
+
element = rootRef;
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
// Read one time: the component writes the value back, thus a check that follows the value
|
|
83
|
+
// would report the echo of its own write.
|
|
84
|
+
if (dev && untrack(() => value !== undefined && defaultValue !== undefined)) {
|
|
85
|
+
console.warn(
|
|
86
|
+
'[PinInput]: Both "value" and "defaultValue" are provided. ' +
|
|
87
|
+
'Use "value" for controlled mode or "defaultValue" for uncontrolled mode, not both.'
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// --- Value --------------------------------------------------------------------------------
|
|
92
|
+
|
|
93
|
+
const initialValue = (untrack(() => value) ?? untrack(() => defaultValue) ?? '').slice(
|
|
94
|
+
0,
|
|
95
|
+
untrack(() => Math.max(1, Math.floor(length)))
|
|
96
|
+
);
|
|
97
|
+
let valueInternal = $state(initialValue);
|
|
98
|
+
|
|
99
|
+
// Controlled-ness is opt-in, not inferred: `bind:value` and `value={...}` are the same thing
|
|
100
|
+
// at runtime. The prop wins whenever the consumer supplies it.
|
|
101
|
+
const currentValue = $derived(
|
|
102
|
+
(controlledValue || value !== undefined ? (value ?? valueInternal) : valueInternal).slice(
|
|
103
|
+
0,
|
|
104
|
+
resolvedLength
|
|
105
|
+
)
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
// A `bind:value` that starts undefined gets the value at mount, thus the parent reads the
|
|
109
|
+
// default without a first change.
|
|
110
|
+
$effect(() => {
|
|
111
|
+
if (controlledValue) return;
|
|
112
|
+
untrack(() => {
|
|
113
|
+
if (value === undefined) value = valueInternal;
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
function publish(next: string, details: PinInputChangeDetails) {
|
|
118
|
+
if (next === currentValue) return;
|
|
119
|
+
if (!controlledValue) {
|
|
120
|
+
valueInternal = next;
|
|
121
|
+
value = next;
|
|
122
|
+
}
|
|
123
|
+
onChange?.(next, details);
|
|
124
|
+
if (next.length === resolvedLength) onComplete?.(next);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function insertText(index: number, text: string, details: PinInputChangeDetails) {
|
|
128
|
+
if (disabled || readonly) return;
|
|
129
|
+
const characters = filterPinInputText(text, resolvedPattern);
|
|
130
|
+
if (!characters) return;
|
|
131
|
+
|
|
132
|
+
const at = Math.min(Math.max(index, 0), currentValue.length);
|
|
133
|
+
const next = insertPinInputText(currentValue, at, characters, resolvedLength);
|
|
134
|
+
publish(next, details);
|
|
135
|
+
focusCell(at + characters.length);
|
|
136
|
+
// The focus goes off after it moves: the move above would take it back.
|
|
137
|
+
if (blurOnComplete && next.length === resolvedLength) {
|
|
138
|
+
cells
|
|
139
|
+
.get(resolvedLength - 1)
|
|
140
|
+
?.inputRef()
|
|
141
|
+
?.blur();
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function deleteCharacter(index: number, details: PinInputChangeDetails) {
|
|
146
|
+
if (disabled || readonly) return;
|
|
147
|
+
publish(deletePinInputCharacter(currentValue, index), details);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function clear(event?: Event) {
|
|
151
|
+
if (disabled || readonly) return;
|
|
152
|
+
publish('', { reason: 'clear', event });
|
|
153
|
+
focusCell(0);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// --- Focus --------------------------------------------------------------------------------
|
|
157
|
+
|
|
158
|
+
function focusCell(index: number, modality: 'keyboard' | 'pointer' = 'keyboard') {
|
|
159
|
+
if (disabled) return;
|
|
160
|
+
const target = clampPinInputIndex(index, currentValue, resolvedLength);
|
|
161
|
+
const input = cells.get(target)?.inputRef();
|
|
162
|
+
if (!input) return;
|
|
163
|
+
if (document.activeElement === input) {
|
|
164
|
+
input.select();
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
focusWithModality(input, modality);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
$effect(() => {
|
|
171
|
+
if (!disabled) return;
|
|
172
|
+
focusedIndex = null;
|
|
173
|
+
focusVisible = false;
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
// --- Form reset ---------------------------------------------------------------------------
|
|
177
|
+
|
|
178
|
+
$effect(() => {
|
|
179
|
+
const formElement = hiddenInputRef?.form;
|
|
180
|
+
if (!formElement) return;
|
|
181
|
+
|
|
182
|
+
const handleFormReset = () => {
|
|
183
|
+
// The browser resets the native inputs after the `reset` event; re-sync afterwards.
|
|
184
|
+
queueMicrotask(() => {
|
|
185
|
+
if (currentValue === initialValue) return;
|
|
186
|
+
if (!controlledValue) {
|
|
187
|
+
valueInternal = initialValue;
|
|
188
|
+
value = initialValue;
|
|
189
|
+
}
|
|
190
|
+
onChange?.(initialValue, { reason: 'form-reset' });
|
|
191
|
+
});
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
formElement.addEventListener('reset', handleFormReset);
|
|
195
|
+
return () => formElement.removeEventListener('reset', handleFormReset);
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
// --- Context ------------------------------------------------------------------------------
|
|
199
|
+
|
|
200
|
+
const pinInputContext: PinInputContext = {
|
|
201
|
+
instanceId,
|
|
202
|
+
rootId,
|
|
203
|
+
get labelId() {
|
|
204
|
+
return labelId;
|
|
205
|
+
},
|
|
206
|
+
get value() {
|
|
207
|
+
return currentValue;
|
|
208
|
+
},
|
|
209
|
+
get length() {
|
|
210
|
+
return resolvedLength;
|
|
211
|
+
},
|
|
212
|
+
get type() {
|
|
213
|
+
return type;
|
|
214
|
+
},
|
|
215
|
+
get mask() {
|
|
216
|
+
return mask;
|
|
217
|
+
},
|
|
218
|
+
get otp() {
|
|
219
|
+
return otp;
|
|
220
|
+
},
|
|
221
|
+
get placeholder() {
|
|
222
|
+
return placeholder;
|
|
223
|
+
},
|
|
224
|
+
get isDisabled() {
|
|
225
|
+
return disabled;
|
|
226
|
+
},
|
|
227
|
+
get isReadOnly() {
|
|
228
|
+
return readonly;
|
|
229
|
+
},
|
|
230
|
+
get isRequired() {
|
|
231
|
+
return required;
|
|
232
|
+
},
|
|
233
|
+
get isInvalid() {
|
|
234
|
+
return invalid;
|
|
235
|
+
},
|
|
236
|
+
get focusedIndex() {
|
|
237
|
+
return focusedIndex;
|
|
238
|
+
},
|
|
239
|
+
get isFocusVisible() {
|
|
240
|
+
return focusVisible;
|
|
241
|
+
},
|
|
242
|
+
get ariaDescribedBy() {
|
|
243
|
+
return ariaDescribedBy;
|
|
244
|
+
},
|
|
245
|
+
registerLabel(id) {
|
|
246
|
+
labelId = id;
|
|
247
|
+
return () => {
|
|
248
|
+
if (labelId === id) labelId = null;
|
|
249
|
+
};
|
|
250
|
+
},
|
|
251
|
+
registerCell(options) {
|
|
252
|
+
let index = options.index;
|
|
253
|
+
if (index === undefined) {
|
|
254
|
+
index = 0;
|
|
255
|
+
while (cells.has(index)) index += 1;
|
|
256
|
+
}
|
|
257
|
+
cells.set(index, { inputRef: options.inputRef });
|
|
258
|
+
const registeredIndex = index;
|
|
259
|
+
return {
|
|
260
|
+
index: registeredIndex,
|
|
261
|
+
unregister: () => {
|
|
262
|
+
if (cells.get(registeredIndex)?.inputRef === options.inputRef) {
|
|
263
|
+
cells.delete(registeredIndex);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
};
|
|
267
|
+
},
|
|
268
|
+
getCellId(index) {
|
|
269
|
+
return `${rootId}-cell-${index}`;
|
|
270
|
+
},
|
|
271
|
+
getCellCharacter(index) {
|
|
272
|
+
return currentValue[index] ?? '';
|
|
273
|
+
},
|
|
274
|
+
getCellLabel(index) {
|
|
275
|
+
const key = type === 'numeric' ? 'pinInput.digitLabel' : 'pinInput.characterLabel';
|
|
276
|
+
return resolveLocalizedString(resolvedLocale, key, {
|
|
277
|
+
index: index + 1,
|
|
278
|
+
count: resolvedLength
|
|
279
|
+
});
|
|
280
|
+
},
|
|
281
|
+
insertText,
|
|
282
|
+
deleteCharacter,
|
|
283
|
+
clear,
|
|
284
|
+
focusCell,
|
|
285
|
+
setCellFocus(index, focused) {
|
|
286
|
+
if (focused) {
|
|
287
|
+
focusedIndex = index;
|
|
288
|
+
} else if (focusedIndex === index) {
|
|
289
|
+
focusedIndex = null;
|
|
290
|
+
focusVisible = false;
|
|
291
|
+
}
|
|
292
|
+
},
|
|
293
|
+
setFocusVisible(visible) {
|
|
294
|
+
focusVisible = visible;
|
|
295
|
+
}
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
setPinInputContext(pinInputContext);
|
|
299
|
+
context = pinInputContext;
|
|
300
|
+
|
|
301
|
+
const defaultLabel = $derived(
|
|
302
|
+
otp ? resolveLocalizedString(resolvedLocale, 'pinInput.label') : undefined
|
|
303
|
+
);
|
|
304
|
+
const labelledBy = $derived(ariaLabelledBy ?? labelId ?? undefined);
|
|
305
|
+
</script>
|
|
306
|
+
|
|
307
|
+
<div
|
|
308
|
+
{...restProps}
|
|
309
|
+
bind:this={rootRef}
|
|
310
|
+
id={rootId}
|
|
311
|
+
role="group"
|
|
312
|
+
aria-labelledby={labelledBy}
|
|
313
|
+
aria-label={labelledBy ? undefined : (ariaLabel ?? defaultLabel)}
|
|
314
|
+
aria-describedby={ariaDescribedBy}
|
|
315
|
+
aria-disabled={disabled || undefined}
|
|
316
|
+
class={className}
|
|
317
|
+
data-pin-input-root="true"
|
|
318
|
+
data-complete={currentValue.length === resolvedLength || undefined}
|
|
319
|
+
data-disabled={disabled || undefined}
|
|
320
|
+
data-readonly={readonly || undefined}
|
|
321
|
+
data-required={required || undefined}
|
|
322
|
+
data-invalid={invalid || undefined}
|
|
323
|
+
data-focus-within={focusedIndex !== null || undefined}
|
|
324
|
+
data-focus-visible={(focusedIndex !== null && focusVisible) || undefined}
|
|
325
|
+
>
|
|
326
|
+
{#if name}
|
|
327
|
+
<input
|
|
328
|
+
bind:this={hiddenInputRef}
|
|
329
|
+
type="hidden"
|
|
330
|
+
{name}
|
|
331
|
+
{form}
|
|
332
|
+
value={currentValue}
|
|
333
|
+
data-pin-input-value="true"
|
|
334
|
+
/>
|
|
335
|
+
{/if}
|
|
336
|
+
{@render children?.()}
|
|
337
|
+
</div>
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export type PinInputType = 'numeric' | 'alphanumeric' | 'alphabetic';
|
|
2
|
+
/** The test one character must pass. A pattern of the consumer replaces the one of the type. */
|
|
3
|
+
export declare function getPinInputPattern(type: PinInputType, pattern?: RegExp): RegExp;
|
|
4
|
+
/** The characters of a text that pass the test, in order. */
|
|
5
|
+
export declare function filterPinInputText(text: string, pattern: RegExp): string;
|
|
6
|
+
/**
|
|
7
|
+
* Writes text into the value at one position. The value has no holes: a write past the end goes
|
|
8
|
+
* to the end, and the text after the position moves to the right. The result is not longer than
|
|
9
|
+
* the length.
|
|
10
|
+
*/
|
|
11
|
+
export declare function insertPinInputText(value: string, index: number, text: string, length: number): string;
|
|
12
|
+
/** Removes one character. The characters after it move to the left. */
|
|
13
|
+
export declare function deletePinInputCharacter(value: string, index: number): string;
|
|
14
|
+
/**
|
|
15
|
+
* The cell the focus goes to. The value has no holes, thus the first empty cell is the one after
|
|
16
|
+
* the last character. A full value keeps the focus on the last cell.
|
|
17
|
+
*/
|
|
18
|
+
export declare function clampPinInputIndex(index: number, value: string, length: number): number;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
const PATTERNS = {
|
|
2
|
+
numeric: /[0-9]/,
|
|
3
|
+
alphanumeric: /[a-zA-Z0-9]/,
|
|
4
|
+
alphabetic: /[a-zA-Z]/
|
|
5
|
+
};
|
|
6
|
+
/** The test one character must pass. A pattern of the consumer replaces the one of the type. */
|
|
7
|
+
export function getPinInputPattern(type, pattern) {
|
|
8
|
+
return pattern ?? PATTERNS[type];
|
|
9
|
+
}
|
|
10
|
+
/** The characters of a text that pass the test, in order. */
|
|
11
|
+
export function filterPinInputText(text, pattern) {
|
|
12
|
+
let result = '';
|
|
13
|
+
for (const character of text) {
|
|
14
|
+
// A global pattern holds `lastIndex` between two tests, thus a fresh one each time.
|
|
15
|
+
if (new RegExp(pattern.source, pattern.flags.replace(/[gy]/g, '')).test(character)) {
|
|
16
|
+
result += character;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return result;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Writes text into the value at one position. The value has no holes: a write past the end goes
|
|
23
|
+
* to the end, and the text after the position moves to the right. The result is not longer than
|
|
24
|
+
* the length.
|
|
25
|
+
*/
|
|
26
|
+
export function insertPinInputText(value, index, text, length) {
|
|
27
|
+
const at = Math.min(Math.max(index, 0), value.length);
|
|
28
|
+
const next = value.slice(0, at) + text + value.slice(at + text.length);
|
|
29
|
+
return next.slice(0, length);
|
|
30
|
+
}
|
|
31
|
+
/** Removes one character. The characters after it move to the left. */
|
|
32
|
+
export function deletePinInputCharacter(value, index) {
|
|
33
|
+
if (index < 0 || index >= value.length)
|
|
34
|
+
return value;
|
|
35
|
+
return value.slice(0, index) + value.slice(index + 1);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* The cell the focus goes to. The value has no holes, thus the first empty cell is the one after
|
|
39
|
+
* the last character. A full value keeps the focus on the last cell.
|
|
40
|
+
*/
|
|
41
|
+
export function clampPinInputIndex(index, value, length) {
|
|
42
|
+
const lastOpen = Math.min(value.length, length - 1);
|
|
43
|
+
return Math.min(Math.max(index, 0), lastOpen);
|
|
44
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import type { Snippet } from 'svelte';
|
|
2
|
+
import type { HTMLAttributes, HTMLInputAttributes } from 'svelte/elements';
|
|
3
|
+
import type { PinInputChangeDetails, PinInputContext, PinInputType } from './root/context';
|
|
4
|
+
export type { PinInputChangeDetails, PinInputContext, PinInputType };
|
|
5
|
+
export type PinInputRootProps = {
|
|
6
|
+
/** A stable id, from which the component makes its internal ARIA ids. Give one on a server. */
|
|
7
|
+
id?: string;
|
|
8
|
+
/**
|
|
9
|
+
* The value. It is never longer than `length`, and it has no holes: the characters fill the
|
|
10
|
+
* cells from the first one. You can bind it with `bind:value`.
|
|
11
|
+
*/
|
|
12
|
+
value?: string;
|
|
13
|
+
/** The value at the start, for when you give no `value`. */
|
|
14
|
+
defaultValue?: string;
|
|
15
|
+
/**
|
|
16
|
+
* Give your own code full control of the value. The component stops to write back to `value`,
|
|
17
|
+
* and it reports only through `onChange`. Thus the parent can refuse a change.
|
|
18
|
+
*/
|
|
19
|
+
controlledValue?: boolean;
|
|
20
|
+
/** The component calls it on each change. */
|
|
21
|
+
onChange?: (value: string, details: PinInputChangeDetails) => void;
|
|
22
|
+
/** The component calls it one time, when the last cell takes a character. */
|
|
23
|
+
onComplete?: (value: string) => void;
|
|
24
|
+
/** The count of cells. The default is 6. */
|
|
25
|
+
length?: number;
|
|
26
|
+
/**
|
|
27
|
+
* Which characters the cells accept: only the digits, the letters, or both. The default is
|
|
28
|
+
* `numeric`, which also sets the numeric keyboard on a telephone.
|
|
29
|
+
*/
|
|
30
|
+
type?: PinInputType;
|
|
31
|
+
/** A test of your own for one character. It replaces the test of `type`. */
|
|
32
|
+
pattern?: RegExp;
|
|
33
|
+
/**
|
|
34
|
+
* Marks the cells as a code from a message. The telephone then offers the code of the last
|
|
35
|
+
* message above the keyboard, and one touch fills each cell.
|
|
36
|
+
*/
|
|
37
|
+
otp?: boolean;
|
|
38
|
+
/** Hides the characters, as a password field does. */
|
|
39
|
+
mask?: boolean;
|
|
40
|
+
/** The text of an empty cell. */
|
|
41
|
+
placeholder?: string;
|
|
42
|
+
/** Moves the focus off the last cell when the value is complete. */
|
|
43
|
+
blurOnComplete?: boolean;
|
|
44
|
+
/** Disables each cell. The cells leave the tab order. */
|
|
45
|
+
disabled?: boolean;
|
|
46
|
+
/** Keeps the cells in the tab order, but the value does not change. */
|
|
47
|
+
readonly?: boolean;
|
|
48
|
+
/** Marks the value as necessary. Each cell gets `required`. */
|
|
49
|
+
required?: boolean;
|
|
50
|
+
/** Marks the value as invalid. Each cell gets `aria-invalid`. */
|
|
51
|
+
invalid?: boolean;
|
|
52
|
+
/** The name of the hidden input that carries the whole value in a form. */
|
|
53
|
+
name?: string;
|
|
54
|
+
/** The id of the form that the hidden input belongs to, when the group is outside it. */
|
|
55
|
+
form?: string;
|
|
56
|
+
/** The accessible name of the group, for when there is no `PinInput.Label`. */
|
|
57
|
+
'aria-label'?: string;
|
|
58
|
+
/** The id of the element that gives the group its name, in place of `PinInput.Label`. */
|
|
59
|
+
'aria-labelledby'?: string;
|
|
60
|
+
/** The id of the element that describes the group, for example an error message. */
|
|
61
|
+
'aria-describedby'?: string;
|
|
62
|
+
/** The content: the label and the cells. */
|
|
63
|
+
children?: Snippet;
|
|
64
|
+
/** The CSS class names of the root element. */
|
|
65
|
+
class?: string;
|
|
66
|
+
/** A bindable reference to the root element. */
|
|
67
|
+
element?: HTMLDivElement | null;
|
|
68
|
+
/** A bindable reference to the context, for a composition of your own. */
|
|
69
|
+
context?: PinInputContext;
|
|
70
|
+
} & Omit<HTMLAttributes<HTMLDivElement>, 'class' | 'children' | 'id' | 'role' | 'aria-label' | 'aria-labelledby' | 'aria-describedby' | 'onchange'>;
|
|
71
|
+
export type PinInputLabelProps = Omit<HTMLAttributes<HTMLSpanElement>, 'class'> & {
|
|
72
|
+
/** The CSS class names of the label. */
|
|
73
|
+
class?: string;
|
|
74
|
+
};
|
|
75
|
+
export type PinInputCellProps = Omit<HTMLInputAttributes, 'class' | 'children' | 'value' | 'type' | 'aria-label' | 'maxlength' | 'size'> & {
|
|
76
|
+
/** The index of the cell. Without it, the cells take the indexes in mount order. */
|
|
77
|
+
index?: number;
|
|
78
|
+
/** The name of this cell, for the screen reader. The default is "Digit 2 of 6". */
|
|
79
|
+
'aria-label'?: string;
|
|
80
|
+
/** The CSS class names of the cell. */
|
|
81
|
+
class?: string;
|
|
82
|
+
/** A bindable reference to the input element. */
|
|
83
|
+
element?: HTMLInputElement | null;
|
|
84
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -7,6 +7,19 @@
|
|
|
7
7
|
* hides nodes appended to the body afterwards (portalled content, toasts), with
|
|
8
8
|
* two exemptions (see `EXEMPT_SELECTOR`).
|
|
9
9
|
*/
|
|
10
|
+
/**
|
|
11
|
+
* Live regions are exempt from hiding ENTIRELY (neither `aria-hidden` nor `inert` is applied,
|
|
12
|
+
* and their subtree is left untouched): `aria-hidden` would silence their announcements, and
|
|
13
|
+
* `inert` would block interacting with e.g. a toast's dismiss button. The trade-off — a live
|
|
14
|
+
* region staying reachable behind a modal — is intentional: status/alert content is transient
|
|
15
|
+
* and announcing it is the whole point.
|
|
16
|
+
*/
|
|
17
|
+
/**
|
|
18
|
+
* An element that opts out of the hide, without being a live region itself: a toast viewport,
|
|
19
|
+
* whose toasts are announced by a live region beside it, but whose buttons must stay reachable
|
|
20
|
+
* behind a modal.
|
|
21
|
+
*/
|
|
22
|
+
export declare const HIDE_OUTSIDE_EXEMPT_ATTRIBUTE = "data-hk-hide-outside-exempt";
|
|
10
23
|
interface HideOutsideResult {
|
|
11
24
|
/** Call this to restore the original state */
|
|
12
25
|
restore: () => void;
|
|
@@ -15,7 +15,13 @@ import { TOP_LAYER_SELECTOR } from './click-outside';
|
|
|
15
15
|
* region staying reachable behind a modal — is intentional: status/alert content is transient
|
|
16
16
|
* and announcing it is the whole point.
|
|
17
17
|
*/
|
|
18
|
-
|
|
18
|
+
/**
|
|
19
|
+
* An element that opts out of the hide, without being a live region itself: a toast viewport,
|
|
20
|
+
* whose toasts are announced by a live region beside it, but whose buttons must stay reachable
|
|
21
|
+
* behind a modal.
|
|
22
|
+
*/
|
|
23
|
+
export const HIDE_OUTSIDE_EXEMPT_ATTRIBUTE = 'data-hk-hide-outside-exempt';
|
|
24
|
+
const LIVE_REGION_SELECTOR = `[aria-live], [role="status"], [role="alert"], [role="log"], [${HIDE_OUTSIDE_EXEMPT_ATTRIBUTE}]`;
|
|
19
25
|
/**
|
|
20
26
|
* Elements the MutationObserver must never hide when they appear while a modal is active:
|
|
21
27
|
* - top layers (dialog/popover/menu — the shared `TOP_LAYER_SELECTOR` from click-outside):
|
|
@@ -7,6 +7,8 @@ export type FocusTrapOptions = {
|
|
|
7
7
|
restoreFocus?: boolean;
|
|
8
8
|
initialFocus?: HTMLElement | string | (() => HTMLElement | null | undefined);
|
|
9
9
|
};
|
|
10
|
+
/** The elements in `container` that a `Tab` can reach, in DOM order. */
|
|
11
|
+
export declare function getFocusableElements(container: HTMLElement): HTMLElement[];
|
|
10
12
|
/**
|
|
11
13
|
* Svelte action that traps focus within an element.
|
|
12
14
|
*
|