@barqjs/aria 0.0.0-stage → 0.2.0-next.1
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 +304 -2
- package/package.json +437 -4
- package/src/autocomplete.tsx +581 -0
- package/src/breadcrumbs.tsx +297 -0
- package/src/button.tsx +468 -0
- package/src/calendar.tsx +2316 -0
- package/src/checkbox.tsx +732 -0
- package/src/collections.ts +1059 -0
- package/src/color.ts +2119 -0
- package/src/colorpicker.tsx +2023 -0
- package/src/colorswatchpicker.tsx +240 -0
- package/src/combobox.tsx +3003 -0
- package/src/command.tsx +1203 -0
- package/src/date.ts +674 -0
- package/src/datefield.tsx +2066 -0
- package/src/datepicker.tsx +907 -0
- package/src/dialog.tsx +1077 -0
- package/src/disclosure.tsx +711 -0
- package/src/dnd/clipboard.ts +125 -0
- package/src/dnd/collection.ts +1265 -0
- package/src/dnd/data.ts +409 -0
- package/src/dnd/drag-drop.ts +801 -0
- package/src/dnd/index.ts +133 -0
- package/src/dnd/manager.ts +639 -0
- package/src/dnd/strings.ts +1279 -0
- package/src/dnd/targets.ts +726 -0
- package/src/dnd/types.ts +212 -0
- package/src/dom.ts +703 -0
- package/src/drag-and-drop.tsx +669 -0
- package/src/drawer.ts +674 -0
- package/src/dropzone.tsx +208 -0
- package/src/file-trigger.tsx +103 -0
- package/src/focus.ts +1370 -0
- package/src/form.tsx +94 -0
- package/src/gridlist.tsx +1354 -0
- package/src/group.tsx +146 -0
- package/src/i18n.ts +823 -0
- package/src/index.ts +1672 -0
- package/src/interactions/description.ts +73 -0
- package/src/interactions/flags.ts +35 -0
- package/src/interactions/focus-events.ts +226 -0
- package/src/interactions/focusable.ts +86 -0
- package/src/interactions/hover.ts +213 -0
- package/src/interactions/interact-outside.ts +123 -0
- package/src/interactions/keyboard.ts +298 -0
- package/src/interactions/listeners.ts +78 -0
- package/src/interactions/long-press.ts +160 -0
- package/src/interactions/modality.ts +428 -0
- package/src/interactions/move.ts +257 -0
- package/src/interactions/open-link.ts +226 -0
- package/src/interactions/press.ts +915 -0
- package/src/interactions/prevent-focus.ts +102 -0
- package/src/interactions/scroll-wheel.ts +43 -0
- package/src/interactions/text-selection.ts +77 -0
- package/src/interactions.ts +81 -0
- package/src/label.ts +140 -0
- package/src/link.tsx +477 -0
- package/src/listbox.tsx +1247 -0
- package/src/live.ts +208 -0
- package/src/menu.tsx +2383 -0
- package/src/menubar.tsx +579 -0
- package/src/navigation-tree.tsx +357 -0
- package/src/navigation.ts +735 -0
- package/src/numberfield.tsx +942 -0
- package/src/otp.ts +430 -0
- package/src/overlay-arrow.tsx +157 -0
- package/src/overlays.ts +1549 -0
- package/src/platform.ts +75 -0
- package/src/presence.ts +164 -0
- package/src/preview-trigger.tsx +582 -0
- package/src/progress-bar-context.ts +15 -0
- package/src/questionnaire.tsx +1717 -0
- package/src/radio.tsx +641 -0
- package/src/register-dom.ts +12 -0
- package/src/scroller.ts +1165 -0
- package/src/select.tsx +1745 -0
- package/src/selection-indicator.tsx +74 -0
- package/src/selection.ts +1472 -0
- package/src/shared-element-transition.tsx +316 -0
- package/src/sheet.tsx +1461 -0
- package/src/slider.tsx +1137 -0
- package/src/splitter.ts +1152 -0
- package/src/static-collection.ts +211 -0
- package/src/switch.tsx +212 -0
- package/src/table.tsx +2218 -0
- package/src/tabs.tsx +1127 -0
- package/src/tag.tsx +637 -0
- package/src/textarea.tsx +186 -0
- package/src/textfield.tsx +483 -0
- package/src/toast-components.tsx +589 -0
- package/src/toast.ts +658 -0
- package/src/toggle-button-group.tsx +175 -0
- package/src/toggle-input.tsx +86 -0
- package/src/toggle.ts +384 -0
- package/src/togglegroup.ts +365 -0
- package/src/tokenfield.tsx +1673 -0
- package/src/toolbar.tsx +184 -0
- package/src/tooltip.tsx +674 -0
- package/src/tree.tsx +2075 -0
- package/src/utils.ts +787 -0
- package/src/validation.ts +504 -0
- package/src/virtualizer.tsx +837 -0
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A toggle button group: the component over `togglegroup.ts`.
|
|
3
|
+
*
|
|
4
|
+
* The state and the keyboard are that module's, and so is the one decision
|
|
5
|
+
* that sets this apart from React Aria's component: a `single` group stays a
|
|
6
|
+
* `group` of `aria-pressed` buttons rather than becoming a `radiogroup` of
|
|
7
|
+
* `radio`s, because pressing the pressed item releases it and a radio promises
|
|
8
|
+
* a choice that cannot be undone. `disallowEmptySelection` is the way to make
|
|
9
|
+
* one that cannot be emptied.
|
|
10
|
+
*
|
|
11
|
+
* The items sit in a {@link SharedElementTransition} and offer their pressed
|
|
12
|
+
* state to a {@link SelectionIndicator}, so a pill drawn behind the pressed
|
|
13
|
+
* item slides to the next one.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { cells, type Child, createContext, getContext, omit, provide } from "@barqjs/core";
|
|
17
|
+
import { ref as makeRef, mergeRefs, type RefTarget } from "@barqjs/primitives/refs";
|
|
18
|
+
import { ToggleButton } from "./button.tsx";
|
|
19
|
+
import type { PressEvent } from "./interactions/press.ts";
|
|
20
|
+
import type { Orientation } from "./selection.ts";
|
|
21
|
+
import { SharedElementTransition } from "./shared-element-transition.tsx";
|
|
22
|
+
import {
|
|
23
|
+
toggleGroup,
|
|
24
|
+
toggleGroupItem,
|
|
25
|
+
toggleGroupState,
|
|
26
|
+
type ToggleGroupSelectionMode,
|
|
27
|
+
type ToggleGroupState,
|
|
28
|
+
} from "./togglegroup.ts";
|
|
29
|
+
import { filterDOMProps, fromProps, mergeProps, type StyleProps, styleProps } from "./utils.ts";
|
|
30
|
+
|
|
31
|
+
interface ToggleButtonGroupValue {
|
|
32
|
+
state: ToggleGroupState;
|
|
33
|
+
disallowEmptySelection: () => boolean;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const ToggleButtonGroupContext = createContext<ToggleButtonGroupValue | null>(null);
|
|
37
|
+
|
|
38
|
+
/** The enclosing {@link ToggleButtonGroup}'s state, for an item of the caller's own. */
|
|
39
|
+
export function useToggleButtonGroup(): ToggleGroupState | null {
|
|
40
|
+
return getContext(ToggleButtonGroupContext)?.state ?? null;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface ToggleButtonGroupComponentProps extends StyleProps {
|
|
44
|
+
children?: Child;
|
|
45
|
+
/** @default "single" */
|
|
46
|
+
selectionMode?: ToggleGroupSelectionMode;
|
|
47
|
+
selectedKeys?: readonly string[];
|
|
48
|
+
defaultSelectedKeys?: readonly string[];
|
|
49
|
+
/** Pressing the last pressed item does not release it. */
|
|
50
|
+
disallowEmptySelection?: boolean;
|
|
51
|
+
isDisabled?: boolean;
|
|
52
|
+
/** @default "horizontal" */
|
|
53
|
+
orientation?: Orientation;
|
|
54
|
+
/** One tab stop, with the arrows moving inside. @default true */
|
|
55
|
+
rovingFocus?: boolean;
|
|
56
|
+
/** Whether the arrows wrap from the last item to the first. @default true */
|
|
57
|
+
loop?: boolean;
|
|
58
|
+
"aria-label"?: string;
|
|
59
|
+
"aria-labelledby"?: string;
|
|
60
|
+
"aria-describedby"?: string;
|
|
61
|
+
ref?: RefTarget<HTMLDivElement>;
|
|
62
|
+
onSelectionChange?: (keys: string[]) => void;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* ```tsx
|
|
67
|
+
* <ToggleButtonGroup aria-label="Alignment" defaultSelectedKeys={["left"]}>
|
|
68
|
+
* <ToggleButtonGroupItem id="left">Left</ToggleButtonGroupItem>
|
|
69
|
+
* <ToggleButtonGroupItem id="center">Center</ToggleButtonGroupItem>
|
|
70
|
+
* </ToggleButtonGroup>
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
export function ToggleButtonGroup(props: ToggleButtonGroupComponentProps) {
|
|
74
|
+
const domRef = makeRef<HTMLDivElement>();
|
|
75
|
+
const state = toggleGroupState({
|
|
76
|
+
selectionMode: () => props.selectionMode,
|
|
77
|
+
selectedKeys: () => props.selectedKeys,
|
|
78
|
+
defaultSelectedKeys: () => props.defaultSelectedKeys,
|
|
79
|
+
isDisabled: () => props.isDisabled,
|
|
80
|
+
rovingFocus: () => props.rovingFocus,
|
|
81
|
+
onSelectionChange: (keys) => props.onSelectionChange?.(keys),
|
|
82
|
+
});
|
|
83
|
+
const { groupProps } = toggleGroup(
|
|
84
|
+
{
|
|
85
|
+
ref: domRef,
|
|
86
|
+
orientation: () => props.orientation ?? "horizontal",
|
|
87
|
+
direction: () => (props.dir === "auto" ? undefined : props.dir),
|
|
88
|
+
loop: () => props.loop,
|
|
89
|
+
"aria-label": () => props["aria-label"],
|
|
90
|
+
"aria-labelledby": () => props["aria-labelledby"],
|
|
91
|
+
"aria-describedby": () => props["aria-describedby"],
|
|
92
|
+
},
|
|
93
|
+
state,
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
const value: ToggleButtonGroupValue = {
|
|
97
|
+
state,
|
|
98
|
+
disallowEmptySelection: () => props.disallowEmptySelection === true,
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
const elementProps = mergeProps(
|
|
102
|
+
groupProps,
|
|
103
|
+
filterDOMProps(fromProps(cells(props)), { global: true }),
|
|
104
|
+
styleProps(cells(props)),
|
|
105
|
+
{
|
|
106
|
+
"data-orientation": () => props.orientation ?? "horizontal",
|
|
107
|
+
"data-disabled": state.isDisabled,
|
|
108
|
+
"data-testid": () => props["data-testid"],
|
|
109
|
+
},
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
return (
|
|
113
|
+
<div {...elementProps} ref={mergeRefs(domRef.set, props.ref)}>
|
|
114
|
+
<GroupProvider value={value}>
|
|
115
|
+
<SharedElementTransition>{props.children}</SharedElementTransition>
|
|
116
|
+
</GroupProvider>
|
|
117
|
+
</div>
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function GroupProvider(props: { value: ToggleButtonGroupValue; children?: Child }) {
|
|
122
|
+
return provide(
|
|
123
|
+
ToggleButtonGroupContext,
|
|
124
|
+
() => props.value,
|
|
125
|
+
() => props.children as unknown,
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export interface ToggleButtonGroupItemComponentProps extends StyleProps {
|
|
130
|
+
/** What this item contributes to the group's `selectedKeys`. */
|
|
131
|
+
id: string;
|
|
132
|
+
children?: Child;
|
|
133
|
+
isDisabled?: boolean;
|
|
134
|
+
autoFocus?: boolean;
|
|
135
|
+
"aria-label"?: string;
|
|
136
|
+
"aria-labelledby"?: string;
|
|
137
|
+
"aria-describedby"?: string;
|
|
138
|
+
ref?: RefTarget<HTMLButtonElement>;
|
|
139
|
+
onPress?: (event: PressEvent) => void;
|
|
140
|
+
onPressStart?: (event: PressEvent) => void;
|
|
141
|
+
onPressEnd?: (event: PressEvent) => void;
|
|
142
|
+
onPressChange?: (isPressed: boolean) => void;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** One item: a {@link ToggleButton} whose pressed state is the group's. */
|
|
146
|
+
export function ToggleButtonGroupItem(props: ToggleButtonGroupItemComponentProps) {
|
|
147
|
+
const group = getContext(ToggleButtonGroupContext);
|
|
148
|
+
if (group === null || group === undefined) {
|
|
149
|
+
throw new Error("A ToggleButtonGroupItem must be rendered inside a ToggleButtonGroup.");
|
|
150
|
+
}
|
|
151
|
+
const domRef = makeRef<HTMLButtonElement>();
|
|
152
|
+
const item = toggleGroupItem(
|
|
153
|
+
{ value: () => props.id, isDisabled: () => props.isDisabled, ref: domRef },
|
|
154
|
+
group.state,
|
|
155
|
+
);
|
|
156
|
+
|
|
157
|
+
const onChange = (next: boolean): void => {
|
|
158
|
+
// Releasing the only pressed item would leave the group empty.
|
|
159
|
+
if (!next && group.disallowEmptySelection() && group.state.selectedKeys().length <= 1) return;
|
|
160
|
+
item.onChange(next);
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
return (
|
|
164
|
+
<ToggleButton
|
|
165
|
+
{...omit(props, "id", "children", "isDisabled", "ref")}
|
|
166
|
+
isSelected={item.isSelected()}
|
|
167
|
+
isDisabled={item.isDisabled()}
|
|
168
|
+
excludeFromTabOrder={item.excludeFromTabOrder()}
|
|
169
|
+
onChange={onChange}
|
|
170
|
+
ref={mergeRefs(domRef.set, props.ref)}
|
|
171
|
+
>
|
|
172
|
+
{props.children}
|
|
173
|
+
</ToggleButton>
|
|
174
|
+
);
|
|
175
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import type { Accessor, Child, Incoming } from "@barqjs/core";
|
|
2
|
+
import { mergeRefs, type Ref, type RefTarget } from "@barqjs/primitives/refs";
|
|
3
|
+
|
|
4
|
+
import { HIDDEN_INPUT_STYLE, pressesInput, type ToggleElementType } from "./toggle.ts";
|
|
5
|
+
import { access, type DOMProps, mergeProps } from "./utils.ts";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A toggle's own element with its hidden input inside, the children after it.
|
|
9
|
+
* A `<span>` forwards a press on itself to the input, which a `<label>` does
|
|
10
|
+
* without being asked.
|
|
11
|
+
*/
|
|
12
|
+
export function toggleInput(parts: {
|
|
13
|
+
elementType: ToggleElementType;
|
|
14
|
+
outerProps: DOMProps;
|
|
15
|
+
inputProps: DOMProps;
|
|
16
|
+
focusProps: DOMProps;
|
|
17
|
+
inputRef: Ref<HTMLInputElement>;
|
|
18
|
+
ref: RefTarget<HTMLInputElement> | undefined;
|
|
19
|
+
children: Incoming<{ children?: Child }>["children"];
|
|
20
|
+
isDisabled: Accessor<boolean>;
|
|
21
|
+
}) {
|
|
22
|
+
const { inputProps, focusProps, inputRef, ref, children } = parts;
|
|
23
|
+
if (parts.elementType === "span") {
|
|
24
|
+
const onClick = pressesInput(() => access(inputRef), parts.isDisabled);
|
|
25
|
+
|
|
26
|
+
return (
|
|
27
|
+
<span {...mergeProps(parts.outerProps, { onClick })}>
|
|
28
|
+
<input
|
|
29
|
+
{...mergeProps(inputProps, focusProps)}
|
|
30
|
+
ref={mergeRefs(inputRef.set, ref)}
|
|
31
|
+
style={HIDDEN_INPUT_STYLE}
|
|
32
|
+
/>
|
|
33
|
+
{children}
|
|
34
|
+
</span>
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return (
|
|
39
|
+
<label {...parts.outerProps}>
|
|
40
|
+
<input
|
|
41
|
+
{...mergeProps(inputProps, focusProps)}
|
|
42
|
+
ref={mergeRefs(inputRef.set, ref)}
|
|
43
|
+
style={HIDDEN_INPUT_STYLE}
|
|
44
|
+
/>
|
|
45
|
+
{children}
|
|
46
|
+
</label>
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function present(value: unknown): boolean {
|
|
51
|
+
return value !== undefined && value !== null && value !== false && value !== "";
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* A toggle with its help text and its error after it.
|
|
56
|
+
*
|
|
57
|
+
* After, not inside: the label's content is the control's name, and text in it
|
|
58
|
+
* would be read once as the name and again as the description. Nothing is
|
|
59
|
+
* rendered for either while it has nothing to say.
|
|
60
|
+
*/
|
|
61
|
+
export function withHelpText(
|
|
62
|
+
control: Child,
|
|
63
|
+
parts: {
|
|
64
|
+
descriptionProps: DOMProps;
|
|
65
|
+
errorMessageProps: DOMProps;
|
|
66
|
+
description: Accessor<unknown>;
|
|
67
|
+
/** What the error says, or nothing while the control is valid. */
|
|
68
|
+
errorMessage: Accessor<unknown>;
|
|
69
|
+
},
|
|
70
|
+
) {
|
|
71
|
+
return (
|
|
72
|
+
<>
|
|
73
|
+
{control}
|
|
74
|
+
{() =>
|
|
75
|
+
present(parts.description()) ? (
|
|
76
|
+
<span {...parts.descriptionProps}>{parts.description() as Child}</span>
|
|
77
|
+
) : null
|
|
78
|
+
}
|
|
79
|
+
{() =>
|
|
80
|
+
present(parts.errorMessage()) ? (
|
|
81
|
+
<span {...parts.errorMessageProps}>{parts.errorMessage() as Child}</span>
|
|
82
|
+
) : null
|
|
83
|
+
}
|
|
84
|
+
</>
|
|
85
|
+
);
|
|
86
|
+
}
|
package/src/toggle.ts
ADDED
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The shared half of a checkbox and a switch.
|
|
3
|
+
*
|
|
4
|
+
* Both are a real `<input type="checkbox">`, hidden but focusable, inside a
|
|
5
|
+
* `<label>` that carries the visual. That is not a stylistic choice: the input
|
|
6
|
+
* is what puts the control in a form's data, what the platform's own keyboard
|
|
7
|
+
* handling activates, what a screen reader announces as a checkbox, and what
|
|
8
|
+
* autofill and password managers can see. A `<div role="checkbox">` has none
|
|
9
|
+
* of it and must reimplement all of it, imperfectly.
|
|
10
|
+
*
|
|
11
|
+
* The press handling is on the LABEL, so the visual is pressed while the
|
|
12
|
+
* pointer is down anywhere on it, and the input's own keyboard activation is
|
|
13
|
+
* left alone.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { type Accessor, createEffect, createSignal } from "@barqjs/core";
|
|
17
|
+
import { ownerDocument } from "./dom.ts";
|
|
18
|
+
import { focusable, type FocusableOptions } from "./interactions/focusable.ts";
|
|
19
|
+
import { press, type ElementRef, type PressEvent } from "./interactions/press.ts";
|
|
20
|
+
import type { ToggleState } from "./button.tsx";
|
|
21
|
+
import {
|
|
22
|
+
fieldValidation,
|
|
23
|
+
formValidation,
|
|
24
|
+
type FormValidationState,
|
|
25
|
+
type ValidateFunction,
|
|
26
|
+
type ValidationBehavior,
|
|
27
|
+
} from "./validation.ts";
|
|
28
|
+
import {
|
|
29
|
+
access,
|
|
30
|
+
filterDOMProps,
|
|
31
|
+
id,
|
|
32
|
+
mergeProps,
|
|
33
|
+
type DOMProps,
|
|
34
|
+
type MaybeAccessor,
|
|
35
|
+
} from "./utils.ts";
|
|
36
|
+
|
|
37
|
+
export { toggleState } from "./button.tsx";
|
|
38
|
+
export type { ToggleState, ToggleStateOptions } from "./button.tsx";
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Return the control to its default when the form is reset.
|
|
42
|
+
*
|
|
43
|
+
* A reset fires no `change` or `input`, so a controlled component that does
|
|
44
|
+
* not listen for it shows a value the form no longer has.
|
|
45
|
+
*/
|
|
46
|
+
export function formReset<T>(
|
|
47
|
+
ref: ElementRef<HTMLInputElement>,
|
|
48
|
+
initial: MaybeAccessor<T>,
|
|
49
|
+
onReset: (value: T) => void,
|
|
50
|
+
): void {
|
|
51
|
+
createEffect(
|
|
52
|
+
() => access(ref) as HTMLInputElement | null,
|
|
53
|
+
(element) => {
|
|
54
|
+
const form = element?.form;
|
|
55
|
+
if (form === null || form === undefined) return undefined;
|
|
56
|
+
|
|
57
|
+
const handle = (event: Event): void => {
|
|
58
|
+
if (!event.defaultPrevented) onReset(access(initial) as T);
|
|
59
|
+
};
|
|
60
|
+
form.addEventListener("reset", handle);
|
|
61
|
+
return () => form.removeEventListener("reset", handle);
|
|
62
|
+
},
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface ToggleOptions extends FocusableOptions {
|
|
67
|
+
isDisabled?: MaybeAccessor<boolean | undefined>;
|
|
68
|
+
isReadOnly?: MaybeAccessor<boolean | undefined>;
|
|
69
|
+
isRequired?: MaybeAccessor<boolean | undefined>;
|
|
70
|
+
isInvalid?: MaybeAccessor<boolean | undefined>;
|
|
71
|
+
/** What the page thinks of the value, checked as it changes. */
|
|
72
|
+
validate?: ValidateFunction<boolean>;
|
|
73
|
+
/** @default "aria" */
|
|
74
|
+
validationBehavior?: MaybeAccessor<ValidationBehavior | undefined>;
|
|
75
|
+
/** The caller's own message, which wins over anything validation found. */
|
|
76
|
+
errorMessage?: MaybeAccessor<unknown>;
|
|
77
|
+
/** Help text for the control, which describes it when there is any. */
|
|
78
|
+
description?: MaybeAccessor<unknown>;
|
|
79
|
+
/**
|
|
80
|
+
* The validity to show, when it belongs to something bigger than this one
|
|
81
|
+
* control: a checkbox in a group is valid or not with its group.
|
|
82
|
+
*/
|
|
83
|
+
validationState?: FormValidationState;
|
|
84
|
+
/** The form value when selected. */
|
|
85
|
+
value?: MaybeAccessor<string | undefined>;
|
|
86
|
+
name?: MaybeAccessor<string | undefined>;
|
|
87
|
+
form?: MaybeAccessor<string | undefined>;
|
|
88
|
+
/** Whether there is a visible label. Without one, `aria-label` is required. */
|
|
89
|
+
children?: MaybeAccessor<unknown>;
|
|
90
|
+
"aria-label"?: MaybeAccessor<string | undefined>;
|
|
91
|
+
"aria-required"?: MaybeAccessor<boolean | undefined>;
|
|
92
|
+
"aria-labelledby"?: MaybeAccessor<string | undefined>;
|
|
93
|
+
"aria-describedby"?: MaybeAccessor<string | undefined>;
|
|
94
|
+
"aria-controls"?: MaybeAccessor<string | undefined>;
|
|
95
|
+
"aria-errormessage"?: MaybeAccessor<string | undefined>;
|
|
96
|
+
onPress?: (event: PressEvent) => void;
|
|
97
|
+
onPressStart?: (event: PressEvent) => void;
|
|
98
|
+
onPressEnd?: (event: PressEvent) => void;
|
|
99
|
+
onPressChange?: (isPressed: boolean) => void;
|
|
100
|
+
onFocus?: (event: FocusEvent) => void;
|
|
101
|
+
onBlur?: (event: FocusEvent) => void;
|
|
102
|
+
onFocusChange?: (isFocused: boolean) => void;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export interface ToggleResult {
|
|
106
|
+
labelProps: DOMProps;
|
|
107
|
+
inputProps: DOMProps;
|
|
108
|
+
/** For the help text, outside the label so it is not read as the name. */
|
|
109
|
+
descriptionProps: DOMProps;
|
|
110
|
+
/** For the error, outside the label for the same reason. */
|
|
111
|
+
errorMessageProps: DOMProps;
|
|
112
|
+
isSelected: Accessor<boolean>;
|
|
113
|
+
isPressed: Accessor<boolean>;
|
|
114
|
+
isDisabled: Accessor<boolean>;
|
|
115
|
+
isReadOnly: Accessor<boolean>;
|
|
116
|
+
/** What is wrong with the value, and whether the user is being told. */
|
|
117
|
+
validation: FormValidationState;
|
|
118
|
+
/** The messages to show, which may be the browser's own. */
|
|
119
|
+
errors: Accessor<string[]>;
|
|
120
|
+
isInvalid: Accessor<boolean>;
|
|
121
|
+
/** For an error element, so `aria-describedby` can point at it. */
|
|
122
|
+
errorMessage: Accessor<unknown>;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export function toggle(
|
|
126
|
+
options: ToggleOptions,
|
|
127
|
+
state: ToggleState,
|
|
128
|
+
ref: ElementRef<HTMLInputElement>,
|
|
129
|
+
): ToggleResult {
|
|
130
|
+
const isDisabled = (): boolean => access(options.isDisabled) === true;
|
|
131
|
+
const isReadOnly = (): boolean => access(options.isReadOnly) === true;
|
|
132
|
+
|
|
133
|
+
// The validation state lives here rather than in `toggleState`: a toggle's
|
|
134
|
+
// VALUE is a boolean either way, and a `<ToggleButton>` built on the same
|
|
135
|
+
// state is not a form field.
|
|
136
|
+
const {
|
|
137
|
+
state: validation,
|
|
138
|
+
behavior,
|
|
139
|
+
isInvalid,
|
|
140
|
+
errors,
|
|
141
|
+
errorMessage,
|
|
142
|
+
} = fieldValidation<boolean>({
|
|
143
|
+
value: state.isSelected,
|
|
144
|
+
validate: options.validate,
|
|
145
|
+
validationBehavior: options.validationBehavior,
|
|
146
|
+
isInvalid: options.isInvalid,
|
|
147
|
+
errorMessage: options.errorMessage,
|
|
148
|
+
name: options.name,
|
|
149
|
+
state: options.validationState,
|
|
150
|
+
});
|
|
151
|
+
const isNative = (): boolean => behavior() === "native";
|
|
152
|
+
const isRequired = (): boolean => access(options.isRequired) === true;
|
|
153
|
+
|
|
154
|
+
const descriptionId = id();
|
|
155
|
+
const errorMessageId = id();
|
|
156
|
+
const describedBy = (): string | undefined =>
|
|
157
|
+
[
|
|
158
|
+
present(access(options.description)) ? descriptionId() : undefined,
|
|
159
|
+
isInvalid() && present(errorMessage()) ? errorMessageId() : undefined,
|
|
160
|
+
access(options["aria-describedby"]),
|
|
161
|
+
]
|
|
162
|
+
.filter((part): part is string => part !== undefined)
|
|
163
|
+
.join(" ") || undefined;
|
|
164
|
+
|
|
165
|
+
formValidation(
|
|
166
|
+
{
|
|
167
|
+
validationBehavior: behavior,
|
|
168
|
+
focus: () => (access(ref) as HTMLInputElement | null)?.focus(),
|
|
169
|
+
},
|
|
170
|
+
validation,
|
|
171
|
+
ref,
|
|
172
|
+
);
|
|
173
|
+
|
|
174
|
+
// For a keyboard activation, and for a caller that puts the input somewhere
|
|
175
|
+
// the label does not cover.
|
|
176
|
+
const { pressProps, isPressed } = press({
|
|
177
|
+
onPress: options.onPress,
|
|
178
|
+
onPressStart: options.onPressStart,
|
|
179
|
+
onPressEnd: options.onPressEnd,
|
|
180
|
+
onPressChange: options.onPressChange,
|
|
181
|
+
isDisabled,
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
const [labelPressed, setLabelPressed] = createSignal(false);
|
|
185
|
+
|
|
186
|
+
// On the label, for the pressed STATE only.
|
|
187
|
+
//
|
|
188
|
+
// The toggle itself is left to the platform: a press on a `<label>` runs the
|
|
189
|
+
// label's activation behaviour, which checks the input, fires `change`, and
|
|
190
|
+
// moves focus to it. Doing it here instead — press, `preventDefault`, toggle
|
|
191
|
+
// by hand — means reimplementing three platform behaviours to arrive at the
|
|
192
|
+
// same place, and getting one of them wrong on any engine that runs the
|
|
193
|
+
// activation at a moment the cancellation cannot reach.
|
|
194
|
+
//
|
|
195
|
+
// A keyboard or virtual press belongs to the INPUT, which the platform also
|
|
196
|
+
// activates, so those are let through untouched.
|
|
197
|
+
const { pressProps: labelPressProps } = press({
|
|
198
|
+
isDisabled: () => isDisabled() || isReadOnly(),
|
|
199
|
+
onPressStart(event) {
|
|
200
|
+
if (event.pointerType === "keyboard" || event.pointerType === "virtual") {
|
|
201
|
+
event.continuePropagation();
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
options.onPressStart?.(event);
|
|
205
|
+
options.onPressChange?.(true);
|
|
206
|
+
setLabelPressed(true);
|
|
207
|
+
},
|
|
208
|
+
onPressEnd(event) {
|
|
209
|
+
if (event.pointerType === "keyboard" || event.pointerType === "virtual") {
|
|
210
|
+
event.continuePropagation();
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
options.onPressEnd?.(event);
|
|
214
|
+
options.onPressChange?.(false);
|
|
215
|
+
setLabelPressed(false);
|
|
216
|
+
},
|
|
217
|
+
onPress(event) {
|
|
218
|
+
if (event.pointerType === "keyboard" || event.pointerType === "virtual") {
|
|
219
|
+
event.continuePropagation();
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
options.onPress?.(event);
|
|
223
|
+
focusInput(ref);
|
|
224
|
+
},
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
const { focusableProps } = focusable(options, ref);
|
|
228
|
+
|
|
229
|
+
formReset(ref, state.isSelected(), (value) => state.setSelected(value));
|
|
230
|
+
|
|
231
|
+
const onChange = (event: Event): void => {
|
|
232
|
+
// The label carries the press props, so a change would be seen twice.
|
|
233
|
+
event.stopPropagation();
|
|
234
|
+
const input = event.target as HTMLInputElement;
|
|
235
|
+
state.setSelected(input.checked);
|
|
236
|
+
|
|
237
|
+
// The input has ALREADY changed itself; the state may not have followed,
|
|
238
|
+
// because the component is controlled and its owner declined, or because
|
|
239
|
+
// it is read-only. Nothing re-renders on a value that did not change, so
|
|
240
|
+
// without this the checkbox shows a state the component does not hold.
|
|
241
|
+
input.checked = state.isSelected();
|
|
242
|
+
validation.commitValidation();
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
return {
|
|
246
|
+
descriptionProps: { id: descriptionId },
|
|
247
|
+
errorMessageProps: { id: errorMessageId },
|
|
248
|
+
// The label's own click is what toggles the input, so it is left alone.
|
|
249
|
+
// `mousedown` is not: without this the label takes focus, and the ring
|
|
250
|
+
// appears around the text rather than the control.
|
|
251
|
+
labelProps: mergeProps(labelPressProps, {
|
|
252
|
+
onMouseDown: (event: MouseEvent) => event.preventDefault(),
|
|
253
|
+
}),
|
|
254
|
+
|
|
255
|
+
inputProps: mergeProps(
|
|
256
|
+
// Its own description first, then the caller's, which is the order they
|
|
257
|
+
// are read in; `describedBy` puts the caller's back at the end.
|
|
258
|
+
filterDOMProps({ ...options, "aria-describedby": undefined }, { labelable: true }),
|
|
259
|
+
pressProps,
|
|
260
|
+
focusableProps,
|
|
261
|
+
{
|
|
262
|
+
type: "checkbox",
|
|
263
|
+
checked: () => state.isSelected(),
|
|
264
|
+
disabled: isDisabled,
|
|
265
|
+
// `required` is the browser's, and the browser refuses the submit for
|
|
266
|
+
// it, so only when the page asked for that. Otherwise the form still
|
|
267
|
+
// submits and assistive technology is told instead.
|
|
268
|
+
required: () => isRequired() && isNative(),
|
|
269
|
+
name: () => access(options.name),
|
|
270
|
+
form: () => access(options.form),
|
|
271
|
+
value: () => access(options.value),
|
|
272
|
+
// The caller's own word wins: a kit whose control states `required`
|
|
273
|
+
// both ways, as Radix's switch does, says so here.
|
|
274
|
+
"aria-required": () =>
|
|
275
|
+
access(options["aria-required"]) ?? ((isRequired() && !isNative()) || undefined),
|
|
276
|
+
"aria-invalid": () => isInvalid() || undefined,
|
|
277
|
+
"aria-describedby": describedBy,
|
|
278
|
+
"aria-errormessage": () => access(options["aria-errormessage"]),
|
|
279
|
+
"aria-controls": () => access(options["aria-controls"]),
|
|
280
|
+
"aria-readonly": () => isReadOnly() || undefined,
|
|
281
|
+
onChange,
|
|
282
|
+
},
|
|
283
|
+
),
|
|
284
|
+
|
|
285
|
+
isSelected: state.isSelected,
|
|
286
|
+
isPressed: () => isPressed() || labelPressed(),
|
|
287
|
+
isDisabled,
|
|
288
|
+
isReadOnly,
|
|
289
|
+
validation,
|
|
290
|
+
errors,
|
|
291
|
+
isInvalid,
|
|
292
|
+
errorMessage,
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
function present(value: unknown): boolean {
|
|
297
|
+
return value !== undefined && value !== null && value !== false && value !== "";
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Focus the input a press on its label was for.
|
|
302
|
+
*
|
|
303
|
+
* WebKit does not focus a checkbox or a radio for a mouse click, its label's
|
|
304
|
+
* included, so without this the press leaves focus wherever it was and the
|
|
305
|
+
* next Space toggles something else.
|
|
306
|
+
*/
|
|
307
|
+
export function focusInput(ref: ElementRef<HTMLInputElement>): void {
|
|
308
|
+
const input = access(ref) as HTMLInputElement | null;
|
|
309
|
+
if (input !== null && input.ownerDocument.activeElement !== input) input.focus();
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/** The element a toggle's visual is drawn on. */
|
|
313
|
+
export type ToggleElementType = "label" | "span";
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* What a toggle's own element is when the caller did not say.
|
|
317
|
+
*
|
|
318
|
+
* A `<label>` when the children are its name. A `<span>` when there are none:
|
|
319
|
+
* an empty label names nothing, and a label may not contain another, so it
|
|
320
|
+
* could not sit inside the `<label>` or `<Field>` that does carry the text.
|
|
321
|
+
*/
|
|
322
|
+
export function toggleElementType(
|
|
323
|
+
asked: ToggleElementType | undefined,
|
|
324
|
+
children: unknown,
|
|
325
|
+
): ToggleElementType {
|
|
326
|
+
return asked ?? (children === undefined ? "span" : "label");
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* The click handler for a toggle drawn on a `<span>`.
|
|
331
|
+
*
|
|
332
|
+
* A `<label>` forwards a press on itself to its input; a `<span>` does not, so
|
|
333
|
+
* this does, unless a label around it is about to. An enclosing label runs its
|
|
334
|
+
* activation for a press anywhere inside it, this element included, and
|
|
335
|
+
* forwarding as well would toggle twice and leave the control where it started.
|
|
336
|
+
*
|
|
337
|
+
* What is handed on is a click with the original's `detail`, not
|
|
338
|
+
* `input.click()`. That one counts no clicks, which is how a screen reader's
|
|
339
|
+
* activation is told from a mouse's: the page would read a mouse press as
|
|
340
|
+
* virtual and draw a focus ring for it.
|
|
341
|
+
*/
|
|
342
|
+
export function pressesInput(
|
|
343
|
+
input: () => HTMLInputElement | null,
|
|
344
|
+
isDisabled: Accessor<boolean>,
|
|
345
|
+
): (event: MouseEvent) => void {
|
|
346
|
+
return (event) => {
|
|
347
|
+
const control = input();
|
|
348
|
+
if (control === null || event.target === control || isDisabled()) return;
|
|
349
|
+
const label = (event.currentTarget as Element).closest("label");
|
|
350
|
+
if (label !== null && label.control === control) return;
|
|
351
|
+
control.dispatchEvent(
|
|
352
|
+
new MouseEvent("click", {
|
|
353
|
+
bubbles: true,
|
|
354
|
+
cancelable: true,
|
|
355
|
+
composed: true,
|
|
356
|
+
detail: event.detail,
|
|
357
|
+
ctrlKey: event.ctrlKey,
|
|
358
|
+
shiftKey: event.shiftKey,
|
|
359
|
+
altKey: event.altKey,
|
|
360
|
+
metaKey: event.metaKey,
|
|
361
|
+
}),
|
|
362
|
+
);
|
|
363
|
+
control.focus();
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/** The style that hides the input without hiding it from assistive technology. */
|
|
368
|
+
export const HIDDEN_INPUT_STYLE = {
|
|
369
|
+
position: "absolute",
|
|
370
|
+
width: "1px",
|
|
371
|
+
height: "1px",
|
|
372
|
+
padding: "0",
|
|
373
|
+
margin: "-1px",
|
|
374
|
+
overflow: "hidden",
|
|
375
|
+
clip: "rect(0 0 0 0)",
|
|
376
|
+
clipPath: "inset(50%)",
|
|
377
|
+
whiteSpace: "nowrap",
|
|
378
|
+
border: "0",
|
|
379
|
+
} as const;
|
|
380
|
+
|
|
381
|
+
/** The document a ref's element belongs to, for a component that needs it. */
|
|
382
|
+
export function documentOf(ref: ElementRef): Document {
|
|
383
|
+
return ownerDocument(access(ref));
|
|
384
|
+
}
|