@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.
Files changed (76) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +293 -2
  3. package/package.json +347 -4
  4. package/src/breadcrumbs.tsx +296 -0
  5. package/src/button.tsx +342 -0
  6. package/src/calendar.tsx +1389 -0
  7. package/src/checkbox.tsx +570 -0
  8. package/src/collections.ts +976 -0
  9. package/src/color.ts +562 -0
  10. package/src/colorpicker.tsx +1478 -0
  11. package/src/combobox.tsx +2481 -0
  12. package/src/command.tsx +1184 -0
  13. package/src/date.ts +652 -0
  14. package/src/datefield.tsx +951 -0
  15. package/src/datepicker.tsx +556 -0
  16. package/src/dialog.tsx +890 -0
  17. package/src/disclosure.tsx +659 -0
  18. package/src/dom.ts +706 -0
  19. package/src/drawer.ts +674 -0
  20. package/src/focus.ts +1370 -0
  21. package/src/form.tsx +94 -0
  22. package/src/gridlist.tsx +563 -0
  23. package/src/i18n.ts +823 -0
  24. package/src/index.ts +1270 -0
  25. package/src/interactions/description.ts +73 -0
  26. package/src/interactions/flags.ts +35 -0
  27. package/src/interactions/focus-events.ts +218 -0
  28. package/src/interactions/focusable.ts +86 -0
  29. package/src/interactions/hover.ts +213 -0
  30. package/src/interactions/interact-outside.ts +123 -0
  31. package/src/interactions/keyboard.ts +105 -0
  32. package/src/interactions/listeners.ts +78 -0
  33. package/src/interactions/long-press.ts +160 -0
  34. package/src/interactions/modality.ts +416 -0
  35. package/src/interactions/move.ts +257 -0
  36. package/src/interactions/open-link.ts +193 -0
  37. package/src/interactions/press.ts +898 -0
  38. package/src/interactions/prevent-focus.ts +102 -0
  39. package/src/interactions/scroll-wheel.ts +43 -0
  40. package/src/interactions/text-selection.ts +77 -0
  41. package/src/interactions.ts +81 -0
  42. package/src/label.ts +128 -0
  43. package/src/link.tsx +381 -0
  44. package/src/listbox.tsx +721 -0
  45. package/src/live.ts +204 -0
  46. package/src/menu.tsx +2290 -0
  47. package/src/menubar.tsx +575 -0
  48. package/src/navigation.ts +735 -0
  49. package/src/numberfield.tsx +838 -0
  50. package/src/otp.ts +430 -0
  51. package/src/overlays.ts +1247 -0
  52. package/src/platform.ts +74 -0
  53. package/src/presence.ts +164 -0
  54. package/src/questionnaire.tsx +1712 -0
  55. package/src/radio.tsx +567 -0
  56. package/src/register-dom.ts +12 -0
  57. package/src/scroller.ts +1165 -0
  58. package/src/select.tsx +1665 -0
  59. package/src/selection.ts +1415 -0
  60. package/src/slider.tsx +1042 -0
  61. package/src/splitter.ts +1152 -0
  62. package/src/static-collection.ts +211 -0
  63. package/src/switch.tsx +186 -0
  64. package/src/table.tsx +0 -0
  65. package/src/tabs.tsx +1018 -0
  66. package/src/tag.tsx +452 -0
  67. package/src/textfield.tsx +417 -0
  68. package/src/toast.ts +658 -0
  69. package/src/toggle-input.tsx +48 -0
  70. package/src/toggle.ts +321 -0
  71. package/src/togglegroup.ts +365 -0
  72. package/src/toolbar.tsx +179 -0
  73. package/src/tooltip.tsx +657 -0
  74. package/src/utils.ts +758 -0
  75. package/src/validation.ts +447 -0
  76. package/src/virtualizer.tsx +674 -0
@@ -0,0 +1,193 @@
1
+ /**
2
+ * Following a link the browser would not follow on its own.
3
+ *
4
+ * Two cases need it. A keyboard activation with a key other than Enter on an
5
+ * element whose role was overridden to `link`: the browser only follows on
6
+ * Enter, so anything else has to be dispatched here. And a non-anchor element
7
+ * that carries `data-href`, which is how a pressable row or a menu item
8
+ * behaves as a link without nesting one.
9
+ *
10
+ * The dispatch is a real click on a real anchor, so `target`, `download`,
11
+ * `ping` and `rel` behave exactly as the platform defines them, and a client
12
+ * router that is listening sees the navigation it expects.
13
+ */
14
+
15
+ import { createContext, getContext } from "@barqjs/core";
16
+ import { focusWithoutScrolling } from "../dom.ts";
17
+ import { isFirefox, isIPad, isMac, isWebKit } from "../platform.ts";
18
+ import { setOpeningLink } from "./flags.ts";
19
+
20
+ export interface LinkModifiers {
21
+ metaKey?: boolean;
22
+ ctrlKey?: boolean;
23
+ altKey?: boolean;
24
+ shiftKey?: boolean;
25
+ }
26
+
27
+ export interface Router {
28
+ /** Whether navigation is the browser's, rather than a client router's. */
29
+ isNative: boolean;
30
+ open: (target: Element, modifiers: LinkModifiers, href: string) => void;
31
+ href: (href: string) => string;
32
+ }
33
+
34
+ const NATIVE_ROUTER: Router = {
35
+ isNative: true,
36
+ open: (target, modifiers) => withSyntheticLink(target, (link) => openLink(link, modifiers)),
37
+ href: (href) => href,
38
+ };
39
+
40
+ /** The router every link in the tree navigates through. */
41
+ export const RouterContext = createContext<Router>(NATIVE_ROUTER);
42
+
43
+ /** The router in scope, or the browser's own. */
44
+ export function router(): Router {
45
+ return getContext(RouterContext) ?? NATIVE_ROUTER;
46
+ }
47
+
48
+ /**
49
+ * Whether the modifiers and the link itself allow a client-side navigation.
50
+ *
51
+ * `getAttribute("target")` rather than `link.target`: Firefox defaults the
52
+ * property to `_parent` inside an iframe, which would refuse every navigation.
53
+ */
54
+ export function shouldClientNavigate(link: HTMLAnchorElement, modifiers: LinkModifiers): boolean {
55
+ const target = link.getAttribute("target");
56
+ return (
57
+ (!target || target === "_self") &&
58
+ link.origin === location.origin &&
59
+ !link.hasAttribute("download") &&
60
+ !modifiers.metaKey &&
61
+ !modifiers.ctrlKey &&
62
+ !modifiers.altKey &&
63
+ !modifiers.shiftKey
64
+ );
65
+ }
66
+
67
+ /** Open an anchor as though the user had clicked it with `modifiers` held. */
68
+ export function openLink(
69
+ target: HTMLAnchorElement,
70
+ modifiers: LinkModifiers,
71
+ setOpening = true,
72
+ ): void {
73
+ let { metaKey, ctrlKey } = modifiers;
74
+ const { altKey, shiftKey } = modifiers;
75
+
76
+ // Firefox does not count a keyboard event as a user action, so its popup
77
+ // blocker stops `target="_blank"`. It does allow it with Command or Control
78
+ // held, which opens a background tab instead.
79
+ if (
80
+ !isWebKit() &&
81
+ isFirefox() &&
82
+ window.event?.type?.startsWith("key") &&
83
+ target.target === "_blank"
84
+ ) {
85
+ if (isMac()) metaKey = true;
86
+ else ctrlKey = true;
87
+ }
88
+
89
+ // WebKit ignores modifier keys on a synthesised click but honours them on a
90
+ // keyboard event.
91
+ const event =
92
+ isWebKit() && isMac() && !isIPad()
93
+ ? new KeyboardEvent("keydown", {
94
+ key: "Enter",
95
+ metaKey,
96
+ ctrlKey,
97
+ altKey,
98
+ shiftKey,
99
+ })
100
+ : new MouseEvent("click", {
101
+ metaKey,
102
+ ctrlKey,
103
+ altKey,
104
+ shiftKey,
105
+ detail: 1,
106
+ bubbles: true,
107
+ cancelable: true,
108
+ });
109
+
110
+ setOpeningLink(setOpening);
111
+ focusWithoutScrolling(target);
112
+ target.dispatchEvent(event);
113
+ setOpeningLink(false);
114
+ }
115
+
116
+ const LINK_DATA: [attribute: string, property: keyof HTMLAnchorElement][] = [
117
+ ["data-target", "target"],
118
+ ["data-rel", "rel"],
119
+ ["data-download", "download"],
120
+ ["data-ping", "ping"],
121
+ ["data-referrer-policy", "referrerPolicy"],
122
+ ];
123
+
124
+ /**
125
+ * Run `open` with an anchor for `target`: itself when it is one, otherwise a
126
+ * throwaway built from its `data-href` and removed again straight after.
127
+ */
128
+ function withSyntheticLink(target: Element, open: (link: HTMLAnchorElement) => void): void {
129
+ if (target instanceof HTMLAnchorElement) {
130
+ open(target);
131
+ return;
132
+ }
133
+
134
+ const href = target.getAttribute("data-href");
135
+ if (href === null) return;
136
+
137
+ const link = target.ownerDocument.createElement("a");
138
+ link.href = href;
139
+ for (const [attribute, property] of LINK_DATA) {
140
+ const value = target.getAttribute(attribute);
141
+ if (value !== null) (link as unknown as Record<string, string>)[property] = value;
142
+ }
143
+
144
+ target.appendChild(link);
145
+ try {
146
+ open(link);
147
+ } finally {
148
+ target.removeChild(link);
149
+ }
150
+ }
151
+
152
+ /** The `data-*` attributes that make a non-anchor element behave as a link. */
153
+ export function syntheticLinkProps(props: {
154
+ href?: string;
155
+ target?: string;
156
+ rel?: string;
157
+ download?: boolean | string;
158
+ ping?: string;
159
+ referrerPolicy?: string;
160
+ }): Record<string, unknown> {
161
+ return {
162
+ "data-href": props.href === undefined ? undefined : router().href(props.href),
163
+ "data-target": props.target,
164
+ "data-rel": props.rel,
165
+ "data-download": props.download,
166
+ "data-ping": props.ping,
167
+ "data-referrer-policy": props.referrerPolicy,
168
+ };
169
+ }
170
+
171
+ /**
172
+ * Hand a click on a link to the client router, when there is one and the
173
+ * modifiers allow it.
174
+ *
175
+ * `active` is the {@link router} the link read while it was being set up: a
176
+ * click has no owner to find one from.
177
+ */
178
+ export function handleLinkClick(event: MouseEvent, href: string | undefined, active: Router): void {
179
+ const target = event.currentTarget;
180
+ if (
181
+ active.isNative ||
182
+ href === undefined ||
183
+ !(target instanceof HTMLAnchorElement) ||
184
+ !target.href ||
185
+ event.defaultPrevented ||
186
+ !shouldClientNavigate(target, event)
187
+ ) {
188
+ return;
189
+ }
190
+
191
+ event.preventDefault();
192
+ active.open(target, event, href);
193
+ }