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,99 @@
1
+ import { createControllableSignal } from "../../core/createControllableSignal.js";
2
+ import { createActiveDescendantList } from "../../floating/list-navigation/activeDescendant.js";
3
+ import { createTypeahead } from "../../floating/typeahead/typeahead.js";
4
+ import { isDev } from "@solidjs/web";
5
+ import { createContext, createMemo, createUniqueId, useContext } from "solid-js";
6
+ // src/select/root/SelectContext.ts
7
+ /** Creates a select's state, its list navigation and its typeahead. */
8
+ function createSelect(props) {
9
+ const [open, setOpen] = createControllableSignal({
10
+ get value() {
11
+ return props.open;
12
+ },
13
+ get defaultValue() {
14
+ return props.defaultOpen ?? false;
15
+ },
16
+ onChange: (value, details) => props.onOpenChange?.(value, details)
17
+ });
18
+ const [value, setValue, requested] = createControllableSignal({
19
+ get value() {
20
+ return props.value;
21
+ },
22
+ get defaultValue() {
23
+ return props.defaultValue ?? null;
24
+ },
25
+ onChange: (value) => props.onValueChange?.(value)
26
+ });
27
+ const list = createActiveDescendantList({ get open() {
28
+ return open();
29
+ } });
30
+ const typeahead = createTypeahead({
31
+ get open() {
32
+ return open();
33
+ },
34
+ get items() {
35
+ return list.items();
36
+ },
37
+ get active() {
38
+ return active();
39
+ },
40
+ move: (item) => list.move(item)
41
+ });
42
+ const selectedItem = createMemo(() => {
43
+ const selected = value();
44
+ return list.items().find((item) => item.value === selected);
45
+ });
46
+ const initial = createMemo(() => open() && !list.active() ? selectedItem() ?? list.items()[0] : void 0);
47
+ const active = () => list.active() ?? initial();
48
+ const select = {
49
+ open,
50
+ setOpen,
51
+ value,
52
+ setValue,
53
+ select: (item) => {
54
+ if (!item || item.disabled) return false;
55
+ const input = select.input;
56
+ if (item.value !== requested()) {
57
+ if (input) {
58
+ input.value = item.value;
59
+ input.dispatchEvent(new Event("input", { bubbles: true }));
60
+ input.dispatchEvent(new Event("change", { bubbles: true }));
61
+ } else setValue(item.value);
62
+ }
63
+ return true;
64
+ },
65
+ selectedItem,
66
+ active,
67
+ isActive: (item) => open() && (list.isActive(item) || initial() === item),
68
+ initial,
69
+ list,
70
+ typeahead,
71
+ popupId: createUniqueId(),
72
+ triggerId: () => void 0,
73
+ trigger: void 0,
74
+ input: void 0,
75
+ attributes: {
76
+ get "data-open"() {
77
+ return open() ? "" : void 0;
78
+ },
79
+ get "data-closed"() {
80
+ return open() ? void 0 : "";
81
+ },
82
+ get "data-disabled"() {
83
+ return props.disabled ? "" : void 0;
84
+ },
85
+ get "data-required"() {
86
+ return props.required ? "" : void 0;
87
+ }
88
+ },
89
+ props
90
+ };
91
+ return select;
92
+ }
93
+ var SelectContext = createContext(null);
94
+ function useSelectContext() {
95
+ const context = useContext(SelectContext);
96
+ if (isDev && !context) throw new Error("[menhir] Select parts must be placed within <Select.Root>.");
97
+ return context;
98
+ }
99
+ export { SelectContext, createSelect, useSelectContext };
@@ -0,0 +1,21 @@
1
+ import { SelectContext, createSelect } from "./SelectContext.js";
2
+ import { createComponent } from "@solidjs/web";
3
+ // src/select/root/SelectRoot.tsx
4
+ /**
5
+ * A value selected from a list that a button opens: holds the value and
6
+ * whether the list is open, and shares them with `Select.Trigger`,
7
+ * `Select.Value`, `Select.Popup` and the items inside it. Its form props
8
+ * go on the hidden input the Trigger renders. Renders no element of its
9
+ * own.
10
+ */
11
+ function SelectRoot(props) {
12
+ return createComponent(SelectContext, {
13
+ get value() {
14
+ return createSelect(props);
15
+ },
16
+ get children() {
17
+ return props.children;
18
+ }
19
+ });
20
+ }
21
+ export { SelectRoot };
@@ -0,0 +1,163 @@
1
+ import { warnUnlessButton } from "../../core/button.js";
2
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
3
+ import { renderElement } from "../../core/renderElement.js";
4
+ import { useSelectContext } from "../root/SelectContext.js";
5
+ import { effect, heldRead, isDev, memo, ref, setAttribute, spread, template } from "@solidjs/web";
6
+ import { createUniqueId, merge, omit, onFlush, onSettled, untrack } from "solid-js";
7
+ // src/select/trigger/SelectTrigger.tsx
8
+ var _tmpl$ = /* @__PURE__ */ template(`<input aria-hidden=true tabindex=-1 style=position:absolute;width:1px;height:1px;margin:-1px;padding:0;border:0;overflow:hidden;clip-path:inset(50%);white-space:nowrap;pointer-events:none>`);
9
+ var _tmpl$2 = /* @__PURE__ */ template(`<button>`);
10
+ /**
11
+ * The button that shows the value and opens the list: a `<button
12
+ * type="button" role="combobox" aria-haspopup="listbox">` with
13
+ * `aria-expanded`, naming the Popup in `popovertarget` and `aria-controls`
14
+ * and the active item in `aria-activedescendant`. It keeps focus while the
15
+ * list is open and takes every key. A visually hidden `<input>` follows
16
+ * it, the form's view of the value. Name it with a `<label>`. Place it
17
+ * inside `Select.Root`, right before `Select.Popup`.
18
+ */
19
+ function SelectTrigger(props) {
20
+ const select = useSelectContext();
21
+ const { list } = select;
22
+ const generated = createUniqueId();
23
+ const id = () => props.id ?? generated;
24
+ select.triggerId = id;
25
+ let input;
26
+ const press = (event) => ({
27
+ reason: "trigger-press",
28
+ event
29
+ });
30
+ const openList = (event, edge) => {
31
+ select.setOpen(true, press(event));
32
+ const items = list.items();
33
+ if (edge) list.move(edge === "last" ? items[items.length - 1] : items[0]);
34
+ };
35
+ const selectActive = (event, reason) => {
36
+ if (select.select(select.active()) || reason === "focus-out") select.setOpen(false, {
37
+ reason,
38
+ event
39
+ });
40
+ };
41
+ const ownProps = merge(select.attributes, {
42
+ type: "button",
43
+ role: "combobox",
44
+ get id() {
45
+ return id();
46
+ },
47
+ get disabled() {
48
+ return select.props.disabled;
49
+ },
50
+ popovertarget: select.popupId,
51
+ "aria-haspopup": "listbox",
52
+ get "aria-expanded"() {
53
+ return select.open() ? "true" : "false";
54
+ },
55
+ "aria-controls": select.popupId,
56
+ get "aria-activedescendant"() {
57
+ return select.open() ? select.active()?.id : void 0;
58
+ },
59
+ get "aria-required"() {
60
+ return select.props.required ? "true" : void 0;
61
+ },
62
+ onClick: (event) => {
63
+ event.preventDefault();
64
+ select.setOpen(!select.open(), press(event));
65
+ },
66
+ onKeyDown: (event) => {
67
+ const isOpen = select.open();
68
+ if (select.typeahead.onKeyDown(event)) {
69
+ if (!isOpen) select.setOpen(true, press(event));
70
+ return;
71
+ }
72
+ const { key, altKey } = event;
73
+ if (event.ctrlKey || event.metaKey) return;
74
+ if (key === "Tab") {
75
+ if (isOpen) selectActive(event, "focus-out");
76
+ return;
77
+ }
78
+ if (event.shiftKey) return;
79
+ const enterOrSpace = key === "Enter" || key === " ";
80
+ if (!isOpen) {
81
+ if (enterOrSpace || key === "ArrowDown") openList(event);
82
+ else if (altKey) return;
83
+ else if (key === "ArrowUp" || key === "Home") openList(event, "first");
84
+ else if (key === "End") openList(event, "last");
85
+ else return;
86
+ } else if (enterOrSpace || altKey && key === "ArrowUp") selectActive(event, "item-press");
87
+ else {
88
+ const active = select.active();
89
+ if (!list.active()) list.move(active);
90
+ if (key === "PageDown" || key === "PageUp") {
91
+ const items = list.items();
92
+ const index = items.indexOf(active) + (key === "PageUp" ? -10 : 10);
93
+ list.move(items[Math.min(Math.max(index, 0), items.length - 1)]);
94
+ } else if (!list.onKeyDown(event)) return;
95
+ }
96
+ event.preventDefault();
97
+ },
98
+ ref: (element) => {
99
+ select.trigger = element;
100
+ if (isDev) warnUnlessButton(element, "Select.Trigger");
101
+ }
102
+ });
103
+ const syncInput = () => {
104
+ if (input) input.value = untrack(select.value) ?? "";
105
+ };
106
+ const onInputChange = () => {
107
+ const filled = input.value;
108
+ const items = list.items();
109
+ const label = filled.toLowerCase();
110
+ const item = items.find((item) => item.value === filled) ?? items.find((item) => (item.text ?? item.element.textContent ?? "").trim().toLowerCase() === label);
111
+ if (item) select.setValue(item.value);
112
+ else if (!filled) select.setValue(null);
113
+ onFlush(syncInput);
114
+ };
115
+ onSettled(() => {
116
+ const form = input?.form;
117
+ if (!form) return;
118
+ const onReset = () => {
119
+ select.setValue(select.props.defaultValue ?? null);
120
+ onFlush(syncInput);
121
+ };
122
+ form.addEventListener("reset", onReset);
123
+ return () => form.removeEventListener("reset", onReset);
124
+ });
125
+ const elementProps = mergeElementProps(ownProps, omit(props, "render", "id"));
126
+ return [memo(() => {
127
+ return renderElement(props, elementProps, (p) => (() => {
128
+ var _el$2 = _tmpl$2();
129
+ spread(_el$2, p, false);
130
+ return _el$2;
131
+ })());
132
+ }), (() => {
133
+ var _el$ = _tmpl$();
134
+ _el$.addEventListener("focus", () => select.trigger?.focus());
135
+ _el$.addEventListener("change", onInputChange);
136
+ ref(() => {
137
+ return (element) => {
138
+ input = element;
139
+ select.input = element;
140
+ };
141
+ }, _el$);
142
+ effect(() => {
143
+ return {
144
+ e: select.props.name,
145
+ t: select.props.form,
146
+ a: select.props.required,
147
+ o: select.props.disabled,
148
+ i: select.props.autocomplete,
149
+ n: select.value() ?? "",
150
+ s: heldRead()
151
+ };
152
+ }, ({ e, t, a, o, i, n, s }, _p$) => {
153
+ e !== _p$?.e && setAttribute(_el$, "name", e);
154
+ t !== _p$?.t && setAttribute(_el$, "form", t);
155
+ a !== _p$?.a && setAttribute(_el$, "required", a);
156
+ o !== _p$?.o && setAttribute(_el$, "disabled", o);
157
+ i !== _p$?.i && setAttribute(_el$, "autocomplete", i);
158
+ (n !== _p$?.n || !s) && (_el$.value = n ?? "");
159
+ });
160
+ return _el$;
161
+ })()];
162
+ }
163
+ export { SelectTrigger };
@@ -0,0 +1,32 @@
1
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
2
+ import { renderElement } from "../../core/renderElement.js";
3
+ import { useSelectContext } from "../root/SelectContext.js";
4
+ import { spread, template } from "@solidjs/web";
5
+ import { omit } from "solid-js";
6
+ // src/select/value/SelectValue.tsx
7
+ var _tmpl$ = /* @__PURE__ */ template(`<span>`);
8
+ /**
9
+ * The selected item's label: a `<span>`, with the `placeholder` and
10
+ * `data-placeholder` while no item is selected. Place it inside
11
+ * `Select.Trigger`.
12
+ */
13
+ function SelectValue(props) {
14
+ const select = useSelectContext();
15
+ const elementProps = mergeElementProps({
16
+ get "data-placeholder"() {
17
+ return select.value() === null ? "" : void 0;
18
+ },
19
+ get children() {
20
+ const value = select.value();
21
+ if (value === null) return props.placeholder;
22
+ if (props.children) return props.children(value);
23
+ return select.selectedItem()?.text.trim() ?? value;
24
+ }
25
+ }, omit(props, "render", "placeholder", "children"));
26
+ return renderElement(props, elementProps, (p) => (() => {
27
+ var _el$ = _tmpl$();
28
+ spread(_el$, p, false);
29
+ return _el$;
30
+ })());
31
+ }
32
+ export { SelectValue };
@@ -0,0 +1,2 @@
1
+ export * as Autocomplete from "./index.parts.ts";
2
+ export { includesText } from "../combobox/filter/includesText.ts";
@@ -0,0 +1,4 @@
1
+ export * as Autocomplete from "./index.parts.js";
2
+ // Combobox's, its own import, so that an app that filters otherwise does
3
+ // not bundle it (../combobox/README.md, "Filtering is the consumer's").
4
+ export { includesText } from "../combobox/filter/includesText.js";
@@ -0,0 +1,10 @@
1
+ export { AutocompleteRoot as Root } from "./root/AutocompleteRoot.tsx";
2
+ export { AutocompleteInput as Input } from "./input/AutocompleteInput.tsx";
3
+ export { ComboboxTrigger as Trigger } from "../combobox/trigger/ComboboxTrigger.tsx";
4
+ export { ComboboxClear as Clear } from "../combobox/clear/ComboboxClear.tsx";
5
+ export { ComboboxPopup as Popup } from "../combobox/popup/ComboboxPopup.tsx";
6
+ export { ComboboxList as List } from "../combobox/list/ComboboxList.tsx";
7
+ export { AutocompleteItem as Item } from "./item/AutocompleteItem.tsx";
8
+ export { ComboboxGroup as Group } from "../combobox/group/ComboboxGroup.tsx";
9
+ export { ComboboxGroupLabel as GroupLabel } from "../combobox/group-label/ComboboxGroupLabel.tsx";
10
+ export { ComboboxStatus as Status } from "../combobox/status/ComboboxStatus.tsx";
@@ -0,0 +1,12 @@
1
+ // Its own Root, Input and Item, and Combobox's other parts (README,
2
+ // "Decisions"; #104).
3
+ export { AutocompleteRoot as Root } from "./root/AutocompleteRoot.jsx";
4
+ export { AutocompleteInput as Input } from "./input/AutocompleteInput.jsx";
5
+ export { ComboboxTrigger as Trigger } from "../combobox/trigger/ComboboxTrigger.jsx";
6
+ export { ComboboxClear as Clear } from "../combobox/clear/ComboboxClear.jsx";
7
+ export { ComboboxPopup as Popup } from "../combobox/popup/ComboboxPopup.jsx";
8
+ export { ComboboxList as List } from "../combobox/list/ComboboxList.jsx";
9
+ export { AutocompleteItem as Item } from "./item/AutocompleteItem.jsx";
10
+ export { ComboboxGroup as Group } from "../combobox/group/ComboboxGroup.jsx";
11
+ export { ComboboxGroupLabel as GroupLabel } from "../combobox/group-label/ComboboxGroupLabel.jsx";
12
+ export { ComboboxStatus as Status } from "../combobox/status/ComboboxStatus.jsx";
@@ -0,0 +1,18 @@
1
+ import type { JSX } from "@solidjs/web";
2
+ import type { RenderProp } from "../../core/renderElement.tsx";
3
+ export interface AutocompleteInputProps extends JSX.HTMLAttributes<HTMLElement> {
4
+ /** What the browser may suggest. Default `"off"`, so its list does not cover the Autocomplete's. */
5
+ autocomplete?: string;
6
+ /** Shown while the input is empty. */
7
+ placeholder?: string;
8
+ render?: RenderProp;
9
+ }
10
+ /**
11
+ * The autocomplete itself, and the form's control: an `<input type="text"
12
+ * role="combobox" aria-autocomplete="list">` that takes `name`, `form`,
13
+ * `required` and `disabled` from the Root, so the form receives its text.
14
+ * It keeps focus while the list is open: typing opens it, and the arrow
15
+ * keys, Enter and Escape act on it. Name it with a `<label>`. Place it
16
+ * inside `Autocomplete.Root`.
17
+ */
18
+ export declare function AutocompleteInput(props: AutocompleteInputProps): JSX.Element;
@@ -0,0 +1,37 @@
1
+ import { merge, omit, onFlush } from "solid-js";
2
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
3
+ import { renderElement } from "../../core/renderElement.jsx";
4
+ import { createComboboxInput, onFormReset, } from "../../combobox/input/createComboboxInput.js";
5
+ import { useComboboxContext } from "../../combobox/root/ComboboxContext.js";
6
+ /**
7
+ * The autocomplete itself, and the form's control: an `<input type="text"
8
+ * role="combobox" aria-autocomplete="list">` that takes `name`, `form`,
9
+ * `required` and `disabled` from the Root, so the form receives its text.
10
+ * It keeps focus while the list is open: typing opens it, and the arrow
11
+ * keys, Enter and Escape act on it. Name it with a `<label>`. Place it
12
+ * inside `Autocomplete.Root`.
13
+ */
14
+ export function AutocompleteInput(props) {
15
+ const autocomplete = useComboboxContext("Autocomplete");
16
+ const input = createComboboxInput(autocomplete);
17
+ onFormReset(() => autocomplete.input, (event) => {
18
+ autocomplete.setInputValue(autocomplete.props.defaultValue ?? "", {
19
+ reason: "reset",
20
+ event,
21
+ });
22
+ onFlush(input.syncText);
23
+ });
24
+ const ownProps = merge(input.props, {
25
+ get name() {
26
+ return autocomplete.props.name;
27
+ },
28
+ get form() {
29
+ return autocomplete.props.form;
30
+ },
31
+ get required() {
32
+ return autocomplete.props.required;
33
+ },
34
+ });
35
+ const elementProps = mergeElementProps(ownProps, omit(props, "render"));
36
+ return renderElement(props, elementProps, (p) => <input {...p}/>);
37
+ }
@@ -0,0 +1,20 @@
1
+ import type { JSX } from "@solidjs/web";
2
+ import type { RenderProp } from "../../core/renderElement.tsx";
3
+ export interface AutocompleteItemProps extends JSX.HTMLAttributes<HTMLElement> {
4
+ /** The text it fills the input with when accepted. */
5
+ value: string;
6
+ /**
7
+ * Whether the item is disabled: still reached by the keys and the
8
+ * pointer, but not accepted.
9
+ */
10
+ disabled?: boolean;
11
+ render?: RenderProp;
12
+ }
13
+ /**
14
+ * A suggestion: a `<div role="option">`, with `aria-selected="true"` while
15
+ * it is the active item. A click accepts it, sets the input's text to its
16
+ * `value` and closes the list, and so does Enter while it is active; a
17
+ * disabled item is not accepted. Render the suggestions that match the
18
+ * text, and place them inside `Autocomplete.List`.
19
+ */
20
+ export declare function AutocompleteItem(props: AutocompleteItemProps): JSX.Element;
@@ -0,0 +1,25 @@
1
+ import { merge, omit } from "solid-js";
2
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
3
+ import { renderElement } from "../../core/renderElement.jsx";
4
+ import { useComboboxContext } from "../../combobox/root/ComboboxContext.js";
5
+ import { createComboboxItem } from "../../combobox/item/createComboboxItem.js";
6
+ /**
7
+ * A suggestion: a `<div role="option">`, with `aria-selected="true"` while
8
+ * it is the active item. A click accepts it, sets the input's text to its
9
+ * `value` and closes the list, and so does Enter while it is active; a
10
+ * disabled item is not accepted. Render the suggestions that match the
11
+ * text, and place them inside `Autocomplete.List`.
12
+ */
13
+ export function AutocompleteItem(props) {
14
+ const autocomplete = useComboboxContext("Autocomplete");
15
+ const { item, props: itemProps } = createComboboxItem(autocomplete, props, () => props.value);
16
+ const ownProps = merge(itemProps, {
17
+ // Only on the active item, as the list autocomplete example: there is
18
+ // no selected item, only the text (README, "Decisions").
19
+ get "aria-selected"() {
20
+ return autocomplete.isActive(item) ? "true" : undefined;
21
+ },
22
+ });
23
+ const elementProps = mergeElementProps(ownProps, omit(props, "render", "value", "disabled", "id"));
24
+ return renderElement(props, elementProps, (p) => <div {...p}/>);
25
+ }
@@ -0,0 +1,59 @@
1
+ import type { ComboboxBase, InputValueChangeReason } from "../../combobox/root/ComboboxContext.ts";
2
+ import type { OpenChangeDetails } from "../../floating/dismissal/reason.ts";
3
+ /**
4
+ * Why the text changed: Combobox's reasons for its input value, but
5
+ * `focus-out`, since focus leaving keeps the text.
6
+ *
7
+ * - `input-change`: the user typed or edited it.
8
+ * - `item-press`: an item was accepted, and the text is its `value`.
9
+ * - `escape-key`: Escape on a closed list emptied it.
10
+ * - `clear-press`: `Autocomplete.Clear` emptied it.
11
+ * - `reset`: its form was reset.
12
+ */
13
+ export type ValueChangeReason = Exclude<InputValueChangeReason, "focus-out">;
14
+ /** The second argument of `onValueChange`. */
15
+ export interface ValueChangeDetails {
16
+ readonly reason: ValueChangeReason;
17
+ readonly event: Event;
18
+ }
19
+ /** The props of `Autocomplete.Root`, less its children. */
20
+ export interface AutocompleteStateProps {
21
+ /** The text in the input, when controlled. */
22
+ value?: string;
23
+ /** The text at first, when uncontrolled. Default `""`. */
24
+ defaultValue?: string;
25
+ /**
26
+ * Called with the new text, and why, when the user types, accepts an
27
+ * item or empties it, or its form is reset. Filter by it for
28
+ * `input-change` only. When controlled, the text changes only if `value`
29
+ * does.
30
+ */
31
+ onValueChange?: (text: string, details: ValueChangeDetails) => void;
32
+ /** Whether the list is open, when controlled. */
33
+ open?: boolean;
34
+ /** Whether the list is open at first, when uncontrolled. */
35
+ defaultOpen?: boolean;
36
+ /**
37
+ * Called with the new value, and why, when the user opens or closes the
38
+ * list. When controlled, the list changes only if `open` does.
39
+ */
40
+ onOpenChange?: (open: boolean, details: OpenChangeDetails) => void;
41
+ /** Submitted with the form, as the input's `name`. */
42
+ name?: string;
43
+ /** The `id` of the form it belongs to, when it is not inside it. */
44
+ form?: string;
45
+ /** Whether the form can be submitted only with some text. */
46
+ required?: boolean;
47
+ /** Whether it ignores the user. It cannot be focused either. */
48
+ disabled?: boolean;
49
+ }
50
+ /** What `Autocomplete.Root` shares with its parts, through Combobox's context. */
51
+ export interface Autocomplete extends ComboboxBase {
52
+ readonly component: "Autocomplete";
53
+ /** Asks for a text, saying why. */
54
+ setInputValue(text: string, details: ValueChangeDetails): void;
55
+ /** The Root's props, for the Input. */
56
+ readonly props: AutocompleteStateProps;
57
+ }
58
+ /** Creates an autocomplete's state, on Combobox's shared state. */
59
+ export declare function createAutocomplete(props: AutocompleteStateProps): Autocomplete;
@@ -0,0 +1,33 @@
1
+ import { createControllableSignal } from "../../core/createControllableSignal.js";
2
+ import { createComboboxBase } from "../../combobox/root/ComboboxContext.js";
3
+ /** Creates an autocomplete's state, on Combobox's shared state. */
4
+ export function createAutocomplete(props) {
5
+ const [text, setText, requestedText] = createControllableSignal({
6
+ get value() {
7
+ return props.value;
8
+ },
9
+ get defaultValue() {
10
+ return props.defaultValue ?? "";
11
+ },
12
+ onChange: (text, details) => props.onValueChange?.(text, details),
13
+ });
14
+ return {
15
+ ...createComboboxBase(props),
16
+ component: "Autocomplete",
17
+ inputValue: text,
18
+ setInputValue: setText,
19
+ select: (item, event) => {
20
+ if (!item || item.disabled)
21
+ return false;
22
+ setText(item.value, { reason: "item-press", event });
23
+ return true;
24
+ },
25
+ clear: (event, reason) => {
26
+ if (!requestedText())
27
+ return false;
28
+ setText("", { reason, event });
29
+ return true;
30
+ },
31
+ props,
32
+ };
33
+ }
@@ -0,0 +1,13 @@
1
+ import type { JSX } from "@solidjs/web";
2
+ import type { AutocompleteStateProps } from "./AutocompleteContext.ts";
3
+ export interface AutocompleteRootProps extends AutocompleteStateProps {
4
+ children?: JSX.Element;
5
+ }
6
+ /**
7
+ * A text input with suggestions: holds the text, which is its value, and
8
+ * whether the list is open, and shares them with `Autocomplete.Input`,
9
+ * `Autocomplete.Trigger`, `Autocomplete.Clear`, `Autocomplete.Popup` and
10
+ * the parts inside it. Its form props go on the Input. Renders no element
11
+ * of its own.
12
+ */
13
+ export declare function AutocompleteRoot(props: AutocompleteRootProps): JSX.Element;
@@ -0,0 +1,15 @@
1
+ import { ComboboxContext } from "../../combobox/root/ComboboxContext.js";
2
+ import { createAutocomplete } from "./AutocompleteContext.js";
3
+ /**
4
+ * A text input with suggestions: holds the text, which is its value, and
5
+ * whether the list is open, and shares them with `Autocomplete.Input`,
6
+ * `Autocomplete.Trigger`, `Autocomplete.Clear`, `Autocomplete.Popup` and
7
+ * the parts inside it. Its form props go on the Input. Renders no element
8
+ * of its own.
9
+ */
10
+ export function AutocompleteRoot(props) {
11
+ // Through Combobox's context, so that Combobox's parts work inside it.
12
+ return (<ComboboxContext value={createAutocomplete(props)}>
13
+ {props.children}
14
+ </ComboboxContext>);
15
+ }
@@ -0,0 +1,12 @@
1
+ import type { JSX } from "@solidjs/web";
2
+ import type { RenderProp } from "../../core/renderElement.tsx";
3
+ export interface ComboboxClearProps extends JSX.HTMLAttributes<HTMLElement> {
4
+ render?: RenderProp;
5
+ }
6
+ /**
7
+ * The button that empties the text and the value, for the pointer, as
8
+ * Escape on a closed list does from the keyboard: a `<button type="button"
9
+ * tabindex="-1">` that then focuses the input. Name it with `aria-label`.
10
+ * Place it inside `Combobox.Root`.
11
+ */
12
+ export declare function ComboboxClear(props: ComboboxClearProps): JSX.Element;
@@ -0,0 +1,36 @@
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 empties the text and the value, for the pointer, as
9
+ * Escape on a closed list does from the keyboard: a `<button type="button"
10
+ * tabindex="-1">` that then focuses the input. Name it with `aria-label`.
11
+ * Place it inside `Combobox.Root`.
12
+ */
13
+ export function ComboboxClear(props) {
14
+ const combobox = useComboboxContext();
15
+ const ownProps = merge(combobox.attributes, {
16
+ type: "button",
17
+ tabindex: -1,
18
+ get disabled() {
19
+ return combobox.props.disabled;
20
+ },
21
+ // Focus stays in the input, so that leaving it does not put the text
22
+ // back before the click empties it.
23
+ onMouseDown: (event) => event.preventDefault(),
24
+ onClick: (event) => {
25
+ combobox.clear(event, "clear-press");
26
+ combobox.input?.focus();
27
+ },
28
+ ref: (element) => {
29
+ // Production builds drop the check with `isDev`.
30
+ if (isDev)
31
+ warnUnlessButton(element, "Combobox.Clear");
32
+ },
33
+ });
34
+ const elementProps = mergeElementProps(ownProps, omit(props, "render"));
35
+ return renderElement(props, elementProps, (p) => <button {...p}/>);
36
+ }
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Whether `text` contains `query`, case and accents ignored, with the
3
+ * built-in `Intl.Collator`; an empty query matches everything. The filter
4
+ * for a Combobox whose items match what the user types:
5
+ *
6
+ * ```ts
7
+ * const shown = createMemo(() =>
8
+ * countries.filter((country) => includesText(country.name, query())),
9
+ * );
10
+ * ```
11
+ */
12
+ export declare function includesText(text: string, query: string): boolean;