menhir 0.1.15 → 0.1.16

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.
@@ -1,11 +1,23 @@
1
- import { MenuContext } from "../../menu/root/MenuContext.js";
1
+ import { MenuContext, createMenu } from "../../menu/root/MenuContext.js";
2
2
  import { isDev } from "@solidjs/web";
3
- import { useContext } from "solid-js";
3
+ import { createSignal, useContext } from "solid-js";
4
4
  // src/context-menu/root/ContextMenuContext.ts
5
+ /**
6
+ * Creates a context menu's state: Menu's, with where it was asked for and
7
+ * the area, which start unset.
8
+ */
9
+ function createContextMenu(props) {
10
+ const [anchor, setAnchor] = createSignal();
11
+ const menu = createMenu(props);
12
+ menu.anchor = anchor;
13
+ menu.setAnchor = (value) => setAnchor(() => value);
14
+ menu.area = void 0;
15
+ return menu;
16
+ }
5
17
  /** The Root's context menu, for its Trigger and its Popup. */
6
18
  function useContextMenu() {
7
19
  const context = useContext(MenuContext);
8
20
  if (isDev && !context?.setAnchor) throw new Error("[menhir] Context Menu parts must be placed within <ContextMenu.Root>.");
9
21
  return context;
10
22
  }
11
- export { useContextMenu };
23
+ export { createContextMenu, useContextMenu };
@@ -1,6 +1,6 @@
1
- import { MenuContext, createMenu } from "../../menu/root/MenuContext.js";
1
+ import { MenuContext } from "../../menu/root/MenuContext.js";
2
+ import { createContextMenu } from "./ContextMenuContext.js";
2
3
  import { createComponent } from "@solidjs/web";
3
- import { createSignal } from "solid-js";
4
4
  // src/context-menu/root/ContextMenuRoot.tsx
5
5
  /**
6
6
  * A menu that an area opens with the `contextmenu` event: holds whether it
@@ -9,12 +9,10 @@ import { createSignal } from "solid-js";
9
9
  * Renders no element of its own.
10
10
  */
11
11
  function ContextMenuRoot(props) {
12
- const [anchor, setAnchor] = createSignal();
13
- const menu = createMenu(props);
14
- menu.anchor = anchor;
15
- menu.setAnchor = (value) => setAnchor(() => value);
16
12
  return createComponent(MenuContext, {
17
- value: menu,
13
+ get value() {
14
+ return createContextMenu(props);
15
+ },
18
16
  get children() {
19
17
  return props.children;
20
18
  }
@@ -25,13 +25,13 @@ function ContextMenuTrigger(props) {
25
25
  const { clientX, clientY } = event;
26
26
  menu.setAnchor(pointerType === "" || event.button < 0 && pointerType === "mouse" ? event.target : { getBoundingClientRect: () => new DOMRect(clientX, clientY) });
27
27
  const focused = document.activeElement;
28
- menu.trigger = focused === document.body ? void 0 : focused;
28
+ menu.returnTo = focused === document.body ? void 0 : focused;
29
29
  const details = {
30
30
  reason: "trigger-press",
31
31
  event
32
32
  };
33
33
  menu.closeSubmenus(details);
34
- menu.openFromKey(details);
34
+ menu.openAtFirst(details);
35
35
  },
36
36
  ref: (element) => {
37
37
  menu.area = element;
@@ -36,7 +36,7 @@ function MenuPopup(props) {
36
36
  },
37
37
  onDismiss: (details) => menu.close(details),
38
38
  get returnFocus() {
39
- return props.returnFocus ?? ((focused) => focused ?? (focusInside ? menu.trigger : void 0));
39
+ return props.returnFocus ?? ((focused) => focused ?? (focusInside ? menu.returnTo ?? menu.trigger : void 0));
40
40
  }
41
41
  };
42
42
  createPresence(options);
@@ -106,7 +106,7 @@ function MenuPopup(props) {
106
106
  reason: "focus-out",
107
107
  event
108
108
  });
109
- if (popup.contains(event.relatedTarget)) menu.trigger?.focus();
109
+ if (popup.contains(event.relatedTarget)) (menu.returnTo ?? menu.trigger)?.focus();
110
110
  },
111
111
  ref: (element) => {
112
112
  popup = element;
@@ -27,7 +27,7 @@ function createMenu(props, parent) {
27
27
  onArrowIn: (item, event) => {
28
28
  event.preventDefault();
29
29
  if (item.submenu) {
30
- if (!item.disabled) item.submenu.openFromKey({
30
+ if (!item.disabled) item.submenu.openAtFirst({
31
31
  reason: "arrow-key",
32
32
  event
33
33
  });
@@ -62,7 +62,7 @@ function createMenu(props, parent) {
62
62
  for (const submenu of submenus) submenu.close(details);
63
63
  },
64
64
  closeAll: (details) => parent ? parent.closeAll(details) : menu.close(details),
65
- openFromKey: (details) => {
65
+ openAtFirst: (details) => {
66
66
  setOpen(true, details);
67
67
  list.move(list.items()[0]);
68
68
  },
@@ -28,7 +28,7 @@ function MenuSubmenuTrigger(props) {
28
28
  reason: "trigger-press",
29
29
  event: key ?? event
30
30
  };
31
- if (key) submenu.openFromKey(details);
31
+ if (key) submenu.openAtFirst(details);
32
32
  else submenu.setOpen(true, details);
33
33
  },
34
34
  submenu,
@@ -1,5 +1,5 @@
1
1
  import type { MenuAnchor } from "../../menu/popup/MenuPopup.tsx";
2
- import type { Menu } from "../../menu/root/MenuContext.ts";
2
+ import type { Menu, MenuStateProps } from "../../menu/root/MenuContext.ts";
3
3
  /**
4
4
  * A context menu: Menu's state, plus where the menu was asked for, which
5
5
  * its Trigger sets and its Popup is placed against.
@@ -14,5 +14,10 @@ export interface ContextMenu extends Menu {
14
14
  /** The Trigger's element, once rendered: the anchor before the first. */
15
15
  area: HTMLElement | undefined;
16
16
  }
17
+ /**
18
+ * Creates a context menu's state: Menu's, with where it was asked for and
19
+ * the area, which start unset.
20
+ */
21
+ export declare function createContextMenu(props: MenuStateProps): ContextMenu;
17
22
  /** The Root's context menu, for its Trigger and its Popup. */
18
23
  export declare function useContextMenu(): ContextMenu;
@@ -1,6 +1,19 @@
1
1
  import { isDev } from "@solidjs/web";
2
- import { useContext } from "solid-js";
3
- import { MenuContext } from "../../menu/root/MenuContext.js";
2
+ import { createSignal, useContext } from "solid-js";
3
+ import { createMenu, MenuContext } from "../../menu/root/MenuContext.js";
4
+ /**
5
+ * Creates a context menu's state: Menu's, with where it was asked for and
6
+ * the area, which start unset.
7
+ */
8
+ export function createContextMenu(props) {
9
+ const [anchor, setAnchor] = createSignal();
10
+ // Added to Menu's state, not copied: its functions write to that object.
11
+ const menu = createMenu(props);
12
+ menu.anchor = anchor;
13
+ menu.setAnchor = (value) => setAnchor(() => value);
14
+ menu.area = undefined;
15
+ return menu;
16
+ }
4
17
  /** The Root's context menu, for its Trigger and its Popup. */
5
18
  export function useContextMenu() {
6
19
  const context = useContext(MenuContext);
@@ -1,5 +1,5 @@
1
- import { createSignal } from "solid-js";
2
- import { createMenu, MenuContext } from "../../menu/root/MenuContext.js";
1
+ import { MenuContext } from "../../menu/root/MenuContext.js";
2
+ import { createContextMenu } from "./ContextMenuContext.js";
3
3
  /**
4
4
  * A menu that an area opens with the `contextmenu` event: holds whether it
5
5
  * is open, and where it was asked for, and shares them with
@@ -7,10 +7,5 @@ import { createMenu, MenuContext } from "../../menu/root/MenuContext.js";
7
7
  * Renders no element of its own.
8
8
  */
9
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>;
10
+ return (<MenuContext value={createContextMenu(props)}>{props.children}</MenuContext>);
16
11
  }
@@ -29,13 +29,14 @@ export function ContextMenuTrigger(props) {
29
29
  : { getBoundingClientRect: () => new DOMRect(clientX, clientY) });
30
30
  // The element the menu opens from, where Menu returns focus when the
31
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").
32
+ // was pressed on, or the one a press focused. Not Menu's trigger, so
33
+ // a press on it is a press outside (README, "Decisions").
33
34
  const focused = document.activeElement;
34
- menu.trigger = focused === document.body ? undefined : focused;
35
+ menu.returnTo = focused === document.body ? undefined : focused;
35
36
  const details = { reason: "trigger-press", event };
36
37
  // A second one while open moves the menu and starts it over.
37
38
  menu.closeSubmenus(details);
38
- menu.openFromKey(details);
39
+ menu.openAtFirst(details);
39
40
  },
40
41
  ref: (element) => {
41
42
  menu.area = element;
@@ -42,7 +42,7 @@ export function MenuPopup(props) {
42
42
  get returnFocus() {
43
43
  return (props.returnFocus ??
44
44
  ((focused) => focused ??
45
- (focusInside ? menu.trigger : undefined)));
45
+ (focusInside ? (menu.returnTo ?? menu.trigger) : undefined)));
46
46
  },
47
47
  };
48
48
  // Presence first: the popover is shown before it is placed and focused.
@@ -142,7 +142,7 @@ export function MenuPopup(props) {
142
142
  // Shift+Tab from a submenu lands on its trigger, inside the menu that
143
143
  // closes: focus goes on to the Trigger, where it leaves the menu.
144
144
  if (popup.contains(event.relatedTarget))
145
- menu.trigger?.focus();
145
+ (menu.returnTo ?? menu.trigger)?.focus();
146
146
  },
147
147
  ref: (element) => {
148
148
  popup = element;
@@ -34,8 +34,11 @@ export interface Menu {
34
34
  closeSubmenus(details: OpenChangeDetails): void;
35
35
  /** Closes every menu, from the Root's down, as an activated item does. */
36
36
  closeAll(details: OpenChangeDetails): void;
37
- /** Opens the menu by a key, with focus on its first item. */
38
- openFromKey(details: OpenChangeDetails): void;
37
+ /**
38
+ * Opens the menu with focus on its first item: from a key, or from a
39
+ * Context Menu's `contextmenu`, which a pointer fires too.
40
+ */
41
+ openAtFirst(details: OpenChangeDetails): void;
39
42
  /** The menu around a submenu; `undefined` for the Root's. */
40
43
  readonly parent: Menu | undefined;
41
44
  /**
@@ -54,6 +57,13 @@ export interface Menu {
54
57
  triggerId(): string | false | undefined;
55
58
  /** The trigger's element, once rendered: the anchor by default. */
56
59
  trigger: HTMLElement | undefined;
60
+ /**
61
+ * Where focus returns when the browser does not, as after an item in a
62
+ * submenu, instead of `trigger`. Set by a Context Menu's Trigger to the
63
+ * element the menu opened from, left out of `trigger` so that a press on
64
+ * it is a press outside.
65
+ */
66
+ returnTo?: HTMLElement;
57
67
  /** A submenu's trigger, as an item of its parent menu. */
58
68
  triggerItem: MenuItemEntry | undefined;
59
69
  /** The Popup's element, once rendered. */
@@ -28,7 +28,7 @@ export function createMenu(props, parent) {
28
28
  event.preventDefault();
29
29
  if (item.submenu) {
30
30
  if (!item.disabled)
31
- item.submenu.openFromKey({ reason: "arrow-key", event });
31
+ item.submenu.openAtFirst({ reason: "arrow-key", event });
32
32
  return;
33
33
  }
34
34
  // Right Arrow on an item with no submenu moves along a menubar, from
@@ -78,7 +78,7 @@ export function createMenu(props, parent) {
78
78
  submenu.close(details);
79
79
  },
80
80
  closeAll: (details) => parent ? parent.closeAll(details) : menu.close(details),
81
- openFromKey: (details) => {
81
+ openAtFirst: (details) => {
82
82
  setOpen(true, details);
83
83
  // While closed, the move waits for the menu to open (list navigation).
84
84
  list.move(list.items()[0]);
@@ -28,7 +28,7 @@ export function MenuSubmenuTrigger(props) {
28
28
  const details = { reason: "trigger-press", event: key ?? event };
29
29
  // A key moves focus into the submenu; a click leaves it here.
30
30
  if (key)
31
- submenu.openFromKey(details);
31
+ submenu.openAtFirst(details);
32
32
  else
33
33
  submenu.setOpen(true, details);
34
34
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "menhir",
3
- "version": "0.1.15",
3
+ "version": "0.1.16",
4
4
  "description": "Unstyled, accessible UI primitives for SolidJS 2.0, with small and measurable bundles",
5
5
  "keywords": [
6
6
  "solid",