menhir 0.1.5 → 0.1.7

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 (117) hide show
  1. package/README.md +2 -2
  2. package/dist/autocomplete/index.js +3 -0
  3. package/dist/autocomplete/index.parts.js +11 -0
  4. package/dist/autocomplete/input/AutocompleteInput.js +45 -0
  5. package/dist/autocomplete/item/AutocompleteItem.js +29 -0
  6. package/dist/autocomplete/root/AutocompleteContext.js +39 -0
  7. package/dist/autocomplete/root/AutocompleteRoot.js +22 -0
  8. package/dist/combobox/clear/ComboboxClear.js +39 -0
  9. package/dist/combobox/filter/includesText.js +24 -0
  10. package/dist/combobox/group/ComboboxGroup.js +32 -0
  11. package/dist/combobox/group/ComboboxGroupContext.js +10 -0
  12. package/dist/combobox/group-label/ComboboxGroupLabel.js +26 -0
  13. package/dist/combobox/index.js +3 -0
  14. package/dist/combobox/index.parts.js +12 -0
  15. package/dist/combobox/input/ComboboxInput.js +78 -0
  16. package/dist/combobox/input/createComboboxInput.js +112 -0
  17. package/dist/combobox/item/ComboboxItem.js +54 -0
  18. package/dist/combobox/item/ComboboxItemContext.js +10 -0
  19. package/dist/combobox/item/createComboboxItem.js +62 -0
  20. package/dist/combobox/item-indicator/ComboboxItemIndicator.js +22 -0
  21. package/dist/combobox/list/ComboboxList.js +26 -0
  22. package/dist/combobox/popup/ComboboxPopup.js +78 -0
  23. package/dist/combobox/root/ComboboxContext.js +161 -0
  24. package/dist/combobox/root/ComboboxRoot.js +21 -0
  25. package/dist/combobox/status/ComboboxStatus.js +31 -0
  26. package/dist/combobox/trigger/ComboboxTrigger.js +47 -0
  27. package/dist/select/group/SelectGroup.js +32 -0
  28. package/dist/select/group/SelectGroupContext.js +10 -0
  29. package/dist/select/group-label/SelectGroupLabel.js +26 -0
  30. package/dist/select/index.js +2 -0
  31. package/dist/select/index.parts.js +9 -0
  32. package/dist/select/item/SelectItem.js +97 -0
  33. package/dist/select/item/SelectItemContext.js +10 -0
  34. package/dist/select/item-indicator/SelectItemIndicator.js +22 -0
  35. package/dist/select/popup/SelectPopup.js +92 -0
  36. package/dist/select/root/SelectContext.js +99 -0
  37. package/dist/select/root/SelectRoot.js +21 -0
  38. package/dist/select/trigger/SelectTrigger.js +163 -0
  39. package/dist/select/value/SelectValue.js +32 -0
  40. package/dist/source/autocomplete/index.d.ts +2 -0
  41. package/dist/source/autocomplete/index.js +4 -0
  42. package/dist/source/autocomplete/index.parts.d.ts +10 -0
  43. package/dist/source/autocomplete/index.parts.js +12 -0
  44. package/dist/source/autocomplete/input/AutocompleteInput.d.ts +18 -0
  45. package/dist/source/autocomplete/input/AutocompleteInput.jsx +37 -0
  46. package/dist/source/autocomplete/item/AutocompleteItem.d.ts +20 -0
  47. package/dist/source/autocomplete/item/AutocompleteItem.jsx +25 -0
  48. package/dist/source/autocomplete/root/AutocompleteContext.d.ts +59 -0
  49. package/dist/source/autocomplete/root/AutocompleteContext.js +33 -0
  50. package/dist/source/autocomplete/root/AutocompleteRoot.d.ts +13 -0
  51. package/dist/source/autocomplete/root/AutocompleteRoot.jsx +15 -0
  52. package/dist/source/combobox/clear/ComboboxClear.d.ts +12 -0
  53. package/dist/source/combobox/clear/ComboboxClear.jsx +36 -0
  54. package/dist/source/combobox/filter/includesText.d.ts +12 -0
  55. package/dist/source/combobox/filter/includesText.js +26 -0
  56. package/dist/source/combobox/group/ComboboxGroup.d.ts +10 -0
  57. package/dist/source/combobox/group/ComboboxGroup.jsx +23 -0
  58. package/dist/source/combobox/group/ComboboxGroupContext.d.ts +10 -0
  59. package/dist/source/combobox/group/ComboboxGroupContext.js +12 -0
  60. package/dist/source/combobox/group-label/ComboboxGroupLabel.d.ts +10 -0
  61. package/dist/source/combobox/group-label/ComboboxGroupLabel.jsx +24 -0
  62. package/dist/source/combobox/index.d.ts +2 -0
  63. package/dist/source/combobox/index.js +4 -0
  64. package/dist/source/combobox/index.parts.d.ts +11 -0
  65. package/dist/source/combobox/index.parts.js +11 -0
  66. package/dist/source/combobox/input/ComboboxInput.d.ts +19 -0
  67. package/dist/source/combobox/input/ComboboxInput.jsx +52 -0
  68. package/dist/source/combobox/input/createComboboxInput.d.ts +30 -0
  69. package/dist/source/combobox/input/createComboboxInput.js +140 -0
  70. package/dist/source/combobox/item/ComboboxItem.d.ts +25 -0
  71. package/dist/source/combobox/item/ComboboxItem.jsx +50 -0
  72. package/dist/source/combobox/item/ComboboxItemContext.d.ts +11 -0
  73. package/dist/source/combobox/item/ComboboxItemContext.js +12 -0
  74. package/dist/source/combobox/item/createComboboxItem.d.ts +21 -0
  75. package/dist/source/combobox/item/createComboboxItem.js +61 -0
  76. package/dist/source/combobox/item-indicator/ComboboxItemIndicator.d.ts +11 -0
  77. package/dist/source/combobox/item-indicator/ComboboxItemIndicator.jsx +16 -0
  78. package/dist/source/combobox/list/ComboboxList.d.ts +12 -0
  79. package/dist/source/combobox/list/ComboboxList.jsx +17 -0
  80. package/dist/source/combobox/popup/ComboboxPopup.d.ts +34 -0
  81. package/dist/source/combobox/popup/ComboboxPopup.jsx +76 -0
  82. package/dist/source/combobox/root/ComboboxContext.d.ts +179 -0
  83. package/dist/source/combobox/root/ComboboxContext.js +186 -0
  84. package/dist/source/combobox/root/ComboboxRoot.d.ts +13 -0
  85. package/dist/source/combobox/root/ComboboxRoot.jsx +13 -0
  86. package/dist/source/combobox/status/ComboboxStatus.d.ts +19 -0
  87. package/dist/source/combobox/status/ComboboxStatus.jsx +27 -0
  88. package/dist/source/combobox/trigger/ComboboxTrigger.d.ts +13 -0
  89. package/dist/source/combobox/trigger/ComboboxTrigger.jsx +40 -0
  90. package/dist/source/floating/dismissal/reason.d.ts +7 -2
  91. package/dist/source/select/group/SelectGroup.d.ts +10 -0
  92. package/dist/source/select/group/SelectGroup.jsx +23 -0
  93. package/dist/source/select/group/SelectGroupContext.d.ts +10 -0
  94. package/dist/source/select/group/SelectGroupContext.js +12 -0
  95. package/dist/source/select/group-label/SelectGroupLabel.d.ts +10 -0
  96. package/dist/source/select/group-label/SelectGroupLabel.jsx +24 -0
  97. package/dist/source/select/index.d.ts +1 -0
  98. package/dist/source/select/index.js +1 -0
  99. package/dist/source/select/index.parts.d.ts +8 -0
  100. package/dist/source/select/index.parts.js +8 -0
  101. package/dist/source/select/item/SelectItem.d.ts +24 -0
  102. package/dist/source/select/item/SelectItem.jsx +92 -0
  103. package/dist/source/select/item/SelectItemContext.d.ts +11 -0
  104. package/dist/source/select/item/SelectItemContext.js +12 -0
  105. package/dist/source/select/item-indicator/SelectItemIndicator.d.ts +11 -0
  106. package/dist/source/select/item-indicator/SelectItemIndicator.jsx +16 -0
  107. package/dist/source/select/popup/SelectPopup.d.ts +32 -0
  108. package/dist/source/select/popup/SelectPopup.jsx +97 -0
  109. package/dist/source/select/root/SelectContext.d.ts +106 -0
  110. package/dist/source/select/root/SelectContext.js +119 -0
  111. package/dist/source/select/root/SelectRoot.d.ts +13 -0
  112. package/dist/source/select/root/SelectRoot.jsx +11 -0
  113. package/dist/source/select/trigger/SelectTrigger.d.ts +15 -0
  114. package/dist/source/select/trigger/SelectTrigger.jsx +181 -0
  115. package/dist/source/select/value/SelectValue.d.ts +20 -0
  116. package/dist/source/select/value/SelectValue.jsx +27 -0
  117. package/package.json +16 -1
@@ -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,179 @@
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
+ /**
85
+ * What a combobox's shared parts read through context: the Trigger, Clear,
86
+ * the Popup, the List, the Status, Group and the Input's shared code.
87
+ * `Combobox.Root` shares a `Combobox`, and `Autocomplete.Root`, which builds
88
+ * on Combobox, shares its own (`src/autocomplete/README.md`).
89
+ */
90
+ export interface ComboboxBase {
91
+ /**
92
+ * Which Root made it, so that a part only one of them takes can say it
93
+ * is inside the other.
94
+ */
95
+ readonly component: "Combobox" | "Autocomplete";
96
+ /** Whether the list is open. */
97
+ open(): boolean;
98
+ /** Asks to open or close the list, saying why. */
99
+ setOpen(open: boolean, details: OpenChangeDetails): void;
100
+ /** The text the input shows. */
101
+ inputValue(): string;
102
+ /**
103
+ * Asks for the text the user typed: the one reason the shared parts
104
+ * report. Each Root's own parts set it for their own reasons too.
105
+ */
106
+ setInputValue(text: string, details: {
107
+ readonly reason: "input-change";
108
+ readonly event: Event;
109
+ }): void;
110
+ /**
111
+ * Selects `item`, or accepts it, as Enter or a click does. A disabled
112
+ * item changes nothing. Returns whether it could be selected.
113
+ */
114
+ select(item: ComboboxItemEntry | undefined, event: Event): boolean;
115
+ /** Empties the text, and the value. Returns whether there was any. */
116
+ clear(event: Event, reason: "escape-key" | "clear-press"): boolean;
117
+ /** Whether `item` is the active one of an open list, for its `data-active`. */
118
+ isActive(item: ComboboxItemEntry): boolean;
119
+ /** The number of items in the list, for `Combobox.Status`. */
120
+ count(): number;
121
+ readonly list: ActiveDescendantList<ComboboxItemEntry>;
122
+ /** The List's `id`, which the Input names before the List is rendered. */
123
+ readonly listId: string;
124
+ /** The input, once rendered: focused by the Trigger and Clear. */
125
+ input: HTMLInputElement | undefined;
126
+ /** The `data-*` attributes of the Input, the Trigger and the Popup. */
127
+ readonly attributes: ComboboxStateAttributes;
128
+ /** The Root's props that the shared parts read. */
129
+ readonly props: {
130
+ readonly disabled?: boolean;
131
+ readonly required?: boolean;
132
+ };
133
+ }
134
+ /** What `Combobox.Root` shares with its parts through context. */
135
+ export interface Combobox extends ComboboxBase {
136
+ readonly component: "Combobox";
137
+ /** The selected item's `value`, or `null`. */
138
+ value(): string | null;
139
+ /** Asks for a value, as the hidden input's `change` and a reset do. */
140
+ setValue(value: string | null): void;
141
+ /** Asks for a text, saying why. */
142
+ setInputValue(text: string, details: InputValueChangeDetails): void;
143
+ /**
144
+ * Selects `item`: its value through the hidden input, which fires
145
+ * `input` and `change` for the form, and its label as the text.
146
+ */
147
+ select(item: ComboboxItemEntry | undefined, event: Event): boolean;
148
+ /**
149
+ * Puts the text back to the selected item's label as focus leaves, or
150
+ * clears the value if the user emptied the text: the Input's `onFocusOut`.
151
+ */
152
+ revert(event: Event): void;
153
+ /** The label of the item with `value`, if one has mounted. */
154
+ labelOfValue(value: string | null): string | undefined;
155
+ /** The hidden input, once rendered: the form's view of the value. */
156
+ hiddenInput: HTMLInputElement | undefined;
157
+ /** The Root's props, for the parts' elements. */
158
+ readonly props: ComboboxStateProps;
159
+ }
160
+ /** The Root's props that every combobox's shared state reads. */
161
+ export type ComboboxBaseProps = Pick<ComboboxStateProps, "open" | "defaultOpen" | "onOpenChange" | "disabled" | "required">;
162
+ /** What `createComboboxBase` makes: the state no Root does otherwise. */
163
+ export type ComboboxBaseState = Pick<ComboboxBase, "open" | "setOpen" | "isActive" | "count" | "list" | "listId" | "input" | "attributes">;
164
+ /**
165
+ * The state `Combobox.Root` and `Autocomplete.Root` share: whether the list
166
+ * is open, its list navigation, the List's `id` and the `data-*`
167
+ * attributes. Each Root spreads it into its own combobox, beside its text
168
+ * and what accepting an item does.
169
+ */
170
+ export declare function createComboboxBase(props: ComboboxBaseProps): ComboboxBaseState;
171
+ /** Creates a combobox's state and its list navigation. */
172
+ export declare function createCombobox(props: ComboboxStateProps): Combobox;
173
+ export declare const ComboboxContext: import("solid-js").Context<ComboboxBase | null>;
174
+ /**
175
+ * The combobox of the Root around the part. A shared part passes no
176
+ * `component` and takes either Root's; a part only one Root takes passes
177
+ * that Root's `component`, and its type, `Combobox` or an Autocomplete's.
178
+ */
179
+ export declare function useComboboxContext<T extends ComboboxBase = ComboboxBase>(component?: T["component"]): T;
@@ -0,0 +1,186 @@
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
+ /**
6
+ * The state `Combobox.Root` and `Autocomplete.Root` share: whether the list
7
+ * is open, its list navigation, the List's `id` and the `data-*`
8
+ * attributes. Each Root spreads it into its own combobox, beside its text
9
+ * and what accepting an item does.
10
+ */
11
+ export function createComboboxBase(props) {
12
+ const [open, setOpen] = createControllableSignal({
13
+ get value() {
14
+ return props.open;
15
+ },
16
+ get defaultValue() {
17
+ return props.defaultOpen ?? false;
18
+ },
19
+ onChange: (value, details) => props.onOpenChange?.(value, details),
20
+ });
21
+ // Disabled items stay reachable; Home and End belong to the text, so the
22
+ // arrow keys wrap, as the list autocomplete example's do (README).
23
+ const list = createActiveDescendantList({
24
+ get open() {
25
+ return open();
26
+ },
27
+ wrap: true,
28
+ });
29
+ return {
30
+ open,
31
+ setOpen,
32
+ // Only while open, as the input names it.
33
+ isActive: (item) => open() && list.isActive(item),
34
+ count: () => list.items().length,
35
+ list,
36
+ listId: createUniqueId(),
37
+ input: undefined,
38
+ attributes: {
39
+ get "data-open"() {
40
+ return open() ? "" : undefined;
41
+ },
42
+ get "data-closed"() {
43
+ return open() ? undefined : "";
44
+ },
45
+ get "data-disabled"() {
46
+ return props.disabled ? "" : undefined;
47
+ },
48
+ get "data-required"() {
49
+ return props.required ? "" : undefined;
50
+ },
51
+ },
52
+ };
53
+ }
54
+ /** Creates a combobox's state and its list navigation. */
55
+ export function createCombobox(props) {
56
+ const base = createComboboxBase(props);
57
+ const { list } = base;
58
+ const [value, setValue, requestedValue] = createControllableSignal({
59
+ get value() {
60
+ return props.value;
61
+ },
62
+ get defaultValue() {
63
+ return props.defaultValue ?? null;
64
+ },
65
+ onChange: (value) => props.onValueChange?.(value),
66
+ });
67
+ // `null` until the user, a selection or a reset sets a text: the input
68
+ // then shows the selected item's label, once it has mounted (README,
69
+ // "Two values"). The consumer only ever sees a string.
70
+ const [text, setText, requestedText] = createControllableSignal({
71
+ get value() {
72
+ return props.inputValue;
73
+ },
74
+ get defaultValue() {
75
+ return props.defaultInputValue ?? null;
76
+ },
77
+ onChange: (text, details) => props.onInputValueChange?.(text, details),
78
+ });
79
+ const labelOfValue = (selected) => {
80
+ const item = list.items().find((item) => item.value === selected);
81
+ // An item's text is always given: its `label`, else its content.
82
+ return item?.text.trim();
83
+ };
84
+ // The selected item's label, kept while the consumer has filtered that
85
+ // item out of the list, so focus leaving can put it back. Runs again when
86
+ // an item mounts or leaves, and when the value changes; a string, so an
87
+ // equal label stops there.
88
+ let labelled = null;
89
+ const selectedLabel = createMemo((previous) => {
90
+ const selected = value();
91
+ const label = labelOfValue(selected) ?? (selected === labelled ? previous : undefined);
92
+ labelled = selected;
93
+ return label;
94
+ });
95
+ // A memo, so that the input's draft, derived from it, is not derived
96
+ // again, over an edit, when nothing it shows changed.
97
+ const inputValue = createMemo(() => text() ?? selectedLabel() ?? "");
98
+ // What the input shows after the sets of this turn.
99
+ const requestedInputValue = () => requestedText() ?? untrack(selectedLabel) ?? "";
100
+ let edited = false;
101
+ const setInputValue = (next, details) => {
102
+ // Whether the user has edited the text since it was last set for
103
+ // another reason: only then does focus leaving change anything.
104
+ edited = details.reason === "input-change";
105
+ if (next !== requestedInputValue())
106
+ setText(next, details);
107
+ };
108
+ // Through the hidden input, which fires the events a native control
109
+ // fires, so the form sees them; the Combobox takes the value from its
110
+ // `change`.
111
+ const commit = (next) => {
112
+ if (next === requestedValue())
113
+ return;
114
+ const input = combobox.hiddenInput;
115
+ if (!input)
116
+ return setValue(next);
117
+ input.value = next ?? "";
118
+ input.dispatchEvent(new Event("input", { bubbles: true }));
119
+ input.dispatchEvent(new Event("change", { bubbles: true }));
120
+ };
121
+ const combobox = {
122
+ ...base,
123
+ component: "Combobox",
124
+ value,
125
+ setValue,
126
+ inputValue,
127
+ setInputValue,
128
+ select: (item, event) => {
129
+ if (!item || item.disabled)
130
+ return false;
131
+ commit(item.value);
132
+ setInputValue(item.text.trim(), { reason: "item-press", event });
133
+ return true;
134
+ },
135
+ clear: (event, reason) => {
136
+ if (!requestedInputValue() && requestedValue() === null)
137
+ return false;
138
+ commit(null);
139
+ setInputValue("", { reason, event });
140
+ return true;
141
+ },
142
+ revert: (event) => {
143
+ // An untouched Combobox keeps its value, though its text may be
144
+ // empty while its items have not mounted.
145
+ if (!edited)
146
+ return;
147
+ if (!requestedInputValue().trim()) {
148
+ commit(null);
149
+ setInputValue("", { reason: "focus-out", event });
150
+ return;
151
+ }
152
+ const selected = requestedValue();
153
+ const label = selected === null
154
+ ? ""
155
+ : (labelOfValue(selected) ?? selectedLabel() ?? "");
156
+ setInputValue(label, { reason: "focus-out", event });
157
+ },
158
+ labelOfValue,
159
+ hiddenInput: undefined,
160
+ props,
161
+ };
162
+ return combobox;
163
+ }
164
+ // `null` outside a Root, so that the part can name what is missing.
165
+ export const ComboboxContext = createContext(null);
166
+ /**
167
+ * The combobox of the Root around the part. A shared part passes no
168
+ * `component` and takes either Root's; a part only one Root takes passes
169
+ * that Root's `component`, and its type, `Combobox` or an Autocomplete's.
170
+ */
171
+ export function useComboboxContext(component) {
172
+ const context = useContext(ComboboxContext);
173
+ // Production builds drop the checks, and their messages, with `isDev`.
174
+ if (isDev) {
175
+ if (!context) {
176
+ // A shared part cannot tell which Root was meant, so it names both.
177
+ throw new Error(component
178
+ ? `[menhir] ${component} parts must be placed within <${component}.Root>.`
179
+ : "[menhir] Combobox parts must be placed within <Combobox.Root>, and Autocomplete parts within <Autocomplete.Root>.");
180
+ }
181
+ if (component && context.component !== component) {
182
+ throw new Error(`[menhir] ${component} parts must be placed within <${component}.Root>, not <${context.component}.Root>.`);
183
+ }
184
+ }
185
+ return context;
186
+ }
@@ -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
+ }
@@ -0,0 +1,13 @@
1
+ import type { JSX } from "@solidjs/web";
2
+ import type { RenderProp } from "../../core/renderElement.tsx";
3
+ export interface ComboboxTriggerProps extends JSX.HTMLAttributes<HTMLElement> {
4
+ render?: RenderProp;
5
+ }
6
+ /**
7
+ * The button that opens and closes the list, for the pointer and touch: a
8
+ * `<button type="button" tabindex="-1">` with `aria-expanded` and
9
+ * `aria-controls` naming the List. Out of the tab order, since the input
10
+ * takes the keys, and a press leaves focus in the input. Name it with
11
+ * `aria-label`. Place it inside `Combobox.Root`.
12
+ */
13
+ export declare function ComboboxTrigger(props: ComboboxTriggerProps): JSX.Element;
@@ -0,0 +1,40 @@
1
+ import { isDev } from "@solidjs/web";
2
+ import { 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 { useComboboxContext } from "../root/ComboboxContext.js";
7
+ /**
8
+ * The button that opens and closes the list, for the pointer and touch: a
9
+ * `<button type="button" tabindex="-1">` with `aria-expanded` and
10
+ * `aria-controls` naming the List. Out of the tab order, since the input
11
+ * takes the keys, and a press leaves focus in the input. Name it with
12
+ * `aria-label`. Place it inside `Combobox.Root`.
13
+ */
14
+ export function ComboboxTrigger(props) {
15
+ const combobox = useComboboxContext();
16
+ const ownProps = merge(combobox.attributes, {
17
+ type: "button",
18
+ tabindex: -1,
19
+ get disabled() {
20
+ return combobox.props.disabled;
21
+ },
22
+ get "aria-expanded"() {
23
+ return combobox.open() ? "true" : "false";
24
+ },
25
+ "aria-controls": combobox.listId,
26
+ // Focus stays in the input, which holds it for the keys.
27
+ onMouseDown: (event) => event.preventDefault(),
28
+ onClick: (event) => {
29
+ combobox.setOpen(!combobox.open(), { reason: "trigger-press", event });
30
+ combobox.input?.focus();
31
+ },
32
+ ref: (element) => {
33
+ // Production builds drop the check with `isDev`.
34
+ if (isDev)
35
+ warnUnlessButton(element, "Combobox.Trigger");
36
+ },
37
+ });
38
+ const elementProps = mergeElementProps(ownProps, omit(props, "render"));
39
+ return renderElement(props, elementProps, (p) => <button {...p}/>);
40
+ }
@@ -24,9 +24,14 @@
24
24
  * - `item-press`: an item of a menu was activated, by pointer or keyboard,
25
25
  * which closes every open menu (Menu).
26
26
  * - `arrow-key`: an arrow key opened or closed a submenu, or a key that
27
- * moves through a menu closed its open submenu (Menu).
27
+ * moves through a menu closed its open submenu (Menu); an arrow key
28
+ * opened or closed a combobox's list (Combobox).
29
+ * - `input-change`: the user typed in a combobox's input, which opened its
30
+ * list (Combobox).
31
+ * - `enter-key`: Enter closed a combobox's list with no item active
32
+ * (Combobox).
28
33
  */
29
- export type OpenChangeReason = "escape-key" | "outside-press" | "trigger-press" | "close-request" | "trigger-hover" | "close-press" | "trigger-focus" | "focus-out" | "link-press" | "item-press" | "arrow-key";
34
+ export type OpenChangeReason = "escape-key" | "outside-press" | "trigger-press" | "close-request" | "trigger-hover" | "close-press" | "trigger-focus" | "focus-out" | "link-press" | "item-press" | "arrow-key" | "input-change" | "enter-key";
30
35
  /**
31
36
  * The second argument of `onOpenChange`. `event` is the event that caused
32
37
  * the change: the key or pointer event when there was one, else the
@@ -0,0 +1,10 @@
1
+ import type { JSX } from "@solidjs/web";
2
+ import type { RenderProp } from "../../core/renderElement.tsx";
3
+ export interface SelectGroupProps extends JSX.HTMLAttributes<HTMLElement> {
4
+ render?: RenderProp;
5
+ }
6
+ /**
7
+ * Items that belong together: a `<div role="group">` named by its
8
+ * `Select.GroupLabel`. Place it inside `Select.Popup`.
9
+ */
10
+ export declare function SelectGroup(props: SelectGroupProps): JSX.Element;