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,12 @@
1
+ import { isDev } from "@solidjs/web";
2
+ import { useContext } from "solid-js";
3
+ import { MenuContext } from "../../menu/root/MenuContext.js";
4
+ /** The Root's context menu, for its Trigger and its Popup. */
5
+ export function useContextMenu() {
6
+ const context = useContext(MenuContext);
7
+ // Production builds drop the check, and its message, with `isDev`.
8
+ if (isDev && !context?.setAnchor) {
9
+ throw new Error("[menhir] Context Menu parts must be placed within <ContextMenu.Root>.");
10
+ }
11
+ return context;
12
+ }
@@ -0,0 +1,12 @@
1
+ import type { JSX } from "@solidjs/web";
2
+ import type { MenuStateProps } from "../../menu/root/MenuContext.ts";
3
+ export interface ContextMenuRootProps extends MenuStateProps {
4
+ children?: JSX.Element;
5
+ }
6
+ /**
7
+ * A menu that an area opens with the `contextmenu` event: holds whether it
8
+ * is open, and where it was asked for, and shares them with
9
+ * `ContextMenu.Trigger`, `ContextMenu.Popup` and the items inside it.
10
+ * Renders no element of its own.
11
+ */
12
+ export declare function ContextMenuRoot(props: ContextMenuRootProps): JSX.Element;
@@ -0,0 +1,16 @@
1
+ import { createSignal } from "solid-js";
2
+ import { createMenu, MenuContext } from "../../menu/root/MenuContext.js";
3
+ /**
4
+ * A menu that an area opens with the `contextmenu` event: holds whether it
5
+ * is open, and where it was asked for, and shares them with
6
+ * `ContextMenu.Trigger`, `ContextMenu.Popup` and the items inside it.
7
+ * Renders no element of its own.
8
+ */
9
+ export function ContextMenuRoot(props) {
10
+ const [anchor, setAnchor] = createSignal();
11
+ // Added to Menu's state, not copied: its functions write to that object.
12
+ const menu = createMenu(props);
13
+ menu.anchor = anchor;
14
+ menu.setAnchor = (value) => setAnchor(() => value);
15
+ return <MenuContext value={menu}>{props.children}</MenuContext>;
16
+ }
@@ -0,0 +1,15 @@
1
+ import type { JSX } from "@solidjs/web";
2
+ import type { RenderProp } from "../../core/renderElement.tsx";
3
+ export interface ContextMenuTriggerProps extends JSX.HTMLAttributes<HTMLElement> {
4
+ /** Opens nothing, so the browser's own menu shows instead. */
5
+ disabled?: boolean;
6
+ render?: RenderProp;
7
+ }
8
+ /**
9
+ * The area that opens the menu: a `<div>` whose `contextmenu` event, from a
10
+ * secondary-button press, a long press, Shift+F10 or the Menu key, opens
11
+ * the menu at the pointer, or below the focused element for a key, with
12
+ * focus on its first item. Place it inside `ContextMenu.Root`, right before
13
+ * `ContextMenu.Popup`.
14
+ */
15
+ export declare function ContextMenuTrigger(props: ContextMenuTriggerProps): JSX.Element;
@@ -0,0 +1,46 @@
1
+ import { merge, omit } from "solid-js";
2
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
3
+ import { renderElement } from "../../core/renderElement.jsx";
4
+ import { useContextMenu } from "../root/ContextMenuContext.js";
5
+ /**
6
+ * The area that opens the menu: a `<div>` whose `contextmenu` event, from a
7
+ * secondary-button press, a long press, Shift+F10 or the Menu key, opens
8
+ * the menu at the pointer, or below the focused element for a key, with
9
+ * focus on its first item. Place it inside `ContextMenu.Root`, right before
10
+ * `ContextMenu.Popup`.
11
+ */
12
+ export function ContextMenuTrigger(props) {
13
+ const menu = useContextMenu();
14
+ const ownProps = merge(menu.attributes, {
15
+ get "data-disabled"() {
16
+ return props.disabled ? "" : undefined;
17
+ },
18
+ onContextMenu: (event) => {
19
+ if (props.disabled)
20
+ return;
21
+ event.preventDefault();
22
+ // A key has no pointer: Chromium sends its `contextmenu` with no
23
+ // button, as it does a long press, which is a touch, and the Pointer
24
+ // Events spec with no pointer type (README, "Decisions").
25
+ const { pointerType } = event;
26
+ const { clientX, clientY } = event;
27
+ menu.setAnchor(pointerType === "" || (event.button < 0 && pointerType === "mouse")
28
+ ? event.target
29
+ : { getBoundingClientRect: () => new DOMRect(clientX, clientY) });
30
+ // The element the menu opens from, where Menu returns focus when the
31
+ // browser does not, as after an item in a submenu: the element a key
32
+ // was pressed on, or the one a press focused (README, "Decisions").
33
+ const focused = document.activeElement;
34
+ menu.trigger = focused === document.body ? undefined : focused;
35
+ const details = { reason: "trigger-press", event };
36
+ // A second one while open moves the menu and starts it over.
37
+ menu.closeSubmenus(details);
38
+ menu.openFromKey(details);
39
+ },
40
+ ref: (element) => {
41
+ menu.area = element;
42
+ },
43
+ });
44
+ const elementProps = mergeElementProps(ownProps, omit(props, "render", "disabled"));
45
+ return renderElement(props, elementProps, (p) => <div {...p}/>);
46
+ }
@@ -1,5 +1,13 @@
1
1
  import type { JSX } from "@solidjs/web";
2
2
  import type { ElementProps } from "./renderElement.tsx";
3
+ export type Handler = ((event: Event) => void) | [(data: unknown, event: Event) => void, unknown];
4
+ /**
5
+ * Calls a JSX event handler in either of Solid's forms, a function or a
6
+ * `[handler, data]` pair, for a part that calls the consumer's handler
7
+ * itself instead of through the merge, such as a menu item that skips it
8
+ * while disabled.
9
+ */
10
+ export declare function callHandler(handler: Handler | undefined, event: Event): void;
3
11
  /**
4
12
  * Merges a part's own props with the consumer's, for one element. The
5
13
  * consumer's props win, except for these, where both apply:
@@ -1,5 +1,11 @@
1
1
  import { merge } from "solid-js";
2
- function callHandler(handler, event) {
2
+ /**
3
+ * Calls a JSX event handler in either of Solid's forms, a function or a
4
+ * `[handler, data]` pair, for a part that calls the consumer's handler
5
+ * itself instead of through the merge, such as a menu item that skips it
6
+ * while disabled.
7
+ */
8
+ export function callHandler(handler, event) {
3
9
  if (typeof handler === "function")
4
10
  handler(event);
5
11
  else if (handler)
@@ -6,7 +6,8 @@
6
6
  * - `escape-key`: Escape.
7
7
  * - `outside-press`: a press outside the popup, on a dialog's backdrop or
8
8
  * in a popover's light dismiss.
9
- * - `trigger-press`: the trigger, pressed by pointer or keyboard.
9
+ * - `trigger-press`: the trigger, pressed by pointer or keyboard, or a
10
+ * Context Menu's area by its `contextmenu` event.
10
11
  * - `close-request`: the browser closed the popup for another reason, such
11
12
  * as the back gesture on Android, a `<form method="dialog">` or another
12
13
  * popover opening.
@@ -20,8 +21,12 @@
20
21
  * Tab out of a Navigation Menu's `<nav>`.
21
22
  * - `link-press`: a link inside the component was activated, by pointer or
22
23
  * keyboard, such as a Navigation Menu's `Link`.
24
+ * - `item-press`: an item of a menu was activated, by pointer or keyboard,
25
+ * which closes every open menu (Menu).
26
+ * - `arrow-key`: an arrow key opened or closed a submenu, or a key that
27
+ * moves through a menu closed its open submenu (Menu).
23
28
  */
24
- export type OpenChangeReason = "escape-key" | "outside-press" | "trigger-press" | "close-request" | "trigger-hover" | "close-press" | "trigger-focus" | "focus-out" | "link-press";
29
+ export type OpenChangeReason = "escape-key" | "outside-press" | "trigger-press" | "close-request" | "trigger-hover" | "close-press" | "trigger-focus" | "focus-out" | "link-press" | "item-press" | "arrow-key";
25
30
  /**
26
31
  * The second argument of `onOpenChange`. `event` is the event that caused
27
32
  * the change: the key or pointer event when there was one, else the
@@ -0,0 +1,20 @@
1
+ import type { RenderProp } from "../../core/renderElement.tsx";
2
+ import type { MenuItemBaseProps } from "../item/menuItemProps.ts";
3
+ export interface MenuCheckboxItemProps extends MenuItemBaseProps {
4
+ /** Whether it is checked, when controlled. */
5
+ checked?: boolean;
6
+ /** Whether it is checked at first, when uncontrolled. */
7
+ defaultChecked?: boolean;
8
+ /**
9
+ * Called with the new value when the user checks or unchecks it. When
10
+ * controlled, it changes only if `checked` does.
11
+ */
12
+ onCheckedChange?: (checked: boolean) => void;
13
+ render?: RenderProp;
14
+ }
15
+ /**
16
+ * An option that is on or off: a `<div role="menuitemcheckbox">` with
17
+ * `aria-checked`. A click and Enter change it and close every open menu;
18
+ * Space changes it and keeps the menu open. Place it inside `Menu.Popup`.
19
+ */
20
+ export declare function MenuCheckboxItem(props: MenuCheckboxItemProps): import("@solidjs/web").JSX.Element;
@@ -0,0 +1,26 @@
1
+ import { omit } from "solid-js";
2
+ import { createControllableSignal } from "../../core/createControllableSignal.js";
3
+ import { renderCheckedItem } from "../item/renderCheckedItem.jsx";
4
+ /**
5
+ * An option that is on or off: a `<div role="menuitemcheckbox">` with
6
+ * `aria-checked`. A click and Enter change it and close every open menu;
7
+ * Space changes it and keeps the menu open. Place it inside `Menu.Popup`.
8
+ */
9
+ export function MenuCheckboxItem(props) {
10
+ const [checked, setChecked] = createControllableSignal({
11
+ get value() {
12
+ return props.checked;
13
+ },
14
+ get defaultValue() {
15
+ return props.defaultChecked ?? false;
16
+ },
17
+ onChange: (value) => props.onCheckedChange?.(value),
18
+ });
19
+ return renderCheckedItem(props, omit(props, "render", "disabled", "label", "onClick", "checked", "defaultChecked", "onCheckedChange"), {
20
+ role: "menuitemcheckbox",
21
+ get checked() {
22
+ return checked();
23
+ },
24
+ onCheck: () => setChecked(!checked()),
25
+ });
26
+ }
@@ -0,0 +1,16 @@
1
+ import type { JSX } from "@solidjs/web";
2
+ import type { RenderProp } from "../../core/renderElement.tsx";
3
+ export interface MenuGroupProps extends JSX.HTMLAttributes<HTMLElement> {
4
+ render?: RenderProp;
5
+ }
6
+ /**
7
+ * Renders a group of items, `<div role="group">`, named by the GroupLabel
8
+ * inside it, for both `Menu.Group` and `Menu.RadioGroup`. `props` are the
9
+ * consumer's, less the group's own.
10
+ */
11
+ export declare function renderGroup(props: MenuGroupProps, rest: JSX.HTMLAttributes<HTMLElement>): JSX.Element;
12
+ /**
13
+ * Items that belong together: a `<div role="group">` named by its
14
+ * `Menu.GroupLabel`. Place it inside `Menu.Popup`.
15
+ */
16
+ export declare function MenuGroup(props: MenuGroupProps): JSX.Element;
@@ -0,0 +1,31 @@
1
+ import { omit } from "solid-js";
2
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
3
+ import { renderElement } from "../../core/renderElement.jsx";
4
+ import { createIdSlot } from "../../core/slot.js";
5
+ import { MenuGroupContext } from "./MenuGroupContext.js";
6
+ /**
7
+ * Renders a group of items, `<div role="group">`, named by the GroupLabel
8
+ * inside it, for both `Menu.Group` and `Menu.RadioGroup`. `props` are the
9
+ * consumer's, less the group's own.
10
+ */
11
+ export function renderGroup(props, rest) {
12
+ const [labelId, registerLabelId] = createIdSlot();
13
+ const elementProps = mergeElementProps({
14
+ role: "group",
15
+ // The consumer's, else the GroupLabel's id, while one is rendered.
16
+ // Kept out of the merge ("`undefined` is a value", PLAN.md section 8).
17
+ get "aria-labelledby"() {
18
+ return props["aria-labelledby"] ?? labelId();
19
+ },
20
+ }, omit(rest, "aria-labelledby"));
21
+ return (<MenuGroupContext value={{ registerLabelId }}>
22
+ {renderElement(props, elementProps, (p) => (<div {...p}/>))}
23
+ </MenuGroupContext>);
24
+ }
25
+ /**
26
+ * Items that belong together: a `<div role="group">` named by its
27
+ * `Menu.GroupLabel`. Place it inside `Menu.Popup`.
28
+ */
29
+ export function MenuGroup(props) {
30
+ return renderGroup(props, omit(props, "render"));
31
+ }
@@ -0,0 +1,10 @@
1
+ /** What a group shares with its label. */
2
+ export interface MenuGroupContextValue {
3
+ /**
4
+ * Registers the `id` of a mounted GroupLabel, as a getter. Returns what
5
+ * unregisters it, for the label's disposal.
6
+ */
7
+ registerLabelId(id: () => string | false): () => void;
8
+ }
9
+ export declare const MenuGroupContext: import("solid-js").Context<MenuGroupContextValue | null>;
10
+ export declare function useMenuGroupContext(): MenuGroupContextValue;
@@ -0,0 +1,12 @@
1
+ import { createContext, useContext } from "solid-js";
2
+ import { isDev } from "@solidjs/web";
3
+ // `null` outside a group, so that the part can name what is missing.
4
+ export const MenuGroupContext = createContext(null);
5
+ export function useMenuGroupContext() {
6
+ const context = useContext(MenuGroupContext);
7
+ // Production builds drop the check, and its message, with `isDev`.
8
+ if (isDev && !context) {
9
+ throw new Error("[menhir] Menu.GroupLabel must be placed within <Menu.Group> or <Menu.RadioGroup>.");
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 MenuGroupLabelProps extends JSX.HTMLAttributes<HTMLElement> {
4
+ render?: RenderProp;
5
+ }
6
+ /**
7
+ * The label of a group, which names it in `aria-labelledby`: a `<div>`,
8
+ * not reached by the keys. Place it inside `Menu.Group` or
9
+ * `Menu.RadioGroup`.
10
+ */
11
+ export declare function MenuGroupLabel(props: MenuGroupLabelProps): JSX.Element;
@@ -0,0 +1,25 @@
1
+ import { createUniqueId, omit, onSettled } from "solid-js";
2
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
3
+ import { renderElement } from "../../core/renderElement.jsx";
4
+ import { useMenuGroupContext } from "../group/MenuGroupContext.js";
5
+ /**
6
+ * The label of a group, which names it in `aria-labelledby`: a `<div>`,
7
+ * not reached by the keys. Place it inside `Menu.Group` or
8
+ * `Menu.RadioGroup`.
9
+ */
10
+ export function MenuGroupLabel(props) {
11
+ const group = useMenuGroupContext();
12
+ const generated = createUniqueId();
13
+ // The consumer's `id`, else the generated one. Kept out of the merge,
14
+ // where a wrapper's `id={undefined}` would remove it.
15
+ const id = () => props.id ?? generated;
16
+ // Named once mounted, so the group names only a label that is rendered,
17
+ // and none in the server HTML, as a popup's Title.
18
+ onSettled(() => group.registerLabelId(id));
19
+ const elementProps = mergeElementProps({
20
+ get id() {
21
+ return id();
22
+ },
23
+ }, omit(props, "render", "id"));
24
+ return renderElement(props, elementProps, (p) => <div {...p}/>);
25
+ }
@@ -0,0 +1 @@
1
+ export * as Menu from "./index.parts.ts";
@@ -0,0 +1 @@
1
+ export * as Menu from "./index.parts.js";
@@ -0,0 +1,12 @@
1
+ export { MenuRoot as Root } from "./root/MenuRoot.tsx";
2
+ export { MenuTrigger as Trigger } from "./trigger/MenuTrigger.tsx";
3
+ export { MenuPopup as Popup } from "./popup/MenuPopup.tsx";
4
+ export { MenuItem as Item } from "./item/MenuItem.tsx";
5
+ export { MenuCheckboxItem as CheckboxItem } from "./checkbox-item/MenuCheckboxItem.tsx";
6
+ export { MenuItemIndicator as ItemIndicator } from "./item-indicator/MenuItemIndicator.tsx";
7
+ export { MenuGroup as Group } from "./group/MenuGroup.tsx";
8
+ export { MenuGroupLabel as GroupLabel } from "./group-label/MenuGroupLabel.tsx";
9
+ export { MenuRadioGroup as RadioGroup } from "./radio-group/MenuRadioGroup.tsx";
10
+ export { MenuRadioItem as RadioItem } from "./radio-item/MenuRadioItem.tsx";
11
+ export { MenuSubmenu as Submenu } from "./submenu/MenuSubmenu.tsx";
12
+ export { MenuSubmenuTrigger as SubmenuTrigger } from "./submenu-trigger/MenuSubmenuTrigger.tsx";
@@ -0,0 +1,12 @@
1
+ export { MenuRoot as Root } from "./root/MenuRoot.jsx";
2
+ export { MenuTrigger as Trigger } from "./trigger/MenuTrigger.jsx";
3
+ export { MenuPopup as Popup } from "./popup/MenuPopup.jsx";
4
+ export { MenuItem as Item } from "./item/MenuItem.jsx";
5
+ export { MenuCheckboxItem as CheckboxItem } from "./checkbox-item/MenuCheckboxItem.jsx";
6
+ export { MenuItemIndicator as ItemIndicator } from "./item-indicator/MenuItemIndicator.jsx";
7
+ export { MenuGroup as Group } from "./group/MenuGroup.jsx";
8
+ export { MenuGroupLabel as GroupLabel } from "./group-label/MenuGroupLabel.jsx";
9
+ export { MenuRadioGroup as RadioGroup } from "./radio-group/MenuRadioGroup.jsx";
10
+ export { MenuRadioItem as RadioItem } from "./radio-item/MenuRadioItem.jsx";
11
+ export { MenuSubmenu as Submenu } from "./submenu/MenuSubmenu.jsx";
12
+ export { MenuSubmenuTrigger as SubmenuTrigger } from "./submenu-trigger/MenuSubmenuTrigger.jsx";
@@ -0,0 +1,11 @@
1
+ import type { RenderProp } from "../../core/renderElement.tsx";
2
+ import type { MenuItemBaseProps } from "./menuItemProps.ts";
3
+ export interface MenuItemProps extends MenuItemBaseProps {
4
+ render?: RenderProp;
5
+ }
6
+ /**
7
+ * An action in the menu: a `<div role="menuitem">`. A click, Enter or
8
+ * Space calls its `onClick` and closes every open menu; a disabled item
9
+ * does neither. Render a link with `render`. Place it inside `Menu.Popup`.
10
+ */
11
+ export declare function MenuItem(props: MenuItemProps): import("@solidjs/web").JSX.Element;
@@ -0,0 +1,21 @@
1
+ import { omit } from "solid-js";
2
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
3
+ import { renderElement } from "../../core/renderElement.jsx";
4
+ import { useMenuContext } from "../root/MenuContext.js";
5
+ import { menuItemProps } from "./menuItemProps.js";
6
+ /**
7
+ * An action in the menu: a `<div role="menuitem">`. A click, Enter or
8
+ * Space calls its `onClick` and closes every open menu; a disabled item
9
+ * does neither. Render a link with `render`. Place it inside `Menu.Popup`.
10
+ */
11
+ export function MenuItem(props) {
12
+ const menu = useMenuContext();
13
+ const ownProps = menuItemProps(menu, props, {
14
+ role: "menuitem",
15
+ onActivate: (event, key) => menu.closeAll({ reason: "item-press", event: key ?? event }),
16
+ });
17
+ // The consumer's `onClick` is called by the item, not by the merge, so a
18
+ // disabled item does not call it.
19
+ const elementProps = mergeElementProps(ownProps, omit(props, "render", "disabled", "label", "onClick"));
20
+ return renderElement(props, elementProps, (p) => <div {...p}/>);
21
+ }
@@ -0,0 +1,35 @@
1
+ import type { JSX } from "@solidjs/web";
2
+ import type { ElementProps } from "../../core/renderElement.tsx";
3
+ import type { Menu } from "../root/MenuContext.ts";
4
+ /** The props every kind of item takes. */
5
+ export interface MenuItemBaseProps extends JSX.HTMLAttributes<HTMLElement> {
6
+ /**
7
+ * Whether the item is disabled: still reached by the keys and the
8
+ * pointer, but not activated.
9
+ */
10
+ disabled?: boolean;
11
+ /**
12
+ * The text typeahead matches, when the item's content is not its label,
13
+ * such as an icon and a shortcut beside the text.
14
+ */
15
+ label?: string;
16
+ }
17
+ /** What kind of item `menuItemProps` makes, and what activating it does. */
18
+ export interface MenuItemOptions {
19
+ readonly role: "menuitem" | "menuitemcheckbox" | "menuitemradio";
20
+ /**
21
+ * Called on a click, Enter or Space, unless the item is disabled, with the
22
+ * key event that activated it, if any. It runs after the consumer's
23
+ * `onClick`, which a disabled item does not call.
24
+ */
25
+ onActivate(event: MouseEvent, key: KeyboardEvent | undefined): void;
26
+ /** The menu a SubmenuTrigger opens. */
27
+ readonly submenu?: Menu;
28
+ /** Receives the element, as the item's own ref does. */
29
+ ref?(element: HTMLElement): void;
30
+ }
31
+ /**
32
+ * The own props of an item of `menu`: its role, its place in the list's
33
+ * roving focus, typeahead, and activation.
34
+ */
35
+ export declare function menuItemProps(menu: Menu, props: MenuItemBaseProps, options: MenuItemOptions): ElementProps;
@@ -0,0 +1,78 @@
1
+ import { onSettled } from "solid-js";
2
+ import { callHandler } from "../../core/mergeElementProps.js";
3
+ /**
4
+ * The own props of an item of `menu`: its role, its place in the list's
5
+ * roving focus, typeahead, and activation.
6
+ */
7
+ export function menuItemProps(menu, props, options) {
8
+ let element;
9
+ const item = {
10
+ get element() {
11
+ return element;
12
+ },
13
+ get text() {
14
+ return props.label;
15
+ },
16
+ get disabled() {
17
+ return props.disabled;
18
+ },
19
+ submenu: options.submenu,
20
+ };
21
+ // A SubmenuTrigger is where Left Arrow in its submenu moves back to.
22
+ if (options.submenu)
23
+ options.submenu.triggerItem = item;
24
+ onSettled(() => menu.list.register(item));
25
+ // The key event whose click is under way, for `onActivate`.
26
+ let key;
27
+ return {
28
+ role: options.role,
29
+ get tabindex() {
30
+ return menu.list.isActive(item) ? 0 : -1;
31
+ },
32
+ get "data-active"() {
33
+ return menu.list.isActive(item) ? "" : undefined;
34
+ },
35
+ get "aria-disabled"() {
36
+ return props.disabled ? "true" : undefined;
37
+ },
38
+ get "data-disabled"() {
39
+ return props.disabled ? "" : undefined;
40
+ },
41
+ onKeyDown: (event) => {
42
+ // A key that moves through the menu, typed or Up, Down, Home or End,
43
+ // closes the submenu that was open.
44
+ const closeSubmenus = () => menu.closeSubmenus({ reason: "arrow-key", event });
45
+ if (menu.typeahead.onKeyDown(event)) {
46
+ closeSubmenus();
47
+ return;
48
+ }
49
+ if (["ArrowDown", "ArrowUp", "Home", "End"].includes(event.key))
50
+ closeSubmenus();
51
+ if (menu.list.onKeyDown(event) || event.target !== element)
52
+ return;
53
+ if (event.key !== " " && event.key !== "Enter")
54
+ return;
55
+ // A link follows itself on Enter, with a click of its own.
56
+ if (event.key === "Enter" && element.matches("a[href]"))
57
+ return;
58
+ event.preventDefault();
59
+ key = event;
60
+ element.click();
61
+ key = undefined;
62
+ },
63
+ onPointerMove: (event) => menu.list.onPointerMove(item, event),
64
+ onClick: (event) => {
65
+ // Nor does a disabled link follow itself, by a click or Enter.
66
+ if (props.disabled) {
67
+ event.preventDefault();
68
+ return;
69
+ }
70
+ callHandler(props.onClick, event);
71
+ options.onActivate(event, key);
72
+ },
73
+ ref: (el) => {
74
+ element = el;
75
+ options.ref?.(el);
76
+ },
77
+ };
78
+ }
@@ -0,0 +1,22 @@
1
+ import type { JSX } from "@solidjs/web";
2
+ import type { RenderProp } from "../../core/renderElement.tsx";
3
+ import type { MenuItemBaseProps } from "./menuItemProps.ts";
4
+ /** What makes a checkbox or radio item, as getters and functions. */
5
+ export interface CheckedItemOptions {
6
+ readonly role: "menuitemcheckbox" | "menuitemradio";
7
+ /** Whether the item is checked. */
8
+ readonly checked: boolean;
9
+ /** Called when the item is activated: checks, or toggles, it. */
10
+ onCheck(): void;
11
+ }
12
+ /**
13
+ * Renders a checkbox or radio item, `Menu.CheckboxItem` and
14
+ * `Menu.RadioItem`: an item with `aria-checked`, `data-checked` or
15
+ * `data-unchecked`, which its `Menu.ItemIndicator` carries too. A click
16
+ * and Enter check it and close every open menu; Space checks it and keeps
17
+ * the menu open, as the menu pattern's optional Space does. `rest` is the
18
+ * consumer's props, less the item's own.
19
+ */
20
+ export declare function renderCheckedItem(props: MenuItemBaseProps & {
21
+ render?: RenderProp;
22
+ }, rest: JSX.HTMLAttributes<HTMLElement>, options: CheckedItemOptions): JSX.Element;
@@ -0,0 +1,41 @@
1
+ import { merge } from "solid-js";
2
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
3
+ import { renderElement } from "../../core/renderElement.jsx";
4
+ import { MenuItemIndicatorContext } from "../item-indicator/MenuItemIndicatorContext.js";
5
+ import { useMenuContext } from "../root/MenuContext.js";
6
+ import { menuItemProps } from "./menuItemProps.js";
7
+ /**
8
+ * Renders a checkbox or radio item, `Menu.CheckboxItem` and
9
+ * `Menu.RadioItem`: an item with `aria-checked`, `data-checked` or
10
+ * `data-unchecked`, which its `Menu.ItemIndicator` carries too. A click
11
+ * and Enter check it and close every open menu; Space checks it and keeps
12
+ * the menu open, as the menu pattern's optional Space does. `rest` is the
13
+ * consumer's props, less the item's own.
14
+ */
15
+ export function renderCheckedItem(props, rest, options) {
16
+ const menu = useMenuContext();
17
+ const attributes = {
18
+ get "data-checked"() {
19
+ return options.checked ? "" : undefined;
20
+ },
21
+ get "data-unchecked"() {
22
+ return options.checked ? undefined : "";
23
+ },
24
+ };
25
+ const ownProps = merge(attributes, menuItemProps(menu, props, {
26
+ role: options.role,
27
+ onActivate: (event, key) => {
28
+ options.onCheck();
29
+ if (key?.key !== " ")
30
+ menu.closeAll({ reason: "item-press", event: key ?? event });
31
+ },
32
+ }), {
33
+ get "aria-checked"() {
34
+ return options.checked ? "true" : "false";
35
+ },
36
+ });
37
+ const elementProps = mergeElementProps(ownProps, rest);
38
+ return (<MenuItemIndicatorContext value={attributes}>
39
+ {renderElement(props, elementProps, (p) => (<div {...p}/>))}
40
+ </MenuItemIndicatorContext>);
41
+ }
@@ -0,0 +1,12 @@
1
+ import type { JSX } from "@solidjs/web";
2
+ import type { RenderProp } from "../../core/renderElement.tsx";
3
+ export interface MenuItemIndicatorProps extends JSX.HTMLAttributes<HTMLElement> {
4
+ render?: RenderProp;
5
+ }
6
+ /**
7
+ * The check mark of a checkbox or radio item: an `aria-hidden` `<span>`
8
+ * with its item's `data-checked` or `data-unchecked`, always rendered, so
9
+ * that CSS shows it by state. Place it inside `Menu.CheckboxItem` or
10
+ * `Menu.RadioItem`.
11
+ */
12
+ export declare function MenuItemIndicator(props: MenuItemIndicatorProps): JSX.Element;
@@ -0,0 +1,17 @@
1
+ import { merge, omit } from "solid-js";
2
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
3
+ import { renderElement } from "../../core/renderElement.jsx";
4
+ import { useMenuItemIndicatorContext } from "./MenuItemIndicatorContext.js";
5
+ /**
6
+ * The check mark of a checkbox or radio item: an `aria-hidden` `<span>`
7
+ * with its item's `data-checked` or `data-unchecked`, always rendered, so
8
+ * that CSS shows it by state. Place it inside `Menu.CheckboxItem` or
9
+ * `Menu.RadioItem`.
10
+ */
11
+ export function MenuItemIndicator(props) {
12
+ const ownProps = merge(useMenuItemIndicatorContext(), {
13
+ "aria-hidden": "true",
14
+ });
15
+ const elementProps = mergeElementProps(ownProps, omit(props, "render"));
16
+ return renderElement(props, elementProps, (p) => <span {...p}/>);
17
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * A checkbox or radio item's state as `data-*` attributes, each `""` when
3
+ * it applies and absent otherwise: what the item and its
4
+ * `Menu.ItemIndicator` carry.
5
+ */
6
+ export type MenuItemIndicatorAttributes = {
7
+ readonly "data-checked": "" | undefined;
8
+ readonly "data-unchecked": "" | undefined;
9
+ };
10
+ export declare const MenuItemIndicatorContext: import("solid-js").Context<MenuItemIndicatorAttributes | null>;
11
+ export declare function useMenuItemIndicatorContext(): MenuItemIndicatorAttributes;
@@ -0,0 +1,12 @@
1
+ import { createContext, useContext } from "solid-js";
2
+ import { isDev } from "@solidjs/web";
3
+ // `null` outside a checkbox or radio item, so that the part can say so.
4
+ export const MenuItemIndicatorContext = createContext(null);
5
+ export function useMenuItemIndicatorContext() {
6
+ const context = useContext(MenuItemIndicatorContext);
7
+ // Production builds drop the check, and its message, with `isDev`.
8
+ if (isDev && !context) {
9
+ throw new Error("[menhir] Menu.ItemIndicator must be placed within <Menu.CheckboxItem> or <Menu.RadioItem>.");
10
+ }
11
+ return context;
12
+ }