menhir 0.1.4 → 0.1.5

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 (99) hide show
  1. package/README.md +2 -2
  2. package/dist/context-menu/index.js +2 -0
  3. package/dist/context-menu/index.parts.js +13 -0
  4. package/dist/context-menu/popup/ContextMenuPopup.js +30 -0
  5. package/dist/context-menu/root/ContextMenuContext.js +11 -0
  6. package/dist/context-menu/root/ContextMenuRoot.js +23 -0
  7. package/dist/context-menu/trigger/ContextMenuTrigger.js +47 -0
  8. package/dist/core/mergeElementProps.js +7 -1
  9. package/dist/menu/checkbox-item/MenuCheckboxItem.js +28 -0
  10. package/dist/menu/group/MenuGroup.js +40 -0
  11. package/dist/menu/group/MenuGroupContext.js +10 -0
  12. package/dist/menu/group-label/MenuGroupLabel.js +27 -0
  13. package/dist/menu/index.js +2 -0
  14. package/dist/menu/index.parts.js +13 -0
  15. package/dist/menu/item/MenuItem.js +30 -0
  16. package/dist/menu/item/menuItemProps.js +77 -0
  17. package/dist/menu/item/renderCheckedItem.js +52 -0
  18. package/dist/menu/item-indicator/MenuItemIndicator.js +23 -0
  19. package/dist/menu/item-indicator/MenuItemIndicatorContext.js +10 -0
  20. package/dist/menu/popup/MenuPopup.js +123 -0
  21. package/dist/menu/radio-group/MenuRadioGroup.js +39 -0
  22. package/dist/menu/radio-item/MenuRadioItem.js +21 -0
  23. package/dist/menu/root/MenuContext.js +114 -0
  24. package/dist/menu/root/MenuRoot.js +19 -0
  25. package/dist/menu/submenu/MenuSubmenu.js +18 -0
  26. package/dist/menu/submenu-trigger/MenuSubmenuTrigger.js +114 -0
  27. package/dist/menu/trigger/MenuTrigger.js +67 -0
  28. package/dist/menubar/index.js +2 -0
  29. package/dist/menubar/index.parts.js +14 -0
  30. package/dist/menubar/menu/MenubarMenu.js +30 -0
  31. package/dist/menubar/root/MenubarContext.js +10 -0
  32. package/dist/menubar/root/MenubarRoot.js +68 -0
  33. package/dist/menubar/trigger/MenubarTrigger.js +73 -0
  34. package/dist/source/context-menu/index.d.ts +1 -0
  35. package/dist/source/context-menu/index.js +1 -0
  36. package/dist/source/context-menu/index.parts.d.ts +12 -0
  37. package/dist/source/context-menu/index.parts.js +13 -0
  38. package/dist/source/context-menu/popup/ContextMenuPopup.d.ts +12 -0
  39. package/dist/source/context-menu/popup/ContextMenuPopup.jsx +36 -0
  40. package/dist/source/context-menu/root/ContextMenuContext.d.ts +18 -0
  41. package/dist/source/context-menu/root/ContextMenuContext.js +12 -0
  42. package/dist/source/context-menu/root/ContextMenuRoot.d.ts +12 -0
  43. package/dist/source/context-menu/root/ContextMenuRoot.jsx +16 -0
  44. package/dist/source/context-menu/trigger/ContextMenuTrigger.d.ts +15 -0
  45. package/dist/source/context-menu/trigger/ContextMenuTrigger.jsx +46 -0
  46. package/dist/source/core/mergeElementProps.d.ts +8 -0
  47. package/dist/source/core/mergeElementProps.js +7 -1
  48. package/dist/source/floating/dismissal/reason.d.ts +7 -2
  49. package/dist/source/menu/checkbox-item/MenuCheckboxItem.d.ts +20 -0
  50. package/dist/source/menu/checkbox-item/MenuCheckboxItem.jsx +26 -0
  51. package/dist/source/menu/group/MenuGroup.d.ts +16 -0
  52. package/dist/source/menu/group/MenuGroup.jsx +31 -0
  53. package/dist/source/menu/group/MenuGroupContext.d.ts +10 -0
  54. package/dist/source/menu/group/MenuGroupContext.js +12 -0
  55. package/dist/source/menu/group-label/MenuGroupLabel.d.ts +11 -0
  56. package/dist/source/menu/group-label/MenuGroupLabel.jsx +25 -0
  57. package/dist/source/menu/index.d.ts +1 -0
  58. package/dist/source/menu/index.js +1 -0
  59. package/dist/source/menu/index.parts.d.ts +12 -0
  60. package/dist/source/menu/index.parts.js +12 -0
  61. package/dist/source/menu/item/MenuItem.d.ts +11 -0
  62. package/dist/source/menu/item/MenuItem.jsx +21 -0
  63. package/dist/source/menu/item/menuItemProps.d.ts +35 -0
  64. package/dist/source/menu/item/menuItemProps.js +78 -0
  65. package/dist/source/menu/item/renderCheckedItem.d.ts +22 -0
  66. package/dist/source/menu/item/renderCheckedItem.jsx +41 -0
  67. package/dist/source/menu/item-indicator/MenuItemIndicator.d.ts +12 -0
  68. package/dist/source/menu/item-indicator/MenuItemIndicator.jsx +17 -0
  69. package/dist/source/menu/item-indicator/MenuItemIndicatorContext.d.ts +11 -0
  70. package/dist/source/menu/item-indicator/MenuItemIndicatorContext.js +12 -0
  71. package/dist/source/menu/popup/MenuPopup.d.ts +41 -0
  72. package/dist/source/menu/popup/MenuPopup.jsx +156 -0
  73. package/dist/source/menu/radio-group/MenuRadioGroup.d.ts +27 -0
  74. package/dist/source/menu/radio-group/MenuRadioGroup.jsx +36 -0
  75. package/dist/source/menu/radio-item/MenuRadioItem.d.ts +14 -0
  76. package/dist/source/menu/radio-item/MenuRadioItem.jsx +19 -0
  77. package/dist/source/menu/root/MenuContext.d.ts +114 -0
  78. package/dist/source/menu/root/MenuContext.js +123 -0
  79. package/dist/source/menu/root/MenuRoot.d.ts +11 -0
  80. package/dist/source/menu/root/MenuRoot.jsx +9 -0
  81. package/dist/source/menu/submenu/MenuSubmenu.d.ts +22 -0
  82. package/dist/source/menu/submenu/MenuSubmenu.jsx +10 -0
  83. package/dist/source/menu/submenu-trigger/MenuSubmenuTrigger.d.ts +14 -0
  84. package/dist/source/menu/submenu-trigger/MenuSubmenuTrigger.jsx +121 -0
  85. package/dist/source/menu/trigger/MenuTrigger.d.ts +13 -0
  86. package/dist/source/menu/trigger/MenuTrigger.jsx +73 -0
  87. package/dist/source/menubar/index.d.ts +1 -0
  88. package/dist/source/menubar/index.js +1 -0
  89. package/dist/source/menubar/index.parts.d.ts +13 -0
  90. package/dist/source/menubar/index.parts.js +14 -0
  91. package/dist/source/menubar/menu/MenubarMenu.d.ts +11 -0
  92. package/dist/source/menubar/menu/MenubarMenu.jsx +24 -0
  93. package/dist/source/menubar/root/MenubarContext.d.ts +30 -0
  94. package/dist/source/menubar/root/MenubarContext.js +12 -0
  95. package/dist/source/menubar/root/MenubarRoot.d.ts +11 -0
  96. package/dist/source/menubar/root/MenubarRoot.jsx +77 -0
  97. package/dist/source/menubar/trigger/MenubarTrigger.d.ts +11 -0
  98. package/dist/source/menubar/trigger/MenubarTrigger.jsx +78 -0
  99. package/package.json +16 -1
@@ -0,0 +1,41 @@
1
+ import type { JSX } from "@solidjs/web";
2
+ import type { RenderProp } from "../../core/renderElement.tsx";
3
+ import type { FocusTarget } from "../../floating/focus/target.ts";
4
+ import type { Align, Side, VirtualElement } from "../../floating/positioning/createPosition.ts";
5
+ /** What the Popup is placed against: an element, or a virtual one. */
6
+ export type MenuAnchor = Element | VirtualElement;
7
+ export interface MenuPopupProps extends Omit<JSX.HTMLAttributes<HTMLElement>, "id"> {
8
+ /**
9
+ * The side of the anchor it is placed against. Default `"bottom"`, and
10
+ * for a submenu the inline end: `"right"`, or `"left"` right to left.
11
+ */
12
+ side?: Side;
13
+ /** Where along that side, following the writing direction. Default `"start"`. */
14
+ align?: Align;
15
+ /** The gap from the anchor, in pixels. Default 0. */
16
+ sideOffset?: number;
17
+ /** The shift along the anchor's side, in pixels. Default 0. */
18
+ alignOffset?: number;
19
+ /** The space kept from the viewport's edges, in pixels. Default 5. */
20
+ collisionPadding?: number;
21
+ /**
22
+ * What it is placed against instead of its trigger: an element, a virtual
23
+ * element such as a point, or a function returning one.
24
+ */
25
+ anchor?: MenuAnchor | (() => MenuAnchor | undefined);
26
+ /**
27
+ * Where focus goes once the menu has closed, instead of its trigger,
28
+ * where it returns when it was inside. An element, or a function called
29
+ * with the element the browser focused, or `null` when it found none.
30
+ */
31
+ returnFocus?: FocusTarget;
32
+ render?: RenderProp;
33
+ }
34
+ /**
35
+ * The menu itself: a `<div popover="auto" role="menu">` in the top layer,
36
+ * placed against its trigger and labelled by it. Escape, a press outside,
37
+ * Tab and activating an item close it. Place it inside `Menu.Root`, right
38
+ * after `Menu.Trigger`, or inside `Menu.Submenu`, right after
39
+ * `Menu.SubmenuTrigger`.
40
+ */
41
+ export declare function MenuPopup(props: MenuPopupProps): JSX.Element;
@@ -0,0 +1,156 @@
1
+ import { createEffect, merge, omit, untrack } from "solid-js";
2
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
3
+ import { renderElement } from "../../core/renderElement.jsx";
4
+ import { createPopoverClose } from "../../floating/dismissal/popoverClose.js";
5
+ import { createReturnFocus } from "../../floating/focus/returnFocus.js";
6
+ import { createPosition } from "../../floating/positioning/createPosition.js";
7
+ import { flip } from "../../floating/positioning/flip.js";
8
+ import { offset } from "../../floating/positioning/offset.js";
9
+ import { shift } from "../../floating/positioning/shift.js";
10
+ import { createPresence } from "../../floating/presence/presence.js";
11
+ import { useMenuContext } from "../root/MenuContext.js";
12
+ /**
13
+ * The menu itself: a `<div popover="auto" role="menu">` in the top layer,
14
+ * placed against its trigger and labelled by it. Escape, a press outside,
15
+ * Tab and activating an item close it. Place it inside `Menu.Root`, right
16
+ * after `Menu.Trigger`, or inside `Menu.Submenu`, right after
17
+ * `Menu.SubmenuTrigger`.
18
+ */
19
+ export function MenuPopup(props) {
20
+ const menu = useMenuContext();
21
+ let popup;
22
+ // Whether focus was inside as the menu closed, read on `beforetoggle`,
23
+ // before the browser moves it.
24
+ let focusInside = false;
25
+ // The Tab key of this task, which closes every menu once focus has left
26
+ // (README, "Decisions"). A timer forgets it.
27
+ let tab;
28
+ const options = {
29
+ get popup() {
30
+ return popup;
31
+ },
32
+ get trigger() {
33
+ return menu.trigger;
34
+ },
35
+ get open() {
36
+ return menu.open();
37
+ },
38
+ onDismiss: (details) => menu.close(details),
39
+ // The browser returns focus to the trigger only for the first popover
40
+ // of a stack (HTML standard), so a submenu left focus on the `<body>`.
41
+ // A focus that left the menu before it closed stays.
42
+ get returnFocus() {
43
+ return (props.returnFocus ??
44
+ ((focused) => focused ??
45
+ (focusInside ? menu.trigger : undefined)));
46
+ },
47
+ };
48
+ // Presence first: the popover is shown before it is placed and focused.
49
+ createPresence(options);
50
+ createPopoverClose(options);
51
+ createReturnFocus(options);
52
+ // Focus goes to the first item whenever the Root's menu opens, unless the
53
+ // Trigger's Up Arrow chose the last, or a menubar keeps it on the bar. A
54
+ // submenu opened by hover leaves it on its trigger (README, "Decisions").
55
+ if (!menu.parent)
56
+ createEffect(
57
+ // The items register once mounted, after a `defaultOpen` menu opens.
58
+ () => menu.open() && menu.list.items()[0], (first) => {
59
+ if (!first)
60
+ return;
61
+ if (!menu.keepFocus && !untrack(menu.list.active))
62
+ untrack(() => menu.list.move(first));
63
+ menu.keepFocus = false;
64
+ });
65
+ createPosition({
66
+ get anchor() {
67
+ const anchor = props.anchor;
68
+ return (typeof anchor === "function" ? anchor() : anchor) ?? menu.trigger;
69
+ },
70
+ get popup() {
71
+ return popup;
72
+ },
73
+ get open() {
74
+ return menu.open();
75
+ },
76
+ get side() {
77
+ if (props.side)
78
+ return props.side;
79
+ if (!menu.parent)
80
+ return "bottom";
81
+ // A submenu opens toward the inline end of its trigger.
82
+ const trigger = menu.trigger;
83
+ return trigger && getComputedStyle(trigger).direction === "rtl"
84
+ ? "left"
85
+ : "right";
86
+ },
87
+ get align() {
88
+ return props.align ?? "start";
89
+ },
90
+ get middleware() {
91
+ const padding = props.collisionPadding ?? 5;
92
+ return [
93
+ offset({
94
+ mainAxis: props.sideOffset ?? 0,
95
+ alignmentAxis: props.alignOffset ?? 0,
96
+ }),
97
+ flip({ padding }),
98
+ shift({ padding }),
99
+ ];
100
+ },
101
+ });
102
+ const ownProps = merge(menu.attributes, {
103
+ id: menu.popupId,
104
+ popover: "auto",
105
+ role: "menu",
106
+ // The consumer's, else the trigger's id. Kept out of the merge, where a
107
+ // wrapper's `aria-labelledby={undefined}` would remove the trigger's
108
+ // ("`undefined` is a value", PLAN.md section 8).
109
+ get "aria-labelledby"() {
110
+ return props["aria-labelledby"] ?? menu.triggerId();
111
+ },
112
+ onBeforeToggle: (event) => {
113
+ if (event.target !== popup)
114
+ return;
115
+ focusInside =
116
+ event.newState === "closed" &&
117
+ (menu.focusWasInside || popup.contains(document.activeElement));
118
+ menu.focusWasInside = false;
119
+ },
120
+ // A submenu's keys and focus bubble through the Root's Popup, which
121
+ // holds them: Tab out of any menu closes every menu.
122
+ onKeyDown: (event) => {
123
+ if (menu.parent || event.key !== "Tab")
124
+ return;
125
+ const noted = (tab = event);
126
+ setTimeout(() => {
127
+ if (tab === noted)
128
+ tab = undefined;
129
+ });
130
+ },
131
+ // Tab and Shift+Tab leave the menu that has focus: out of the Root's
132
+ // menu, or from a submenu back to its trigger, which keeps the parent's
133
+ // tab stop. Either closes every menu, as the pattern says.
134
+ onFocusOut: (event) => {
135
+ const left = event.target.closest("[popover]");
136
+ if (!tab ||
137
+ menu.parent ||
138
+ left?.contains(event.relatedTarget))
139
+ return;
140
+ tab = undefined;
141
+ menu.close({ reason: "focus-out", event });
142
+ // Shift+Tab from a submenu lands on its trigger, inside the menu that
143
+ // closes: focus goes on to the Trigger, where it leaves the menu.
144
+ if (popup.contains(event.relatedTarget))
145
+ menu.trigger?.focus();
146
+ },
147
+ ref: (element) => {
148
+ popup = element;
149
+ menu.popup = element;
150
+ },
151
+ });
152
+ const elementProps = mergeElementProps(ownProps, omit(props, "render", "side", "align", "sideOffset", "alignOffset", "collisionPadding", "anchor", "returnFocus", "aria-labelledby",
153
+ // Kept out, as typed: a stray one would break `popovertarget`.
154
+ "id"));
155
+ return renderElement(props, elementProps, (p) => <div {...p}/>);
156
+ }
@@ -0,0 +1,27 @@
1
+ import type { JSX } from "@solidjs/web";
2
+ import type { RenderProp } from "../../core/renderElement.tsx";
3
+ export interface MenuRadioGroupProps extends JSX.HTMLAttributes<HTMLElement> {
4
+ /** The checked item's value, when controlled; `null` for none. */
5
+ value?: string | null;
6
+ /** The checked item's value at first, when uncontrolled. Default `null`. */
7
+ defaultValue?: string | null;
8
+ /**
9
+ * Called with the new value when the user checks an item. When
10
+ * controlled, the group changes only if `value` does.
11
+ */
12
+ onValueChange?: (value: string) => void;
13
+ render?: RenderProp;
14
+ }
15
+ /** What a radio group shares with its items. */
16
+ export interface MenuRadioGroupContextValue {
17
+ value(): string | null;
18
+ setValue(value: string): void;
19
+ }
20
+ export declare const MenuRadioGroupContext: import("solid-js").Context<MenuRadioGroupContextValue | null>;
21
+ export declare function useMenuRadioGroupContext(): MenuRadioGroupContextValue;
22
+ /**
23
+ * Options of which one is checked: a `<div role="group">` that holds the
24
+ * checked `Menu.RadioItem`'s value, named by its `Menu.GroupLabel`. Place
25
+ * it inside `Menu.Popup`.
26
+ */
27
+ export declare function MenuRadioGroup(props: MenuRadioGroupProps): JSX.Element;
@@ -0,0 +1,36 @@
1
+ import { createContext, omit, useContext } from "solid-js";
2
+ import { isDev } from "@solidjs/web";
3
+ import { createControllableSignal } from "../../core/createControllableSignal.js";
4
+ import { renderGroup } from "../group/MenuGroup.jsx";
5
+ // `null` outside a radio group, so that the part can name what is missing.
6
+ export const MenuRadioGroupContext = createContext(null);
7
+ export function useMenuRadioGroupContext() {
8
+ const context = useContext(MenuRadioGroupContext);
9
+ // Production builds drop the check, and its message, with `isDev`.
10
+ if (isDev && !context) {
11
+ throw new Error("[menhir] Menu.RadioItem must be placed within <Menu.RadioGroup>.");
12
+ }
13
+ return context;
14
+ }
15
+ /**
16
+ * Options of which one is checked: a `<div role="group">` that holds the
17
+ * checked `Menu.RadioItem`'s value, named by its `Menu.GroupLabel`. Place
18
+ * it inside `Menu.Popup`.
19
+ */
20
+ export function MenuRadioGroup(props) {
21
+ const [value, setValue] = createControllableSignal({
22
+ get value() {
23
+ return props.value;
24
+ },
25
+ get defaultValue() {
26
+ return props.defaultValue ?? null;
27
+ },
28
+ onChange: (next) => {
29
+ if (next !== null)
30
+ props.onValueChange?.(next);
31
+ },
32
+ });
33
+ return (<MenuRadioGroupContext value={{ value, setValue }}>
34
+ {renderGroup(props, omit(props, "render", "value", "defaultValue", "onValueChange"))}
35
+ </MenuRadioGroupContext>);
36
+ }
@@ -0,0 +1,14 @@
1
+ import type { RenderProp } from "../../core/renderElement.tsx";
2
+ import type { MenuItemBaseProps } from "../item/menuItemProps.ts";
3
+ export interface MenuRadioItemProps extends MenuItemBaseProps {
4
+ /** The value its group holds while it is checked. */
5
+ value: string;
6
+ render?: RenderProp;
7
+ }
8
+ /**
9
+ * One of a radio group's options: a `<div role="menuitemradio">` with
10
+ * `aria-checked`. A click and Enter check it and close every open menu;
11
+ * Space checks it and keeps the menu open. Place it inside
12
+ * `Menu.RadioGroup`.
13
+ */
14
+ export declare function MenuRadioItem(props: MenuRadioItemProps): import("@solidjs/web").JSX.Element;
@@ -0,0 +1,19 @@
1
+ import { omit } from "solid-js";
2
+ import { renderCheckedItem } from "../item/renderCheckedItem.jsx";
3
+ import { useMenuRadioGroupContext } from "../radio-group/MenuRadioGroup.jsx";
4
+ /**
5
+ * One of a radio group's options: a `<div role="menuitemradio">` with
6
+ * `aria-checked`. A click and Enter check it and close every open menu;
7
+ * Space checks it and keeps the menu open. Place it inside
8
+ * `Menu.RadioGroup`.
9
+ */
10
+ export function MenuRadioItem(props) {
11
+ const group = useMenuRadioGroupContext();
12
+ return renderCheckedItem(props, omit(props, "render", "disabled", "label", "onClick", "value"), {
13
+ role: "menuitemradio",
14
+ get checked() {
15
+ return group.value() === props.value;
16
+ },
17
+ onCheck: () => group.setValue(props.value),
18
+ });
19
+ }
@@ -0,0 +1,114 @@
1
+ import type { OpenChangeDetails } from "../../floating/dismissal/reason.ts";
2
+ import type { ListItem, ListNavigation } from "../../floating/list-navigation/listNavigation.ts";
3
+ import type { Typeahead } from "../../floating/typeahead/typeahead.ts";
4
+ /**
5
+ * A menu's state as `data-*` attributes, each `""` when it applies and
6
+ * absent otherwise: one of `data-open` and `data-closed`.
7
+ */
8
+ export type MenuStateAttributes = {
9
+ readonly "data-open": "" | undefined;
10
+ readonly "data-closed": "" | undefined;
11
+ };
12
+ /** An item of a menu, as its list navigation knows it. */
13
+ export interface MenuItemEntry extends ListItem {
14
+ /** The submenu a SubmenuTrigger opens. */
15
+ readonly submenu?: Menu;
16
+ /** Whether the item is disabled, which opens no submenu. */
17
+ readonly disabled?: boolean;
18
+ }
19
+ /**
20
+ * One menu: the Root's, or a Submenu's. Its Popup, items and trigger share
21
+ * it through context.
22
+ */
23
+ export interface Menu {
24
+ /** Whether the menu is open. */
25
+ open(): boolean;
26
+ /** Asks to open or close this menu, saying why. */
27
+ setOpen(open: boolean, details: OpenChangeDetails): void;
28
+ /**
29
+ * Closes this menu and its open submenus. `cascade` marks a submenu
30
+ * closed with the menu around it.
31
+ */
32
+ close(details: OpenChangeDetails, cascade?: boolean): void;
33
+ /** Closes the open submenus of this menu, keeping it open. */
34
+ closeSubmenus(details: OpenChangeDetails): void;
35
+ /** Closes every menu, from the Root's down, as an activated item does. */
36
+ closeAll(details: OpenChangeDetails): void;
37
+ /** Opens the menu by a key, with focus on its first item. */
38
+ openFromKey(details: OpenChangeDetails): void;
39
+ /** The menu around a submenu; `undefined` for the Root's. */
40
+ readonly parent: Menu | undefined;
41
+ /**
42
+ * The submenus inside this menu, closed with it and by the keys that move
43
+ * through it. Not reactive: read only when closing.
44
+ */
45
+ readonly submenus: Set<Menu>;
46
+ readonly list: ListNavigation<MenuItemEntry>;
47
+ readonly typeahead: Typeahead;
48
+ /**
49
+ * The Popup's `id`, which the trigger names in `popovertarget` or
50
+ * `aria-controls` before the Popup is rendered.
51
+ */
52
+ readonly popupId: string;
53
+ /** The trigger's `id`, which labels the Popup; set by the trigger. */
54
+ triggerId(): string | false | undefined;
55
+ /** The trigger's element, once rendered: the anchor by default. */
56
+ trigger: HTMLElement | undefined;
57
+ /** A submenu's trigger, as an item of its parent menu. */
58
+ triggerItem: MenuItemEntry | undefined;
59
+ /** The Popup's element, once rendered. */
60
+ popup: HTMLElement | undefined;
61
+ /**
62
+ * Whether focus was inside the Popup when this menu began to close with
63
+ * its submenus: hiding them first moves focus out before the Popup's
64
+ * own `beforetoggle`, which reads it.
65
+ */
66
+ focusWasInside: boolean;
67
+ /**
68
+ * Whether the next opening leaves focus where it is, instead of on the
69
+ * first item: set by a menubar before it opens a menu by moving along
70
+ * the bar or by hover, and cleared once the menu has opened, or by the
71
+ * menubar once the opening has been refused.
72
+ */
73
+ keepFocus: boolean;
74
+ /**
75
+ * Whether hover opened this menu and the pointer is still on its
76
+ * trigger: a press there then keeps it open and moves focus into it,
77
+ * where a press on an open menu's trigger closes it. Set by a menubar.
78
+ */
79
+ hoverOpened: boolean;
80
+ /**
81
+ * Set by a menubar on each of its menus: moves along the bar, `1` to the
82
+ * next item and `-1` to the previous, from the key that points into an
83
+ * item with no submenu, or out of the menu.
84
+ */
85
+ along?: (step: 1 | -1, event: KeyboardEvent) => void;
86
+ /** The `data-*` attributes of the trigger and the Popup. */
87
+ readonly attributes: MenuStateAttributes;
88
+ /** A submenu's hover delays, from `Menu.Submenu`. */
89
+ readonly delays: MenuDelayProps;
90
+ }
91
+ /** The hover delays of `Menu.Submenu`, in milliseconds. */
92
+ export interface MenuDelayProps {
93
+ readonly delay?: number;
94
+ readonly closeDelay?: number;
95
+ }
96
+ /** The state props of `Menu.Root` and `Menu.Submenu`. */
97
+ export interface MenuStateProps {
98
+ /** Whether the menu is open, when controlled. */
99
+ open?: boolean;
100
+ /** Whether the menu is open at first, when uncontrolled. */
101
+ defaultOpen?: boolean;
102
+ /**
103
+ * Called with the new value, and why, when the user opens or closes the
104
+ * menu. When controlled, the menu changes only if `open` does.
105
+ */
106
+ onOpenChange?: (open: boolean, details: OpenChangeDetails) => void;
107
+ }
108
+ /**
109
+ * Creates a menu's state, its list navigation and its typeahead. A submenu
110
+ * passes its `parent`, which closes it with itself.
111
+ */
112
+ export declare function createMenu(props: MenuStateProps & MenuDelayProps, parent?: Menu): Menu;
113
+ export declare const MenuContext: import("solid-js").Context<Menu | null>;
114
+ export declare function useMenuContext(): Menu;
@@ -0,0 +1,123 @@
1
+ import { isDev } from "@solidjs/web";
2
+ import { createContext, createUniqueId, onCleanup, useContext } from "solid-js";
3
+ import { createControllableSignal } from "../../core/createControllableSignal.js";
4
+ import { createRovingList } from "../../floating/list-navigation/roving.js";
5
+ import { createTypeahead } from "../../floating/typeahead/typeahead.js";
6
+ /**
7
+ * Creates a menu's state, its list navigation and its typeahead. A submenu
8
+ * passes its `parent`, which closes it with itself.
9
+ */
10
+ export function createMenu(props, parent) {
11
+ const [open, setOpen] = createControllableSignal({
12
+ get value() {
13
+ return props.open;
14
+ },
15
+ get defaultValue() {
16
+ return props.defaultOpen ?? false;
17
+ },
18
+ onChange: (value, details) => props.onOpenChange?.(value, details),
19
+ });
20
+ const submenus = new Set();
21
+ const list = createRovingList({
22
+ get open() {
23
+ return open();
24
+ },
25
+ // A menu is short: the pattern allows wrapping (README, "Decisions").
26
+ wrap: true,
27
+ onArrowIn: (item, event) => {
28
+ event.preventDefault();
29
+ if (item.submenu) {
30
+ if (!item.disabled)
31
+ item.submenu.openFromKey({ reason: "arrow-key", event });
32
+ return;
33
+ }
34
+ // Right Arrow on an item with no submenu moves along a menubar, from
35
+ // any depth, and does nothing in a menu that a button opened (menu
36
+ // and menubar pattern).
37
+ let root = menu;
38
+ while (root.parent)
39
+ root = root.parent;
40
+ root.along?.(1, event);
41
+ },
42
+ // Left Arrow closes a submenu; in a menubar's menu it moves along the
43
+ // bar, and in a Root's menu it does nothing.
44
+ onArrowOut: (_item, event) => {
45
+ if (!parent) {
46
+ menu.along?.(-1, event);
47
+ return;
48
+ }
49
+ event.preventDefault();
50
+ menu.close({ reason: "arrow-key", event });
51
+ parent.list.move(menu.triggerItem);
52
+ },
53
+ });
54
+ const typeahead = createTypeahead({
55
+ get open() {
56
+ return open();
57
+ },
58
+ get items() {
59
+ return list.items();
60
+ },
61
+ get active() {
62
+ return list.active();
63
+ },
64
+ move: (item) => list.move(item),
65
+ });
66
+ const menu = {
67
+ open,
68
+ setOpen,
69
+ close: (details, cascade) => {
70
+ if (!cascade)
71
+ menu.focusWasInside = !!menu.popup?.contains(document.activeElement);
72
+ for (const submenu of submenus)
73
+ submenu.close(details, true);
74
+ setOpen(false, details);
75
+ },
76
+ closeSubmenus: (details) => {
77
+ for (const submenu of submenus)
78
+ submenu.close(details);
79
+ },
80
+ closeAll: (details) => parent ? parent.closeAll(details) : menu.close(details),
81
+ openFromKey: (details) => {
82
+ setOpen(true, details);
83
+ // While closed, the move waits for the menu to open (list navigation).
84
+ list.move(list.items()[0]);
85
+ },
86
+ parent,
87
+ submenus,
88
+ list,
89
+ typeahead,
90
+ popupId: createUniqueId(),
91
+ triggerId: () => undefined,
92
+ trigger: undefined,
93
+ triggerItem: undefined,
94
+ popup: undefined,
95
+ focusWasInside: false,
96
+ keepFocus: false,
97
+ hoverOpened: false,
98
+ delays: props,
99
+ attributes: {
100
+ get "data-open"() {
101
+ return open() ? "" : undefined;
102
+ },
103
+ get "data-closed"() {
104
+ return open() ? undefined : "";
105
+ },
106
+ },
107
+ };
108
+ if (parent) {
109
+ parent.submenus.add(menu);
110
+ onCleanup(() => parent.submenus.delete(menu));
111
+ }
112
+ return menu;
113
+ }
114
+ // `null` outside a Root, so that the part can name what is missing.
115
+ export const MenuContext = createContext(null);
116
+ export function useMenuContext() {
117
+ const context = useContext(MenuContext);
118
+ // Production builds drop the check, and its message, with `isDev`.
119
+ if (isDev && !context) {
120
+ throw new Error("[menhir] Menu parts must be placed within <Menu.Root>, Context Menu parts within <ContextMenu.Root>, and Menubar parts within <Menubar.Menu>.");
121
+ }
122
+ return context;
123
+ }
@@ -0,0 +1,11 @@
1
+ import type { JSX } from "@solidjs/web";
2
+ import type { MenuStateProps } from "./MenuContext.ts";
3
+ export interface MenuRootProps extends MenuStateProps {
4
+ children?: JSX.Element;
5
+ }
6
+ /**
7
+ * A menu that a button opens: holds whether it is open and shares it with
8
+ * `Menu.Trigger`, `Menu.Popup` and the items inside it. Renders no element
9
+ * of its own.
10
+ */
11
+ export declare function MenuRoot(props: MenuRootProps): JSX.Element;
@@ -0,0 +1,9 @@
1
+ import { createMenu, MenuContext } from "./MenuContext.js";
2
+ /**
3
+ * A menu that a button opens: holds whether it is open and shares it with
4
+ * `Menu.Trigger`, `Menu.Popup` and the items inside it. Renders no element
5
+ * of its own.
6
+ */
7
+ export function MenuRoot(props) {
8
+ return <MenuContext value={createMenu(props)}>{props.children}</MenuContext>;
9
+ }
@@ -0,0 +1,22 @@
1
+ import type { JSX } from "@solidjs/web";
2
+ import type { MenuStateProps } from "../root/MenuContext.ts";
3
+ export interface MenuSubmenuProps extends MenuStateProps {
4
+ /**
5
+ * How long the pointer rests on the trigger before the submenu opens, in
6
+ * milliseconds. Default 100.
7
+ */
8
+ delay?: number;
9
+ /**
10
+ * How long after the pointer has left the trigger and the submenu it
11
+ * closes, in milliseconds. Default 0: the safe area carries the pointer
12
+ * across.
13
+ */
14
+ closeDelay?: number;
15
+ children?: JSX.Element;
16
+ }
17
+ /**
18
+ * A menu inside a menu: holds whether it is open and shares it with
19
+ * `Menu.SubmenuTrigger` and the `Menu.Popup` right after it. Renders no
20
+ * element of its own. Place it inside `Menu.Popup`.
21
+ */
22
+ export declare function MenuSubmenu(props: MenuSubmenuProps): JSX.Element;
@@ -0,0 +1,10 @@
1
+ import { createMenu, MenuContext, useMenuContext, } from "../root/MenuContext.js";
2
+ /**
3
+ * A menu inside a menu: holds whether it is open and shares it with
4
+ * `Menu.SubmenuTrigger` and the `Menu.Popup` right after it. Renders no
5
+ * element of its own. Place it inside `Menu.Popup`.
6
+ */
7
+ export function MenuSubmenu(props) {
8
+ const menu = createMenu(props, useMenuContext());
9
+ return <MenuContext value={menu}>{props.children}</MenuContext>;
10
+ }
@@ -0,0 +1,14 @@
1
+ import type { RenderProp } from "../../core/renderElement.tsx";
2
+ import type { MenuItemBaseProps } from "../item/menuItemProps.ts";
3
+ export interface MenuSubmenuTriggerProps extends MenuItemBaseProps {
4
+ render?: RenderProp;
5
+ }
6
+ /**
7
+ * The item that opens a submenu: a `<div role="menuitem"
8
+ * aria-haspopup="menu">` with `aria-expanded`, an item of the menu around
9
+ * its `Menu.Submenu`. Right Arrow, Enter and Space open the submenu with
10
+ * focus on its first item; the pointer resting on it, or a click, opens it
11
+ * and leaves focus here. Place it inside `Menu.Submenu`, right before the
12
+ * submenu's `Menu.Popup`.
13
+ */
14
+ export declare function MenuSubmenuTrigger(props: MenuSubmenuTriggerProps): import("@solidjs/web").JSX.Element;