menhir 0.1.3 → 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 (62) hide show
  1. package/LICENSE +6 -0
  2. package/README.md +8 -1
  3. package/dist/core/createControllableSignal.js +11 -2
  4. package/dist/core/listValue.js +2 -7
  5. package/dist/floating/dismissal/escape.js +2 -1
  6. package/dist/floating/list-navigation/activeDescendant.js +29 -0
  7. package/dist/floating/list-navigation/listNavigation.js +108 -0
  8. package/dist/floating/list-navigation/roving.js +27 -0
  9. package/dist/floating/typeahead/match.js +24 -0
  10. package/dist/floating/typeahead/typeahead.js +56 -0
  11. package/dist/navigation-menu/index.js +2 -0
  12. package/dist/navigation-menu/index.parts.js +6 -0
  13. package/dist/navigation-menu/item/NavigationMenuItem.js +83 -0
  14. package/dist/navigation-menu/item/NavigationMenuItemContext.js +10 -0
  15. package/dist/navigation-menu/link/NavigationMenuLink.js +26 -0
  16. package/dist/navigation-menu/panel/NavigationMenuPanel.js +31 -0
  17. package/dist/navigation-menu/root/NavigationMenuRoot.js +100 -0
  18. package/dist/navigation-menu/root/NavigationMenuRootContext.js +10 -0
  19. package/dist/navigation-menu/trigger/NavigationMenuTrigger.js +36 -0
  20. package/dist/source/core/createControllableSignal.d.ts +9 -1
  21. package/dist/source/core/createControllableSignal.js +7 -2
  22. package/dist/source/core/listValue.js +4 -10
  23. package/dist/source/floating/dismissal/escape.d.ts +2 -1
  24. package/dist/source/floating/dismissal/escape.js +2 -1
  25. package/dist/source/floating/dismissal/reason.d.ts +5 -1
  26. package/dist/source/floating/list-navigation/activeDescendant.d.ts +30 -0
  27. package/dist/source/floating/list-navigation/activeDescendant.js +24 -0
  28. package/dist/source/floating/list-navigation/listNavigation.d.ts +68 -0
  29. package/dist/source/floating/list-navigation/listNavigation.js +148 -0
  30. package/dist/source/floating/list-navigation/roving.d.ts +23 -0
  31. package/dist/source/floating/list-navigation/roving.js +25 -0
  32. package/dist/source/floating/typeahead/match.d.ts +14 -0
  33. package/dist/source/floating/typeahead/match.js +32 -0
  34. package/dist/source/floating/typeahead/typeahead.d.ts +41 -0
  35. package/dist/source/floating/typeahead/typeahead.js +81 -0
  36. package/dist/source/navigation-menu/index.d.ts +1 -0
  37. package/dist/source/navigation-menu/index.js +1 -0
  38. package/dist/source/navigation-menu/index.parts.d.ts +5 -0
  39. package/dist/source/navigation-menu/index.parts.js +5 -0
  40. package/dist/source/navigation-menu/item/NavigationMenuItem.d.ts +16 -0
  41. package/dist/source/navigation-menu/item/NavigationMenuItem.jsx +85 -0
  42. package/dist/source/navigation-menu/item/NavigationMenuItemContext.d.ts +27 -0
  43. package/dist/source/navigation-menu/item/NavigationMenuItemContext.js +12 -0
  44. package/dist/source/navigation-menu/link/NavigationMenuLink.d.ts +12 -0
  45. package/dist/source/navigation-menu/link/NavigationMenuLink.jsx +18 -0
  46. package/dist/source/navigation-menu/panel/NavigationMenuPanel.d.ts +11 -0
  47. package/dist/source/navigation-menu/panel/NavigationMenuPanel.jsx +26 -0
  48. package/dist/source/navigation-menu/root/NavigationMenuRoot.d.ts +37 -0
  49. package/dist/source/navigation-menu/root/NavigationMenuRoot.jsx +103 -0
  50. package/dist/source/navigation-menu/root/NavigationMenuRootContext.d.ts +33 -0
  51. package/dist/source/navigation-menu/root/NavigationMenuRootContext.js +13 -0
  52. package/dist/source/navigation-menu/trigger/NavigationMenuTrigger.d.ts +12 -0
  53. package/dist/source/navigation-menu/trigger/NavigationMenuTrigger.jsx +32 -0
  54. package/package.json +8 -2
  55. package/skills/menhir-styling/SKILL.md +35 -0
  56. package/skills/menhir-styling/looks/shadcn/LICENSE +24 -0
  57. package/skills/menhir-styling/looks/shadcn/LOOK.md +106 -0
  58. package/skills/menhir-styling/looks/shadcn/dialog.md +63 -0
  59. package/skills/menhir-styling/looks/shadcn/popover.md +39 -0
  60. package/skills/menhir-styling/looks/shadcn/switch.md +26 -0
  61. package/skills/menhir-styling/looks/shadcn/tabs.md +62 -0
  62. package/skills/menhir-styling/looks/shadcn/tokens.css +80 -0
package/LICENSE CHANGED
@@ -19,3 +19,9 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
19
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
20
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
21
  SOFTWARE.
22
+
23
+ ---
24
+
25
+ The shadcn look of the styling skill, skills/menhir-styling/looks/shadcn/,
26
+ reproduces class strings and token values from shadcn/ui, under the licence in
27
+ that folder's LICENSE file.
package/README.md CHANGED
@@ -45,9 +45,16 @@ Nothing is styled. Every part exposes its state as data attributes, such as `dat
45
45
  }
46
46
  ```
47
47
 
48
+ ## Styling with a coding agent
49
+
50
+ The package ships a skill for coding agents, [`skills/menhir-styling`](skills/menhir-styling/SKILL.md), that styles Menhir components in one consistent look, such as the shadcn look in Tailwind CSS. Point your agent at it with a line in your project's `AGENTS.md`, "To style Menhir components, follow `node_modules/menhir/skills/menhir-styling/SKILL.md`", or copy the folder into your agent's skills folder. What to ask for, and how a look of your own works: [Styling](https://menhir-ui.com/docs/styling).
51
+
52
+ ## Docs
53
+
48
54
  For everything else, go to the docs:
49
55
 
50
56
  - [Getting started](https://menhir-ui.com/docs/getting-started)
57
+ - [Styling](https://menhir-ui.com/docs/styling)
51
58
  - [Components](https://menhir-ui.com/docs/components)
52
59
  - [Bundle size](https://menhir-ui.com/docs/bundle-size) of every entry point
53
60
 
@@ -55,7 +62,7 @@ For everything else, go to the docs:
55
62
 
56
63
  Early 0.x versions: the API may change between minor versions. Menu and the other list popups come next; the plan and roadmap are in the [repository](https://github.com/j13n/menhir). Built so far:
57
64
 
58
- - Components: Accordion, Alert Dialog, Checkbox, Collapsible, Dialog, Meter, Popover, Progress, Separator, Switch, Tabs, Toggle, Toggle Group and Tooltip.
65
+ - Components: Accordion, Alert Dialog, Checkbox, Collapsible, Dialog, Meter, Navigation Menu, Popover, Progress, Separator, Switch, Tabs, Toggle, Toggle Group and Tooltip.
59
66
  - Utilities: color scheme and theme, each with local-storage and cookie storage.
60
67
 
61
68
  ## Licence
@@ -22,6 +22,10 @@ import { createSignal, onFlush, untrack } from "solid-js";
22
22
  * does nothing. Set from event handlers, not from a component body or a
23
23
  * memo. A set may pass `details`, such as why a popup closed, which
24
24
  * `onChange` receives after the value.
25
+ *
26
+ * The third item, `requested`, reads untracked the value set last in this
27
+ * turn, or `value` if nothing was: for a handler that builds on a set made
28
+ * earlier in the same turn, which `value` shows only after the flush.
25
29
  */
26
30
  function createControllableSignal(options) {
27
31
  const [internal, setInternal] = createSignal(() => untrack(() => options.defaultValue));
@@ -36,8 +40,9 @@ function createControllableSignal(options) {
36
40
  };
37
41
  const value = () => isControlled() ? options.value : internal();
38
42
  let pending;
43
+ const requested = () => pending ? pending[0] : untrack(value);
39
44
  const setValue = (next, details) => {
40
- if (Object.is(next, pending ? pending[0] : untrack(value))) return;
45
+ if (Object.is(next, requested())) return;
41
46
  if (!untrack(isControlled)) onFlush(() => setInternal(() => next));
42
47
  options.onChange?.(next, details);
43
48
  const set = [next];
@@ -46,6 +51,10 @@ function createControllableSignal(options) {
46
51
  if (pending === set) pending = void 0;
47
52
  });
48
53
  };
49
- return [value, setValue];
54
+ return [
55
+ value,
56
+ setValue,
57
+ requested
58
+ ];
50
59
  }
51
60
  export { createControllableSignal };
@@ -1,5 +1,4 @@
1
1
  import { createControllableSignal } from "./createControllableSignal.js";
2
- import { onFlush } from "solid-js";
3
2
  // src/core/listValue.ts
4
3
  /**
5
4
  * A part's value that lists items and that the consumer may control, as an
@@ -21,7 +20,7 @@ import { onFlush } from "solid-js";
21
20
  * Sets in one turn build on each other, before the flush.
22
21
  */
23
22
  function createListValue(options) {
24
- const [value, setValue] = createControllableSignal({
23
+ const [value, setValue, requested] = createControllableSignal({
25
24
  get value() {
26
25
  return options.value;
27
26
  },
@@ -30,18 +29,14 @@ function createListValue(options) {
30
29
  },
31
30
  onChange: (value) => options.onChange?.(value)
32
31
  });
33
- let pending;
34
32
  return {
35
33
  has: (item) => value().includes(item),
36
34
  set: (item, included) => {
37
- const current = pending ?? value();
38
35
  let next = included ? [item] : [];
39
36
  if (options.multiple) {
40
- next = current.filter((other) => other !== item);
37
+ next = requested().filter((other) => other !== item);
41
38
  if (included) next.push(item);
42
39
  }
43
- if (!pending) onFlush(() => pending = void 0);
44
- pending = next;
45
40
  setValue(next);
46
41
  }
47
42
  };
@@ -2,7 +2,8 @@ import { createEffect } from "solid-js";
2
2
  // src/floating/dismissal/escape.ts
3
3
  /**
4
4
  * Escape for a popup the browser does not close on Escape, a `manual`
5
- * popover such as Tooltip's. While `open`, an Escape key calls `onDismiss`
5
+ * popover such as Tooltip's, or for content in the page's flow, such as
6
+ * Navigation Menu's Panels. While `open`, an Escape key calls `onDismiss`
6
7
  * with the reason `escape-key`:
7
8
  *
8
9
  * ```ts
@@ -0,0 +1,29 @@
1
+ import { createListNavigation } from "./listNavigation.js";
2
+ // src/floating/list-navigation/activeDescendant.ts
3
+ /**
4
+ * List navigation where focus stays on another element, such as a
5
+ * combobox's input, which names the active item in `aria-activedescendant`.
6
+ * The item part takes `props.id ?? createUniqueId()` and registers once
7
+ * mounted, so the server HTML names no item:
8
+ *
9
+ * ```ts
10
+ * const list = createActiveDescendantList<Entry>({
11
+ * get open() { return open(); },
12
+ * });
13
+ * // On the input:
14
+ * const inputProps = {
15
+ * get "aria-activedescendant"() { return list.activeId(); },
16
+ * onKeyDown: (event) => list.onKeyDown(event),
17
+ * };
18
+ * // In the item part:
19
+ * onSettled(() => list.register(item));
20
+ * ```
21
+ */
22
+ function createActiveDescendantList(options) {
23
+ const list = createListNavigation(options);
24
+ return {
25
+ ...list,
26
+ activeId: () => list.active()?.id
27
+ };
28
+ }
29
+ export { createActiveDescendantList };
@@ -0,0 +1,108 @@
1
+ import { createEffect, createMemo, createSignal } from "solid-js";
2
+ // src/floating/list-navigation/listNavigation.ts
3
+ var byPosition = (a, b) => a.element.compareDocumentPosition(b.element) & Node.DOCUMENT_POSITION_FOLLOWING ? -1 : 1;
4
+ /**
5
+ * The items of a list in document order, the active item, the keys and the
6
+ * pointer: what both ways of holding focus share. `focusItem` is the way's
7
+ * own: the roving form focuses the item. Nothing runs at import time, nor,
8
+ * until a key or the pointer, on the server.
9
+ */
10
+ function createListNavigation(options, focusItem) {
11
+ const [registered, setRegistered] = createSignal([]);
12
+ const ordered = createMemo(() => [...registered()].sort(byPosition));
13
+ const reachable = (item) => options.reachable?.(item) ?? true;
14
+ const [active, setActive] = createSignal();
15
+ const activeSignals = /* @__PURE__ */ new WeakMap();
16
+ const activeSignal = (item) => {
17
+ let signal = activeSignals.get(item);
18
+ if (!signal) {
19
+ const [read, write] = createSignal(false);
20
+ activeSignals.set(item, signal = [read, write]);
21
+ }
22
+ return signal;
23
+ };
24
+ let activeItem;
25
+ const activate = (item) => {
26
+ if (activeItem) activeSignal(activeItem)[1](false);
27
+ if (item) activeSignal(item)[1](true);
28
+ activeItem = item;
29
+ setActive(() => item);
30
+ };
31
+ const focusAndScroll = (item) => {
32
+ focusItem?.(item);
33
+ item.element.scrollIntoView({
34
+ block: "nearest",
35
+ inline: "nearest"
36
+ });
37
+ };
38
+ const list = {
39
+ register: (item) => {
40
+ setRegistered((items) => [...items, item]);
41
+ return () => {
42
+ setRegistered((items) => items.filter((other) => other !== item));
43
+ if (activeItem === item) activate(void 0);
44
+ };
45
+ },
46
+ items: () => ordered().filter(reachable),
47
+ active,
48
+ isActive: (item) => activeSignal(item)[0](),
49
+ move: (item) => {
50
+ if (item && !reachable(item)) return;
51
+ activate(item);
52
+ if (item && options.open !== false) focusAndScroll(item);
53
+ },
54
+ onKeyDown: (event) => {
55
+ const { target } = event;
56
+ if (target !== event.currentTarget && !registered().some((item) => item.element === target)) return false;
57
+ if (event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) return false;
58
+ const items = list.items();
59
+ const itemCount = items.length;
60
+ const activeIndex = items.indexOf(activeItem);
61
+ const directionSource = (activeItem ?? items[0])?.element;
62
+ const [leftKey, rightKey] = directionSource && getComputedStyle(directionSource).direction === "rtl" ? ["ArrowRight", "ArrowLeft"] : ["ArrowLeft", "ArrowRight"];
63
+ const [backKey, forwardKey, outKey, intoKey] = options.orientation === "horizontal" ? [
64
+ leftKey,
65
+ rightKey,
66
+ "ArrowUp",
67
+ "ArrowDown"
68
+ ] : [
69
+ "ArrowUp",
70
+ "ArrowDown",
71
+ leftKey,
72
+ rightKey
73
+ ];
74
+ const { onArrowIn, onArrowOut } = options;
75
+ if (event.key === intoKey && activeItem && onArrowIn) {
76
+ onArrowIn(activeItem, event);
77
+ return true;
78
+ }
79
+ if (event.key === outKey && onArrowOut) {
80
+ onArrowOut(activeItem, event);
81
+ return true;
82
+ }
83
+ const toIndex = event.key === backKey ? (activeIndex < 0 ? itemCount : activeIndex) - 1 : event.key === forwardKey ? activeIndex + 1 : event.key === "Home" ? 0 : event.key === "End" ? itemCount - 1 : void 0;
84
+ if (toIndex === void 0 || !itemCount) return false;
85
+ event.preventDefault();
86
+ list.move(items[options.wrap ? (toIndex + itemCount) % itemCount : Math.min(Math.max(toIndex, 0), itemCount - 1)]);
87
+ return true;
88
+ },
89
+ onPointerMove: (item, event) => {
90
+ if (options.open === false || event.pointerType === "touch") return;
91
+ if (activeItem === item) return;
92
+ if (!reachable(item)) return;
93
+ activate(item);
94
+ focusItem?.(item);
95
+ }
96
+ };
97
+ createEffect(() => options.open, (open) => {
98
+ if (open === false) {
99
+ activate(void 0);
100
+ return;
101
+ }
102
+ queueMicrotask(() => {
103
+ if (activeItem) focusAndScroll(activeItem);
104
+ });
105
+ });
106
+ return list;
107
+ }
108
+ export { createListNavigation };
@@ -0,0 +1,27 @@
1
+ import { createListNavigation } from "./listNavigation.js";
2
+ // src/floating/list-navigation/roving.ts
3
+ /**
4
+ * List navigation where focus moves to the active item (roving `tabindex`),
5
+ * as in a menu. The item part registers once mounted, sets `tabindex` to 0
6
+ * on the active item and -1 on the others, and hands its key and pointer
7
+ * events over:
8
+ *
9
+ * ```ts
10
+ * const list = createRovingList<Entry>({
11
+ * get open() { return open(); },
12
+ * reachable: (item) => true,
13
+ * });
14
+ * // In the item part:
15
+ * onSettled(() => list.register(item));
16
+ * const ownProps = {
17
+ * get tabindex() { return list.isActive(item) ? 0 : -1; },
18
+ * get "data-active"() { return list.isActive(item) ? "" : undefined; },
19
+ * onKeyDown: (event) => list.onKeyDown(event),
20
+ * onPointerMove: (event) => list.onPointerMove(item, event),
21
+ * };
22
+ * ```
23
+ */
24
+ function createRovingList(options) {
25
+ return createListNavigation(options, (item) => item.element.focus({ preventScroll: true }));
26
+ }
27
+ export { createRovingList };
@@ -0,0 +1,24 @@
1
+ // src/floating/typeahead/match.ts
2
+ var collator;
3
+ /**
4
+ * The item that `typed` moves to, from `active`, or `undefined`:
5
+ *
6
+ * ```ts
7
+ * const item = findMatch(list.items(), list.active(), "ba");
8
+ * ```
9
+ */
10
+ function findMatch(items, active, typed) {
11
+ const characters = [...typed];
12
+ const cycles = characters.every((character) => character === characters[0]);
13
+ const search = cycles ? characters[0] : typed;
14
+ const count = items.length;
15
+ const index = items.indexOf(active);
16
+ const start = index < 0 ? 0 : index + (cycles ? 1 : 0);
17
+ for (let offset = 0; offset < count; offset++) {
18
+ const item = items[(start + offset) % count];
19
+ const text = (item.text ?? item.element.textContent ?? "").trimStart();
20
+ collator ??= new Intl.Collator(void 0, { sensitivity: "base" });
21
+ if (!collator.compare(text.slice(0, search.length), search)) return item;
22
+ }
23
+ }
24
+ export { findMatch };
@@ -0,0 +1,56 @@
1
+ import { findMatch } from "./match.js";
2
+ import { createEffect, onCleanup } from "solid-js";
3
+ // src/floating/typeahead/typeahead.ts
4
+ var resetDelay = 1e3;
5
+ /**
6
+ * Typing characters moves to the item whose text starts with them
7
+ * (`findMatch`). Characters typed less than a second apart make one string;
8
+ * Space joins a string being typed. The component hands over list
9
+ * navigation's items, active item and move, and calls the handler from its
10
+ * items' key events, before its own Space activation:
11
+ *
12
+ * ```ts
13
+ * const typeahead = createTypeahead<Entry>({
14
+ * get open() { return open(); },
15
+ * get items() { return list.items(); },
16
+ * get active() { return list.active(); },
17
+ * move: (item) => list.move(item),
18
+ * });
19
+ * // In the item part:
20
+ * onKeyDown: (event) => {
21
+ * if (typeahead.onKeyDown(event) || list.onKeyDown(event)) return;
22
+ * if (event.key === " " || event.key === "Enter") activate(event);
23
+ * },
24
+ * ```
25
+ *
26
+ * The handler cancels each key it uses. Closing the popup (`open` turning
27
+ * false) and disposal clear the string and its timer; opening does not, so
28
+ * a closed Select's trigger may open on the first key and the string goes
29
+ * on. Nothing runs at import time, nor, until a key, on the server.
30
+ */
31
+ function createTypeahead(options) {
32
+ let typed = "";
33
+ let timer;
34
+ const reset = () => {
35
+ clearTimeout(timer);
36
+ typed = "";
37
+ };
38
+ createEffect(() => options.open, (open) => {
39
+ if (open === false) reset();
40
+ });
41
+ onCleanup(reset);
42
+ return { onKeyDown: (event) => {
43
+ const { key, target } = event;
44
+ const items = options.items;
45
+ if (target !== event.currentTarget && !items.some((item) => item.element === target) || event.isComposing || event.ctrlKey || event.altKey || event.metaKey || !/^.$/u.test(key) || key === " " && !typed) return false;
46
+ event.preventDefault();
47
+ typed += key;
48
+ clearTimeout(timer);
49
+ timer = setTimeout(reset, resetDelay);
50
+ const active = options.active;
51
+ const match = findMatch(items, active, typed);
52
+ if (match && match !== active) options.move(match);
53
+ return true;
54
+ } };
55
+ }
56
+ export { createTypeahead };
@@ -0,0 +1,2 @@
1
+ import * as NavigationMenu from "./index.parts.js";
2
+ export { NavigationMenu };
@@ -0,0 +1,6 @@
1
+ import { NavigationMenuRoot } from "./root/NavigationMenuRoot.js";
2
+ import { NavigationMenuItem } from "./item/NavigationMenuItem.js";
3
+ import { NavigationMenuTrigger } from "./trigger/NavigationMenuTrigger.js";
4
+ import { NavigationMenuPanel } from "./panel/NavigationMenuPanel.js";
5
+ import { NavigationMenuLink } from "./link/NavigationMenuLink.js";
6
+ export { NavigationMenuItem as Item, NavigationMenuLink as Link, NavigationMenuPanel as Panel, NavigationMenuRoot as Root, NavigationMenuTrigger as Trigger };
@@ -0,0 +1,83 @@
1
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
2
+ import { renderElement } from "../../core/renderElement.js";
3
+ import { createEscape } from "../../floating/dismissal/escape.js";
4
+ import { createHover } from "../../floating/hover/hover.js";
5
+ import { useNavigationMenuRootContext } from "../root/NavigationMenuRootContext.js";
6
+ import { NavigationMenuItemContext } from "./NavigationMenuItemContext.js";
7
+ import { createComponent, spread, template } from "@solidjs/web";
8
+ import { createUniqueId, merge, omit } from "solid-js";
9
+ // src/navigation-menu/item/NavigationMenuItem.tsx
10
+ var _tmpl$ = /* @__PURE__ */ template(`<li>`);
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
+ function NavigationMenuItem(props) {
17
+ const root = useNavigationMenuRootContext("Item");
18
+ const panelId = createUniqueId();
19
+ const value = () => props.value ?? panelId;
20
+ const open = () => root.value() === value();
21
+ const attributes = {
22
+ get "data-open"() {
23
+ return open() ? "" : void 0;
24
+ },
25
+ get "data-closed"() {
26
+ return open() ? void 0 : "";
27
+ }
28
+ };
29
+ const context = {
30
+ open,
31
+ toggle: (event) => root.toggle(value(), event),
32
+ panelId,
33
+ trigger: void 0,
34
+ panel: void 0,
35
+ attributes
36
+ };
37
+ let element;
38
+ createHover({
39
+ get trigger() {
40
+ return context.trigger;
41
+ },
42
+ get popup() {
43
+ return context.panel;
44
+ },
45
+ get open() {
46
+ return open();
47
+ },
48
+ get delay() {
49
+ return root.hoverDelay();
50
+ },
51
+ get closeDelay() {
52
+ return root.closeDelay();
53
+ },
54
+ onOpenChange: (next, details) => {
55
+ if (next) root.hoverOpen(value(), details);
56
+ else if (!context.panel?.contains(document.activeElement)) root.hoverClose(value(), details);
57
+ }
58
+ });
59
+ createEscape({
60
+ get open() {
61
+ return open();
62
+ },
63
+ onDismiss: (details) => {
64
+ if (element?.contains(document.activeElement)) context.trigger?.focus();
65
+ root.close(details);
66
+ }
67
+ });
68
+ const ownProps = merge(attributes, { ref: (el) => {
69
+ element = el;
70
+ } });
71
+ const elementProps = mergeElementProps(ownProps, omit(props, "render", "value"));
72
+ return createComponent(NavigationMenuItemContext, {
73
+ value: context,
74
+ get children() {
75
+ return renderElement(props, elementProps, (p) => (() => {
76
+ var _el$ = _tmpl$();
77
+ spread(_el$, p, false);
78
+ return _el$;
79
+ })());
80
+ }
81
+ });
82
+ }
83
+ export { NavigationMenuItem };
@@ -0,0 +1,10 @@
1
+ import { isDev } from "@solidjs/web";
2
+ import { createContext, useContext } from "solid-js";
3
+ // src/navigation-menu/item/NavigationMenuItemContext.ts
4
+ var NavigationMenuItemContext = createContext(null);
5
+ function useNavigationMenuItemContext() {
6
+ const context = useContext(NavigationMenuItemContext);
7
+ if (isDev && !context) throw new Error("[menhir] NavigationMenu.Trigger and NavigationMenu.Panel must be placed within <NavigationMenu.Item>.");
8
+ return context;
9
+ }
10
+ export { NavigationMenuItemContext, useNavigationMenuItemContext };
@@ -0,0 +1,26 @@
1
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
2
+ import { renderElement } from "../../core/renderElement.js";
3
+ import { useNavigationMenuRootContext } from "../root/NavigationMenuRootContext.js";
4
+ import { spread, template } from "@solidjs/web";
5
+ import { omit } from "solid-js";
6
+ // src/navigation-menu/link/NavigationMenuLink.tsx
7
+ var _tmpl$ = /* @__PURE__ */ template(`<a>`);
8
+ /**
9
+ * A link of the navigation: an `<a>` that closes the open Panel when it is
10
+ * activated, by pointer or by Enter, so a router's page change leaves no
11
+ * menu open. Pass `href`, and `aria-current="page"` on the current page's
12
+ * link. Place it anywhere inside `NavigationMenu.Root`.
13
+ */
14
+ function NavigationMenuLink(props) {
15
+ const root = useNavigationMenuRootContext("Link");
16
+ const elementProps = mergeElementProps({ onClick: (event) => root.close({
17
+ reason: "link-press",
18
+ event
19
+ }) }, omit(props, "render"));
20
+ return renderElement(props, elementProps, (p) => (() => {
21
+ var _el$ = _tmpl$();
22
+ spread(_el$, p, false);
23
+ return _el$;
24
+ })());
25
+ }
26
+ export { NavigationMenuLink };
@@ -0,0 +1,31 @@
1
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
2
+ import { renderElement } from "../../core/renderElement.js";
3
+ import { useNavigationMenuItemContext } from "../item/NavigationMenuItemContext.js";
4
+ import { spread, template } from "@solidjs/web";
5
+ import { merge, omit } from "solid-js";
6
+ // src/navigation-menu/panel/NavigationMenuPanel.tsx
7
+ var _tmpl$ = /* @__PURE__ */ template(`<div>`);
8
+ /**
9
+ * The links its Item's Trigger shows and hides: a `<div>` in the page's
10
+ * flow, `hidden` while closed, which your CSS places under the Item. Place
11
+ * it inside `NavigationMenu.Item`, after `NavigationMenu.Trigger`.
12
+ */
13
+ function NavigationMenuPanel(props) {
14
+ const item = useNavigationMenuItemContext();
15
+ const ownProps = merge(item.attributes, {
16
+ id: item.panelId,
17
+ get hidden() {
18
+ return !item.open();
19
+ },
20
+ ref: (element) => {
21
+ item.panel = element;
22
+ }
23
+ });
24
+ const elementProps = mergeElementProps(ownProps, omit(props, "render", "id"));
25
+ return renderElement(props, elementProps, (p) => (() => {
26
+ var _el$ = _tmpl$();
27
+ spread(_el$, p, false);
28
+ return _el$;
29
+ })());
30
+ }
31
+ export { NavigationMenuPanel };
@@ -0,0 +1,100 @@
1
+ import { createControllableSignal } from "../../core/createControllableSignal.js";
2
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
3
+ import { renderElement } from "../../core/renderElement.js";
4
+ import { NavigationMenuRootContext } from "./NavigationMenuRootContext.js";
5
+ import { createComponent, spread, template } from "@solidjs/web";
6
+ import { createEffect, omit } from "solid-js";
7
+ // src/navigation-menu/root/NavigationMenuRoot.tsx
8
+ var _tmpl$ = /* @__PURE__ */ template(`<nav>`);
9
+ /**
10
+ * A site's navigation: a `<nav>` that holds which `NavigationMenu.Item` is
11
+ * open, one at a time. Focus moving out of it, and a press outside it,
12
+ * close the open Item.
13
+ */
14
+ function NavigationMenuRoot(props) {
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
+ let pressedItem = null;
25
+ let closedAt = -Infinity;
26
+ const set = (next, details) => {
27
+ if (next === null && requested() !== null) closedAt = Date.now();
28
+ pressedItem = null;
29
+ setValue(next, details);
30
+ };
31
+ const close = (details) => set(null, details);
32
+ let nav;
33
+ const inside = (target) => !!target && !!nav?.contains(target);
34
+ let pressOutside = false;
35
+ createEffect(() => value() !== null, (open) => {
36
+ if (!open) return;
37
+ const down = (event) => {
38
+ pressOutside = event.button === 0 && !inside(event.target);
39
+ };
40
+ const cancel = () => {
41
+ pressOutside = false;
42
+ };
43
+ const click = (event) => {
44
+ if (pressOutside && !inside(event.target)) close({
45
+ reason: "outside-press",
46
+ event
47
+ });
48
+ pressOutside = false;
49
+ };
50
+ document.addEventListener("pointerdown", down, true);
51
+ document.addEventListener("pointercancel", cancel, true);
52
+ document.addEventListener("click", click, true);
53
+ return () => {
54
+ cancel();
55
+ document.removeEventListener("pointerdown", down, true);
56
+ document.removeEventListener("pointercancel", cancel, true);
57
+ document.removeEventListener("click", click, true);
58
+ };
59
+ });
60
+ const context = {
61
+ value,
62
+ toggle: (item, event) => {
63
+ const next = requested() === item ? null : item;
64
+ set(next, {
65
+ reason: "trigger-press",
66
+ event
67
+ });
68
+ pressedItem = next;
69
+ },
70
+ hoverOpen: set,
71
+ hoverClose: (item, details) => {
72
+ if (requested() === item && pressedItem !== item) close(details);
73
+ },
74
+ close,
75
+ hoverDelay: () => requested() !== null || Date.now() - closedAt < (props.timeout ?? 400) ? 0 : props.delay ?? 300,
76
+ closeDelay: () => props.closeDelay ?? 200
77
+ };
78
+ const elementProps = mergeElementProps({
79
+ onFocusOut: (event) => {
80
+ if (event.relatedTarget && !inside(event.relatedTarget) && !pressOutside) close({
81
+ reason: "focus-out",
82
+ event
83
+ });
84
+ },
85
+ ref: (element) => {
86
+ nav = element;
87
+ }
88
+ }, omit(props, "render", "value", "defaultValue", "onValueChange", "delay", "closeDelay", "timeout"));
89
+ return createComponent(NavigationMenuRootContext, {
90
+ value: context,
91
+ get children() {
92
+ return renderElement(props, elementProps, (p) => (() => {
93
+ var _el$ = _tmpl$();
94
+ spread(_el$, p, false);
95
+ return _el$;
96
+ })());
97
+ }
98
+ });
99
+ }
100
+ export { NavigationMenuRoot };
@@ -0,0 +1,10 @@
1
+ import { isDev } from "@solidjs/web";
2
+ import { createContext, useContext } from "solid-js";
3
+ // src/navigation-menu/root/NavigationMenuRootContext.ts
4
+ var NavigationMenuRootContext = createContext(null);
5
+ function useNavigationMenuRootContext(part) {
6
+ const context = useContext(NavigationMenuRootContext);
7
+ if (isDev && !context) throw new Error(`[menhir] NavigationMenu.${part} must be placed within <NavigationMenu.Root>.`);
8
+ return context;
9
+ }
10
+ export { NavigationMenuRootContext, useNavigationMenuRootContext };