menhir 0.1.4 → 0.1.6

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 (189) hide show
  1. package/README.md +2 -2
  2. package/dist/combobox/clear/ComboboxClear.js +39 -0
  3. package/dist/combobox/filter/includesText.js +24 -0
  4. package/dist/combobox/group/ComboboxGroup.js +32 -0
  5. package/dist/combobox/group/ComboboxGroupContext.js +10 -0
  6. package/dist/combobox/group-label/ComboboxGroupLabel.js +26 -0
  7. package/dist/combobox/index.js +3 -0
  8. package/dist/combobox/index.parts.js +12 -0
  9. package/dist/combobox/input/ComboboxInput.js +160 -0
  10. package/dist/combobox/item/ComboboxItem.js +98 -0
  11. package/dist/combobox/item/ComboboxItemContext.js +10 -0
  12. package/dist/combobox/item-indicator/ComboboxItemIndicator.js +22 -0
  13. package/dist/combobox/list/ComboboxList.js +26 -0
  14. package/dist/combobox/popup/ComboboxPopup.js +78 -0
  15. package/dist/combobox/root/ComboboxContext.js +137 -0
  16. package/dist/combobox/root/ComboboxRoot.js +21 -0
  17. package/dist/combobox/status/ComboboxStatus.js +31 -0
  18. package/dist/combobox/trigger/ComboboxTrigger.js +47 -0
  19. package/dist/context-menu/index.js +2 -0
  20. package/dist/context-menu/index.parts.js +13 -0
  21. package/dist/context-menu/popup/ContextMenuPopup.js +30 -0
  22. package/dist/context-menu/root/ContextMenuContext.js +11 -0
  23. package/dist/context-menu/root/ContextMenuRoot.js +23 -0
  24. package/dist/context-menu/trigger/ContextMenuTrigger.js +47 -0
  25. package/dist/core/mergeElementProps.js +7 -1
  26. package/dist/menu/checkbox-item/MenuCheckboxItem.js +28 -0
  27. package/dist/menu/group/MenuGroup.js +40 -0
  28. package/dist/menu/group/MenuGroupContext.js +10 -0
  29. package/dist/menu/group-label/MenuGroupLabel.js +27 -0
  30. package/dist/menu/index.js +2 -0
  31. package/dist/menu/index.parts.js +13 -0
  32. package/dist/menu/item/MenuItem.js +30 -0
  33. package/dist/menu/item/menuItemProps.js +77 -0
  34. package/dist/menu/item/renderCheckedItem.js +52 -0
  35. package/dist/menu/item-indicator/MenuItemIndicator.js +23 -0
  36. package/dist/menu/item-indicator/MenuItemIndicatorContext.js +10 -0
  37. package/dist/menu/popup/MenuPopup.js +123 -0
  38. package/dist/menu/radio-group/MenuRadioGroup.js +39 -0
  39. package/dist/menu/radio-item/MenuRadioItem.js +21 -0
  40. package/dist/menu/root/MenuContext.js +114 -0
  41. package/dist/menu/root/MenuRoot.js +19 -0
  42. package/dist/menu/submenu/MenuSubmenu.js +18 -0
  43. package/dist/menu/submenu-trigger/MenuSubmenuTrigger.js +114 -0
  44. package/dist/menu/trigger/MenuTrigger.js +67 -0
  45. package/dist/menubar/index.js +2 -0
  46. package/dist/menubar/index.parts.js +14 -0
  47. package/dist/menubar/menu/MenubarMenu.js +30 -0
  48. package/dist/menubar/root/MenubarContext.js +10 -0
  49. package/dist/menubar/root/MenubarRoot.js +68 -0
  50. package/dist/menubar/trigger/MenubarTrigger.js +73 -0
  51. package/dist/select/group/SelectGroup.js +32 -0
  52. package/dist/select/group/SelectGroupContext.js +10 -0
  53. package/dist/select/group-label/SelectGroupLabel.js +26 -0
  54. package/dist/select/index.js +2 -0
  55. package/dist/select/index.parts.js +9 -0
  56. package/dist/select/item/SelectItem.js +97 -0
  57. package/dist/select/item/SelectItemContext.js +10 -0
  58. package/dist/select/item-indicator/SelectItemIndicator.js +22 -0
  59. package/dist/select/popup/SelectPopup.js +92 -0
  60. package/dist/select/root/SelectContext.js +99 -0
  61. package/dist/select/root/SelectRoot.js +21 -0
  62. package/dist/select/trigger/SelectTrigger.js +163 -0
  63. package/dist/select/value/SelectValue.js +32 -0
  64. package/dist/source/combobox/clear/ComboboxClear.d.ts +12 -0
  65. package/dist/source/combobox/clear/ComboboxClear.jsx +36 -0
  66. package/dist/source/combobox/filter/includesText.d.ts +12 -0
  67. package/dist/source/combobox/filter/includesText.js +26 -0
  68. package/dist/source/combobox/group/ComboboxGroup.d.ts +10 -0
  69. package/dist/source/combobox/group/ComboboxGroup.jsx +23 -0
  70. package/dist/source/combobox/group/ComboboxGroupContext.d.ts +10 -0
  71. package/dist/source/combobox/group/ComboboxGroupContext.js +12 -0
  72. package/dist/source/combobox/group-label/ComboboxGroupLabel.d.ts +10 -0
  73. package/dist/source/combobox/group-label/ComboboxGroupLabel.jsx +24 -0
  74. package/dist/source/combobox/index.d.ts +2 -0
  75. package/dist/source/combobox/index.js +4 -0
  76. package/dist/source/combobox/index.parts.d.ts +11 -0
  77. package/dist/source/combobox/index.parts.js +11 -0
  78. package/dist/source/combobox/input/ComboboxInput.d.ts +19 -0
  79. package/dist/source/combobox/input/ComboboxInput.jsx +174 -0
  80. package/dist/source/combobox/item/ComboboxItem.d.ts +25 -0
  81. package/dist/source/combobox/item/ComboboxItem.jsx +93 -0
  82. package/dist/source/combobox/item/ComboboxItemContext.d.ts +11 -0
  83. package/dist/source/combobox/item/ComboboxItemContext.js +12 -0
  84. package/dist/source/combobox/item-indicator/ComboboxItemIndicator.d.ts +11 -0
  85. package/dist/source/combobox/item-indicator/ComboboxItemIndicator.jsx +16 -0
  86. package/dist/source/combobox/list/ComboboxList.d.ts +12 -0
  87. package/dist/source/combobox/list/ComboboxList.jsx +17 -0
  88. package/dist/source/combobox/popup/ComboboxPopup.d.ts +34 -0
  89. package/dist/source/combobox/popup/ComboboxPopup.jsx +76 -0
  90. package/dist/source/combobox/root/ComboboxContext.d.ts +132 -0
  91. package/dist/source/combobox/root/ComboboxContext.js +156 -0
  92. package/dist/source/combobox/root/ComboboxRoot.d.ts +13 -0
  93. package/dist/source/combobox/root/ComboboxRoot.jsx +13 -0
  94. package/dist/source/combobox/status/ComboboxStatus.d.ts +19 -0
  95. package/dist/source/combobox/status/ComboboxStatus.jsx +27 -0
  96. package/dist/source/combobox/trigger/ComboboxTrigger.d.ts +13 -0
  97. package/dist/source/combobox/trigger/ComboboxTrigger.jsx +40 -0
  98. package/dist/source/context-menu/index.d.ts +1 -0
  99. package/dist/source/context-menu/index.js +1 -0
  100. package/dist/source/context-menu/index.parts.d.ts +12 -0
  101. package/dist/source/context-menu/index.parts.js +13 -0
  102. package/dist/source/context-menu/popup/ContextMenuPopup.d.ts +12 -0
  103. package/dist/source/context-menu/popup/ContextMenuPopup.jsx +36 -0
  104. package/dist/source/context-menu/root/ContextMenuContext.d.ts +18 -0
  105. package/dist/source/context-menu/root/ContextMenuContext.js +12 -0
  106. package/dist/source/context-menu/root/ContextMenuRoot.d.ts +12 -0
  107. package/dist/source/context-menu/root/ContextMenuRoot.jsx +16 -0
  108. package/dist/source/context-menu/trigger/ContextMenuTrigger.d.ts +15 -0
  109. package/dist/source/context-menu/trigger/ContextMenuTrigger.jsx +46 -0
  110. package/dist/source/core/mergeElementProps.d.ts +8 -0
  111. package/dist/source/core/mergeElementProps.js +7 -1
  112. package/dist/source/floating/dismissal/reason.d.ts +12 -2
  113. package/dist/source/menu/checkbox-item/MenuCheckboxItem.d.ts +20 -0
  114. package/dist/source/menu/checkbox-item/MenuCheckboxItem.jsx +26 -0
  115. package/dist/source/menu/group/MenuGroup.d.ts +16 -0
  116. package/dist/source/menu/group/MenuGroup.jsx +31 -0
  117. package/dist/source/menu/group/MenuGroupContext.d.ts +10 -0
  118. package/dist/source/menu/group/MenuGroupContext.js +12 -0
  119. package/dist/source/menu/group-label/MenuGroupLabel.d.ts +11 -0
  120. package/dist/source/menu/group-label/MenuGroupLabel.jsx +25 -0
  121. package/dist/source/menu/index.d.ts +1 -0
  122. package/dist/source/menu/index.js +1 -0
  123. package/dist/source/menu/index.parts.d.ts +12 -0
  124. package/dist/source/menu/index.parts.js +12 -0
  125. package/dist/source/menu/item/MenuItem.d.ts +11 -0
  126. package/dist/source/menu/item/MenuItem.jsx +21 -0
  127. package/dist/source/menu/item/menuItemProps.d.ts +35 -0
  128. package/dist/source/menu/item/menuItemProps.js +78 -0
  129. package/dist/source/menu/item/renderCheckedItem.d.ts +22 -0
  130. package/dist/source/menu/item/renderCheckedItem.jsx +41 -0
  131. package/dist/source/menu/item-indicator/MenuItemIndicator.d.ts +12 -0
  132. package/dist/source/menu/item-indicator/MenuItemIndicator.jsx +17 -0
  133. package/dist/source/menu/item-indicator/MenuItemIndicatorContext.d.ts +11 -0
  134. package/dist/source/menu/item-indicator/MenuItemIndicatorContext.js +12 -0
  135. package/dist/source/menu/popup/MenuPopup.d.ts +41 -0
  136. package/dist/source/menu/popup/MenuPopup.jsx +156 -0
  137. package/dist/source/menu/radio-group/MenuRadioGroup.d.ts +27 -0
  138. package/dist/source/menu/radio-group/MenuRadioGroup.jsx +36 -0
  139. package/dist/source/menu/radio-item/MenuRadioItem.d.ts +14 -0
  140. package/dist/source/menu/radio-item/MenuRadioItem.jsx +19 -0
  141. package/dist/source/menu/root/MenuContext.d.ts +114 -0
  142. package/dist/source/menu/root/MenuContext.js +123 -0
  143. package/dist/source/menu/root/MenuRoot.d.ts +11 -0
  144. package/dist/source/menu/root/MenuRoot.jsx +9 -0
  145. package/dist/source/menu/submenu/MenuSubmenu.d.ts +22 -0
  146. package/dist/source/menu/submenu/MenuSubmenu.jsx +10 -0
  147. package/dist/source/menu/submenu-trigger/MenuSubmenuTrigger.d.ts +14 -0
  148. package/dist/source/menu/submenu-trigger/MenuSubmenuTrigger.jsx +121 -0
  149. package/dist/source/menu/trigger/MenuTrigger.d.ts +13 -0
  150. package/dist/source/menu/trigger/MenuTrigger.jsx +73 -0
  151. package/dist/source/menubar/index.d.ts +1 -0
  152. package/dist/source/menubar/index.js +1 -0
  153. package/dist/source/menubar/index.parts.d.ts +13 -0
  154. package/dist/source/menubar/index.parts.js +14 -0
  155. package/dist/source/menubar/menu/MenubarMenu.d.ts +11 -0
  156. package/dist/source/menubar/menu/MenubarMenu.jsx +24 -0
  157. package/dist/source/menubar/root/MenubarContext.d.ts +30 -0
  158. package/dist/source/menubar/root/MenubarContext.js +12 -0
  159. package/dist/source/menubar/root/MenubarRoot.d.ts +11 -0
  160. package/dist/source/menubar/root/MenubarRoot.jsx +77 -0
  161. package/dist/source/menubar/trigger/MenubarTrigger.d.ts +11 -0
  162. package/dist/source/menubar/trigger/MenubarTrigger.jsx +78 -0
  163. package/dist/source/select/group/SelectGroup.d.ts +10 -0
  164. package/dist/source/select/group/SelectGroup.jsx +23 -0
  165. package/dist/source/select/group/SelectGroupContext.d.ts +10 -0
  166. package/dist/source/select/group/SelectGroupContext.js +12 -0
  167. package/dist/source/select/group-label/SelectGroupLabel.d.ts +10 -0
  168. package/dist/source/select/group-label/SelectGroupLabel.jsx +24 -0
  169. package/dist/source/select/index.d.ts +1 -0
  170. package/dist/source/select/index.js +1 -0
  171. package/dist/source/select/index.parts.d.ts +8 -0
  172. package/dist/source/select/index.parts.js +8 -0
  173. package/dist/source/select/item/SelectItem.d.ts +24 -0
  174. package/dist/source/select/item/SelectItem.jsx +92 -0
  175. package/dist/source/select/item/SelectItemContext.d.ts +11 -0
  176. package/dist/source/select/item/SelectItemContext.js +12 -0
  177. package/dist/source/select/item-indicator/SelectItemIndicator.d.ts +11 -0
  178. package/dist/source/select/item-indicator/SelectItemIndicator.jsx +16 -0
  179. package/dist/source/select/popup/SelectPopup.d.ts +32 -0
  180. package/dist/source/select/popup/SelectPopup.jsx +97 -0
  181. package/dist/source/select/root/SelectContext.d.ts +106 -0
  182. package/dist/source/select/root/SelectContext.js +119 -0
  183. package/dist/source/select/root/SelectRoot.d.ts +13 -0
  184. package/dist/source/select/root/SelectRoot.jsx +11 -0
  185. package/dist/source/select/trigger/SelectTrigger.d.ts +15 -0
  186. package/dist/source/select/trigger/SelectTrigger.jsx +181 -0
  187. package/dist/source/select/value/SelectValue.d.ts +20 -0
  188. package/dist/source/select/value/SelectValue.jsx +27 -0
  189. package/package.json +26 -1
@@ -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;
@@ -0,0 +1,121 @@
1
+ import { createEffect, createUniqueId, merge, omit, onCleanup } from "solid-js";
2
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
3
+ import { renderElement } from "../../core/renderElement.jsx";
4
+ import { trackSafeArea } from "../../floating/hover/safeArea.js";
5
+ import { menuItemProps } from "../item/menuItemProps.js";
6
+ import { useMenuContext } from "../root/MenuContext.js";
7
+ /**
8
+ * The item that opens a submenu: a `<div role="menuitem"
9
+ * aria-haspopup="menu">` with `aria-expanded`, an item of the menu around
10
+ * its `Menu.Submenu`. Right Arrow, Enter and Space open the submenu with
11
+ * focus on its first item; the pointer resting on it, or a click, opens it
12
+ * and leaves focus here. Place it inside `Menu.Submenu`, right before the
13
+ * submenu's `Menu.Popup`.
14
+ */
15
+ export function MenuSubmenuTrigger(props) {
16
+ const submenu = useMenuContext();
17
+ // An item of the menu around the submenu.
18
+ const parent = submenu.parent;
19
+ const generated = createUniqueId();
20
+ // The consumer's `id`, else the generated one, which labels the submenu's
21
+ // Popup rendered after it. Kept out of the merge ("`undefined` is a
22
+ // value", PLAN.md section 8).
23
+ const id = () => props.id ?? generated;
24
+ submenu.triggerId = id;
25
+ const itemProps = menuItemProps(parent, props, {
26
+ role: "menuitem",
27
+ onActivate: (event, key) => {
28
+ const details = { reason: "trigger-press", event: key ?? event };
29
+ // A key moves focus into the submenu; a click leaves it here.
30
+ if (key)
31
+ submenu.openFromKey(details);
32
+ else
33
+ submenu.setOpen(true, details);
34
+ },
35
+ submenu,
36
+ ref: (element) => {
37
+ submenu.trigger = element;
38
+ },
39
+ });
40
+ // The pointer resting on the trigger opens the submenu after `delay`;
41
+ // leaving the trigger and the submenu closes it, across the safe area
42
+ // between them. Only a pointer that moves counts: a menu that opens
43
+ // under a still pointer gets boundary events, and opens no submenu.
44
+ let timer;
45
+ let stopSafeArea;
46
+ const cancel = () => {
47
+ clearTimeout(timer);
48
+ timer = undefined;
49
+ stopSafeArea?.();
50
+ stopSafeArea = undefined;
51
+ };
52
+ onCleanup(cancel);
53
+ const close = (event) => {
54
+ const closeDelay = submenu.delays.closeDelay ?? 0;
55
+ const details = { reason: "trigger-hover", event };
56
+ if (closeDelay > 0)
57
+ timer = setTimeout(() => submenu.close(details), closeDelay);
58
+ else
59
+ submenu.close(details);
60
+ };
61
+ /** The pointer leaving `from` on its way to `to`, or elsewhere. */
62
+ const leave = (event, to) => {
63
+ cancel();
64
+ if (event.pointerType === "touch" || !submenu.open())
65
+ return;
66
+ if (to && event.relatedTarget)
67
+ stopSafeArea = trackSafeArea(event, to, close);
68
+ else
69
+ close(event);
70
+ };
71
+ const hoverProps = {
72
+ onPointerMove: (event) => {
73
+ // List navigation's move first: the trigger becomes the active item.
74
+ itemProps.onPointerMove(event);
75
+ if (event.pointerType === "touch" || props.disabled)
76
+ return;
77
+ if (submenu.open()) {
78
+ // Back on the trigger: no longer on its way anywhere.
79
+ cancel();
80
+ return;
81
+ }
82
+ if (timer)
83
+ return;
84
+ const details = { reason: "trigger-hover", event };
85
+ timer = setTimeout(() => {
86
+ timer = undefined;
87
+ submenu.setOpen(true, details);
88
+ }, submenu.delays.delay ?? 100);
89
+ },
90
+ onPointerLeave: (event) => leave(event, submenu.popup),
91
+ };
92
+ // While open, the pointer on the submenu keeps it open, and leaving it
93
+ // closes it, unless on its way back to the trigger.
94
+ createEffect(() => submenu.open(), (open) => {
95
+ // Opened or closed, from here or elsewhere: a pending delay is moot.
96
+ cancel();
97
+ const popup = submenu.popup;
98
+ if (!open || !popup)
99
+ return;
100
+ const enter = () => cancel();
101
+ const exit = (event) => leave(event, submenu.trigger);
102
+ popup.addEventListener("pointerenter", enter);
103
+ popup.addEventListener("pointerleave", exit);
104
+ return () => {
105
+ popup.removeEventListener("pointerenter", enter);
106
+ popup.removeEventListener("pointerleave", exit);
107
+ };
108
+ });
109
+ const ownProps = merge(submenu.attributes, itemProps, hoverProps, {
110
+ get id() {
111
+ return id();
112
+ },
113
+ "aria-haspopup": "menu",
114
+ get "aria-expanded"() {
115
+ return submenu.open() ? "true" : "false";
116
+ },
117
+ "aria-controls": submenu.popupId,
118
+ });
119
+ const elementProps = mergeElementProps(ownProps, omit(props, "render", "disabled", "label", "onClick", "id"));
120
+ return renderElement(props, elementProps, (p) => <div {...p}/>);
121
+ }
@@ -0,0 +1,13 @@
1
+ import type { JSX } from "@solidjs/web";
2
+ import type { RenderProp } from "../../core/renderElement.tsx";
3
+ export interface MenuTriggerProps extends JSX.HTMLAttributes<HTMLElement> {
4
+ render?: RenderProp;
5
+ }
6
+ /**
7
+ * The button that opens and closes the menu: a `<button type="button"
8
+ * aria-haspopup="menu">` with `aria-expanded`, naming the Popup in
9
+ * `popovertarget` and `aria-controls`. Enter, Space and Down Arrow open the
10
+ * menu with focus on its first item, Up Arrow on its last. Place it inside
11
+ * `Menu.Root`, right before `Menu.Popup`.
12
+ */
13
+ export declare function MenuTrigger(props: MenuTriggerProps): JSX.Element;
@@ -0,0 +1,73 @@
1
+ import { isDev } from "@solidjs/web";
2
+ import { createUniqueId, 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 { useMenuContext } from "../root/MenuContext.js";
7
+ /**
8
+ * The button that opens and closes the menu: a `<button type="button"
9
+ * aria-haspopup="menu">` with `aria-expanded`, naming the Popup in
10
+ * `popovertarget` and `aria-controls`. Enter, Space and Down Arrow open the
11
+ * menu with focus on its first item, Up Arrow on its last. Place it inside
12
+ * `Menu.Root`, right before `Menu.Popup`.
13
+ */
14
+ export function MenuTrigger(props) {
15
+ const menu = useMenuContext();
16
+ const generated = createUniqueId();
17
+ // The consumer's `id`, else the generated one, which labels the Popup
18
+ // rendered after it. Kept out of the merge, where a wrapper's
19
+ // `id={undefined}` would remove it ("`undefined` is a value", PLAN.md
20
+ // section 8).
21
+ const id = () => props.id ?? generated;
22
+ menu.triggerId = id;
23
+ const ownProps = merge(menu.attributes, {
24
+ type: "button",
25
+ get id() {
26
+ return id();
27
+ },
28
+ // So that the browser counts a press on the Trigger as one on the
29
+ // popover, which light dismiss leaves open (README, "Decisions").
30
+ popovertarget: menu.popupId,
31
+ "aria-haspopup": "menu",
32
+ get "aria-expanded"() {
33
+ return menu.open() ? "true" : "false";
34
+ },
35
+ "aria-controls": menu.popupId,
36
+ onClick: (event) => {
37
+ // The state shows and hides the popover, in the flush that places
38
+ // it: the browser's own toggle would show it a task before.
39
+ event.preventDefault();
40
+ const details = { reason: "trigger-press", event };
41
+ const { hoverOpened } = menu;
42
+ menu.hoverOpened = false;
43
+ if (!menu.open())
44
+ menu.setOpen(true, details);
45
+ else if (hoverOpened)
46
+ menu.list.move(menu.list.items()[0]);
47
+ else
48
+ menu.close(details);
49
+ },
50
+ onKeyDown: (event) => {
51
+ const { key } = event;
52
+ if ((key !== "ArrowDown" && key !== "ArrowUp") ||
53
+ event.altKey ||
54
+ event.ctrlKey ||
55
+ event.metaKey ||
56
+ event.shiftKey)
57
+ return;
58
+ event.preventDefault();
59
+ menu.setOpen(true, { reason: "trigger-press", event });
60
+ const items = menu.list.items();
61
+ menu.list.move(key === "ArrowUp" ? items[items.length - 1] : items[0]);
62
+ },
63
+ ref: (element) => {
64
+ menu.trigger = element;
65
+ // Production builds drop the check with `isDev`.
66
+ // Menubar's Trigger is this one, in a menu of the bar.
67
+ if (isDev)
68
+ warnUnlessButton(element, menu.along ? "Menubar.Trigger" : "Menu.Trigger");
69
+ },
70
+ });
71
+ const elementProps = mergeElementProps(ownProps, omit(props, "render", "id"));
72
+ return renderElement(props, elementProps, (p) => <button {...p}/>);
73
+ }
@@ -0,0 +1 @@
1
+ export * as Menubar from "./index.parts.ts";
@@ -0,0 +1 @@
1
+ export * as Menubar from "./index.parts.js";
@@ -0,0 +1,13 @@
1
+ export { MenubarRoot as Root } from "./root/MenubarRoot.tsx";
2
+ export { MenubarMenu as Menu } from "./menu/MenubarMenu.tsx";
3
+ export { MenubarTrigger as Trigger } from "./trigger/MenubarTrigger.tsx";
4
+ export { MenuPopup as Popup } from "../menu/popup/MenuPopup.tsx";
5
+ export { MenuItem as Item } from "../menu/item/MenuItem.tsx";
6
+ export { MenuCheckboxItem as CheckboxItem } from "../menu/checkbox-item/MenuCheckboxItem.tsx";
7
+ export { MenuItemIndicator as ItemIndicator } from "../menu/item-indicator/MenuItemIndicator.tsx";
8
+ export { MenuGroup as Group } from "../menu/group/MenuGroup.tsx";
9
+ export { MenuGroupLabel as GroupLabel } from "../menu/group-label/MenuGroupLabel.tsx";
10
+ export { MenuRadioGroup as RadioGroup } from "../menu/radio-group/MenuRadioGroup.tsx";
11
+ export { MenuRadioItem as RadioItem } from "../menu/radio-item/MenuRadioItem.tsx";
12
+ export { MenuSubmenu as Submenu } from "../menu/submenu/MenuSubmenu.tsx";
13
+ export { MenuSubmenuTrigger as SubmenuTrigger } from "../menu/submenu-trigger/MenuSubmenuTrigger.tsx";
@@ -0,0 +1,14 @@
1
+ // Menu's parts, but the Root, Menu and Trigger (README, "Decisions"; #101).
2
+ export { MenubarRoot as Root } from "./root/MenubarRoot.jsx";
3
+ export { MenubarMenu as Menu } from "./menu/MenubarMenu.jsx";
4
+ export { MenubarTrigger as Trigger } from "./trigger/MenubarTrigger.jsx";
5
+ export { MenuPopup as Popup } from "../menu/popup/MenuPopup.jsx";
6
+ export { MenuItem as Item } from "../menu/item/MenuItem.jsx";
7
+ export { MenuCheckboxItem as CheckboxItem } from "../menu/checkbox-item/MenuCheckboxItem.jsx";
8
+ export { MenuItemIndicator as ItemIndicator } from "../menu/item-indicator/MenuItemIndicator.jsx";
9
+ export { MenuGroup as Group } from "../menu/group/MenuGroup.jsx";
10
+ export { MenuGroupLabel as GroupLabel } from "../menu/group-label/MenuGroupLabel.jsx";
11
+ export { MenuRadioGroup as RadioGroup } from "../menu/radio-group/MenuRadioGroup.jsx";
12
+ export { MenuRadioItem as RadioItem } from "../menu/radio-item/MenuRadioItem.jsx";
13
+ export { MenuSubmenu as Submenu } from "../menu/submenu/MenuSubmenu.jsx";
14
+ export { MenuSubmenuTrigger as SubmenuTrigger } from "../menu/submenu-trigger/MenuSubmenuTrigger.jsx";
@@ -0,0 +1,11 @@
1
+ import type { JSX } from "@solidjs/web";
2
+ import type { MenuStateProps } from "../../menu/root/MenuContext.ts";
3
+ export interface MenubarMenuProps extends MenuStateProps {
4
+ children?: JSX.Element;
5
+ }
6
+ /**
7
+ * One menu of the bar: holds whether it is open and shares it with its
8
+ * `Menubar.Trigger`, `Menubar.Popup` and the items inside it. Renders no
9
+ * element of its own. Place it inside `Menubar.Root`.
10
+ */
11
+ export declare function MenubarMenu(props: MenubarMenuProps): JSX.Element;
@@ -0,0 +1,24 @@
1
+ import { createMenu, MenuContext } from "../../menu/root/MenuContext.js";
2
+ import { useMenubarContext } from "../root/MenubarContext.js";
3
+ /**
4
+ * One menu of the bar: holds whether it is open and shares it with its
5
+ * `Menubar.Trigger`, `Menubar.Popup` and the items inside it. Renders no
6
+ * element of its own. Place it inside `Menubar.Root`.
7
+ */
8
+ export function MenubarMenu(props) {
9
+ const bar = useMenubarContext();
10
+ const menu = createMenu(props);
11
+ // Right Arrow on an item with no submenu, and Left Arrow in this menu,
12
+ // open the next or previous item's menu, with focus on that item. In a
13
+ // bar of one menu they do nothing.
14
+ menu.along = (step, event) => {
15
+ const items = bar.list.items();
16
+ const index = items.findIndex((item) => item.menu === menu);
17
+ const to = items[(index + step + items.length) % items.length];
18
+ if (!to || to.menu === menu)
19
+ return;
20
+ event.preventDefault();
21
+ bar.moveTo(to, { reason: "arrow-key", event });
22
+ };
23
+ return <MenuContext value={menu}>{props.children}</MenuContext>;
24
+ }
@@ -0,0 +1,30 @@
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
+ import type { Menu } from "../../menu/root/MenuContext.ts";
5
+ /** An item of the bar, a menu's Trigger, as its list navigation knows it. */
6
+ export interface MenubarItem extends ListItem {
7
+ /** The menu the item opens. */
8
+ readonly menu: Menu;
9
+ }
10
+ /** The bar: its items, the keys along it, and which of its menus is open. */
11
+ export interface Menubar {
12
+ readonly list: ListNavigation<MenubarItem>;
13
+ readonly typeahead: Typeahead;
14
+ /**
15
+ * Whether the item being created is the first, which has the tab stop of
16
+ * the server HTML and of the first render.
17
+ */
18
+ claimTabStop(): boolean;
19
+ /** The bar's open menu, if any. */
20
+ openMenu(): Menu | undefined;
21
+ /**
22
+ * Once focus has moved to another item of the bar: if `open` was open,
23
+ * closes it and opens that item's menu, with focus staying on the item.
24
+ */
25
+ follow(open: Menu | undefined, details: OpenChangeDetails): void;
26
+ /** Moves focus to `item`, and opens its menu if another one was open. */
27
+ moveTo(item: MenubarItem, details: OpenChangeDetails): void;
28
+ }
29
+ export declare const MenubarContext: import("solid-js").Context<Menubar | null>;
30
+ export declare function useMenubarContext(): Menubar;
@@ -0,0 +1,12 @@
1
+ import { isDev } from "@solidjs/web";
2
+ import { createContext, useContext } from "solid-js";
3
+ // `null` outside a Root, so that the part can name what is missing.
4
+ export const MenubarContext = createContext(null);
5
+ export function useMenubarContext() {
6
+ const context = useContext(MenubarContext);
7
+ // Production builds drop the check, and its message, with `isDev`.
8
+ if (isDev && !context) {
9
+ throw new Error("[menhir] Menubar parts must be placed within <Menubar.Root>.");
10
+ }
11
+ return context;
12
+ }
@@ -0,0 +1,11 @@
1
+ import type { JSX } from "@solidjs/web";
2
+ import type { RenderProp } from "../../core/renderElement.tsx";
3
+ export interface MenubarRootProps extends JSX.HTMLAttributes<HTMLElement> {
4
+ render?: RenderProp;
5
+ }
6
+ /**
7
+ * A row of menus: a `<div role="menubar">` around `Menubar.Menu`s, one tab
8
+ * stop, with Right and Left Arrow, Home, End and typed characters moving
9
+ * between its items. Name it with `aria-label` or `aria-labelledby`.
10
+ */
11
+ export declare function MenubarRoot(props: MenubarRootProps): JSX.Element;
@@ -0,0 +1,77 @@
1
+ import { omit } from "solid-js";
2
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
3
+ import { renderElement } from "../../core/renderElement.jsx";
4
+ import { createRovingList } from "../../floating/list-navigation/roving.js";
5
+ import { createTypeahead } from "../../floating/typeahead/typeahead.js";
6
+ import { MenubarContext } from "./MenubarContext.js";
7
+ /**
8
+ * A row of menus: a `<div role="menubar">` around `Menubar.Menu`s, one tab
9
+ * stop, with Right and Left Arrow, Home, End and typed characters moving
10
+ * between its items. Name it with `aria-label` or `aria-labelledby`.
11
+ */
12
+ export function MenubarRoot(props) {
13
+ // Always shown, so its active item lasts: it is the tab stop (README,
14
+ // "Decisions").
15
+ const list = createRovingList({
16
+ orientation: "horizontal",
17
+ wrap: true,
18
+ });
19
+ const typeahead = createTypeahead({
20
+ get items() {
21
+ return list.items();
22
+ },
23
+ get active() {
24
+ return list.active();
25
+ },
26
+ move: (item) => list.move(item),
27
+ });
28
+ // Whether an item has claimed the tab stop of the server HTML.
29
+ let claimed = false;
30
+ const bar = {
31
+ list,
32
+ typeahead,
33
+ claimTabStop: () => !claimed && (claimed = true),
34
+ openMenu: () => list.items().find((item) => item.menu.open())?.menu,
35
+ follow: (open, details) => {
36
+ const to = list
37
+ .items()
38
+ .find((item) => item.element === document.activeElement)?.menu;
39
+ if (!open || !to || to === open)
40
+ return;
41
+ open.close(details);
42
+ to.keepFocus = true;
43
+ to.setOpen(true, details);
44
+ // For this opening only. One that goes through has used it by the
45
+ // next task, after its flushes; a controlled menu that refused it
46
+ // must not keep focus on the bar when it opens later.
47
+ setTimeout(() => (to.keepFocus = false));
48
+ },
49
+ moveTo: (item, details) => {
50
+ const open = bar.openMenu();
51
+ list.move(item);
52
+ bar.follow(open, details);
53
+ },
54
+ };
55
+ const ownProps = {
56
+ role: "menubar",
57
+ // Shift+Tab from a menu goes out of the bar, not to the menu's item in
58
+ // it, which keeps the tab stop: focus moves to the item first, and the
59
+ // browser's own move then goes on from there. The menu closes as focus
60
+ // leaves it by Tab (README, "Decisions").
61
+ onKeyDown: (event) => {
62
+ if (event.key !== "Tab" ||
63
+ !event.shiftKey ||
64
+ event.altKey ||
65
+ event.ctrlKey ||
66
+ event.metaKey)
67
+ return;
68
+ const open = bar.openMenu();
69
+ if (open?.popup?.contains(event.target))
70
+ open.trigger?.focus();
71
+ },
72
+ };
73
+ const elementProps = mergeElementProps(ownProps, omit(props, "render"));
74
+ return (<MenubarContext value={bar}>
75
+ {renderElement(props, elementProps, (p) => (<div {...p}/>))}
76
+ </MenubarContext>);
77
+ }
@@ -0,0 +1,11 @@
1
+ import type { MenuTriggerProps } from "../../menu/trigger/MenuTrigger.tsx";
2
+ export type MenubarTriggerProps = MenuTriggerProps;
3
+ /**
4
+ * An item of the bar, which opens its menu: Menu's `<button type="button"
5
+ * aria-haspopup="menu">`, with the `menuitem` role and the bar's roving
6
+ * `tabindex`. Down Arrow, Enter and Space open the menu with focus on its
7
+ * first item, Up Arrow on its last. While a menu of the bar is open, moving
8
+ * to another item, by a key or the pointer, opens that item's menu instead.
9
+ * Place it inside `Menubar.Menu`, right before `Menubar.Popup`.
10
+ */
11
+ export declare function MenubarTrigger(props: MenubarTriggerProps): import("@solidjs/web").JSX.Element;