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,14 @@
1
+ /** An item typeahead can move to: list navigation's item (#97). */
2
+ export interface TypeaheadItem {
3
+ readonly element: Element;
4
+ /** The item's label, when its content is not; else its text content. */
5
+ readonly text?: string;
6
+ }
7
+ /**
8
+ * The item that `typed` moves to, from `active`, or `undefined`:
9
+ *
10
+ * ```ts
11
+ * const item = findMatch(list.items(), list.active(), "ba");
12
+ * ```
13
+ */
14
+ export declare function findMatch<T extends TypeaheadItem>(items: readonly T[], active: T | undefined, typed: string): T | undefined;
@@ -0,0 +1,32 @@
1
+ // Made on the first key, never at import time (PLAN.md section 6, rule 2),
2
+ // and kept for the page: it holds no state.
3
+ let collator;
4
+ /**
5
+ * The item that `typed` moves to, from `active`, or `undefined`:
6
+ *
7
+ * ```ts
8
+ * const item = findMatch(list.items(), list.active(), "ba");
9
+ * ```
10
+ */
11
+ export function findMatch(items, active, typed) {
12
+ const characters = [...typed];
13
+ // One character, or the same one repeated: the search cycles through
14
+ // the items that start with it.
15
+ const cycles = characters.every((character) => character === characters[0]);
16
+ const search = cycles ? characters[0] : typed;
17
+ const count = items.length;
18
+ const index = items.indexOf(active);
19
+ // One character searches after the active item, a longer string from it
20
+ // on, so the item the first character reached stays while it matches.
21
+ const start = index < 0 ? 0 : index + (cycles ? 1 : 0);
22
+ for (let offset = 0; offset < count; offset++) {
23
+ const item = items[(start + offset) % count];
24
+ // Read now, so a label that changed is matched as it is.
25
+ const text = (item.text ?? item.element.textContent ?? "").trimStart();
26
+ // Case and accents ignored.
27
+ collator ??= new Intl.Collator(undefined, { sensitivity: "base" });
28
+ if (!collator.compare(text.slice(0, search.length), search))
29
+ return item;
30
+ }
31
+ return undefined;
32
+ }
@@ -0,0 +1,41 @@
1
+ import { type TypeaheadItem } from "./match.ts";
2
+ /** What `createTypeahead` returns. */
3
+ export interface Typeahead {
4
+ /**
5
+ * A key from one of the items, or from the element that hands them over;
6
+ * returns whether it used the key.
7
+ */
8
+ onKeyDown(event: KeyboardEvent): boolean;
9
+ }
10
+ /**
11
+ * Typing characters moves to the item whose text starts with them
12
+ * (`findMatch`). Characters typed less than a second apart make one string;
13
+ * Space joins a string being typed. The component hands over list
14
+ * navigation's items, active item and move, and calls the handler from its
15
+ * items' key events, before its own Space activation:
16
+ *
17
+ * ```ts
18
+ * const typeahead = createTypeahead<Entry>({
19
+ * get open() { return open(); },
20
+ * get items() { return list.items(); },
21
+ * get active() { return list.active(); },
22
+ * move: (item) => list.move(item),
23
+ * });
24
+ * // In the item part:
25
+ * onKeyDown: (event) => {
26
+ * if (typeahead.onKeyDown(event) || list.onKeyDown(event)) return;
27
+ * if (event.key === " " || event.key === "Enter") activate(event);
28
+ * },
29
+ * ```
30
+ *
31
+ * The handler cancels each key it uses. Closing the popup (`open` turning
32
+ * false) and disposal clear the string and its timer; opening does not, so
33
+ * a closed Select's trigger may open on the first key and the string goes
34
+ * on. Nothing runs at import time, nor, until a key, on the server.
35
+ */
36
+ export declare function createTypeahead<T extends TypeaheadItem>(options: {
37
+ readonly open?: boolean;
38
+ readonly items: readonly T[];
39
+ readonly active: T | undefined;
40
+ move(item: T): void;
41
+ }): Typeahead;
@@ -0,0 +1,81 @@
1
+ import { createEffect, onCleanup } from "solid-js";
2
+ import { findMatch } from "./match.js";
3
+ // How long after the last key the typed string resets: what Chromium's
4
+ // native <select> waits (README.md, Decisions).
5
+ const resetDelay = 1000;
6
+ /**
7
+ * Typing characters moves to the item whose text starts with them
8
+ * (`findMatch`). Characters typed less than a second apart make one string;
9
+ * Space joins a string being typed. The component hands over list
10
+ * navigation's items, active item and move, and calls the handler from its
11
+ * items' key events, before its own Space activation:
12
+ *
13
+ * ```ts
14
+ * const typeahead = createTypeahead<Entry>({
15
+ * get open() { return open(); },
16
+ * get items() { return list.items(); },
17
+ * get active() { return list.active(); },
18
+ * move: (item) => list.move(item),
19
+ * });
20
+ * // In the item part:
21
+ * onKeyDown: (event) => {
22
+ * if (typeahead.onKeyDown(event) || list.onKeyDown(event)) return;
23
+ * if (event.key === " " || event.key === "Enter") activate(event);
24
+ * },
25
+ * ```
26
+ *
27
+ * The handler cancels each key it uses. Closing the popup (`open` turning
28
+ * false) and disposal clear the string and its timer; opening does not, so
29
+ * a closed Select's trigger may open on the first key and the string goes
30
+ * on. Nothing runs at import time, nor, until a key, on the server.
31
+ */
32
+ export function createTypeahead(options) {
33
+ let typed = "";
34
+ let timer;
35
+ const reset = () => {
36
+ clearTimeout(timer);
37
+ typed = "";
38
+ };
39
+ // Not on opening: a closed Select opens on the first key, and the string
40
+ // goes on.
41
+ createEffect(() => options.open, (open) => {
42
+ if (open === false)
43
+ reset();
44
+ });
45
+ onCleanup(reset);
46
+ return {
47
+ onKeyDown: (event) => {
48
+ const { key, target } = event;
49
+ const items = options.items;
50
+ // Only a key from one of its items, or from the element whose handler
51
+ // calls this, such as a closed Select's trigger: a nested list, such
52
+ // as a submenu in an item, takes its own keys, which bubble through
53
+ // it. As list navigation's check, except that `items` holds only the
54
+ // items the keys reach: a focused item they do not reach hands its
55
+ // key over from its own handler, as the target, and is taken all the
56
+ // same. A printable character is a key of one character, astral ones
57
+ // too; with Control, Alt or Meta it is a shortcut. Space is part of a
58
+ // string being typed, and else the part's. While an input method
59
+ // composes, its text arrives as events of its own.
60
+ if ((target !== event.currentTarget &&
61
+ !items.some((item) => item.element === target)) ||
62
+ event.isComposing ||
63
+ event.ctrlKey ||
64
+ event.altKey ||
65
+ event.metaKey ||
66
+ !/^.$/u.test(key) ||
67
+ (key === " " && !typed))
68
+ return false;
69
+ // So that a key typed into the string does nothing else.
70
+ event.preventDefault();
71
+ typed += key;
72
+ clearTimeout(timer);
73
+ timer = setTimeout(reset, resetDelay);
74
+ const active = options.active;
75
+ const match = findMatch(items, active, typed);
76
+ if (match && match !== active)
77
+ options.move(match);
78
+ return true;
79
+ },
80
+ };
81
+ }
@@ -0,0 +1 @@
1
+ export * as NavigationMenu from "./index.parts.ts";
@@ -0,0 +1 @@
1
+ export * as NavigationMenu from "./index.parts.js";
@@ -0,0 +1,5 @@
1
+ export { NavigationMenuRoot as Root } from "./root/NavigationMenuRoot.tsx";
2
+ export { NavigationMenuItem as Item } from "./item/NavigationMenuItem.tsx";
3
+ export { NavigationMenuTrigger as Trigger } from "./trigger/NavigationMenuTrigger.tsx";
4
+ export { NavigationMenuPanel as Panel } from "./panel/NavigationMenuPanel.tsx";
5
+ export { NavigationMenuLink as Link } from "./link/NavigationMenuLink.tsx";
@@ -0,0 +1,5 @@
1
+ export { NavigationMenuRoot as Root } from "./root/NavigationMenuRoot.jsx";
2
+ export { NavigationMenuItem as Item } from "./item/NavigationMenuItem.jsx";
3
+ export { NavigationMenuTrigger as Trigger } from "./trigger/NavigationMenuTrigger.jsx";
4
+ export { NavigationMenuPanel as Panel } from "./panel/NavigationMenuPanel.jsx";
5
+ export { NavigationMenuLink as Link } from "./link/NavigationMenuLink.jsx";
@@ -0,0 +1,16 @@
1
+ import type { JSX } from "@solidjs/web";
2
+ import type { RenderProp } from "../../core/renderElement.tsx";
3
+ export interface NavigationMenuItemProps extends JSX.HTMLAttributes<HTMLElement> {
4
+ /**
5
+ * Identifies the Item in the Root's value. Without one, the Item uses a
6
+ * generated value, which no other code knows.
7
+ */
8
+ value?: string;
9
+ render?: RenderProp;
10
+ }
11
+ /**
12
+ * One entry of the bar: an `<li>` around a `NavigationMenu.Trigger` and its
13
+ * `NavigationMenu.Panel`. Place it inside `NavigationMenu.Root`, in the
14
+ * `<ul>` of the bar.
15
+ */
16
+ export declare function NavigationMenuItem(props: NavigationMenuItemProps): JSX.Element;
@@ -0,0 +1,85 @@
1
+ import { createUniqueId, merge, omit } from "solid-js";
2
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
3
+ import { renderElement } from "../../core/renderElement.jsx";
4
+ import { createEscape } from "../../floating/dismissal/escape.js";
5
+ import { createHover } from "../../floating/hover/hover.js";
6
+ import { useNavigationMenuRootContext } from "../root/NavigationMenuRootContext.js";
7
+ import { NavigationMenuItemContext } from "./NavigationMenuItemContext.js";
8
+ /**
9
+ * One entry of the bar: an `<li>` around a `NavigationMenu.Trigger` and its
10
+ * `NavigationMenu.Panel`. Place it inside `NavigationMenu.Root`, in the
11
+ * `<ul>` of the bar.
12
+ */
13
+ export function NavigationMenuItem(props) {
14
+ const root = useNavigationMenuRootContext("Item");
15
+ const panelId = createUniqueId();
16
+ // Without a value, the Panel's id serves as one, as on an Accordion Item.
17
+ const value = () => props.value ?? panelId;
18
+ const open = () => root.value() === value();
19
+ const attributes = {
20
+ get "data-open"() {
21
+ return open() ? "" : undefined;
22
+ },
23
+ get "data-closed"() {
24
+ return open() ? undefined : "";
25
+ },
26
+ };
27
+ const context = {
28
+ open,
29
+ toggle: (event) => root.toggle(value(), event),
30
+ panelId,
31
+ trigger: undefined,
32
+ panel: undefined,
33
+ attributes,
34
+ };
35
+ let element;
36
+ createHover({
37
+ get trigger() {
38
+ return context.trigger;
39
+ },
40
+ get popup() {
41
+ return context.panel;
42
+ },
43
+ get open() {
44
+ return open();
45
+ },
46
+ // Read as the pointer enters: 0 while another Item is open (README,
47
+ // "Moving along the bar").
48
+ get delay() {
49
+ return root.hoverDelay();
50
+ },
51
+ get closeDelay() {
52
+ return root.closeDelay();
53
+ },
54
+ // A Panel that holds focus stays open when the pointer leaves: closing
55
+ // it would drop focus to the page. One a press opened stays open too
56
+ // (`hoverClose`).
57
+ onOpenChange: (next, details) => {
58
+ if (next)
59
+ root.hoverOpen(value(), details);
60
+ else if (!context.panel?.contains(document.activeElement))
61
+ root.hoverClose(value(), details);
62
+ },
63
+ });
64
+ createEscape({
65
+ get open() {
66
+ return open();
67
+ },
68
+ onDismiss: (details) => {
69
+ // As the example does: focus inside the Item goes back to its
70
+ // Trigger, which stays in the page; focus elsewhere stays there.
71
+ if (element?.contains(document.activeElement))
72
+ context.trigger?.focus();
73
+ root.close(details);
74
+ },
75
+ });
76
+ const ownProps = merge(attributes, {
77
+ ref: (el) => {
78
+ element = el;
79
+ },
80
+ });
81
+ const elementProps = mergeElementProps(ownProps, omit(props, "render", "value"));
82
+ return (<NavigationMenuItemContext value={context}>
83
+ {renderElement(props, elementProps, (p) => (<li {...p}/>))}
84
+ </NavigationMenuItemContext>);
85
+ }
@@ -0,0 +1,27 @@
1
+ /**
2
+ * An Item's state as `data-*` attributes, each `""` when it applies and
3
+ * absent otherwise: one of `data-open` and `data-closed`. Every part of the
4
+ * Item merges the same object, so its getters are read where each element
5
+ * uses them.
6
+ */
7
+ export type NavigationMenuItemStateAttributes = {
8
+ readonly "data-open": "" | undefined;
9
+ readonly "data-closed": "" | undefined;
10
+ };
11
+ /** What an Item shares with its Trigger and Panel. */
12
+ export interface NavigationMenuItemContextValue {
13
+ /** Whether the Panel is shown. */
14
+ open(): boolean;
15
+ /** Asks to open or close the Panel by a press on the Trigger. */
16
+ toggle(event: MouseEvent): void;
17
+ /** The Panel's `id`, which the Trigger names in `aria-controls`. */
18
+ readonly panelId: string;
19
+ /** The Trigger's element, once rendered. */
20
+ trigger: HTMLElement | undefined;
21
+ /** The Panel's element, once rendered. */
22
+ panel: HTMLElement | undefined;
23
+ /** The `data-*` attributes every part of the Item carries. */
24
+ readonly attributes: NavigationMenuItemStateAttributes;
25
+ }
26
+ export declare const NavigationMenuItemContext: import("solid-js").Context<NavigationMenuItemContextValue | null>;
27
+ export declare function useNavigationMenuItemContext(): NavigationMenuItemContextValue;
@@ -0,0 +1,12 @@
1
+ import { createContext, useContext } from "solid-js";
2
+ import { isDev } from "@solidjs/web";
3
+ // `null` outside an Item, so that the part can name what is missing.
4
+ export const NavigationMenuItemContext = createContext(null);
5
+ export function useNavigationMenuItemContext() {
6
+ const context = useContext(NavigationMenuItemContext);
7
+ // Production builds drop the check, and its message, with `isDev`.
8
+ if (isDev && !context) {
9
+ throw new Error("[menhir] NavigationMenu.Trigger and NavigationMenu.Panel must be placed within <NavigationMenu.Item>.");
10
+ }
11
+ return context;
12
+ }
@@ -0,0 +1,12 @@
1
+ import type { JSX } from "@solidjs/web";
2
+ import type { RenderProp } from "../../core/renderElement.tsx";
3
+ export interface NavigationMenuLinkProps extends JSX.HTMLAttributes<HTMLElement>, Pick<JSX.AnchorHTMLAttributes<HTMLAnchorElement>, "href" | "target" | "rel" | "download" | "hreflang" | "ping" | "referrerpolicy" | "type"> {
4
+ render?: RenderProp;
5
+ }
6
+ /**
7
+ * A link of the navigation: an `<a>` that closes the open Panel when it is
8
+ * activated, by pointer or by Enter, so a router's page change leaves no
9
+ * menu open. Pass `href`, and `aria-current="page"` on the current page's
10
+ * link. Place it anywhere inside `NavigationMenu.Root`.
11
+ */
12
+ export declare function NavigationMenuLink(props: NavigationMenuLinkProps): JSX.Element;
@@ -0,0 +1,18 @@
1
+ import { omit } from "solid-js";
2
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
3
+ import { renderElement } from "../../core/renderElement.jsx";
4
+ import { useNavigationMenuRootContext } from "../root/NavigationMenuRootContext.js";
5
+ /**
6
+ * A link of the navigation: an `<a>` that closes the open Panel when it is
7
+ * activated, by pointer or by Enter, so a router's page change leaves no
8
+ * menu open. Pass `href`, and `aria-current="page"` on the current page's
9
+ * link. Place it anywhere inside `NavigationMenu.Root`.
10
+ */
11
+ export function NavigationMenuLink(props) {
12
+ const root = useNavigationMenuRootContext("Link");
13
+ const ownProps = {
14
+ onClick: (event) => root.close({ reason: "link-press", event }),
15
+ };
16
+ const elementProps = mergeElementProps(ownProps, omit(props, "render"));
17
+ return renderElement(props, elementProps, (p) => <a {...p}/>);
18
+ }
@@ -0,0 +1,11 @@
1
+ import type { JSX } from "@solidjs/web";
2
+ import type { RenderProp } from "../../core/renderElement.tsx";
3
+ export interface NavigationMenuPanelProps extends Omit<JSX.HTMLAttributes<HTMLElement>, "id"> {
4
+ render?: RenderProp;
5
+ }
6
+ /**
7
+ * The links its Item's Trigger shows and hides: a `<div>` in the page's
8
+ * flow, `hidden` while closed, which your CSS places under the Item. Place
9
+ * it inside `NavigationMenu.Item`, after `NavigationMenu.Trigger`.
10
+ */
11
+ export declare function NavigationMenuPanel(props: NavigationMenuPanelProps): JSX.Element;
@@ -0,0 +1,26 @@
1
+ import { merge, omit } from "solid-js";
2
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
3
+ import { renderElement } from "../../core/renderElement.jsx";
4
+ import { useNavigationMenuItemContext } from "../item/NavigationMenuItemContext.js";
5
+ /**
6
+ * The links its Item's Trigger shows and hides: a `<div>` in the page's
7
+ * flow, `hidden` while closed, which your CSS places under the Item. Place
8
+ * it inside `NavigationMenu.Item`, after `NavigationMenu.Trigger`.
9
+ */
10
+ export function NavigationMenuPanel(props) {
11
+ const item = useNavigationMenuItemContext();
12
+ const ownProps = merge(item.attributes, {
13
+ id: item.panelId,
14
+ get hidden() {
15
+ return !item.open();
16
+ },
17
+ ref: (element) => {
18
+ item.panel = element;
19
+ },
20
+ });
21
+ const elementProps = mergeElementProps(ownProps,
22
+ // An `id` from untyped code would break the Trigger's `aria-controls`,
23
+ // so it is dropped too.
24
+ omit(props, "render", "id"));
25
+ return renderElement(props, elementProps, (p) => <div {...p}/>);
26
+ }
@@ -0,0 +1,37 @@
1
+ import type { JSX } from "@solidjs/web";
2
+ import type { RenderProp } from "../../core/renderElement.tsx";
3
+ import type { OpenChangeDetails } from "../../floating/dismissal/reason.ts";
4
+ export interface NavigationMenuRootProps extends JSX.HTMLAttributes<HTMLElement> {
5
+ /** The open Item's value, `null` when none is open, when controlled. */
6
+ value?: string | null;
7
+ /** The value of the Item open at first, when uncontrolled. */
8
+ defaultValue?: string | null;
9
+ /**
10
+ * Called with the new value, and why, when the user opens or closes an
11
+ * Item. When controlled, the menu changes only if `value` does.
12
+ */
13
+ onValueChange?: (value: string | null, details: OpenChangeDetails) => void;
14
+ /**
15
+ * How long the pointer rests on a Trigger before its Panel opens, in
16
+ * milliseconds. Default 300. While an Item is open, or within `timeout`
17
+ * of the last close, the next opens at once.
18
+ */
19
+ delay?: number;
20
+ /**
21
+ * How long after the pointer has left a Trigger and its Panel the Panel
22
+ * closes, in milliseconds. Default 200.
23
+ */
24
+ closeDelay?: number;
25
+ /**
26
+ * How long after the last close hovering a Trigger still opens its Panel
27
+ * at once, in milliseconds. Default 400.
28
+ */
29
+ timeout?: number;
30
+ render?: RenderProp;
31
+ }
32
+ /**
33
+ * A site's navigation: a `<nav>` that holds which `NavigationMenu.Item` is
34
+ * open, one at a time. Focus moving out of it, and a press outside it,
35
+ * close the open Item.
36
+ */
37
+ export declare function NavigationMenuRoot(props: NavigationMenuRootProps): JSX.Element;
@@ -0,0 +1,103 @@
1
+ import { createEffect, omit } from "solid-js";
2
+ import { createControllableSignal } from "../../core/createControllableSignal.js";
3
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
4
+ import { renderElement } from "../../core/renderElement.jsx";
5
+ import { NavigationMenuRootContext } from "./NavigationMenuRootContext.js";
6
+ /**
7
+ * A site's navigation: a `<nav>` that holds which `NavigationMenu.Item` is
8
+ * open, one at a time. Focus moving out of it, and a press outside it,
9
+ * close the open Item.
10
+ */
11
+ export function NavigationMenuRoot(props) {
12
+ // `requested` counts a change asked for earlier in the same turn, which
13
+ // `value` shows only after the flush: two pointer events of one move may
14
+ // ask twice.
15
+ const [value, setValue, requested] = createControllableSignal({
16
+ get value() {
17
+ return props.value;
18
+ },
19
+ get defaultValue() {
20
+ return props.defaultValue ?? null;
21
+ },
22
+ onChange: (next, details) => props.onValueChange?.(next, details),
23
+ });
24
+ // The Item a press opened, whose Panel hover does not close (README,
25
+ // "Decisions").
26
+ let pressedItem = null;
27
+ // When an Item last closed, for `timeout`.
28
+ let closedAt = -Infinity;
29
+ const set = (next, details) => {
30
+ if (next === null && requested() !== null)
31
+ closedAt = Date.now();
32
+ pressedItem = null;
33
+ setValue(next, details);
34
+ };
35
+ const close = (details) => set(null, details);
36
+ let nav;
37
+ const inside = (target) => !!target && !!nav?.contains(target);
38
+ // Whether a primary press started outside and has not ended in a `click`
39
+ // or been cancelled yet. Focus that such a press moves out of the `<nav>`
40
+ // is left to the press, so it closes on release as `outside-press`.
41
+ let pressOutside = false;
42
+ // A press outside closes on release, the `click` of a press that also
43
+ // started outside, so a text selection dragged out of a Panel, a
44
+ // secondary button and a key's `click` close nothing (README).
45
+ createEffect(() => value() !== null, (open) => {
46
+ if (!open)
47
+ return;
48
+ const down = (event) => {
49
+ pressOutside = event.button === 0 && !inside(event.target);
50
+ };
51
+ const cancel = () => {
52
+ pressOutside = false;
53
+ };
54
+ const click = (event) => {
55
+ if (pressOutside && !inside(event.target))
56
+ close({ reason: "outside-press", event });
57
+ pressOutside = false;
58
+ };
59
+ document.addEventListener("pointerdown", down, true);
60
+ document.addEventListener("pointercancel", cancel, true);
61
+ document.addEventListener("click", click, true);
62
+ return () => {
63
+ cancel();
64
+ document.removeEventListener("pointerdown", down, true);
65
+ document.removeEventListener("pointercancel", cancel, true);
66
+ document.removeEventListener("click", click, true);
67
+ };
68
+ });
69
+ const context = {
70
+ value,
71
+ toggle: (item, event) => {
72
+ const next = requested() === item ? null : item;
73
+ set(next, { reason: "trigger-press", event });
74
+ pressedItem = next;
75
+ },
76
+ hoverOpen: set,
77
+ hoverClose: (item, details) => {
78
+ if (requested() === item && pressedItem !== item)
79
+ close(details);
80
+ },
81
+ close,
82
+ hoverDelay: () => requested() !== null || Date.now() - closedAt < (props.timeout ?? 400)
83
+ ? 0
84
+ : (props.delay ?? 300),
85
+ closeDelay: () => props.closeDelay ?? 200,
86
+ };
87
+ const ownProps = {
88
+ // Focus moving to an element outside closes; focus lost to nothing,
89
+ // such as the window's, does not, and focus that a press outside moves
90
+ // closes on its release (README, "Decisions").
91
+ onFocusOut: (event) => {
92
+ if (event.relatedTarget && !inside(event.relatedTarget) && !pressOutside)
93
+ close({ reason: "focus-out", event });
94
+ },
95
+ ref: (element) => {
96
+ nav = element;
97
+ },
98
+ };
99
+ const elementProps = mergeElementProps(ownProps, omit(props, "render", "value", "defaultValue", "onValueChange", "delay", "closeDelay", "timeout"));
100
+ return (<NavigationMenuRootContext value={context}>
101
+ {renderElement(props, elementProps, (p) => (<nav {...p}/>))}
102
+ </NavigationMenuRootContext>);
103
+ }
@@ -0,0 +1,33 @@
1
+ import type { OpenChangeDetails } from "../../floating/dismissal/reason.ts";
2
+ /** What the Root shares with its Items and Links. */
3
+ export interface NavigationMenuRootContextValue {
4
+ /** The open Item's value, `null` when none is open. */
5
+ value(): string | null;
6
+ /**
7
+ * A press on the Item's Trigger: opens it, to stay open when the pointer
8
+ * leaves, or closes it if it is open, counting a change asked for earlier
9
+ * in the same turn.
10
+ */
11
+ toggle(item: string, event: MouseEvent): void;
12
+ /** Hover asking to open the Item, closing any other. */
13
+ hoverOpen(item: string, details: OpenChangeDetails): void;
14
+ /**
15
+ * Hover asking to close the Item, which closes it only if it is the one
16
+ * open, counting a change asked for earlier in the same turn, and a press
17
+ * did not open it: hover's close of the Item the pointer left must not
18
+ * undo the opening of the next.
19
+ */
20
+ hoverClose(item: string, details: OpenChangeDetails): void;
21
+ /** Asks to close whichever Item is open, saying why. */
22
+ close(details: OpenChangeDetails): void;
23
+ /**
24
+ * How long the pointer rests on a Trigger it has just entered before its
25
+ * Panel opens: 0 while an Item is open or within `timeout` of the last
26
+ * close, else the Root's `delay`.
27
+ */
28
+ hoverDelay(): number;
29
+ /** The Root's `closeDelay`. */
30
+ closeDelay(): number;
31
+ }
32
+ export declare const NavigationMenuRootContext: import("solid-js").Context<NavigationMenuRootContextValue | null>;
33
+ export declare function useNavigationMenuRootContext(part: string): NavigationMenuRootContextValue;
@@ -0,0 +1,13 @@
1
+ import { createContext, useContext } from "solid-js";
2
+ import { isDev } from "@solidjs/web";
3
+ // `null` outside a Root, so that the part can name what is missing. Solid's
4
+ // own error for a context with no provider and no default does not.
5
+ export const NavigationMenuRootContext = createContext(null);
6
+ export function useNavigationMenuRootContext(part) {
7
+ const context = useContext(NavigationMenuRootContext);
8
+ // Production builds drop the check, and its message, with `isDev`.
9
+ if (isDev && !context) {
10
+ throw new Error(`[menhir] NavigationMenu.${part} must be placed within <NavigationMenu.Root>.`);
11
+ }
12
+ return context;
13
+ }
@@ -0,0 +1,12 @@
1
+ import type { JSX } from "@solidjs/web";
2
+ import type { RenderProp } from "../../core/renderElement.tsx";
3
+ export interface NavigationMenuTriggerProps extends JSX.HTMLAttributes<HTMLElement> {
4
+ render?: RenderProp;
5
+ }
6
+ /**
7
+ * The button that shows and hides its Item's Panel: a
8
+ * `<button type="button" aria-expanded>`. A press toggles the Panel, and
9
+ * the pointer resting on it opens it. Place it inside
10
+ * `NavigationMenu.Item`, before `NavigationMenu.Panel`.
11
+ */
12
+ export declare function NavigationMenuTrigger(props: NavigationMenuTriggerProps): JSX.Element;
@@ -0,0 +1,32 @@
1
+ import { isDev } from "@solidjs/web";
2
+ import { merge, omit } from "solid-js";
3
+ import { warnUnlessButton } from "../../core/button.js";
4
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
5
+ import { renderElement } from "../../core/renderElement.jsx";
6
+ import { useNavigationMenuItemContext } from "../item/NavigationMenuItemContext.js";
7
+ /**
8
+ * The button that shows and hides its Item's Panel: a
9
+ * `<button type="button" aria-expanded>`. A press toggles the Panel, and
10
+ * the pointer resting on it opens it. Place it inside
11
+ * `NavigationMenu.Item`, before `NavigationMenu.Panel`.
12
+ */
13
+ export function NavigationMenuTrigger(props) {
14
+ const item = useNavigationMenuItemContext();
15
+ const ownProps = merge(item.attributes, {
16
+ type: "button",
17
+ // Solid 2.0 removes an attribute set to false, so spell the value out.
18
+ get "aria-expanded"() {
19
+ return item.open() ? "true" : "false";
20
+ },
21
+ "aria-controls": item.panelId,
22
+ onClick: (event) => item.toggle(event),
23
+ ref: (element) => {
24
+ item.trigger = element;
25
+ // Production builds drop the check with `isDev`.
26
+ if (isDev)
27
+ warnUnlessButton(element, "NavigationMenu.Trigger");
28
+ },
29
+ });
30
+ const elementProps = mergeElementProps(ownProps, omit(props, "render"));
31
+ return renderElement(props, elementProps, (p) => <button {...p}/>);
32
+ }