@barqjs/aria 0.0.0-stage → 0.2.0-next.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 +293 -2
- package/package.json +347 -4
- package/src/breadcrumbs.tsx +296 -0
- package/src/button.tsx +342 -0
- package/src/calendar.tsx +1389 -0
- package/src/checkbox.tsx +570 -0
- package/src/collections.ts +976 -0
- package/src/color.ts +562 -0
- package/src/colorpicker.tsx +1478 -0
- package/src/combobox.tsx +2481 -0
- package/src/command.tsx +1184 -0
- package/src/date.ts +652 -0
- package/src/datefield.tsx +951 -0
- package/src/datepicker.tsx +556 -0
- package/src/dialog.tsx +890 -0
- package/src/disclosure.tsx +659 -0
- package/src/dom.ts +706 -0
- package/src/drawer.ts +674 -0
- package/src/focus.ts +1370 -0
- package/src/form.tsx +94 -0
- package/src/gridlist.tsx +563 -0
- package/src/i18n.ts +823 -0
- package/src/index.ts +1270 -0
- package/src/interactions/description.ts +73 -0
- package/src/interactions/flags.ts +35 -0
- package/src/interactions/focus-events.ts +218 -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 +105 -0
- package/src/interactions/listeners.ts +78 -0
- package/src/interactions/long-press.ts +160 -0
- package/src/interactions/modality.ts +416 -0
- package/src/interactions/move.ts +257 -0
- package/src/interactions/open-link.ts +193 -0
- package/src/interactions/press.ts +898 -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 +128 -0
- package/src/link.tsx +381 -0
- package/src/listbox.tsx +721 -0
- package/src/live.ts +204 -0
- package/src/menu.tsx +2290 -0
- package/src/menubar.tsx +575 -0
- package/src/navigation.ts +735 -0
- package/src/numberfield.tsx +838 -0
- package/src/otp.ts +430 -0
- package/src/overlays.ts +1247 -0
- package/src/platform.ts +74 -0
- package/src/presence.ts +164 -0
- package/src/questionnaire.tsx +1712 -0
- package/src/radio.tsx +567 -0
- package/src/register-dom.ts +12 -0
- package/src/scroller.ts +1165 -0
- package/src/select.tsx +1665 -0
- package/src/selection.ts +1415 -0
- package/src/slider.tsx +1042 -0
- package/src/splitter.ts +1152 -0
- package/src/static-collection.ts +211 -0
- package/src/switch.tsx +186 -0
- package/src/table.tsx +0 -0
- package/src/tabs.tsx +1018 -0
- package/src/tag.tsx +452 -0
- package/src/textfield.tsx +417 -0
- package/src/toast.ts +658 -0
- package/src/toggle-input.tsx +48 -0
- package/src/toggle.ts +321 -0
- package/src/togglegroup.ts +365 -0
- package/src/toolbar.tsx +179 -0
- package/src/tooltip.tsx +657 -0
- package/src/utils.ts +758 -0
- package/src/validation.ts +447 -0
- package/src/virtualizer.tsx +674 -0
package/src/toolbar.tsx
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A toolbar: a row of controls that Tab enters ONCE.
|
|
3
|
+
*
|
|
4
|
+
* Without one, a formatting bar of twelve buttons is twelve Tab stops between
|
|
5
|
+
* the user and the next field. `role="toolbar"` says the arrows move inside it
|
|
6
|
+
* and Tab leaves it, which is the same bargain a listbox or a tab list makes,
|
|
7
|
+
* and it is the reason the role exists.
|
|
8
|
+
*
|
|
9
|
+
* Two things make it work rather than merely claim to:
|
|
10
|
+
*
|
|
11
|
+
* - **Tab leaves from the END.** Focus is moved to the last control before the
|
|
12
|
+
* browser handles the key, so its own Tab continues past the whole toolbar
|
|
13
|
+
* rather than into the middle of it. Shift+Tab moves to the first.
|
|
14
|
+
* - **Coming back lands where you left.** The control that had focus when the
|
|
15
|
+
* toolbar was left is remembered and refocused, so a toolbar is not a place
|
|
16
|
+
* you have to arrow across again every time.
|
|
17
|
+
*
|
|
18
|
+
* A toolbar inside a toolbar takes no arrow keys of its own: the outer one is
|
|
19
|
+
* already handling them, and two handlers on one keypress move focus twice.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import { type Child, createEffect, type Incoming, createSignal } from "@barqjs/core";
|
|
23
|
+
import { isServer } from "@barqjs/web";
|
|
24
|
+
import { ref as makeRef, mergeRefs, type RefTarget } from "@barqjs/primitives/refs";
|
|
25
|
+
import { contains, targetElement } from "./dom.ts";
|
|
26
|
+
import { createFocusManager } from "./focus.ts";
|
|
27
|
+
import { useLocale } from "./i18n.ts";
|
|
28
|
+
import type { ElementRef } from "./interactions/press.ts";
|
|
29
|
+
import type { Orientation } from "./selection.ts";
|
|
30
|
+
import {
|
|
31
|
+
access,
|
|
32
|
+
type DOMProps,
|
|
33
|
+
filterDOMProps,
|
|
34
|
+
fromProps,
|
|
35
|
+
type MaybeAccessor,
|
|
36
|
+
mergeProps,
|
|
37
|
+
type StyleProps,
|
|
38
|
+
styleProps,
|
|
39
|
+
} from "./utils.ts";
|
|
40
|
+
|
|
41
|
+
export interface ToolbarOptions {
|
|
42
|
+
ref: ElementRef;
|
|
43
|
+
/** @default "horizontal" */
|
|
44
|
+
orientation?: MaybeAccessor<Orientation | undefined>;
|
|
45
|
+
"aria-label"?: MaybeAccessor<string | undefined>;
|
|
46
|
+
"aria-labelledby"?: MaybeAccessor<string | undefined>;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface ToolbarResult {
|
|
50
|
+
toolbarProps: DOMProps;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function toolbar(options: ToolbarOptions): ToolbarResult {
|
|
54
|
+
const locale = useLocale();
|
|
55
|
+
const manager = createFocusManager(options.ref);
|
|
56
|
+
const [nested, setNested] = createSignal(false);
|
|
57
|
+
|
|
58
|
+
const orientation = (): Orientation => access(options.orientation) ?? "horizontal";
|
|
59
|
+
// Left and right follow the WRITING direction; up and down never do.
|
|
60
|
+
const flipped = (): boolean => locale().direction === "rtl" && orientation() === "horizontal";
|
|
61
|
+
|
|
62
|
+
if (!isServer) {
|
|
63
|
+
createEffect(
|
|
64
|
+
() => access(options.ref) as Element | null,
|
|
65
|
+
(element) => {
|
|
66
|
+
const outer = element?.parentElement?.closest('[role="toolbar"]');
|
|
67
|
+
setNested(outer !== null && outer !== undefined);
|
|
68
|
+
},
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
let lastFocused: HTMLElement | null = null;
|
|
73
|
+
|
|
74
|
+
const onKeyDown = (event: KeyboardEvent): void => {
|
|
75
|
+
// A key from a portalled subtree — a menu the toolbar opened — is not the
|
|
76
|
+
// toolbar's to act on, whatever the DOM says about where it bubbled.
|
|
77
|
+
const target = targetElement(event);
|
|
78
|
+
if (!contains(event.currentTarget as Element, target)) return;
|
|
79
|
+
if (nested()) return;
|
|
80
|
+
|
|
81
|
+
const forwards = orientation() === "horizontal" ? "ArrowRight" : "ArrowDown";
|
|
82
|
+
const backwards = orientation() === "horizontal" ? "ArrowLeft" : "ArrowUp";
|
|
83
|
+
|
|
84
|
+
if (event.key === "Tab") {
|
|
85
|
+
// Move to the end the browser is about to leave from, and let it.
|
|
86
|
+
lastFocused = (target as HTMLElement | null) ?? null;
|
|
87
|
+
if (event.shiftKey) manager.focusFirst();
|
|
88
|
+
else manager.focusLast();
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (event.key === forwards) {
|
|
93
|
+
if (flipped()) manager.focusPrevious();
|
|
94
|
+
else manager.focusNext();
|
|
95
|
+
} else if (event.key === backwards) {
|
|
96
|
+
if (flipped()) manager.focusNext();
|
|
97
|
+
else manager.focusPrevious();
|
|
98
|
+
} else {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// A nested action group must not move focus a second time for one key.
|
|
103
|
+
event.stopPropagation();
|
|
104
|
+
event.preventDefault();
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
const onBlur = (event: FocusEvent): void => {
|
|
108
|
+
const current = event.currentTarget as Element;
|
|
109
|
+
if (contains(current, event.relatedTarget as Element | null)) return;
|
|
110
|
+
if (lastFocused !== null) return;
|
|
111
|
+
lastFocused = targetElement(event) as HTMLElement | null;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
const onFocus = (event: FocusEvent): void => {
|
|
115
|
+
const current = event.currentTarget as Element;
|
|
116
|
+
if (lastFocused === null) return;
|
|
117
|
+
// Only when focus came from OUTSIDE: moving between two controls inside
|
|
118
|
+
// is the roving focus doing its job.
|
|
119
|
+
if (contains(current, event.relatedTarget as Element | null)) return;
|
|
120
|
+
if (!contains(current, targetElement(event))) return;
|
|
121
|
+
// Gone from the document since: whatever the browser landed on stands.
|
|
122
|
+
if (lastFocused.isConnected) lastFocused.focus();
|
|
123
|
+
lastFocused = null;
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
return {
|
|
127
|
+
toolbarProps: mergeProps(filterDOMProps(options, { labelable: true }), {
|
|
128
|
+
role: "toolbar",
|
|
129
|
+
"aria-orientation": orientation,
|
|
130
|
+
onKeyDown,
|
|
131
|
+
onFocusIn: onFocus,
|
|
132
|
+
onFocusOut: onBlur,
|
|
133
|
+
}),
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// ---------------------------------------------------------------------------
|
|
138
|
+
// Component
|
|
139
|
+
// ---------------------------------------------------------------------------
|
|
140
|
+
|
|
141
|
+
export interface ToolbarComponentProps extends StyleProps {
|
|
142
|
+
children?: Child;
|
|
143
|
+
/** @default "horizontal" */
|
|
144
|
+
orientation?: Orientation;
|
|
145
|
+
"aria-label"?: string;
|
|
146
|
+
"aria-labelledby"?: string;
|
|
147
|
+
ref?: RefTarget<HTMLDivElement>;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* ```tsx
|
|
152
|
+
* <Toolbar aria-label="Formatting">
|
|
153
|
+
* <ToggleButton>Bold</ToggleButton>
|
|
154
|
+
* <ToggleButton>Italic</ToggleButton>
|
|
155
|
+
* </Toolbar>
|
|
156
|
+
* ```
|
|
157
|
+
*/
|
|
158
|
+
export function Toolbar(props: Incoming<ToolbarComponentProps>) {
|
|
159
|
+
const domRef = makeRef<HTMLDivElement>();
|
|
160
|
+
const options = fromProps(props);
|
|
161
|
+
|
|
162
|
+
const { toolbarProps } = toolbar({ ...(options as unknown as ToolbarOptions), ref: domRef });
|
|
163
|
+
|
|
164
|
+
const elementProps = mergeProps(
|
|
165
|
+
toolbarProps,
|
|
166
|
+
filterDOMProps(options, { global: true }),
|
|
167
|
+
styleProps(props),
|
|
168
|
+
{
|
|
169
|
+
"data-orientation": () => props.orientation?.() ?? "horizontal",
|
|
170
|
+
"data-testid": () => props["data-testid"]?.(),
|
|
171
|
+
},
|
|
172
|
+
);
|
|
173
|
+
|
|
174
|
+
return (
|
|
175
|
+
<div {...elementProps} ref={mergeRefs(domRef.set, props.ref?.())}>
|
|
176
|
+
{props.children}
|
|
177
|
+
</div>
|
|
178
|
+
);
|
|
179
|
+
}
|