menhir 0.1.6 → 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 (33) 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/input/ComboboxInput.js +16 -98
  9. package/dist/combobox/input/createComboboxInput.js +112 -0
  10. package/dist/combobox/item/ComboboxItem.js +7 -51
  11. package/dist/combobox/item/createComboboxItem.js +62 -0
  12. package/dist/combobox/root/ComboboxContext.js +56 -32
  13. package/dist/source/autocomplete/index.d.ts +2 -0
  14. package/dist/source/autocomplete/index.js +4 -0
  15. package/dist/source/autocomplete/index.parts.d.ts +10 -0
  16. package/dist/source/autocomplete/index.parts.js +12 -0
  17. package/dist/source/autocomplete/input/AutocompleteInput.d.ts +18 -0
  18. package/dist/source/autocomplete/input/AutocompleteInput.jsx +37 -0
  19. package/dist/source/autocomplete/item/AutocompleteItem.d.ts +20 -0
  20. package/dist/source/autocomplete/item/AutocompleteItem.jsx +25 -0
  21. package/dist/source/autocomplete/root/AutocompleteContext.d.ts +59 -0
  22. package/dist/source/autocomplete/root/AutocompleteContext.js +33 -0
  23. package/dist/source/autocomplete/root/AutocompleteRoot.d.ts +13 -0
  24. package/dist/source/autocomplete/root/AutocompleteRoot.jsx +15 -0
  25. package/dist/source/combobox/input/ComboboxInput.jsx +13 -135
  26. package/dist/source/combobox/input/createComboboxInput.d.ts +30 -0
  27. package/dist/source/combobox/input/createComboboxInput.js +140 -0
  28. package/dist/source/combobox/item/ComboboxItem.jsx +5 -48
  29. package/dist/source/combobox/item/createComboboxItem.d.ts +21 -0
  30. package/dist/source/combobox/item/createComboboxItem.js +61 -0
  31. package/dist/source/combobox/root/ComboboxContext.d.ts +69 -22
  32. package/dist/source/combobox/root/ComboboxContext.js +66 -36
  33. package/package.json +6 -1
@@ -0,0 +1,140 @@
1
+ import { createSignal, merge, onFlush, onSettled, untrack } from "solid-js";
2
+ /**
3
+ * The input of a combobox, the code `Combobox.Input` and
4
+ * `Autocomplete.Input` share: its attributes, typing, the keys, focus
5
+ * leaving, and the draft its `value` binds to. Focus leaving closes the
6
+ * list and then calls `onFocusOut`, which a Combobox puts its text back
7
+ * in; an Autocomplete keeps the text, and passes none. Returns its props
8
+ * and `syncText`, which puts the element back to the combobox's text once
9
+ * that has settled, as after a form reset.
10
+ */
11
+ export function createComboboxInput(combobox, onFocusOut) {
12
+ const { list } = combobox;
13
+ let input;
14
+ const details = (reason, event) => ({ reason, event });
15
+ const setOpen = (open, reason, event) => combobox.setOpen(open, details(reason, event));
16
+ // What the input's `value` binds to: the combobox's text, and each edit
17
+ // as it is made. The combobox writes its own text after the event's flush
18
+ // (`createControllableSignal`), and a binding that re-ran in that flush,
19
+ // as `aria-activedescendant` does when typing empties the active item,
20
+ // would write the old text over the edit.
21
+ const [draft, setDraft] = createSignal(() => combobox.inputValue());
22
+ // Puts the draft and the element back to the combobox's text, once that
23
+ // has settled: a controlled consumer may have declined the edit, and a
24
+ // form reset sets the element without an `input` event.
25
+ const syncText = () => {
26
+ const text = untrack(combobox.inputValue);
27
+ setDraft(text);
28
+ // Only when it differs, since a write moves the caret to the end.
29
+ if (input && input.value !== text)
30
+ input.value = text;
31
+ };
32
+ const props = merge(combobox.attributes, {
33
+ type: "text",
34
+ role: "combobox",
35
+ "aria-autocomplete": "list",
36
+ autocomplete: "off",
37
+ get value() {
38
+ return draft();
39
+ },
40
+ get disabled() {
41
+ return combobox.props.disabled;
42
+ },
43
+ get "aria-expanded"() {
44
+ return combobox.open() ? "true" : "false";
45
+ },
46
+ "aria-controls": combobox.listId,
47
+ // Only while open, as an item's `data-active`.
48
+ get "aria-activedescendant"() {
49
+ return combobox.open() ? list.activeId() : undefined;
50
+ },
51
+ get "aria-required"() {
52
+ return combobox.props.required ? "true" : undefined;
53
+ },
54
+ onInput: (event) => {
55
+ setDraft(input.value);
56
+ combobox.setInputValue(input.value, details("input-change", event));
57
+ // The keys start again from the text (README, "Keys while open").
58
+ list.move(undefined);
59
+ setOpen(true, "input-change", event);
60
+ onFlush(syncText);
61
+ },
62
+ onKeyDown: (event) => {
63
+ // An input method's keys, such as the Enter that commits what it
64
+ // composed (README, "An input method's composition").
65
+ if (event.isComposing || event.keyCode === 229)
66
+ return;
67
+ if (event.ctrlKey || event.metaKey || event.shiftKey)
68
+ return;
69
+ const isOpen = combobox.open();
70
+ const { key, altKey } = event;
71
+ if (key === "ArrowDown" || key === "ArrowUp") {
72
+ const down = key === "ArrowDown";
73
+ if (altKey) {
74
+ // Alt+Down opens without moving, Alt+Up closes.
75
+ if (down === isOpen)
76
+ return;
77
+ setOpen(down, "arrow-key", event);
78
+ }
79
+ else if (!isOpen) {
80
+ setOpen(true, "arrow-key", event);
81
+ // While closed, the move waits for the list to open.
82
+ const items = list.items();
83
+ list.move(down ? items[0] : items[items.length - 1]);
84
+ }
85
+ else if (!list.onKeyDown(event))
86
+ return;
87
+ }
88
+ else if (key === "Enter" && !altKey) {
89
+ if (!isOpen)
90
+ return;
91
+ const item = list.active();
92
+ if (!item) {
93
+ // A form submits, as it would from a text input.
94
+ setOpen(false, "enter-key", event);
95
+ return;
96
+ }
97
+ if (combobox.select(item, event))
98
+ setOpen(false, "item-press", event);
99
+ }
100
+ else if (key === "Escape" && !altKey) {
101
+ if (isOpen)
102
+ setOpen(false, "escape-key", event);
103
+ // Nothing to close or clear: a dialog around it may close.
104
+ else if (!combobox.clear(event, "escape-key"))
105
+ return;
106
+ }
107
+ else
108
+ return;
109
+ event.preventDefault();
110
+ },
111
+ onBlur: (event) => {
112
+ // Focus lost to another window comes back to the input.
113
+ if (!document.hasFocus())
114
+ return;
115
+ setOpen(false, "focus-out", event);
116
+ onFocusOut?.(event);
117
+ },
118
+ ref: (element) => {
119
+ input = element;
120
+ combobox.input = input;
121
+ },
122
+ });
123
+ return { props, syncText };
124
+ }
125
+ /**
126
+ * Calls `reset` when the form of `input()` is reset, from the time the
127
+ * part has settled: a form reset puts a control back to its default, and
128
+ * so it does a combobox, through its setters, so that a controlled one
129
+ * asks. The browser resets the form's inputs after the event, to their
130
+ * `value` attributes; an `onFlush` in `reset` runs after that.
131
+ */
132
+ export function onFormReset(input, reset) {
133
+ onSettled(() => {
134
+ const form = input()?.form;
135
+ if (!form)
136
+ return;
137
+ form.addEventListener("reset", reset);
138
+ return () => form.removeEventListener("reset", reset);
139
+ });
140
+ }
@@ -1,7 +1,8 @@
1
- import { createUniqueId, merge, omit, onSettled } from "solid-js";
1
+ import { merge, omit } from "solid-js";
2
2
  import { mergeElementProps } from "../../core/mergeElementProps.js";
3
3
  import { renderElement } from "../../core/renderElement.jsx";
4
4
  import { useComboboxContext } from "../root/ComboboxContext.js";
5
+ import { createComboboxItem } from "./createComboboxItem.js";
5
6
  import { ComboboxItemContext } from "./ComboboxItemContext.js";
6
7
  /**
7
8
  * A value to select: a `<div role="option">` with `aria-selected`. A click
@@ -11,31 +12,8 @@ import { ComboboxItemContext } from "./ComboboxItemContext.js";
11
12
  * `Combobox.List`.
12
13
  */
13
14
  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));
15
+ const combobox = useComboboxContext("Combobox");
16
+ const { props: itemProps } = createComboboxItem(combobox, props, (element) => props.label ?? labelOf(element));
39
17
  const selected = () => combobox.value() === props.value;
40
18
  const attributes = {
41
19
  get "data-selected"() {
@@ -45,32 +23,11 @@ export function ComboboxItem(props) {
45
23
  return selected() ? undefined : "";
46
24
  },
47
25
  };
48
- const ownProps = merge(attributes, {
49
- role: "option",
50
- get id() {
51
- return id();
52
- },
26
+ const ownProps = merge(attributes, itemProps, {
53
27
  // Solid 2.0 removes an attribute set to false, so spell the value out.
54
28
  get "aria-selected"() {
55
29
  return selected() ? "true" : "false";
56
30
  },
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
31
  });
75
32
  const elementProps = mergeElementProps(ownProps, omit(props, "render", "value", "disabled", "label", "id"));
76
33
  return (<ComboboxItemContext value={attributes}>
@@ -0,0 +1,21 @@
1
+ import type { JSX } from "@solidjs/web";
2
+ import type { ElementProps } from "../../core/renderElement.tsx";
3
+ import type { ComboboxBase, ComboboxItemEntry } from "../root/ComboboxContext.ts";
4
+ /** The props of an item that `createComboboxItem` reads. */
5
+ export interface ComboboxItemOptions {
6
+ readonly id?: JSX.HTMLAttributes<HTMLElement>["id"];
7
+ readonly value: string;
8
+ readonly disabled?: boolean;
9
+ }
10
+ /**
11
+ * An item of a combobox, the code `Combobox.Item` and `Autocomplete.Item`
12
+ * share: its entry in the list navigation, registered once mounted, and
13
+ * the props of its `option` element, its `id`, `data-active`, the disabled
14
+ * state, the pointer and the click that selects or accepts it. `text` is
15
+ * what the input shows once it is: a Combobox's label, an Autocomplete's
16
+ * value. Each Item adds its own `aria-selected`.
17
+ */
18
+ export declare function createComboboxItem(combobox: ComboboxBase, props: ComboboxItemOptions, text: (element: HTMLElement) => string): {
19
+ item: ComboboxItemEntry;
20
+ props: ElementProps;
21
+ };
@@ -0,0 +1,61 @@
1
+ import { createUniqueId, onSettled } from "solid-js";
2
+ /**
3
+ * An item of a combobox, the code `Combobox.Item` and `Autocomplete.Item`
4
+ * share: its entry in the list navigation, registered once mounted, and
5
+ * the props of its `option` element, its `id`, `data-active`, the disabled
6
+ * state, the pointer and the click that selects or accepts it. `text` is
7
+ * what the input shows once it is: a Combobox's label, an Autocomplete's
8
+ * value. Each Item adds its own `aria-selected`.
9
+ */
10
+ export function createComboboxItem(combobox, props, text) {
11
+ let element;
12
+ const generated = createUniqueId();
13
+ // The consumer's `id`, else the generated one, which the Input names
14
+ // while the item is active, so `false` does not remove it. Kept out of
15
+ // the merge ("`undefined` is a value", PLAN.md section 8).
16
+ const id = () => props.id || generated;
17
+ const item = {
18
+ get element() {
19
+ return element;
20
+ },
21
+ get id() {
22
+ return id();
23
+ },
24
+ get text() {
25
+ return text(element);
26
+ },
27
+ get value() {
28
+ return props.value;
29
+ },
30
+ get disabled() {
31
+ return props.disabled;
32
+ },
33
+ };
34
+ onSettled(() => combobox.list.register(item));
35
+ return {
36
+ item,
37
+ props: {
38
+ role: "option",
39
+ get id() {
40
+ return id();
41
+ },
42
+ get "data-active"() {
43
+ return combobox.isActive(item) ? "" : undefined;
44
+ },
45
+ get "aria-disabled"() {
46
+ return props.disabled ? "true" : undefined;
47
+ },
48
+ get "data-disabled"() {
49
+ return props.disabled ? "" : undefined;
50
+ },
51
+ onPointerMove: (event) => combobox.list.onPointerMove(item, event),
52
+ onClick: (event) => {
53
+ if (combobox.select(item, event))
54
+ combobox.setOpen(false, { reason: "item-press", event });
55
+ },
56
+ ref: (el) => {
57
+ element = el;
58
+ },
59
+ },
60
+ };
61
+ }
@@ -81,52 +81,99 @@ export interface ComboboxStateProps {
81
81
  /** Whether the Combobox ignores the user. It cannot be focused either. */
82
82
  disabled?: boolean;
83
83
  }
84
- /** What `Combobox.Root` shares with its parts through context. */
85
- export interface Combobox {
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";
86
96
  /** Whether the list is open. */
87
97
  open(): boolean;
88
98
  /** Asks to open or close the list, saying why. */
89
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";
90
137
  /** The selected item's `value`, or `null`. */
91
138
  value(): string | null;
92
139
  /** Asks for a value, as the hidden input's `change` and a reset do. */
93
140
  setValue(value: string | null): void;
94
- /** The text the input shows. */
95
- inputValue(): string;
96
141
  /** Asks for a text, saying why. */
97
142
  setInputValue(text: string, details: InputValueChangeDetails): void;
98
143
  /**
99
144
  * 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.
145
+ * `input` and `change` for the form, and its label as the text.
102
146
  */
103
147
  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
148
  /**
107
149
  * Puts the text back to the selected item's label as focus leaves, or
108
- * clears the value if the user emptied the text.
150
+ * clears the value if the user emptied the text: the Input's `onFocusOut`.
109
151
  */
110
152
  revert(event: Event): void;
111
153
  /** The label of the item with `value`, if one has mounted. */
112
154
  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
155
  /** The hidden input, once rendered: the form's view of the value. */
123
156
  hiddenInput: HTMLInputElement | undefined;
124
- /** The `data-*` attributes of the Input, the Trigger and the Popup. */
125
- readonly attributes: ComboboxStateAttributes;
126
157
  /** The Root's props, for the parts' elements. */
127
158
  readonly props: ComboboxStateProps;
128
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;
129
171
  /** Creates a combobox's state and its list navigation. */
130
172
  export declare function createCombobox(props: ComboboxStateProps): Combobox;
131
- export declare const ComboboxContext: import("solid-js").Context<Combobox | null>;
132
- export declare function useComboboxContext(): 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;
@@ -2,8 +2,13 @@ import { isDev } from "@solidjs/web";
2
2
  import { createContext, createMemo, createUniqueId, untrack, useContext, } from "solid-js";
3
3
  import { createControllableSignal } from "../../core/createControllableSignal.js";
4
4
  import { createActiveDescendantList } from "../../floating/list-navigation/activeDescendant.js";
5
- /** Creates a combobox's state and its list navigation. */
6
- export function createCombobox(props) {
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) {
7
12
  const [open, setOpen] = createControllableSignal({
8
13
  get value() {
9
14
  return props.open;
@@ -13,6 +18,43 @@ export function createCombobox(props) {
13
18
  },
14
19
  onChange: (value, details) => props.onOpenChange?.(value, details),
15
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;
16
58
  const [value, setValue, requestedValue] = createControllableSignal({
17
59
  get value() {
18
60
  return props.value;
@@ -34,14 +76,6 @@ export function createCombobox(props) {
34
76
  },
35
77
  onChange: (text, details) => props.onInputValueChange?.(text, details),
36
78
  });
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
79
  const labelOfValue = (selected) => {
46
80
  const item = list.items().find((item) => item.value === selected);
47
81
  // An item's text is always given: its `label`, else its content.
@@ -85,8 +119,8 @@ export function createCombobox(props) {
85
119
  input.dispatchEvent(new Event("change", { bubbles: true }));
86
120
  };
87
121
  const combobox = {
88
- open,
89
- setOpen,
122
+ ...base,
123
+ component: "Combobox",
90
124
  value,
91
125
  setValue,
92
126
  inputValue,
@@ -99,8 +133,11 @@ export function createCombobox(props) {
99
133
  return true;
100
134
  },
101
135
  clear: (event, reason) => {
136
+ if (!requestedInputValue() && requestedValue() === null)
137
+ return false;
102
138
  commit(null);
103
139
  setInputValue("", { reason, event });
140
+ return true;
104
141
  },
105
142
  revert: (event) => {
106
143
  // An untouched Combobox keeps its value, though its text may be
@@ -119,38 +156,31 @@ export function createCombobox(props) {
119
156
  setInputValue(label, { reason: "focus-out", event });
120
157
  },
121
158
  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
159
  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
160
  props,
144
161
  };
145
162
  return combobox;
146
163
  }
147
164
  // `null` outside a Root, so that the part can name what is missing.
148
165
  export const ComboboxContext = createContext(null);
149
- export function useComboboxContext() {
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) {
150
172
  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>.");
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
+ }
154
184
  }
155
185
  return context;
156
186
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "menhir",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
4
4
  "description": "Unstyled, accessible UI primitives for SolidJS 2.0, with small and measurable bundles",
5
5
  "keywords": [
6
6
  "solid",
@@ -34,6 +34,11 @@
34
34
  "solid": "./dist/source/alert-dialog/index.js",
35
35
  "import": "./dist/alert-dialog/index.js"
36
36
  },
37
+ "./autocomplete": {
38
+ "types": "./dist/source/autocomplete/index.d.ts",
39
+ "solid": "./dist/source/autocomplete/index.js",
40
+ "import": "./dist/autocomplete/index.js"
41
+ },
37
42
  "./checkbox": {
38
43
  "types": "./dist/source/checkbox/index.d.ts",
39
44
  "solid": "./dist/source/checkbox/index.js",