menhir 0.1.15 → 0.1.17

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 CHANGED
@@ -45,9 +45,14 @@ Nothing is styled. Every part exposes its state as data attributes, such as `dat
45
45
  }
46
46
  ```
47
47
 
48
- ## Styling with a coding agent
48
+ ## With a coding agent
49
49
 
50
- The package ships a skill for coding agents, [`skills/menhir-styling`](skills/menhir-styling/SKILL.md), that styles Menhir components in one consistent look, such as the shadcn look in Tailwind CSS. Point your agent at it with a line in your project's `AGENTS.md`, "To style Menhir components, follow `node_modules/menhir/skills/menhir-styling/SKILL.md`", or copy the folder into your agent's skills folder. What to ask for, and how a look of your own works: [Styling](https://menhir-ui.com/docs/styling).
50
+ The package ships two skills for coding agents:
51
+
52
+ - [`skills/menhir`](skills/menhir/SKILL.md) wires Menhir components into an app: entry points and parts, controlled and uncontrolled state, forms and Field.
53
+ - [`skills/menhir-styling`](skills/menhir-styling/SKILL.md) styles them in one consistent look, such as the shadcn look in Tailwind CSS. What to ask for, and how a look of your own works: [Styling](https://menhir-ui.com/docs/styling).
54
+
55
+ Point your agent at them with lines in your project's `AGENTS.md`, "To wire up Menhir components, follow `node_modules/menhir/skills/menhir/SKILL.md`" and "To style Menhir components, follow `node_modules/menhir/skills/menhir-styling/SKILL.md`", or copy the folders into your agent's skills folder.
51
56
 
52
57
  ## Docs
53
58
 
@@ -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.17",
4
4
  "description": "Unstyled, accessible UI primitives for SolidJS 2.0, with small and measurable bundles",
5
5
  "keywords": [
6
6
  "solid",
@@ -0,0 +1,44 @@
1
+ ---
2
+ name: menhir
3
+ description: Wire Menhir components (the `menhir` package for SolidJS 2.0) into an app. Use when adding a Menhir component, or changing one's props, state or form wiring.
4
+ ---
5
+
6
+ # Wiring Menhir components
7
+
8
+ Menhir is a set of unstyled, accessible UI primitives for SolidJS 2.0. This skill has what holds for every component. A component's own parts, props and keyboard behaviour are on its page only: read them there. To style a component, follow the styling skill, `node_modules/menhir/skills/menhir-styling/SKILL.md`.
9
+
10
+ ## Steps
11
+
12
+ 1. **Read the component's page**, `https://menhir-ui.com/docs/components/<name>.md`, where `<name>` is its entry point's, such as `toggle-group`. `https://menhir-ui.com/llms.txt` lists every component. The page is the latest release's; the types in `node_modules/menhir/dist/source/<name>/` are the installed version's, and they win where the two differ. Done when you can name the part that takes each prop you will pass, and have the page's Keyboard table for step 5.
13
+ 2. **Compose its parts** as the page's Anatomy nests them. Done when every part this use needs is rendered, in that nesting, imported from the component's own entry point.
14
+ 3. **Wire its state.** Control a value when the app reads it or sets it; leave the rest uncontrolled. Done when every value the app reads or sets is controlled, with its callback writing it back.
15
+ 4. **Join the form**, for a form control: give it a `name`. When its page has an "In a Field" section, put it in a Field as that section shows, with the `name` where it puts it. Done when the form's `FormData` has the control's value under that `name`, and, in a Field, its label, description and error come from the Field.
16
+ 5. **Check it**: run the app's typecheck, then use the component in the browser with the keyboard alone, as its page's Keyboard table says. Done when the typecheck passes and every row of the table works.
17
+
18
+ ## Rules for every component
19
+
20
+ ### Imports and parts
21
+
22
+ - **One entry point per component**, `menhir/<name>`, exporting its parts as a namespace: `import { Switch } from "menhir/switch"`, then `<Switch.Root>` and `<Switch.Thumb>`. A component of one part is exported as itself, such as `Form`. Import only from the entry points in the package's `exports`; a page names anything else its entry point exports, such as `includesText` from `menhir/combobox`.
23
+ - **`render`** renders a part through another element or the app's own component, with the part's props: `<Dialog.Trigger render={Button}>`, or `render={(props) => <a {...props} href="/settings" />}`. Spread the props it receives onto the element.
24
+ - **Popups render where you write them.** Dialog, Popover, Menu, Select, Tooltip and the other popups open in the browser's top layer, so they stay in place in the component tree, with no `<Portal>`.
25
+
26
+ ### State
27
+
28
+ - **Each piece of state is three props**: the value, its default and its change callback. A Switch has `checked`, `defaultChecked` and `onCheckedChange`; `open`, `value` and `pressed` follow the same pattern.
29
+ - **Uncontrolled**: pass the default, or nothing, and the component keeps the state. **Controlled**: pass the value, and write the new one in the callback; the component shows only the value you pass.
30
+ - **A controlled value is never `undefined`**, which means uncontrolled. Where its type allows `null`, `null` is empty: `value={selected() ?? null}`.
31
+ - **The callback's first argument is the new value.** Some callbacks, such as a popup's `onOpenChange`, also get `details`, with the `reason` (such as `"escape-key"`) and the `event`; the page's API reference types each callback.
32
+
33
+ ### Forms
34
+
35
+ - **A form control takes `name`** and submits its value with the form, as a native control does, so a plain `<form>` and its `FormData` work.
36
+ - **Field** (`menhir/field`) gives a control its label, description and error, and reports the browser's validity. A control joins one as its page's "In a Field" section shows, through `Field.Control`'s `render`.
37
+ - **Form** (`menhir/form`) takes `errors` keyed by each Field's `name`, such as a server's, or a schema's turned into them with `issuesToErrors` from `menhir/form/standard-schema`. It holds no values: the app keeps them, in a store or the form's `FormData`. The guide: `https://menhir-ui.com/docs/forms`.
38
+
39
+ ### Solid 2.0
40
+
41
+ Menhir runs on Solid 2.0 only. Write the app's code the 2.0 way: `node_modules/solid-js/skills/solid-2.0-cheatsheet/SKILL.md` has the whole of it. The rules that wiring a component touches:
42
+
43
+ - **Read props where they are used**, as `props.label`. A wrapper passes the rest on with `omit(props, "label")`, and adds defaults with `merge(defaults, props)`.
44
+ - **Pass values to state props**, called: `checked={on()}`. A callback prop takes the function itself: `onCheckedChange={setOn}`.