menhir 0.1.5 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (93) hide show
  1. package/README.md +2 -2
  2. package/dist/combobox/clear/ComboboxClear.js +39 -0
  3. package/dist/combobox/filter/includesText.js +24 -0
  4. package/dist/combobox/group/ComboboxGroup.js +32 -0
  5. package/dist/combobox/group/ComboboxGroupContext.js +10 -0
  6. package/dist/combobox/group-label/ComboboxGroupLabel.js +26 -0
  7. package/dist/combobox/index.js +3 -0
  8. package/dist/combobox/index.parts.js +12 -0
  9. package/dist/combobox/input/ComboboxInput.js +160 -0
  10. package/dist/combobox/item/ComboboxItem.js +98 -0
  11. package/dist/combobox/item/ComboboxItemContext.js +10 -0
  12. package/dist/combobox/item-indicator/ComboboxItemIndicator.js +22 -0
  13. package/dist/combobox/list/ComboboxList.js +26 -0
  14. package/dist/combobox/popup/ComboboxPopup.js +78 -0
  15. package/dist/combobox/root/ComboboxContext.js +137 -0
  16. package/dist/combobox/root/ComboboxRoot.js +21 -0
  17. package/dist/combobox/status/ComboboxStatus.js +31 -0
  18. package/dist/combobox/trigger/ComboboxTrigger.js +47 -0
  19. package/dist/select/group/SelectGroup.js +32 -0
  20. package/dist/select/group/SelectGroupContext.js +10 -0
  21. package/dist/select/group-label/SelectGroupLabel.js +26 -0
  22. package/dist/select/index.js +2 -0
  23. package/dist/select/index.parts.js +9 -0
  24. package/dist/select/item/SelectItem.js +97 -0
  25. package/dist/select/item/SelectItemContext.js +10 -0
  26. package/dist/select/item-indicator/SelectItemIndicator.js +22 -0
  27. package/dist/select/popup/SelectPopup.js +92 -0
  28. package/dist/select/root/SelectContext.js +99 -0
  29. package/dist/select/root/SelectRoot.js +21 -0
  30. package/dist/select/trigger/SelectTrigger.js +163 -0
  31. package/dist/select/value/SelectValue.js +32 -0
  32. package/dist/source/combobox/clear/ComboboxClear.d.ts +12 -0
  33. package/dist/source/combobox/clear/ComboboxClear.jsx +36 -0
  34. package/dist/source/combobox/filter/includesText.d.ts +12 -0
  35. package/dist/source/combobox/filter/includesText.js +26 -0
  36. package/dist/source/combobox/group/ComboboxGroup.d.ts +10 -0
  37. package/dist/source/combobox/group/ComboboxGroup.jsx +23 -0
  38. package/dist/source/combobox/group/ComboboxGroupContext.d.ts +10 -0
  39. package/dist/source/combobox/group/ComboboxGroupContext.js +12 -0
  40. package/dist/source/combobox/group-label/ComboboxGroupLabel.d.ts +10 -0
  41. package/dist/source/combobox/group-label/ComboboxGroupLabel.jsx +24 -0
  42. package/dist/source/combobox/index.d.ts +2 -0
  43. package/dist/source/combobox/index.js +4 -0
  44. package/dist/source/combobox/index.parts.d.ts +11 -0
  45. package/dist/source/combobox/index.parts.js +11 -0
  46. package/dist/source/combobox/input/ComboboxInput.d.ts +19 -0
  47. package/dist/source/combobox/input/ComboboxInput.jsx +174 -0
  48. package/dist/source/combobox/item/ComboboxItem.d.ts +25 -0
  49. package/dist/source/combobox/item/ComboboxItem.jsx +93 -0
  50. package/dist/source/combobox/item/ComboboxItemContext.d.ts +11 -0
  51. package/dist/source/combobox/item/ComboboxItemContext.js +12 -0
  52. package/dist/source/combobox/item-indicator/ComboboxItemIndicator.d.ts +11 -0
  53. package/dist/source/combobox/item-indicator/ComboboxItemIndicator.jsx +16 -0
  54. package/dist/source/combobox/list/ComboboxList.d.ts +12 -0
  55. package/dist/source/combobox/list/ComboboxList.jsx +17 -0
  56. package/dist/source/combobox/popup/ComboboxPopup.d.ts +34 -0
  57. package/dist/source/combobox/popup/ComboboxPopup.jsx +76 -0
  58. package/dist/source/combobox/root/ComboboxContext.d.ts +132 -0
  59. package/dist/source/combobox/root/ComboboxContext.js +156 -0
  60. package/dist/source/combobox/root/ComboboxRoot.d.ts +13 -0
  61. package/dist/source/combobox/root/ComboboxRoot.jsx +13 -0
  62. package/dist/source/combobox/status/ComboboxStatus.d.ts +19 -0
  63. package/dist/source/combobox/status/ComboboxStatus.jsx +27 -0
  64. package/dist/source/combobox/trigger/ComboboxTrigger.d.ts +13 -0
  65. package/dist/source/combobox/trigger/ComboboxTrigger.jsx +40 -0
  66. package/dist/source/floating/dismissal/reason.d.ts +7 -2
  67. package/dist/source/select/group/SelectGroup.d.ts +10 -0
  68. package/dist/source/select/group/SelectGroup.jsx +23 -0
  69. package/dist/source/select/group/SelectGroupContext.d.ts +10 -0
  70. package/dist/source/select/group/SelectGroupContext.js +12 -0
  71. package/dist/source/select/group-label/SelectGroupLabel.d.ts +10 -0
  72. package/dist/source/select/group-label/SelectGroupLabel.jsx +24 -0
  73. package/dist/source/select/index.d.ts +1 -0
  74. package/dist/source/select/index.js +1 -0
  75. package/dist/source/select/index.parts.d.ts +8 -0
  76. package/dist/source/select/index.parts.js +8 -0
  77. package/dist/source/select/item/SelectItem.d.ts +24 -0
  78. package/dist/source/select/item/SelectItem.jsx +92 -0
  79. package/dist/source/select/item/SelectItemContext.d.ts +11 -0
  80. package/dist/source/select/item/SelectItemContext.js +12 -0
  81. package/dist/source/select/item-indicator/SelectItemIndicator.d.ts +11 -0
  82. package/dist/source/select/item-indicator/SelectItemIndicator.jsx +16 -0
  83. package/dist/source/select/popup/SelectPopup.d.ts +32 -0
  84. package/dist/source/select/popup/SelectPopup.jsx +97 -0
  85. package/dist/source/select/root/SelectContext.d.ts +106 -0
  86. package/dist/source/select/root/SelectContext.js +119 -0
  87. package/dist/source/select/root/SelectRoot.d.ts +13 -0
  88. package/dist/source/select/root/SelectRoot.jsx +11 -0
  89. package/dist/source/select/trigger/SelectTrigger.d.ts +15 -0
  90. package/dist/source/select/trigger/SelectTrigger.jsx +181 -0
  91. package/dist/source/select/value/SelectValue.d.ts +20 -0
  92. package/dist/source/select/value/SelectValue.jsx +27 -0
  93. package/package.json +11 -1
@@ -0,0 +1,93 @@
1
+ import { createUniqueId, merge, omit, onSettled } from "solid-js";
2
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
3
+ import { renderElement } from "../../core/renderElement.jsx";
4
+ import { useComboboxContext } from "../root/ComboboxContext.js";
5
+ import { ComboboxItemContext } from "./ComboboxItemContext.js";
6
+ /**
7
+ * A value to select: a `<div role="option">` with `aria-selected`. A click
8
+ * selects it, sets the input's text to its label and closes the list, and
9
+ * so does Enter while it is the active item; a disabled item is not
10
+ * selected. Render the items that match the text, and place them inside
11
+ * `Combobox.List`.
12
+ */
13
+ export function ComboboxItem(props) {
14
+ const combobox = useComboboxContext();
15
+ let element;
16
+ const generated = createUniqueId();
17
+ // The consumer's `id`, else the generated one, which the Input names
18
+ // while the item is active, so `false` does not remove it. Kept out of
19
+ // the merge ("`undefined` is a value", PLAN.md section 8).
20
+ const id = () => props.id || generated;
21
+ const item = {
22
+ get element() {
23
+ return element;
24
+ },
25
+ get id() {
26
+ return id();
27
+ },
28
+ get text() {
29
+ return props.label ?? labelOf(element);
30
+ },
31
+ get value() {
32
+ return props.value;
33
+ },
34
+ get disabled() {
35
+ return props.disabled;
36
+ },
37
+ };
38
+ onSettled(() => combobox.list.register(item));
39
+ const selected = () => combobox.value() === props.value;
40
+ const attributes = {
41
+ get "data-selected"() {
42
+ return selected() ? "" : undefined;
43
+ },
44
+ get "data-unselected"() {
45
+ return selected() ? undefined : "";
46
+ },
47
+ };
48
+ const ownProps = merge(attributes, {
49
+ role: "option",
50
+ get id() {
51
+ return id();
52
+ },
53
+ // Solid 2.0 removes an attribute set to false, so spell the value out.
54
+ get "aria-selected"() {
55
+ return selected() ? "true" : "false";
56
+ },
57
+ get "data-active"() {
58
+ return combobox.isActive(item) ? "" : undefined;
59
+ },
60
+ get "aria-disabled"() {
61
+ return props.disabled ? "true" : undefined;
62
+ },
63
+ get "data-disabled"() {
64
+ return props.disabled ? "" : undefined;
65
+ },
66
+ onPointerMove: (event) => combobox.list.onPointerMove(item, event),
67
+ onClick: (event) => {
68
+ if (combobox.select(item, event))
69
+ combobox.setOpen(false, { reason: "item-press", event });
70
+ },
71
+ ref: (el) => {
72
+ element = el;
73
+ },
74
+ });
75
+ const elementProps = mergeElementProps(ownProps, omit(props, "render", "value", "disabled", "label", "id"));
76
+ return (<ComboboxItemContext value={attributes}>
77
+ {renderElement(props, elementProps, (p) => (<div {...p}/>))}
78
+ </ComboboxItemContext>);
79
+ }
80
+ /**
81
+ * An item's own text: its text content without what is `aria-hidden`, such
82
+ * as its ItemIndicator's check mark, which is no part of its name either.
83
+ */
84
+ function labelOf(node) {
85
+ let text = "";
86
+ for (const child of node.childNodes) {
87
+ if (child.nodeType === 3)
88
+ text += child.nodeValue;
89
+ else if (child.getAttribute?.("aria-hidden") !== "true")
90
+ text += labelOf(child);
91
+ }
92
+ return text;
93
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Whether an item is selected, as `data-*` attributes, each `""` when it
3
+ * applies and absent otherwise: what the item and its
4
+ * `Combobox.ItemIndicator` carry.
5
+ */
6
+ export type ComboboxItemAttributes = {
7
+ readonly "data-selected": "" | undefined;
8
+ readonly "data-unselected": "" | undefined;
9
+ };
10
+ export declare const ComboboxItemContext: import("solid-js").Context<ComboboxItemAttributes | null>;
11
+ export declare function useComboboxItemContext(): ComboboxItemAttributes;
@@ -0,0 +1,12 @@
1
+ import { createContext, useContext } from "solid-js";
2
+ import { isDev } from "@solidjs/web";
3
+ // `null` outside an Item, so that the part can say so.
4
+ export const ComboboxItemContext = createContext(null);
5
+ export function useComboboxItemContext() {
6
+ const context = useContext(ComboboxItemContext);
7
+ // Production builds drop the check, and its message, with `isDev`.
8
+ if (isDev && !context) {
9
+ throw new Error("[menhir] Combobox.ItemIndicator must be placed within <Combobox.Item>.");
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 ComboboxItemIndicatorProps extends JSX.HTMLAttributes<HTMLElement> {
4
+ render?: RenderProp;
5
+ }
6
+ /**
7
+ * The check mark of an item: an `aria-hidden` `<span>` with its item's
8
+ * `data-selected` or `data-unselected`, always rendered, so that CSS shows
9
+ * it by state. Place it inside `Combobox.Item`.
10
+ */
11
+ export declare function ComboboxItemIndicator(props: ComboboxItemIndicatorProps): JSX.Element;
@@ -0,0 +1,16 @@
1
+ import { merge, omit } from "solid-js";
2
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
3
+ import { renderElement } from "../../core/renderElement.jsx";
4
+ import { useComboboxItemContext } from "../item/ComboboxItemContext.js";
5
+ /**
6
+ * The check mark of an item: an `aria-hidden` `<span>` with its item's
7
+ * `data-selected` or `data-unselected`, always rendered, so that CSS shows
8
+ * it by state. Place it inside `Combobox.Item`.
9
+ */
10
+ export function ComboboxItemIndicator(props) {
11
+ const ownProps = merge(useComboboxItemContext(), {
12
+ "aria-hidden": "true",
13
+ });
14
+ const elementProps = mergeElementProps(ownProps, omit(props, "render"));
15
+ return renderElement(props, elementProps, (p) => <span {...p}/>);
16
+ }
@@ -0,0 +1,12 @@
1
+ import type { JSX } from "@solidjs/web";
2
+ import type { RenderProp } from "../../core/renderElement.tsx";
3
+ export interface ComboboxListProps extends Omit<JSX.HTMLAttributes<HTMLElement>, "id"> {
4
+ render?: RenderProp;
5
+ }
6
+ /**
7
+ * The list of items: a `<div role="listbox">` with the `id` the Input
8
+ * names in `aria-controls`. Name it with `aria-labelledby`, such as the
9
+ * `id` of the input's `<label>`, or `aria-label`. Place it inside
10
+ * `Combobox.Popup`, around the items.
11
+ */
12
+ export declare function ComboboxList(props: ComboboxListProps): JSX.Element;
@@ -0,0 +1,17 @@
1
+ import { omit } from "solid-js";
2
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
3
+ import { renderElement } from "../../core/renderElement.jsx";
4
+ import { useComboboxContext } from "../root/ComboboxContext.js";
5
+ /**
6
+ * The list of items: a `<div role="listbox">` with the `id` the Input
7
+ * names in `aria-controls`. Name it with `aria-labelledby`, such as the
8
+ * `id` of the input's `<label>`, or `aria-label`. Place it inside
9
+ * `Combobox.Popup`, around the items.
10
+ */
11
+ export function ComboboxList(props) {
12
+ const combobox = useComboboxContext();
13
+ const elementProps = mergeElementProps({ id: combobox.listId, role: "listbox" },
14
+ // Kept out, as typed: a stray one would break `aria-controls`.
15
+ omit(props, "render", "id"));
16
+ return renderElement(props, elementProps, (p) => <div {...p}/>);
17
+ }
@@ -0,0 +1,34 @@
1
+ import type { JSX } from "@solidjs/web";
2
+ import type { RenderProp } from "../../core/renderElement.tsx";
3
+ import type { Align, Side, VirtualElement } from "../../floating/positioning/createPosition.ts";
4
+ /** What the Popup is placed against: an element, or a virtual one. */
5
+ export type ComboboxAnchor = Element | VirtualElement;
6
+ export interface ComboboxPopupProps extends JSX.HTMLAttributes<HTMLElement> {
7
+ /** The side of the anchor it is placed against. Default `"bottom"`. */
8
+ side?: Side;
9
+ /** Where along that side, following the writing direction. Default `"start"`. */
10
+ align?: Align;
11
+ /** The gap from the anchor, in pixels. Default 0. */
12
+ sideOffset?: number;
13
+ /** The shift along the anchor's side, in pixels. Default 0. */
14
+ alignOffset?: number;
15
+ /** The space kept from the viewport's edges, in pixels. Default 5. */
16
+ collisionPadding?: number;
17
+ /**
18
+ * What it is placed against instead of the Input, such as a box around
19
+ * the Input and the Trigger: an element, a virtual element, or a
20
+ * function returning one.
21
+ */
22
+ anchor?: ComboboxAnchor | (() => ComboboxAnchor | undefined);
23
+ render?: RenderProp;
24
+ }
25
+ /**
26
+ * The box the list shows in: a `<div popover="manual">` in the top layer,
27
+ * placed against the Input. It takes no focus: the input keeps it, and a
28
+ * press in it moves none. Escape, focus leaving the input, the Trigger and
29
+ * selecting an item close it. Its CSS variables `--anchor-width` and
30
+ * `--available-height` are for its `min-width` and `max-height`. Place
31
+ * `Combobox.List` inside it, and `Combobox.Status` or a `<Loading>`
32
+ * fallback beside the List. Place it inside `Combobox.Root`.
33
+ */
34
+ export declare function ComboboxPopup(props: ComboboxPopupProps): JSX.Element;
@@ -0,0 +1,76 @@
1
+ import { merge, omit } from "solid-js";
2
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
3
+ import { renderElement } from "../../core/renderElement.jsx";
4
+ import { createPosition } from "../../floating/positioning/createPosition.js";
5
+ import { flip } from "../../floating/positioning/flip.js";
6
+ import { offset } from "../../floating/positioning/offset.js";
7
+ import { shift } from "../../floating/positioning/shift.js";
8
+ import { size } from "../../floating/positioning/size.js";
9
+ import { createPresence } from "../../floating/presence/presence.js";
10
+ import { useComboboxContext } from "../root/ComboboxContext.js";
11
+ /**
12
+ * The box the list shows in: a `<div popover="manual">` in the top layer,
13
+ * placed against the Input. It takes no focus: the input keeps it, and a
14
+ * press in it moves none. Escape, focus leaving the input, the Trigger and
15
+ * selecting an item close it. Its CSS variables `--anchor-width` and
16
+ * `--available-height` are for its `min-width` and `max-height`. Place
17
+ * `Combobox.List` inside it, and `Combobox.Status` or a `<Loading>`
18
+ * fallback beside the List. Place it inside `Combobox.Root`.
19
+ */
20
+ export function ComboboxPopup(props) {
21
+ const combobox = useComboboxContext();
22
+ let popup;
23
+ // Presence first: the popover is shown before it is placed. A manual
24
+ // popover: the browser would light-dismiss an `auto` one on a press in
25
+ // the input, which is not its invoker (README, "Settled after Phase 2").
26
+ createPresence({
27
+ get popup() {
28
+ return popup;
29
+ },
30
+ get open() {
31
+ return combobox.open();
32
+ },
33
+ });
34
+ createPosition({
35
+ get anchor() {
36
+ const anchor = props.anchor;
37
+ return ((typeof anchor === "function" ? anchor() : anchor) ?? combobox.input);
38
+ },
39
+ get popup() {
40
+ return popup;
41
+ },
42
+ get open() {
43
+ return combobox.open();
44
+ },
45
+ get side() {
46
+ return props.side ?? "bottom";
47
+ },
48
+ get align() {
49
+ return props.align ?? "start";
50
+ },
51
+ get middleware() {
52
+ const padding = props.collisionPadding ?? 5;
53
+ return [
54
+ offset({
55
+ mainAxis: props.sideOffset ?? 0,
56
+ alignmentAxis: props.alignOffset ?? 0,
57
+ }),
58
+ flip({ padding }),
59
+ shift({ padding }),
60
+ // A list is often longer than the space beside the input.
61
+ size({ padding }),
62
+ ];
63
+ },
64
+ });
65
+ const ownProps = merge(combobox.attributes, {
66
+ popover: "manual",
67
+ // A press in the list would take focus from the input, which holds it
68
+ // for the keys, and leaving it would close the list.
69
+ onMouseDown: (event) => event.preventDefault(),
70
+ ref: (element) => {
71
+ popup = element;
72
+ },
73
+ });
74
+ const elementProps = mergeElementProps(ownProps, omit(props, "render", "side", "align", "sideOffset", "alignOffset", "collisionPadding", "anchor"));
75
+ return renderElement(props, elementProps, (p) => <div {...p}/>);
76
+ }
@@ -0,0 +1,132 @@
1
+ import type { OpenChangeDetails } from "../../floating/dismissal/reason.ts";
2
+ import type { ActiveDescendantItem, ActiveDescendantList } from "../../floating/list-navigation/activeDescendant.ts";
3
+ /**
4
+ * Why the text in the input changed:
5
+ *
6
+ * - `input-change`: the user typed or edited it.
7
+ * - `item-press`: an item was selected, and the text is its label.
8
+ * - `escape-key`: Escape on a closed list emptied it.
9
+ * - `clear-press`: `Combobox.Clear` emptied it.
10
+ * - `focus-out`: focus left the input, and the text went back to the
11
+ * selected item's label, or empty.
12
+ * - `reset`: its form was reset.
13
+ */
14
+ export type InputValueChangeReason = "input-change" | "item-press" | "escape-key" | "clear-press" | "focus-out" | "reset";
15
+ /** The second argument of `onInputValueChange`. */
16
+ export interface InputValueChangeDetails {
17
+ readonly reason: InputValueChangeReason;
18
+ readonly event: Event;
19
+ }
20
+ /**
21
+ * A combobox's state as `data-*` attributes, each `""` when it applies and
22
+ * absent otherwise: one of `data-open` and `data-closed`, `data-disabled`
23
+ * and `data-required`.
24
+ */
25
+ export type ComboboxStateAttributes = {
26
+ readonly "data-open": "" | undefined;
27
+ readonly "data-closed": "" | undefined;
28
+ readonly "data-disabled": "" | undefined;
29
+ readonly "data-required": "" | undefined;
30
+ };
31
+ /** An item of a combobox, as its list navigation knows it. */
32
+ export interface ComboboxItemEntry extends ActiveDescendantItem {
33
+ /** What the Combobox's value is while this item is selected. */
34
+ readonly value: string;
35
+ /** Whether the item is disabled: reached, but not selected. */
36
+ readonly disabled?: boolean;
37
+ }
38
+ /** The props of `Combobox.Root`, less its children. */
39
+ export interface ComboboxStateProps {
40
+ /** The selected item's `value`, or `null` for none, when controlled. */
41
+ value?: string | null;
42
+ /** The item selected at first, when uncontrolled. */
43
+ defaultValue?: string | null;
44
+ /**
45
+ * Called with the new value when the user selects an item or empties
46
+ * the Combobox, or its form is reset. When controlled, the selection
47
+ * changes only if `value` does.
48
+ */
49
+ onValueChange?: (value: string | null) => void;
50
+ /** The text in the input, when controlled. */
51
+ inputValue?: string;
52
+ /**
53
+ * The text in the input at first, when uncontrolled. Without it, the
54
+ * selected item's label once the items have mounted, and nothing in the
55
+ * server HTML.
56
+ */
57
+ defaultInputValue?: string;
58
+ /**
59
+ * Called with the new text, and why, when the user types, selects an
60
+ * item or empties the Combobox, when focus leaves and the text goes back
61
+ * to the selection, or when its form is reset. Filter by it for
62
+ * `input-change` only. When controlled, the text changes only if
63
+ * `inputValue` does.
64
+ */
65
+ onInputValueChange?: (text: string, details: InputValueChangeDetails) => void;
66
+ /** Whether the list is open, when controlled. */
67
+ open?: boolean;
68
+ /** Whether the list is open at first, when uncontrolled. */
69
+ defaultOpen?: boolean;
70
+ /**
71
+ * Called with the new value, and why, when the user opens or closes the
72
+ * list. When controlled, the list changes only if `open` does.
73
+ */
74
+ onOpenChange?: (open: boolean, details: OpenChangeDetails) => void;
75
+ /** Submitted with the form, as the hidden input's `name`. */
76
+ name?: string;
77
+ /** The `id` of the form the Combobox belongs to, when it is not inside it. */
78
+ form?: string;
79
+ /** Whether the form can be submitted only once an item is selected. */
80
+ required?: boolean;
81
+ /** Whether the Combobox ignores the user. It cannot be focused either. */
82
+ disabled?: boolean;
83
+ }
84
+ /** What `Combobox.Root` shares with its parts through context. */
85
+ export interface Combobox {
86
+ /** Whether the list is open. */
87
+ open(): boolean;
88
+ /** Asks to open or close the list, saying why. */
89
+ setOpen(open: boolean, details: OpenChangeDetails): void;
90
+ /** The selected item's `value`, or `null`. */
91
+ value(): string | null;
92
+ /** Asks for a value, as the hidden input's `change` and a reset do. */
93
+ setValue(value: string | null): void;
94
+ /** The text the input shows. */
95
+ inputValue(): string;
96
+ /** Asks for a text, saying why. */
97
+ setInputValue(text: string, details: InputValueChangeDetails): void;
98
+ /**
99
+ * Selects `item`: its value through the hidden input, which fires
100
+ * `input` and `change` for the form, and its label as the text. A
101
+ * disabled item changes nothing. Returns whether it could be selected.
102
+ */
103
+ select(item: ComboboxItemEntry | undefined, event: Event): boolean;
104
+ /** Empties the text and the value. */
105
+ clear(event: Event, reason: "escape-key" | "clear-press"): void;
106
+ /**
107
+ * Puts the text back to the selected item's label as focus leaves, or
108
+ * clears the value if the user emptied the text.
109
+ */
110
+ revert(event: Event): void;
111
+ /** The label of the item with `value`, if one has mounted. */
112
+ labelOfValue(value: string | null): string | undefined;
113
+ /** Whether `item` is the active one of an open list, for its `data-active`. */
114
+ isActive(item: ComboboxItemEntry): boolean;
115
+ /** The number of items in the list, for `Combobox.Status`. */
116
+ count(): number;
117
+ readonly list: ActiveDescendantList<ComboboxItemEntry>;
118
+ /** The List's `id`, which the Input names before the List is rendered. */
119
+ readonly listId: string;
120
+ /** The input, once rendered: focused by the Trigger and Clear. */
121
+ input: HTMLInputElement | undefined;
122
+ /** The hidden input, once rendered: the form's view of the value. */
123
+ hiddenInput: HTMLInputElement | undefined;
124
+ /** The `data-*` attributes of the Input, the Trigger and the Popup. */
125
+ readonly attributes: ComboboxStateAttributes;
126
+ /** The Root's props, for the parts' elements. */
127
+ readonly props: ComboboxStateProps;
128
+ }
129
+ /** Creates a combobox's state and its list navigation. */
130
+ export declare function createCombobox(props: ComboboxStateProps): Combobox;
131
+ export declare const ComboboxContext: import("solid-js").Context<Combobox | null>;
132
+ export declare function useComboboxContext(): Combobox;
@@ -0,0 +1,156 @@
1
+ import { isDev } from "@solidjs/web";
2
+ import { createContext, createMemo, createUniqueId, untrack, useContext, } from "solid-js";
3
+ import { createControllableSignal } from "../../core/createControllableSignal.js";
4
+ import { createActiveDescendantList } from "../../floating/list-navigation/activeDescendant.js";
5
+ /** Creates a combobox's state and its list navigation. */
6
+ export function createCombobox(props) {
7
+ const [open, setOpen] = createControllableSignal({
8
+ get value() {
9
+ return props.open;
10
+ },
11
+ get defaultValue() {
12
+ return props.defaultOpen ?? false;
13
+ },
14
+ onChange: (value, details) => props.onOpenChange?.(value, details),
15
+ });
16
+ const [value, setValue, requestedValue] = createControllableSignal({
17
+ get value() {
18
+ return props.value;
19
+ },
20
+ get defaultValue() {
21
+ return props.defaultValue ?? null;
22
+ },
23
+ onChange: (value) => props.onValueChange?.(value),
24
+ });
25
+ // `null` until the user, a selection or a reset sets a text: the input
26
+ // then shows the selected item's label, once it has mounted (README,
27
+ // "Two values"). The consumer only ever sees a string.
28
+ const [text, setText, requestedText] = createControllableSignal({
29
+ get value() {
30
+ return props.inputValue;
31
+ },
32
+ get defaultValue() {
33
+ return props.defaultInputValue ?? null;
34
+ },
35
+ onChange: (text, details) => props.onInputValueChange?.(text, details),
36
+ });
37
+ // Disabled items stay reachable; Home and End belong to the text, so the
38
+ // arrow keys wrap, as the list autocomplete example's do (README).
39
+ const list = createActiveDescendantList({
40
+ get open() {
41
+ return open();
42
+ },
43
+ wrap: true,
44
+ });
45
+ const labelOfValue = (selected) => {
46
+ const item = list.items().find((item) => item.value === selected);
47
+ // An item's text is always given: its `label`, else its content.
48
+ return item?.text.trim();
49
+ };
50
+ // The selected item's label, kept while the consumer has filtered that
51
+ // item out of the list, so focus leaving can put it back. Runs again when
52
+ // an item mounts or leaves, and when the value changes; a string, so an
53
+ // equal label stops there.
54
+ let labelled = null;
55
+ const selectedLabel = createMemo((previous) => {
56
+ const selected = value();
57
+ const label = labelOfValue(selected) ?? (selected === labelled ? previous : undefined);
58
+ labelled = selected;
59
+ return label;
60
+ });
61
+ // A memo, so that the input's draft, derived from it, is not derived
62
+ // again, over an edit, when nothing it shows changed.
63
+ const inputValue = createMemo(() => text() ?? selectedLabel() ?? "");
64
+ // What the input shows after the sets of this turn.
65
+ const requestedInputValue = () => requestedText() ?? untrack(selectedLabel) ?? "";
66
+ let edited = false;
67
+ const setInputValue = (next, details) => {
68
+ // Whether the user has edited the text since it was last set for
69
+ // another reason: only then does focus leaving change anything.
70
+ edited = details.reason === "input-change";
71
+ if (next !== requestedInputValue())
72
+ setText(next, details);
73
+ };
74
+ // Through the hidden input, which fires the events a native control
75
+ // fires, so the form sees them; the Combobox takes the value from its
76
+ // `change`.
77
+ const commit = (next) => {
78
+ if (next === requestedValue())
79
+ return;
80
+ const input = combobox.hiddenInput;
81
+ if (!input)
82
+ return setValue(next);
83
+ input.value = next ?? "";
84
+ input.dispatchEvent(new Event("input", { bubbles: true }));
85
+ input.dispatchEvent(new Event("change", { bubbles: true }));
86
+ };
87
+ const combobox = {
88
+ open,
89
+ setOpen,
90
+ value,
91
+ setValue,
92
+ inputValue,
93
+ setInputValue,
94
+ select: (item, event) => {
95
+ if (!item || item.disabled)
96
+ return false;
97
+ commit(item.value);
98
+ setInputValue(item.text.trim(), { reason: "item-press", event });
99
+ return true;
100
+ },
101
+ clear: (event, reason) => {
102
+ commit(null);
103
+ setInputValue("", { reason, event });
104
+ },
105
+ revert: (event) => {
106
+ // An untouched Combobox keeps its value, though its text may be
107
+ // empty while its items have not mounted.
108
+ if (!edited)
109
+ return;
110
+ if (!requestedInputValue().trim()) {
111
+ commit(null);
112
+ setInputValue("", { reason: "focus-out", event });
113
+ return;
114
+ }
115
+ const selected = requestedValue();
116
+ const label = selected === null
117
+ ? ""
118
+ : (labelOfValue(selected) ?? selectedLabel() ?? "");
119
+ setInputValue(label, { reason: "focus-out", event });
120
+ },
121
+ labelOfValue,
122
+ // Only while open, as the input names it.
123
+ isActive: (item) => open() && list.isActive(item),
124
+ count: () => list.items().length,
125
+ list,
126
+ listId: createUniqueId(),
127
+ input: undefined,
128
+ hiddenInput: undefined,
129
+ attributes: {
130
+ get "data-open"() {
131
+ return open() ? "" : undefined;
132
+ },
133
+ get "data-closed"() {
134
+ return open() ? undefined : "";
135
+ },
136
+ get "data-disabled"() {
137
+ return props.disabled ? "" : undefined;
138
+ },
139
+ get "data-required"() {
140
+ return props.required ? "" : undefined;
141
+ },
142
+ },
143
+ props,
144
+ };
145
+ return combobox;
146
+ }
147
+ // `null` outside a Root, so that the part can name what is missing.
148
+ export const ComboboxContext = createContext(null);
149
+ export function useComboboxContext() {
150
+ const context = useContext(ComboboxContext);
151
+ // Production builds drop the check, and its message, with `isDev`.
152
+ if (isDev && !context) {
153
+ throw new Error("[menhir] Combobox parts must be placed within <Combobox.Root>.");
154
+ }
155
+ return context;
156
+ }
@@ -0,0 +1,13 @@
1
+ import type { JSX } from "@solidjs/web";
2
+ import type { ComboboxStateProps } from "./ComboboxContext.ts";
3
+ export interface ComboboxRootProps extends ComboboxStateProps {
4
+ children?: JSX.Element;
5
+ }
6
+ /**
7
+ * An input that picks a value from a list: holds the value, the text in
8
+ * the input and whether the list is open, and shares them with
9
+ * `Combobox.Input`, `Combobox.Trigger`, `Combobox.Clear`, `Combobox.Popup`
10
+ * and the parts inside it. Its form props go on the hidden input the Input
11
+ * renders. Renders no element of its own.
12
+ */
13
+ export declare function ComboboxRoot(props: ComboboxRootProps): JSX.Element;
@@ -0,0 +1,13 @@
1
+ import { ComboboxContext, createCombobox } from "./ComboboxContext.js";
2
+ /**
3
+ * An input that picks a value from a list: holds the value, the text in
4
+ * the input and whether the list is open, and shares them with
5
+ * `Combobox.Input`, `Combobox.Trigger`, `Combobox.Clear`, `Combobox.Popup`
6
+ * and the parts inside it. Its form props go on the hidden input the Input
7
+ * renders. Renders no element of its own.
8
+ */
9
+ export function ComboboxRoot(props) {
10
+ return (<ComboboxContext value={createCombobox(props)}>
11
+ {props.children}
12
+ </ComboboxContext>);
13
+ }
@@ -0,0 +1,19 @@
1
+ import type { JSX } from "@solidjs/web";
2
+ import type { RenderProp } from "../../core/renderElement.tsx";
3
+ export interface ComboboxStatusProps extends Omit<JSX.HTMLAttributes<HTMLElement>, "children"> {
4
+ /**
5
+ * What it says for the number of items in the list, such as `(count) =>
6
+ * count === 0 && "No results"`, shown while the list is open.
7
+ */
8
+ children: (count: number) => JSX.Element;
9
+ render?: RenderProp;
10
+ }
11
+ /**
12
+ * The number of items, said in the app's words: a `<div role="status">`, a
13
+ * polite live region, with what its function returns while the list is
14
+ * open, and nothing while it is closed. In `Combobox.Popup`, beside the
15
+ * List, it is the message for an empty list; visually hidden beside the
16
+ * input, it tells a screen reader how many items match as the user types.
17
+ * Place it inside `Combobox.Root`.
18
+ */
19
+ export declare function ComboboxStatus(props: ComboboxStatusProps): JSX.Element;
@@ -0,0 +1,27 @@
1
+ import { omit } from "solid-js";
2
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
3
+ import { renderElement } from "../../core/renderElement.jsx";
4
+ import { useComboboxContext } from "../root/ComboboxContext.js";
5
+ /**
6
+ * The number of items, said in the app's words: a `<div role="status">`, a
7
+ * polite live region, with what its function returns while the list is
8
+ * open, and nothing while it is closed. In `Combobox.Popup`, beside the
9
+ * List, it is the message for an empty list; visually hidden beside the
10
+ * input, it tells a screen reader how many items match as the user types.
11
+ * Place it inside `Combobox.Root`.
12
+ */
13
+ export function ComboboxStatus(props) {
14
+ const combobox = useComboboxContext();
15
+ const elementProps = mergeElementProps({
16
+ role: "status",
17
+ get children() {
18
+ // Called while closed too: a function that reads pending data,
19
+ // such as `isPending(() => results())`, reads it from the start, so
20
+ // `<Loading>` shows its fallback then, and opening the list, which
21
+ // would read it first, is not held until the data lands.
22
+ const said = props.children(combobox.count());
23
+ return combobox.open() ? said : undefined;
24
+ },
25
+ }, omit(props, "render", "children"));
26
+ return renderElement(props, elementProps, (p) => <div {...p}/>);
27
+ }