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.
- package/README.md +2 -2
- package/dist/context-menu/index.js +2 -0
- package/dist/context-menu/index.parts.js +13 -0
- package/dist/context-menu/popup/ContextMenuPopup.js +30 -0
- package/dist/context-menu/root/ContextMenuContext.js +11 -0
- package/dist/context-menu/root/ContextMenuRoot.js +23 -0
- package/dist/context-menu/trigger/ContextMenuTrigger.js +47 -0
- package/dist/core/mergeElementProps.js +7 -1
- package/dist/menu/checkbox-item/MenuCheckboxItem.js +28 -0
- package/dist/menu/group/MenuGroup.js +40 -0
- package/dist/menu/group/MenuGroupContext.js +10 -0
- package/dist/menu/group-label/MenuGroupLabel.js +27 -0
- package/dist/menu/index.js +2 -0
- package/dist/menu/index.parts.js +13 -0
- package/dist/menu/item/MenuItem.js +30 -0
- package/dist/menu/item/menuItemProps.js +77 -0
- package/dist/menu/item/renderCheckedItem.js +52 -0
- package/dist/menu/item-indicator/MenuItemIndicator.js +23 -0
- package/dist/menu/item-indicator/MenuItemIndicatorContext.js +10 -0
- package/dist/menu/popup/MenuPopup.js +123 -0
- package/dist/menu/radio-group/MenuRadioGroup.js +39 -0
- package/dist/menu/radio-item/MenuRadioItem.js +21 -0
- package/dist/menu/root/MenuContext.js +114 -0
- package/dist/menu/root/MenuRoot.js +19 -0
- package/dist/menu/submenu/MenuSubmenu.js +18 -0
- package/dist/menu/submenu-trigger/MenuSubmenuTrigger.js +114 -0
- package/dist/menu/trigger/MenuTrigger.js +67 -0
- package/dist/menubar/index.js +2 -0
- package/dist/menubar/index.parts.js +14 -0
- package/dist/menubar/menu/MenubarMenu.js +30 -0
- package/dist/menubar/root/MenubarContext.js +10 -0
- package/dist/menubar/root/MenubarRoot.js +68 -0
- package/dist/menubar/trigger/MenubarTrigger.js +73 -0
- package/dist/source/context-menu/index.d.ts +1 -0
- package/dist/source/context-menu/index.js +1 -0
- package/dist/source/context-menu/index.parts.d.ts +12 -0
- package/dist/source/context-menu/index.parts.js +13 -0
- package/dist/source/context-menu/popup/ContextMenuPopup.d.ts +12 -0
- package/dist/source/context-menu/popup/ContextMenuPopup.jsx +36 -0
- package/dist/source/context-menu/root/ContextMenuContext.d.ts +18 -0
- package/dist/source/context-menu/root/ContextMenuContext.js +12 -0
- package/dist/source/context-menu/root/ContextMenuRoot.d.ts +12 -0
- package/dist/source/context-menu/root/ContextMenuRoot.jsx +16 -0
- package/dist/source/context-menu/trigger/ContextMenuTrigger.d.ts +15 -0
- package/dist/source/context-menu/trigger/ContextMenuTrigger.jsx +46 -0
- package/dist/source/core/mergeElementProps.d.ts +8 -0
- package/dist/source/core/mergeElementProps.js +7 -1
- package/dist/source/floating/dismissal/reason.d.ts +7 -2
- package/dist/source/menu/checkbox-item/MenuCheckboxItem.d.ts +20 -0
- package/dist/source/menu/checkbox-item/MenuCheckboxItem.jsx +26 -0
- package/dist/source/menu/group/MenuGroup.d.ts +16 -0
- package/dist/source/menu/group/MenuGroup.jsx +31 -0
- package/dist/source/menu/group/MenuGroupContext.d.ts +10 -0
- package/dist/source/menu/group/MenuGroupContext.js +12 -0
- package/dist/source/menu/group-label/MenuGroupLabel.d.ts +11 -0
- package/dist/source/menu/group-label/MenuGroupLabel.jsx +25 -0
- package/dist/source/menu/index.d.ts +1 -0
- package/dist/source/menu/index.js +1 -0
- package/dist/source/menu/index.parts.d.ts +12 -0
- package/dist/source/menu/index.parts.js +12 -0
- package/dist/source/menu/item/MenuItem.d.ts +11 -0
- package/dist/source/menu/item/MenuItem.jsx +21 -0
- package/dist/source/menu/item/menuItemProps.d.ts +35 -0
- package/dist/source/menu/item/menuItemProps.js +78 -0
- package/dist/source/menu/item/renderCheckedItem.d.ts +22 -0
- package/dist/source/menu/item/renderCheckedItem.jsx +41 -0
- package/dist/source/menu/item-indicator/MenuItemIndicator.d.ts +12 -0
- package/dist/source/menu/item-indicator/MenuItemIndicator.jsx +17 -0
- package/dist/source/menu/item-indicator/MenuItemIndicatorContext.d.ts +11 -0
- package/dist/source/menu/item-indicator/MenuItemIndicatorContext.js +12 -0
- package/dist/source/menu/popup/MenuPopup.d.ts +41 -0
- package/dist/source/menu/popup/MenuPopup.jsx +156 -0
- package/dist/source/menu/radio-group/MenuRadioGroup.d.ts +27 -0
- package/dist/source/menu/radio-group/MenuRadioGroup.jsx +36 -0
- package/dist/source/menu/radio-item/MenuRadioItem.d.ts +14 -0
- package/dist/source/menu/radio-item/MenuRadioItem.jsx +19 -0
- package/dist/source/menu/root/MenuContext.d.ts +114 -0
- package/dist/source/menu/root/MenuContext.js +123 -0
- package/dist/source/menu/root/MenuRoot.d.ts +11 -0
- package/dist/source/menu/root/MenuRoot.jsx +9 -0
- package/dist/source/menu/submenu/MenuSubmenu.d.ts +22 -0
- package/dist/source/menu/submenu/MenuSubmenu.jsx +10 -0
- package/dist/source/menu/submenu-trigger/MenuSubmenuTrigger.d.ts +14 -0
- package/dist/source/menu/submenu-trigger/MenuSubmenuTrigger.jsx +121 -0
- package/dist/source/menu/trigger/MenuTrigger.d.ts +13 -0
- package/dist/source/menu/trigger/MenuTrigger.jsx +73 -0
- package/dist/source/menubar/index.d.ts +1 -0
- package/dist/source/menubar/index.js +1 -0
- package/dist/source/menubar/index.parts.d.ts +13 -0
- package/dist/source/menubar/index.parts.js +14 -0
- package/dist/source/menubar/menu/MenubarMenu.d.ts +11 -0
- package/dist/source/menubar/menu/MenubarMenu.jsx +24 -0
- package/dist/source/menubar/root/MenubarContext.d.ts +30 -0
- package/dist/source/menubar/root/MenubarContext.js +12 -0
- package/dist/source/menubar/root/MenubarRoot.d.ts +11 -0
- package/dist/source/menubar/root/MenubarRoot.jsx +77 -0
- package/dist/source/menubar/trigger/MenubarTrigger.d.ts +11 -0
- package/dist/source/menubar/trigger/MenubarTrigger.jsx +78 -0
- package/package.json +16 -1
|
@@ -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;
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { merge, onSettled } from "solid-js";
|
|
2
|
+
import { callHandler } from "../../core/mergeElementProps.js";
|
|
3
|
+
import { useMenuContext } from "../../menu/root/MenuContext.js";
|
|
4
|
+
import { MenuTrigger } from "../../menu/trigger/MenuTrigger.jsx";
|
|
5
|
+
import { useMenubarContext } from "../root/MenubarContext.js";
|
|
6
|
+
/**
|
|
7
|
+
* An item of the bar, which opens its menu: Menu's `<button type="button"
|
|
8
|
+
* aria-haspopup="menu">`, with the `menuitem` role and the bar's roving
|
|
9
|
+
* `tabindex`. Down Arrow, Enter and Space open the menu with focus on its
|
|
10
|
+
* first item, Up Arrow on its last. While a menu of the bar is open, moving
|
|
11
|
+
* to another item, by a key or the pointer, opens that item's menu instead.
|
|
12
|
+
* Place it inside `Menubar.Menu`, right before `Menubar.Popup`.
|
|
13
|
+
*/
|
|
14
|
+
export function MenubarTrigger(props) {
|
|
15
|
+
const bar = useMenubarContext();
|
|
16
|
+
const menu = useMenuContext();
|
|
17
|
+
// Its element is the menu's trigger, which Menu's Trigger sets.
|
|
18
|
+
const item = {
|
|
19
|
+
get element() {
|
|
20
|
+
return menu.trigger;
|
|
21
|
+
},
|
|
22
|
+
menu,
|
|
23
|
+
};
|
|
24
|
+
// Read once: the tab stop of the server HTML, and of the first render.
|
|
25
|
+
const first = bar.claimTabStop();
|
|
26
|
+
onSettled(() => bar.list.register(item));
|
|
27
|
+
return MenuTrigger(merge(props, {
|
|
28
|
+
role: "menuitem",
|
|
29
|
+
// The item that last had focus, else the first (README, "Decisions").
|
|
30
|
+
get tabindex() {
|
|
31
|
+
const stop = bar.list.active() ?? bar.list.items()[0];
|
|
32
|
+
return (stop ? stop === item : first) ? 0 : -1;
|
|
33
|
+
},
|
|
34
|
+
// Called here, not merged, so the consumer's handlers run too, first,
|
|
35
|
+
// as in a merge.
|
|
36
|
+
onFocus: (event) => {
|
|
37
|
+
callHandler(props.onFocus, event);
|
|
38
|
+
if (bar.list.active() !== item)
|
|
39
|
+
bar.list.move(item);
|
|
40
|
+
},
|
|
41
|
+
onKeyDown: (event) => {
|
|
42
|
+
callHandler(props.onKeyDown, event);
|
|
43
|
+
const open = bar.openMenu();
|
|
44
|
+
if (bar.typeahead.onKeyDown(event) || bar.list.onKeyDown(event)) {
|
|
45
|
+
bar.follow(open, { reason: "arrow-key", event });
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
const { key } = event;
|
|
49
|
+
if (event.altKey || event.ctrlKey || event.metaKey || !menu.open())
|
|
50
|
+
return;
|
|
51
|
+
// Tab leaves the bar, and closes its menu.
|
|
52
|
+
if (key === "Tab")
|
|
53
|
+
menu.close({ reason: "focus-out", event });
|
|
54
|
+
// Enter and Space open the menu, which is open already: focus moves
|
|
55
|
+
// into it, where Menu's Trigger would close it.
|
|
56
|
+
if ((key === "Enter" || key === " ") && !event.shiftKey) {
|
|
57
|
+
event.preventDefault();
|
|
58
|
+
menu.list.move(menu.list.items()[0]);
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
// While another menu of the bar is open, the pointer moving onto this
|
|
62
|
+
// item opens its menu. Only a pointer that moves counts, and no touch.
|
|
63
|
+
onPointerMove: (event) => {
|
|
64
|
+
callHandler(props.onPointerMove, event);
|
|
65
|
+
const open = bar.openMenu();
|
|
66
|
+
if (event.pointerType === "touch" || !open || open === menu)
|
|
67
|
+
return;
|
|
68
|
+
bar.moveTo(item, { reason: "trigger-hover", event });
|
|
69
|
+
// A press it was on its way to keeps the menu open (README,
|
|
70
|
+
// "Decisions").
|
|
71
|
+
menu.hoverOpened = true;
|
|
72
|
+
},
|
|
73
|
+
onPointerLeave: (event) => {
|
|
74
|
+
callHandler(props.onPointerLeave, event);
|
|
75
|
+
menu.hoverOpened = false;
|
|
76
|
+
},
|
|
77
|
+
}));
|
|
78
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "menhir",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
4
4
|
"description": "Unstyled, accessible UI primitives for SolidJS 2.0, with small and measurable bundles",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"solid",
|
|
@@ -54,11 +54,26 @@
|
|
|
54
54
|
"solid": "./dist/source/color-scheme/local-storage/index.js",
|
|
55
55
|
"import": "./dist/color-scheme/local-storage/index.js"
|
|
56
56
|
},
|
|
57
|
+
"./context-menu": {
|
|
58
|
+
"types": "./dist/source/context-menu/index.d.ts",
|
|
59
|
+
"solid": "./dist/source/context-menu/index.js",
|
|
60
|
+
"import": "./dist/context-menu/index.js"
|
|
61
|
+
},
|
|
57
62
|
"./dialog": {
|
|
58
63
|
"types": "./dist/source/dialog/index.d.ts",
|
|
59
64
|
"solid": "./dist/source/dialog/index.js",
|
|
60
65
|
"import": "./dist/dialog/index.js"
|
|
61
66
|
},
|
|
67
|
+
"./menu": {
|
|
68
|
+
"types": "./dist/source/menu/index.d.ts",
|
|
69
|
+
"solid": "./dist/source/menu/index.js",
|
|
70
|
+
"import": "./dist/menu/index.js"
|
|
71
|
+
},
|
|
72
|
+
"./menubar": {
|
|
73
|
+
"types": "./dist/source/menubar/index.d.ts",
|
|
74
|
+
"solid": "./dist/source/menubar/index.js",
|
|
75
|
+
"import": "./dist/menubar/index.js"
|
|
76
|
+
},
|
|
62
77
|
"./meter": {
|
|
63
78
|
"types": "./dist/source/meter/index.d.ts",
|
|
64
79
|
"solid": "./dist/source/meter/index.js",
|