menhir 0.1.2 → 0.1.4

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 (208) hide show
  1. package/LICENSE +6 -0
  2. package/README.md +9 -2
  3. package/dist/alert-dialog/index.js +2 -0
  4. package/dist/alert-dialog/index.parts.js +7 -0
  5. package/dist/alert-dialog/popup/AlertDialogPopup.js +13 -0
  6. package/dist/core/createControllableSignal.js +15 -5
  7. package/dist/core/listValue.js +2 -7
  8. package/dist/core/slot.js +30 -0
  9. package/dist/dialog/close/DialogClose.js +33 -0
  10. package/dist/dialog/description/DialogDescription.js +27 -0
  11. package/dist/dialog/index.js +2 -0
  12. package/dist/dialog/index.parts.js +7 -0
  13. package/dist/dialog/popup/DialogPopup.js +13 -0
  14. package/dist/dialog/popup/renderDialogPopup.js +71 -0
  15. package/dist/dialog/root/DialogRoot.js +46 -0
  16. package/dist/dialog/root/DialogRootContext.js +10 -0
  17. package/dist/dialog/title/DialogTitle.js +27 -0
  18. package/dist/dialog/trigger/DialogTrigger.js +33 -0
  19. package/dist/floating/dismissal/dialogClose.js +67 -0
  20. package/dist/floating/dismissal/escape.js +44 -0
  21. package/dist/floating/dismissal/outsidePress.js +52 -0
  22. package/dist/floating/dismissal/popoverClose.js +74 -0
  23. package/dist/floating/dismissal/reason.js +0 -0
  24. package/dist/floating/focus/initialFocus.js +33 -0
  25. package/dist/floating/focus/returnFocus.js +65 -0
  26. package/dist/floating/focus/tabWrap.js +55 -0
  27. package/dist/floating/focus/tabbable.js +50 -0
  28. package/dist/floating/focus/target.js +0 -0
  29. package/dist/floating/hover/group.js +36 -0
  30. package/dist/floating/hover/hover.js +105 -0
  31. package/dist/floating/hover/safeArea.js +64 -0
  32. package/dist/floating/list-navigation/activeDescendant.js +29 -0
  33. package/dist/floating/list-navigation/listNavigation.js +108 -0
  34. package/dist/floating/list-navigation/roving.js +27 -0
  35. package/dist/floating/presence/presence.js +57 -0
  36. package/dist/floating/scroll-lock/scrollLock.js +125 -0
  37. package/dist/floating/typeahead/match.js +24 -0
  38. package/dist/floating/typeahead/typeahead.js +56 -0
  39. package/dist/navigation-menu/index.js +2 -0
  40. package/dist/navigation-menu/index.parts.js +6 -0
  41. package/dist/navigation-menu/item/NavigationMenuItem.js +83 -0
  42. package/dist/navigation-menu/item/NavigationMenuItemContext.js +10 -0
  43. package/dist/navigation-menu/link/NavigationMenuLink.js +26 -0
  44. package/dist/navigation-menu/panel/NavigationMenuPanel.js +31 -0
  45. package/dist/navigation-menu/root/NavigationMenuRoot.js +100 -0
  46. package/dist/navigation-menu/root/NavigationMenuRootContext.js +10 -0
  47. package/dist/navigation-menu/trigger/NavigationMenuTrigger.js +36 -0
  48. package/dist/popover/arrow/PopoverArrow.js +39 -0
  49. package/dist/popover/close/PopoverClose.js +33 -0
  50. package/dist/popover/description/PopoverDescription.js +27 -0
  51. package/dist/popover/index.js +2 -0
  52. package/dist/popover/index.parts.js +8 -0
  53. package/dist/popover/popup/PopoverPopup.js +116 -0
  54. package/dist/popover/popup/PopoverPopupContext.js +10 -0
  55. package/dist/popover/root/PopoverRoot.js +50 -0
  56. package/dist/popover/root/PopoverRootContext.js +10 -0
  57. package/dist/popover/title/PopoverTitle.js +27 -0
  58. package/dist/popover/trigger/PopoverTrigger.js +44 -0
  59. package/dist/source/alert-dialog/index.d.ts +1 -0
  60. package/dist/source/alert-dialog/index.js +1 -0
  61. package/dist/source/alert-dialog/index.parts.d.ts +6 -0
  62. package/dist/source/alert-dialog/index.parts.js +7 -0
  63. package/dist/source/alert-dialog/popup/AlertDialogPopup.d.ts +10 -0
  64. package/dist/source/alert-dialog/popup/AlertDialogPopup.jsx +11 -0
  65. package/dist/source/core/createControllableSignal.d.ts +13 -4
  66. package/dist/source/core/createControllableSignal.js +11 -5
  67. package/dist/source/core/listValue.js +4 -10
  68. package/dist/source/core/slot.d.ts +21 -0
  69. package/dist/source/core/slot.js +28 -0
  70. package/dist/source/dialog/close/DialogClose.d.ts +11 -0
  71. package/dist/source/dialog/close/DialogClose.jsx +25 -0
  72. package/dist/source/dialog/description/DialogDescription.d.ts +10 -0
  73. package/dist/source/dialog/description/DialogDescription.jsx +26 -0
  74. package/dist/source/dialog/index.d.ts +1 -0
  75. package/dist/source/dialog/index.js +1 -0
  76. package/dist/source/dialog/index.parts.d.ts +6 -0
  77. package/dist/source/dialog/index.parts.js +6 -0
  78. package/dist/source/dialog/popup/DialogPopup.d.ts +9 -0
  79. package/dist/source/dialog/popup/DialogPopup.jsx +11 -0
  80. package/dist/source/dialog/popup/renderDialogPopup.d.ts +40 -0
  81. package/dist/source/dialog/popup/renderDialogPopup.jsx +73 -0
  82. package/dist/source/dialog/root/DialogRoot.d.ts +20 -0
  83. package/dist/source/dialog/root/DialogRoot.jsx +39 -0
  84. package/dist/source/dialog/root/DialogRootContext.d.ts +32 -0
  85. package/dist/source/dialog/root/DialogRootContext.js +14 -0
  86. package/dist/source/dialog/title/DialogTitle.d.ts +10 -0
  87. package/dist/source/dialog/title/DialogTitle.jsx +26 -0
  88. package/dist/source/dialog/trigger/DialogTrigger.d.ts +10 -0
  89. package/dist/source/dialog/trigger/DialogTrigger.jsx +27 -0
  90. package/dist/source/floating/dismissal/dialogClose.d.ts +26 -0
  91. package/dist/source/floating/dismissal/dialogClose.js +69 -0
  92. package/dist/source/floating/dismissal/escape.d.ts +24 -0
  93. package/dist/source/floating/dismissal/escape.js +48 -0
  94. package/dist/source/floating/dismissal/outsidePress.d.ts +26 -0
  95. package/dist/source/floating/dismissal/outsidePress.js +50 -0
  96. package/dist/source/floating/dismissal/popoverClose.d.ts +28 -0
  97. package/dist/source/floating/dismissal/popoverClose.js +82 -0
  98. package/dist/source/floating/dismissal/reason.d.ts +34 -0
  99. package/dist/source/floating/dismissal/reason.js +1 -0
  100. package/dist/source/floating/focus/initialFocus.d.ts +24 -0
  101. package/dist/source/floating/focus/initialFocus.js +38 -0
  102. package/dist/source/floating/focus/returnFocus.d.ts +27 -0
  103. package/dist/source/floating/focus/returnFocus.js +81 -0
  104. package/dist/source/floating/focus/tabWrap.d.ts +21 -0
  105. package/dist/source/floating/focus/tabWrap.js +69 -0
  106. package/dist/source/floating/focus/tabbable.d.ts +16 -0
  107. package/dist/source/floating/focus/tabbable.js +69 -0
  108. package/dist/source/floating/focus/target.d.ts +8 -0
  109. package/dist/source/floating/focus/target.js +1 -0
  110. package/dist/source/floating/hover/group.d.ts +39 -0
  111. package/dist/source/floating/hover/group.js +38 -0
  112. package/dist/source/floating/hover/hover.d.ts +35 -0
  113. package/dist/source/floating/hover/hover.js +130 -0
  114. package/dist/source/floating/hover/safeArea.d.ts +23 -0
  115. package/dist/source/floating/hover/safeArea.js +67 -0
  116. package/dist/source/floating/list-navigation/activeDescendant.d.ts +30 -0
  117. package/dist/source/floating/list-navigation/activeDescendant.js +24 -0
  118. package/dist/source/floating/list-navigation/listNavigation.d.ts +68 -0
  119. package/dist/source/floating/list-navigation/listNavigation.js +148 -0
  120. package/dist/source/floating/list-navigation/roving.d.ts +23 -0
  121. package/dist/source/floating/list-navigation/roving.js +25 -0
  122. package/dist/source/floating/presence/presence.d.ts +24 -0
  123. package/dist/source/floating/presence/presence.js +73 -0
  124. package/dist/source/floating/scroll-lock/scrollLock.d.ts +27 -0
  125. package/dist/source/floating/scroll-lock/scrollLock.js +151 -0
  126. package/dist/source/floating/typeahead/match.d.ts +14 -0
  127. package/dist/source/floating/typeahead/match.js +32 -0
  128. package/dist/source/floating/typeahead/typeahead.d.ts +41 -0
  129. package/dist/source/floating/typeahead/typeahead.js +81 -0
  130. package/dist/source/navigation-menu/index.d.ts +1 -0
  131. package/dist/source/navigation-menu/index.js +1 -0
  132. package/dist/source/navigation-menu/index.parts.d.ts +5 -0
  133. package/dist/source/navigation-menu/index.parts.js +5 -0
  134. package/dist/source/navigation-menu/item/NavigationMenuItem.d.ts +16 -0
  135. package/dist/source/navigation-menu/item/NavigationMenuItem.jsx +85 -0
  136. package/dist/source/navigation-menu/item/NavigationMenuItemContext.d.ts +27 -0
  137. package/dist/source/navigation-menu/item/NavigationMenuItemContext.js +12 -0
  138. package/dist/source/navigation-menu/link/NavigationMenuLink.d.ts +12 -0
  139. package/dist/source/navigation-menu/link/NavigationMenuLink.jsx +18 -0
  140. package/dist/source/navigation-menu/panel/NavigationMenuPanel.d.ts +11 -0
  141. package/dist/source/navigation-menu/panel/NavigationMenuPanel.jsx +26 -0
  142. package/dist/source/navigation-menu/root/NavigationMenuRoot.d.ts +37 -0
  143. package/dist/source/navigation-menu/root/NavigationMenuRoot.jsx +103 -0
  144. package/dist/source/navigation-menu/root/NavigationMenuRootContext.d.ts +33 -0
  145. package/dist/source/navigation-menu/root/NavigationMenuRootContext.js +13 -0
  146. package/dist/source/navigation-menu/trigger/NavigationMenuTrigger.d.ts +12 -0
  147. package/dist/source/navigation-menu/trigger/NavigationMenuTrigger.jsx +32 -0
  148. package/dist/source/popover/arrow/PopoverArrow.d.ts +11 -0
  149. package/dist/source/popover/arrow/PopoverArrow.jsx +32 -0
  150. package/dist/source/popover/close/PopoverClose.d.ts +11 -0
  151. package/dist/source/popover/close/PopoverClose.jsx +25 -0
  152. package/dist/source/popover/description/PopoverDescription.d.ts +10 -0
  153. package/dist/source/popover/description/PopoverDescription.jsx +26 -0
  154. package/dist/source/popover/index.d.ts +1 -0
  155. package/dist/source/popover/index.js +1 -0
  156. package/dist/source/popover/index.parts.d.ts +7 -0
  157. package/dist/source/popover/index.parts.js +7 -0
  158. package/dist/source/popover/popup/PopoverPopup.d.ts +47 -0
  159. package/dist/source/popover/popup/PopoverPopup.jsx +120 -0
  160. package/dist/source/popover/popup/PopoverPopupContext.d.ts +14 -0
  161. package/dist/source/popover/popup/PopoverPopupContext.js +11 -0
  162. package/dist/source/popover/root/PopoverRoot.d.ts +20 -0
  163. package/dist/source/popover/root/PopoverRoot.jsx +42 -0
  164. package/dist/source/popover/root/PopoverRootContext.d.ts +39 -0
  165. package/dist/source/popover/root/PopoverRootContext.js +13 -0
  166. package/dist/source/popover/title/PopoverTitle.d.ts +10 -0
  167. package/dist/source/popover/title/PopoverTitle.jsx +26 -0
  168. package/dist/source/popover/trigger/PopoverTrigger.d.ts +12 -0
  169. package/dist/source/popover/trigger/PopoverTrigger.jsx +40 -0
  170. package/dist/source/tooltip/arrow/TooltipArrow.d.ts +11 -0
  171. package/dist/source/tooltip/arrow/TooltipArrow.jsx +32 -0
  172. package/dist/source/tooltip/group/TooltipGroup.d.ts +20 -0
  173. package/dist/source/tooltip/group/TooltipGroup.jsx +18 -0
  174. package/dist/source/tooltip/group/TooltipGroupContext.d.ts +2 -0
  175. package/dist/source/tooltip/group/TooltipGroupContext.js +5 -0
  176. package/dist/source/tooltip/index.d.ts +1 -0
  177. package/dist/source/tooltip/index.js +1 -0
  178. package/dist/source/tooltip/index.parts.d.ts +5 -0
  179. package/dist/source/tooltip/index.parts.js +5 -0
  180. package/dist/source/tooltip/popup/TooltipPopup.d.ts +31 -0
  181. package/dist/source/tooltip/popup/TooltipPopup.jsx +77 -0
  182. package/dist/source/tooltip/popup/TooltipPopupContext.d.ts +14 -0
  183. package/dist/source/tooltip/popup/TooltipPopupContext.js +11 -0
  184. package/dist/source/tooltip/root/TooltipRoot.d.ts +31 -0
  185. package/dist/source/tooltip/root/TooltipRoot.jsx +80 -0
  186. package/dist/source/tooltip/root/TooltipRootContext.d.ts +35 -0
  187. package/dist/source/tooltip/root/TooltipRootContext.js +13 -0
  188. package/dist/source/tooltip/trigger/TooltipTrigger.d.ts +12 -0
  189. package/dist/source/tooltip/trigger/TooltipTrigger.jsx +52 -0
  190. package/dist/tooltip/arrow/TooltipArrow.js +39 -0
  191. package/dist/tooltip/group/TooltipGroup.js +26 -0
  192. package/dist/tooltip/group/TooltipGroupContext.js +4 -0
  193. package/dist/tooltip/index.js +2 -0
  194. package/dist/tooltip/index.parts.js +6 -0
  195. package/dist/tooltip/popup/TooltipPopup.js +86 -0
  196. package/dist/tooltip/popup/TooltipPopupContext.js +10 -0
  197. package/dist/tooltip/root/TooltipRoot.js +79 -0
  198. package/dist/tooltip/root/TooltipRootContext.js +10 -0
  199. package/dist/tooltip/trigger/TooltipTrigger.js +54 -0
  200. package/package.json +28 -2
  201. package/skills/menhir-styling/SKILL.md +35 -0
  202. package/skills/menhir-styling/looks/shadcn/LICENSE +24 -0
  203. package/skills/menhir-styling/looks/shadcn/LOOK.md +106 -0
  204. package/skills/menhir-styling/looks/shadcn/dialog.md +63 -0
  205. package/skills/menhir-styling/looks/shadcn/popover.md +39 -0
  206. package/skills/menhir-styling/looks/shadcn/switch.md +26 -0
  207. package/skills/menhir-styling/looks/shadcn/tabs.md +62 -0
  208. package/skills/menhir-styling/looks/shadcn/tokens.css +80 -0
@@ -0,0 +1,67 @@
1
+ /**
2
+ * Follows the pointer once it has left one element on its way to another,
3
+ * such as from a trigger to its popup across the gap between them, and
4
+ * calls `onLeave` with the event once it moves out of the way:
5
+ *
6
+ * ```ts
7
+ * trigger.addEventListener("pointerleave", (event) => {
8
+ * stop = trackSafeArea(event, popup, () => close());
9
+ * });
10
+ * ```
11
+ *
12
+ * The safe area is every point on a line from `exit`, where the pointer
13
+ * left, to the box of `element`, read again on each move. A move outside
14
+ * it, or past the element, ends it, and so does the pointer leaving the
15
+ * window. A touch is ignored. Returns what stops following; call it when
16
+ * the pointer reaches the element. Plain DOM, with no timers and no Solid.
17
+ */
18
+ export function trackSafeArea(exit, element, onLeave) {
19
+ const check = (event) => {
20
+ if (
21
+ // A touch, such as a tap elsewhere while the mouse is in the gap.
22
+ event.pointerType === "touch" ||
23
+ (event.type === "pointerout"
24
+ ? event.relatedTarget
25
+ : inSafeArea(exit, event, element.getBoundingClientRect())))
26
+ return;
27
+ stop();
28
+ onLeave(event);
29
+ };
30
+ const stop = () => {
31
+ document.removeEventListener("pointermove", check);
32
+ document.removeEventListener("pointerout", check);
33
+ };
34
+ document.addEventListener("pointermove", check);
35
+ // Out of the window: a `pointerout` with nothing to go to.
36
+ document.addEventListener("pointerout", check);
37
+ return stop;
38
+ }
39
+ /**
40
+ * Whether `point` is on a line from `exit` to `box`: whether the ray from
41
+ * `exit` through `point` meets the box at or beyond `point`. Each axis
42
+ * gives the stretch of the ray, in multiples of `exit` to `point`, that is
43
+ * within the box's extent on it; the point is inside if the stretches of
44
+ * both axes overlap beyond 1.
45
+ */
46
+ function inSafeArea(exit, point, box) {
47
+ let from = 1;
48
+ let to = Infinity;
49
+ for (const [start, end, min, max] of [
50
+ [exit.clientX, point.clientX, box.left, box.right],
51
+ [exit.clientY, point.clientY, box.top, box.bottom],
52
+ ]) {
53
+ const step = end - start;
54
+ if (step) {
55
+ const a = (min - start) / step;
56
+ const b = (max - start) / step;
57
+ from = Math.max(from, Math.min(a, b));
58
+ to = Math.min(to, Math.max(a, b));
59
+ }
60
+ else if (start < min || start > max) {
61
+ // Still on this axis, and outside the box on it, unless the pointer
62
+ // has not moved at all: the move that left is at `exit` itself.
63
+ return point.clientX === exit.clientX && point.clientY === exit.clientY;
64
+ }
65
+ }
66
+ return from <= to;
67
+ }
@@ -0,0 +1,30 @@
1
+ import { type ListItem, type ListNavigation, type ListNavigationOptions } from "./listNavigation.ts";
2
+ /** An item named in `aria-activedescendant`, which needs an `id`. */
3
+ export interface ActiveDescendantItem extends ListItem {
4
+ readonly id: string;
5
+ }
6
+ /** What `createActiveDescendantList` returns. */
7
+ export interface ActiveDescendantList<T extends ActiveDescendantItem> extends ListNavigation<T> {
8
+ /** The active item's `id`, for `aria-activedescendant`, if any. */
9
+ activeId(): string | undefined;
10
+ }
11
+ /**
12
+ * List navigation where focus stays on another element, such as a
13
+ * combobox's input, which names the active item in `aria-activedescendant`.
14
+ * The item part takes `props.id ?? createUniqueId()` and registers once
15
+ * mounted, so the server HTML names no item:
16
+ *
17
+ * ```ts
18
+ * const list = createActiveDescendantList<Entry>({
19
+ * get open() { return open(); },
20
+ * });
21
+ * // On the input:
22
+ * const inputProps = {
23
+ * get "aria-activedescendant"() { return list.activeId(); },
24
+ * onKeyDown: (event) => list.onKeyDown(event),
25
+ * };
26
+ * // In the item part:
27
+ * onSettled(() => list.register(item));
28
+ * ```
29
+ */
30
+ export declare function createActiveDescendantList<T extends ActiveDescendantItem>(options: ListNavigationOptions<T>): ActiveDescendantList<T>;
@@ -0,0 +1,24 @@
1
+ import { createListNavigation, } from "./listNavigation.js";
2
+ /**
3
+ * List navigation where focus stays on another element, such as a
4
+ * combobox's input, which names the active item in `aria-activedescendant`.
5
+ * The item part takes `props.id ?? createUniqueId()` and registers once
6
+ * mounted, so the server HTML names no item:
7
+ *
8
+ * ```ts
9
+ * const list = createActiveDescendantList<Entry>({
10
+ * get open() { return open(); },
11
+ * });
12
+ * // On the input:
13
+ * const inputProps = {
14
+ * get "aria-activedescendant"() { return list.activeId(); },
15
+ * onKeyDown: (event) => list.onKeyDown(event),
16
+ * };
17
+ * // In the item part:
18
+ * onSettled(() => list.register(item));
19
+ * ```
20
+ */
21
+ export function createActiveDescendantList(options) {
22
+ const list = createListNavigation(options);
23
+ return { ...list, activeId: () => list.active()?.id };
24
+ }
@@ -0,0 +1,68 @@
1
+ /** An item of a list, as list navigation knows it. */
2
+ export interface ListItem {
3
+ readonly element: HTMLElement;
4
+ /**
5
+ * The item's text, for typeahead (#98) and a Select's value, when its
6
+ * content is not its label. Without it, the element's text content.
7
+ */
8
+ readonly text?: string;
9
+ }
10
+ /** The options of `createListNavigation`, as getters and functions. */
11
+ export interface ListNavigationOptions<T extends ListItem> {
12
+ /**
13
+ * Whether the list's popup is open. While it is closed, focus does not
14
+ * move and, once it has closed, no item is active. Left out for a list
15
+ * that is always shown.
16
+ */
17
+ readonly open?: boolean;
18
+ /** Up and Down move in a vertical list (the default), Left and Right in a horizontal one. */
19
+ readonly orientation?: "horizontal" | "vertical";
20
+ /** Whether the keys wrap from the last item to the first and back (default `false`). */
21
+ readonly wrap?: boolean;
22
+ /** Whether the keys and the pointer reach this item (by default, every item). */
23
+ reachable?(item: T): boolean;
24
+ /**
25
+ * The key that points into the active item: Right in a vertical list
26
+ * (Left right to left), Down in a horizontal one. Opens a submenu.
27
+ */
28
+ onArrowIn?(item: T, event: KeyboardEvent): void;
29
+ /**
30
+ * The key that points out of the list: Left in a vertical list (Right
31
+ * right to left), Up in a horizontal one. Closes a submenu.
32
+ */
33
+ onArrowOut?(item: T | undefined, event: KeyboardEvent): void;
34
+ }
35
+ /** What `createListNavigation` returns, in both ways of holding focus. */
36
+ export interface ListNavigation<T extends ListItem> {
37
+ /** Adds a mounted item; returns what removes it. */
38
+ register(item: T): () => void;
39
+ /** The items the keys reach, in document order. */
40
+ items(): T[];
41
+ /** The active item, if any. */
42
+ active(): T | undefined;
43
+ /**
44
+ * Whether `item` is the active one. A move re-runs it for the item that
45
+ * was active and the one that is, not for every item.
46
+ */
47
+ isActive(item: T): boolean;
48
+ /**
49
+ * Makes `item` active as the keyboard does: it is focused, in the roving
50
+ * form, and scrolled into view. An item the keys do not reach is ignored;
51
+ * `undefined` leaves no item active.
52
+ */
53
+ move(item: T | undefined): void;
54
+ /**
55
+ * Arrow keys, Home and End, from one of the items or from the element
56
+ * that hands them over; returns whether it used the key.
57
+ */
58
+ onKeyDown(event: KeyboardEvent): boolean;
59
+ /** The pointer moving on an item makes it active, without scrolling. */
60
+ onPointerMove(item: T, event: PointerEvent): void;
61
+ }
62
+ /**
63
+ * The items of a list in document order, the active item, the keys and the
64
+ * pointer: what both ways of holding focus share. `focusItem` is the way's
65
+ * own: the roving form focuses the item. Nothing runs at import time, nor,
66
+ * until a key or the pointer, on the server.
67
+ */
68
+ export declare function createListNavigation<T extends ListItem>(options: ListNavigationOptions<T>, focusItem?: (item: T) => void): ListNavigation<T>;
@@ -0,0 +1,148 @@
1
+ import { createEffect, createMemo, createSignal } from "solid-js";
2
+ // Document order, which can differ from the order items registered in.
3
+ const byPosition = (a, b) => a.element.compareDocumentPosition(b.element) &
4
+ Node.DOCUMENT_POSITION_FOLLOWING
5
+ ? -1
6
+ : 1;
7
+ /**
8
+ * The items of a list in document order, the active item, the keys and the
9
+ * pointer: what both ways of holding focus share. `focusItem` is the way's
10
+ * own: the roving form focuses the item. Nothing runs at import time, nor,
11
+ * until a key or the pointer, on the server.
12
+ */
13
+ export function createListNavigation(options, focusItem) {
14
+ const [registered, setRegistered] = createSignal([]);
15
+ // Sorted again only when an item registers or leaves.
16
+ const ordered = createMemo(() => [...registered()].sort(byPosition));
17
+ const reachable = (item) => options.reachable?.(item) ?? true;
18
+ const [active, setActive] = createSignal();
19
+ // Each item's own active signal, so a move sets two of them, and only the
20
+ // two items that read them run again. Made on the item's first read.
21
+ const activeSignals = new WeakMap();
22
+ const activeSignal = (item) => {
23
+ let signal = activeSignals.get(item);
24
+ if (!signal) {
25
+ const [read, write] = createSignal(false);
26
+ activeSignals.set(item, (signal = [read, write]));
27
+ }
28
+ return signal;
29
+ };
30
+ // The active item as written, where `active()` lags until the flush.
31
+ let activeItem;
32
+ const activate = (item) => {
33
+ if (activeItem)
34
+ activeSignal(activeItem)[1](false);
35
+ if (item)
36
+ activeSignal(item)[1](true);
37
+ activeItem = item;
38
+ setActive(() => item);
39
+ };
40
+ // As the keyboard shows an item: focused, in the roving form, and
41
+ // scrolled into view at the near edge, so one already in view does not
42
+ // move.
43
+ const focusAndScroll = (item) => {
44
+ focusItem?.(item);
45
+ item.element.scrollIntoView({ block: "nearest", inline: "nearest" });
46
+ };
47
+ const list = {
48
+ register: (item) => {
49
+ setRegistered((items) => [...items, item]);
50
+ return () => {
51
+ setRegistered((items) => items.filter((other) => other !== item));
52
+ if (activeItem === item)
53
+ activate(undefined);
54
+ };
55
+ },
56
+ items: () => ordered().filter(reachable),
57
+ active,
58
+ isActive: (item) => activeSignal(item)[0](),
59
+ move: (item) => {
60
+ if (item && !reachable(item))
61
+ return;
62
+ activate(item);
63
+ // While the popup is closed, the item takes no focus and does not
64
+ // scroll, though an exit animation may still show it: it is focused
65
+ // and scrolled into view once the popup opens (below).
66
+ if (item && options.open !== false)
67
+ focusAndScroll(item);
68
+ },
69
+ onKeyDown: (event) => {
70
+ // Only a key from one of its items, or from the element that hands
71
+ // it over, such as a combobox's input: a nested list, such as a
72
+ // submenu in an item, takes its own keys, which bubble through it.
73
+ const { target } = event;
74
+ if (target !== event.currentTarget &&
75
+ !registered().some((item) => item.element === target))
76
+ return false;
77
+ if (event.altKey || event.ctrlKey || event.metaKey || event.shiftKey)
78
+ return false;
79
+ const items = list.items();
80
+ const itemCount = items.length;
81
+ const activeIndex = items.indexOf(activeItem);
82
+ // Right to left, Left points forward. `direction` inherits, so an
83
+ // item's own is its list's.
84
+ const directionSource = (activeItem ?? items[0])?.element;
85
+ const [leftKey, rightKey] = directionSource && getComputedStyle(directionSource).direction === "rtl"
86
+ ? ["ArrowRight", "ArrowLeft"]
87
+ : ["ArrowLeft", "ArrowRight"];
88
+ const [backKey, forwardKey, outKey, intoKey] = options.orientation === "horizontal"
89
+ ? [leftKey, rightKey, "ArrowUp", "ArrowDown"]
90
+ : ["ArrowUp", "ArrowDown", leftKey, rightKey];
91
+ // Into the active item, or out of the list: the component's to act
92
+ // on, such as a submenu.
93
+ const { onArrowIn, onArrowOut } = options;
94
+ if (event.key === intoKey && activeItem && onArrowIn) {
95
+ onArrowIn(activeItem, event);
96
+ return true;
97
+ }
98
+ if (event.key === outKey && onArrowOut) {
99
+ onArrowOut(activeItem, event);
100
+ return true;
101
+ }
102
+ const toIndex = event.key === backKey
103
+ ? (activeIndex < 0 ? itemCount : activeIndex) - 1
104
+ : event.key === forwardKey
105
+ ? activeIndex + 1
106
+ : event.key === "Home"
107
+ ? 0
108
+ : event.key === "End"
109
+ ? itemCount - 1
110
+ : undefined;
111
+ if (toIndex === undefined || !itemCount)
112
+ return false;
113
+ event.preventDefault();
114
+ list.move(items[options.wrap
115
+ ? (toIndex + itemCount) % itemCount
116
+ : Math.min(Math.max(toIndex, 0), itemCount - 1)]);
117
+ return true;
118
+ },
119
+ onPointerMove: (item, event) => {
120
+ // A finger that scrolls the list would make each item it crosses
121
+ // active; a tap activates an item through the component's click.
122
+ // A closed popup still shown, as during an exit animation, is left
123
+ // alone, as `move` leaves it.
124
+ if (options.open === false || event.pointerType === "touch")
125
+ return;
126
+ if (activeItem === item)
127
+ return;
128
+ if (!reachable(item))
129
+ return;
130
+ activate(item);
131
+ focusItem?.(item);
132
+ },
133
+ };
134
+ createEffect(() => options.open, (open) => {
135
+ // Set again on each opening, by the component.
136
+ if (open === false) {
137
+ activate(undefined);
138
+ return;
139
+ }
140
+ // Once the popup is shown: after its own `showPopover()` in this
141
+ // flush, whichever effect runs first, as `focus/initialFocus.ts`.
142
+ queueMicrotask(() => {
143
+ if (activeItem)
144
+ focusAndScroll(activeItem);
145
+ });
146
+ });
147
+ return list;
148
+ }
@@ -0,0 +1,23 @@
1
+ import { type ListItem, type ListNavigation, type ListNavigationOptions } from "./listNavigation.ts";
2
+ /**
3
+ * List navigation where focus moves to the active item (roving `tabindex`),
4
+ * as in a menu. The item part registers once mounted, sets `tabindex` to 0
5
+ * on the active item and -1 on the others, and hands its key and pointer
6
+ * events over:
7
+ *
8
+ * ```ts
9
+ * const list = createRovingList<Entry>({
10
+ * get open() { return open(); },
11
+ * reachable: (item) => true,
12
+ * });
13
+ * // In the item part:
14
+ * onSettled(() => list.register(item));
15
+ * const ownProps = {
16
+ * get tabindex() { return list.isActive(item) ? 0 : -1; },
17
+ * get "data-active"() { return list.isActive(item) ? "" : undefined; },
18
+ * onKeyDown: (event) => list.onKeyDown(event),
19
+ * onPointerMove: (event) => list.onPointerMove(item, event),
20
+ * };
21
+ * ```
22
+ */
23
+ export declare function createRovingList<T extends ListItem>(options: ListNavigationOptions<T>): ListNavigation<T>;
@@ -0,0 +1,25 @@
1
+ import { createListNavigation, } from "./listNavigation.js";
2
+ /**
3
+ * List navigation where focus moves to the active item (roving `tabindex`),
4
+ * as in a menu. The item part registers once mounted, sets `tabindex` to 0
5
+ * on the active item and -1 on the others, and hands its key and pointer
6
+ * events over:
7
+ *
8
+ * ```ts
9
+ * const list = createRovingList<Entry>({
10
+ * get open() { return open(); },
11
+ * reachable: (item) => true,
12
+ * });
13
+ * // In the item part:
14
+ * onSettled(() => list.register(item));
15
+ * const ownProps = {
16
+ * get tabindex() { return list.isActive(item) ? 0 : -1; },
17
+ * get "data-active"() { return list.isActive(item) ? "" : undefined; },
18
+ * onKeyDown: (event) => list.onKeyDown(event),
19
+ * onPointerMove: (event) => list.onPointerMove(item, event),
20
+ * };
21
+ * ```
22
+ */
23
+ export function createRovingList(options) {
24
+ return createListNavigation(options, (item) => item.element.focus({ preventScroll: true }));
25
+ }
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Shows a popup when `open` turns true, and hides it once its exit
3
+ * animations have ended after `open` turns false:
4
+ *
5
+ * ```ts
6
+ * createPresence({
7
+ * get popup() { return dialog; },
8
+ * get open() { return open(); },
9
+ * });
10
+ * ```
11
+ *
12
+ * A `<dialog>` is shown with `showModal()` and hidden with `close()`; any
13
+ * other element is a popover, shown with `showPopover()` and hidden with
14
+ * `hidePopover()`. On closing it waits for the animations and transitions
15
+ * running in the popup, its descendants and its `::backdrop`, the ones its
16
+ * `data-closed` starts, and hides it at once if none is running; infinite
17
+ * and paused ones do not count. Meanwhile the popup is `inert`, unless it is
18
+ * a popover that holds focus, which only returns focus to its trigger from
19
+ * inside. Opening again cancels the exit. Nothing runs on the server.
20
+ */
21
+ export declare function createPresence(options: {
22
+ readonly popup: HTMLElement | undefined;
23
+ readonly open: boolean;
24
+ }): void;
@@ -0,0 +1,73 @@
1
+ import { createEffect } from "solid-js";
2
+ /**
3
+ * Shows a popup when `open` turns true, and hides it once its exit
4
+ * animations have ended after `open` turns false:
5
+ *
6
+ * ```ts
7
+ * createPresence({
8
+ * get popup() { return dialog; },
9
+ * get open() { return open(); },
10
+ * });
11
+ * ```
12
+ *
13
+ * A `<dialog>` is shown with `showModal()` and hidden with `close()`; any
14
+ * other element is a popover, shown with `showPopover()` and hidden with
15
+ * `hidePopover()`. On closing it waits for the animations and transitions
16
+ * running in the popup, its descendants and its `::backdrop`, the ones its
17
+ * `data-closed` starts, and hides it at once if none is running; infinite
18
+ * and paused ones do not count. Meanwhile the popup is `inert`, unless it is
19
+ * a popover that holds focus, which only returns focus to its trigger from
20
+ * inside. Opening again cancels the exit. Nothing runs on the server.
21
+ */
22
+ export function createPresence(options) {
23
+ // Ends the exit under way, if any, and puts the popup's `inert` back. A
24
+ // wait whose exit has ended does nothing.
25
+ let stopLeaving;
26
+ createEffect(() => options.open, (open) => {
27
+ const popup = options.popup;
28
+ if (!popup)
29
+ return;
30
+ const dialog = popup instanceof HTMLDialogElement ? popup : undefined;
31
+ stopLeaving?.();
32
+ if (open) {
33
+ // Still shown if it was leaving.
34
+ if (dialog) {
35
+ if (!dialog.open)
36
+ dialog.showModal();
37
+ }
38
+ else if (!popup.matches(":popover-open"))
39
+ popup.showPopover();
40
+ return;
41
+ }
42
+ if (!popup.matches("[open], :popover-open"))
43
+ return;
44
+ const inert = popup.inert;
45
+ if (dialog || !popup.contains(document.activeElement))
46
+ popup.inert = true;
47
+ const stop = (stopLeaving = () => {
48
+ stopLeaving = undefined;
49
+ popup.inert = inert;
50
+ });
51
+ // Asks again once they have settled, for one that started meanwhile.
52
+ // A cancelled animation settles too, as do those of a popup the
53
+ // browser hid or that left the document.
54
+ const wait = () => {
55
+ if (stopLeaving !== stop)
56
+ return;
57
+ const running = popup
58
+ .getAnimations({ subtree: true })
59
+ .filter((animation) => animation.playState === "running" &&
60
+ animation.effect?.getComputedTiming().endTime !== Infinity);
61
+ if (running.length) {
62
+ void Promise.allSettled(running.map((animation) => animation.finished)).then(wait);
63
+ return;
64
+ }
65
+ stop();
66
+ if (dialog)
67
+ dialog.close();
68
+ else if (popup.matches(":popover-open"))
69
+ popup.hidePopover();
70
+ };
71
+ wait();
72
+ });
73
+ }
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Keeps the page from scrolling while a modal popup is open: `overflow:
3
+ * hidden` on `<html>`, as `showModal()` leaves the page scrolling.
4
+ *
5
+ * ```ts
6
+ * createScrollLock({
7
+ * get popup() { return dialog; },
8
+ * get open() { return open(); },
9
+ * });
10
+ * ```
11
+ *
12
+ * Counted, so nested popups unlock the page when the last one closes. A
13
+ * page with a scrollbar gets its width as padding on `<html>`, so nothing
14
+ * moves, and as `--hidden-scrollbar-width` for `position: fixed` elements;
15
+ * a page with `scrollbar-gutter: stable` gets neither. Unlocking puts back
16
+ * the page's own inline styles. The page unlocks once the popup has
17
+ * closed, not when its state turns false: after the flush that closed it,
18
+ * or on its `close` or `toggle` event if it is still shown then, as while
19
+ * an exit animation runs. A popup disposed while open unlocks it at once.
20
+ * While open, the popup gets `overscroll-behavior: contain` and, if it
21
+ * does not scroll itself, `touch-action: pinch-zoom`, without which iOS
22
+ * Safari scrolls the page from a drag on it or on its backdrop.
23
+ */
24
+ export declare function createScrollLock(options: {
25
+ readonly popup: HTMLElement | undefined;
26
+ readonly open: boolean;
27
+ }): void;
@@ -0,0 +1,151 @@
1
+ import { createEffect, onCleanup } from "solid-js";
2
+ // The popups holding a lock, and what puts the page's own styles back once
3
+ // none does. Written only from effects, which do not run on the server.
4
+ let locks = 0;
5
+ let restoreStyles;
6
+ /**
7
+ * Keeps the page from scrolling while a modal popup is open: `overflow:
8
+ * hidden` on `<html>`, as `showModal()` leaves the page scrolling.
9
+ *
10
+ * ```ts
11
+ * createScrollLock({
12
+ * get popup() { return dialog; },
13
+ * get open() { return open(); },
14
+ * });
15
+ * ```
16
+ *
17
+ * Counted, so nested popups unlock the page when the last one closes. A
18
+ * page with a scrollbar gets its width as padding on `<html>`, so nothing
19
+ * moves, and as `--hidden-scrollbar-width` for `position: fixed` elements;
20
+ * a page with `scrollbar-gutter: stable` gets neither. Unlocking puts back
21
+ * the page's own inline styles. The page unlocks once the popup has
22
+ * closed, not when its state turns false: after the flush that closed it,
23
+ * or on its `close` or `toggle` event if it is still shown then, as while
24
+ * an exit animation runs. A popup disposed while open unlocks it at once.
25
+ * While open, the popup gets `overscroll-behavior: contain` and, if it
26
+ * does not scroll itself, `touch-action: pinch-zoom`, without which iOS
27
+ * Safari scrolls the page from a drag on it or on its backdrop.
28
+ */
29
+ export function createScrollLock(options) {
30
+ // What ends this popup's lock while it holds one, and what ends the wait
31
+ // for it to close, while it is still shown after its state turned false.
32
+ let held;
33
+ let stopWaiting;
34
+ const release = () => {
35
+ stopWaiting?.();
36
+ held?.();
37
+ held = undefined;
38
+ };
39
+ // Disposal releases at once, before the check queued below runs.
40
+ onCleanup(release);
41
+ createEffect(() => options.open, (open) => {
42
+ const popup = options.popup;
43
+ if (!open || !popup)
44
+ return;
45
+ // Opened again before the last closing finished: it keeps its lock.
46
+ stopWaiting?.();
47
+ if (!held) {
48
+ lock();
49
+ const unlockTouch = lockTouch(popup);
50
+ held = () => {
51
+ unlockTouch();
52
+ unlock();
53
+ };
54
+ }
55
+ return () => {
56
+ // After the popup's own close in this flush, whichever effect runs
57
+ // first. A microtask, not `onFlush`, which is for event handlers:
58
+ // this runs inside the flush.
59
+ queueMicrotask(() => {
60
+ if (!held || stopWaiting)
61
+ return;
62
+ if (!popup.matches("[open], :popover-open"))
63
+ return release();
64
+ const closed = (event) => {
65
+ if (event.newState !== "open")
66
+ release();
67
+ };
68
+ stopWaiting = () => {
69
+ stopWaiting = undefined;
70
+ popup.removeEventListener("close", closed);
71
+ popup.removeEventListener("toggle", closed);
72
+ };
73
+ popup.addEventListener("close", closed);
74
+ popup.addEventListener("toggle", closed);
75
+ });
76
+ };
77
+ });
78
+ }
79
+ function lock() {
80
+ if (locks++)
81
+ return;
82
+ const html = document.documentElement;
83
+ const style = html.style;
84
+ const lockStyles = {
85
+ "overflow-x": "hidden",
86
+ "overflow-y": "hidden",
87
+ };
88
+ // The page's scrollbar, which `overflow: hidden` takes away. A stable
89
+ // gutter stays, so nothing moves.
90
+ const scrollbarWidth = innerWidth - html.clientWidth;
91
+ const computed = getComputedStyle(html);
92
+ if (scrollbarWidth > 0 &&
93
+ !computed.getPropertyValue("scrollbar-gutter").startsWith("stable")) {
94
+ lockStyles["padding-right"] =
95
+ `${parseFloat(computed.paddingRight) + scrollbarWidth}px`;
96
+ lockStyles["--hidden-scrollbar-width"] = `${scrollbarWidth}px`;
97
+ }
98
+ restoreStyles = saveStyles(style, Object.keys(lockStyles));
99
+ for (const name in lockStyles)
100
+ style.setProperty(name, lockStyles[name]);
101
+ }
102
+ function unlock() {
103
+ if (!--locks)
104
+ restoreStyles();
105
+ }
106
+ /**
107
+ * What keeps iOS Safari from scrolling the page through the popup, whatever
108
+ * the page's `overflow`: `overscroll-behavior: contain`, so a popup that
109
+ * scrolls stops at its end, and `touch-action: pinch-zoom` on a popup that
110
+ * does not scroll itself, so that a drag on it, or on its backdrop, which
111
+ * hits the popup, pans nothing. Checked again as the popup or its content
112
+ * changes size. A popup that scrolls keeps its own `touch-action`, since
113
+ * Chromium would no longer scroll it by touch.
114
+ */
115
+ function lockTouch(popup) {
116
+ const style = popup.style;
117
+ const restoreContain = saveStyles(style, ["overscroll-behavior"]);
118
+ const restoreTouch = saveStyles(style, ["touch-action"]);
119
+ style.setProperty("overscroll-behavior", "contain");
120
+ const observer = new ResizeObserver(() => {
121
+ for (const child of popup.children)
122
+ observer.observe(child);
123
+ if (popup.scrollHeight > popup.clientHeight ||
124
+ popup.scrollWidth > popup.clientWidth)
125
+ restoreTouch();
126
+ else
127
+ style.setProperty("touch-action", "pinch-zoom");
128
+ });
129
+ observer.observe(popup);
130
+ return () => {
131
+ observer.disconnect();
132
+ restoreTouch();
133
+ restoreContain();
134
+ };
135
+ }
136
+ /** Saves the inline values of `names`, and returns what puts them back. */
137
+ function saveStyles(style, names) {
138
+ const saved = names.map((name) => [
139
+ name,
140
+ style.getPropertyValue(name),
141
+ style.getPropertyPriority(name),
142
+ ]);
143
+ return () => {
144
+ for (const [name, value, priority] of saved) {
145
+ if (value)
146
+ style.setProperty(name, value, priority);
147
+ else
148
+ style.removeProperty(name);
149
+ }
150
+ };
151
+ }