menhir 0.1.6 → 0.1.8

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 (57) 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/field/control/FieldControl.js +39 -0
  14. package/dist/field/description/FieldDescription.js +25 -0
  15. package/dist/field/error/FieldError.js +45 -0
  16. package/dist/field/index.js +2 -0
  17. package/dist/field/index.parts.js +6 -0
  18. package/dist/field/label/FieldLabel.js +23 -0
  19. package/dist/field/root/FieldRoot.js +113 -0
  20. package/dist/field/root/FieldRootContext.js +10 -0
  21. package/dist/source/autocomplete/index.d.ts +2 -0
  22. package/dist/source/autocomplete/index.js +4 -0
  23. package/dist/source/autocomplete/index.parts.d.ts +10 -0
  24. package/dist/source/autocomplete/index.parts.js +12 -0
  25. package/dist/source/autocomplete/input/AutocompleteInput.d.ts +18 -0
  26. package/dist/source/autocomplete/input/AutocompleteInput.jsx +37 -0
  27. package/dist/source/autocomplete/item/AutocompleteItem.d.ts +20 -0
  28. package/dist/source/autocomplete/item/AutocompleteItem.jsx +25 -0
  29. package/dist/source/autocomplete/root/AutocompleteContext.d.ts +59 -0
  30. package/dist/source/autocomplete/root/AutocompleteContext.js +33 -0
  31. package/dist/source/autocomplete/root/AutocompleteRoot.d.ts +13 -0
  32. package/dist/source/autocomplete/root/AutocompleteRoot.jsx +15 -0
  33. package/dist/source/combobox/input/ComboboxInput.jsx +13 -135
  34. package/dist/source/combobox/input/createComboboxInput.d.ts +30 -0
  35. package/dist/source/combobox/input/createComboboxInput.js +140 -0
  36. package/dist/source/combobox/item/ComboboxItem.jsx +5 -48
  37. package/dist/source/combobox/item/createComboboxItem.d.ts +21 -0
  38. package/dist/source/combobox/item/createComboboxItem.js +61 -0
  39. package/dist/source/combobox/root/ComboboxContext.d.ts +69 -22
  40. package/dist/source/combobox/root/ComboboxContext.js +66 -36
  41. package/dist/source/field/control/FieldControl.d.ts +20 -0
  42. package/dist/source/field/control/FieldControl.jsx +36 -0
  43. package/dist/source/field/description/FieldDescription.d.ts +12 -0
  44. package/dist/source/field/description/FieldDescription.jsx +19 -0
  45. package/dist/source/field/error/FieldError.d.ts +19 -0
  46. package/dist/source/field/error/FieldError.jsx +39 -0
  47. package/dist/source/field/index.d.ts +1 -0
  48. package/dist/source/field/index.js +1 -0
  49. package/dist/source/field/index.parts.d.ts +5 -0
  50. package/dist/source/field/index.parts.js +5 -0
  51. package/dist/source/field/label/FieldLabel.d.ts +11 -0
  52. package/dist/source/field/label/FieldLabel.jsx +19 -0
  53. package/dist/source/field/root/FieldRoot.d.ts +25 -0
  54. package/dist/source/field/root/FieldRoot.jsx +133 -0
  55. package/dist/source/field/root/FieldRootContext.d.ts +50 -0
  56. package/dist/source/field/root/FieldRootContext.js +13 -0
  57. package/package.json +11 -1
@@ -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
  }
@@ -0,0 +1,20 @@
1
+ import type { JSX } from "@solidjs/web";
2
+ import type { RenderProp } from "../../core/renderElement.tsx";
3
+ export interface FieldControlProps extends Omit<JSX.InputHTMLAttributes<HTMLInputElement>, "id" | "ref"> {
4
+ /** Typed for any element, since `render` may render a `<textarea>` or a `<select>`. */
5
+ ref?: JSX.Ref<HTMLElement>;
6
+ /**
7
+ * Renders the control as another element or component, such as a
8
+ * `<textarea>`, a `<select>` or `Switch.Root`, which gets the field's
9
+ * `id`, `name`, `disabled`, `aria-describedby`, `aria-invalid` and
10
+ * `data-*` attributes. Read once.
11
+ */
12
+ render?: RenderProp;
13
+ }
14
+ /**
15
+ * The field's control: an `<input>`, or what `render` renders, with the
16
+ * field's `id`, which the Label names, its `name` and `disabled`, and
17
+ * `aria-describedby` naming the Description and the rendered Error. Takes
18
+ * no `id`: the Root generates it. Place it inside `Field.Root`.
19
+ */
20
+ export declare function FieldControl(props: FieldControlProps): JSX.Element;
@@ -0,0 +1,36 @@
1
+ import { merge, omit } from "solid-js";
2
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
3
+ import { renderElement } from "../../core/renderElement.jsx";
4
+ import { useFieldRootContext } from "../root/FieldRootContext.js";
5
+ /**
6
+ * The field's control: an `<input>`, or what `render` renders, with the
7
+ * field's `id`, which the Label names, its `name` and `disabled`, and
8
+ * `aria-describedby` naming the Description and the rendered Error. Takes
9
+ * no `id`: the Root generates it. Place it inside `Field.Root`.
10
+ */
11
+ export function FieldControl(props) {
12
+ const root = useFieldRootContext();
13
+ const ownProps = merge(root.attributes, {
14
+ id: root.controlId,
15
+ get name() {
16
+ return root.name();
17
+ },
18
+ get disabled() {
19
+ return root.disabled();
20
+ },
21
+ // Solid 2.0 removes an attribute set to false, so spell the value out.
22
+ get "aria-invalid"() {
23
+ return root.showsError() ? "true" : undefined;
24
+ },
25
+ // The consumer's wins. Kept out of the merge, where a wrapper's
26
+ // `aria-describedby={undefined}` would remove the Field's ("`undefined`
27
+ // is a value", PLAN.md section 8).
28
+ get "aria-describedby"() {
29
+ return props["aria-describedby"] ?? root.describedBy();
30
+ },
31
+ });
32
+ const consumerProps = omit(props, "render", "id", "aria-describedby");
33
+ // Typed for an input; the merge takes any element's attributes.
34
+ const elementProps = mergeElementProps(ownProps, consumerProps);
35
+ return renderElement(props, elementProps, (p) => <input {...p}/>);
36
+ }
@@ -0,0 +1,12 @@
1
+ import type { JSX } from "@solidjs/web";
2
+ import type { RenderProp } from "../../core/renderElement.tsx";
3
+ export interface FieldDescriptionProps extends Omit<JSX.HTMLAttributes<HTMLElement>, "id"> {
4
+ render?: RenderProp;
5
+ }
6
+ /**
7
+ * What the control is for, or what it takes, which describes the control
8
+ * in `aria-describedby`: a `<p>`. Its `id` is the Root's, so the server
9
+ * HTML links it before hydration; it takes none from the consumer. Place
10
+ * it inside `Field.Root`.
11
+ */
12
+ export declare function FieldDescription(props: FieldDescriptionProps): JSX.Element;
@@ -0,0 +1,19 @@
1
+ import { merge, omit, onSettled } from "solid-js";
2
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
3
+ import { renderElement } from "../../core/renderElement.jsx";
4
+ import { useFieldRootContext } from "../root/FieldRootContext.js";
5
+ /**
6
+ * What the control is for, or what it takes, which describes the control
7
+ * in `aria-describedby`: a `<p>`. Its `id` is the Root's, so the server
8
+ * HTML links it before hydration; it takes none from the consumer. Place
9
+ * it inside `Field.Root`.
10
+ */
11
+ export function FieldDescription(props) {
12
+ const root = useFieldRootContext();
13
+ // Counted once mounted, so that a Field without a Description names none
14
+ // (README, "Decisions").
15
+ onSettled(() => root.registerDescription());
16
+ const ownProps = merge(root.attributes, { id: root.descriptionId });
17
+ const elementProps = mergeElementProps(ownProps, omit(props, "render", "id"));
18
+ return renderElement(props, elementProps, (p) => <p {...p}/>);
19
+ }
@@ -0,0 +1,19 @@
1
+ import type { JSX } from "@solidjs/web";
2
+ import type { RenderProp } from "../../core/renderElement.tsx";
3
+ /** A flag of the browser's validity, such as `"valueMissing"`. */
4
+ export type ValidityFlag = Exclude<keyof ValidityState, "valid">;
5
+ export interface FieldErrorProps extends JSX.HTMLAttributes<HTMLElement> {
6
+ /**
7
+ * Shows the Error only while the browser's validity has this flag, such
8
+ * as `"valueMissing"`, so each state gets its own message.
9
+ */
10
+ match?: ValidityFlag;
11
+ render?: RenderProp;
12
+ }
13
+ /**
14
+ * The field's error message: a `<p>`, rendered only while the field shows
15
+ * an error, which the control names in `aria-describedby` while it is
16
+ * rendered. Without children it shows the error from outside, else the
17
+ * browser's message. Place it inside `Field.Root`.
18
+ */
19
+ export declare function FieldError(props: FieldErrorProps): JSX.Element;
@@ -0,0 +1,39 @@
1
+ import { Show, createUniqueId, merge, omit, onSettled } from "solid-js";
2
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
3
+ import { renderElement } from "../../core/renderElement.jsx";
4
+ import { useFieldRootContext } from "../root/FieldRootContext.js";
5
+ /**
6
+ * The field's error message: a `<p>`, rendered only while the field shows
7
+ * an error, which the control names in `aria-describedby` while it is
8
+ * rendered. Without children it shows the error from outside, else the
9
+ * browser's message. Place it inside `Field.Root`.
10
+ */
11
+ export function FieldError(props) {
12
+ const root = useFieldRootContext();
13
+ const generated = createUniqueId();
14
+ // The consumer's `id`, else the generated one. Kept out of the merge,
15
+ // where a wrapper's `id={undefined}` would remove it ("`undefined` is a
16
+ // value", PLAN.md section 8).
17
+ const id = () => props.id ?? generated;
18
+ // Without children, an Error with no message to show is not rendered,
19
+ // so the control never names an empty element. Whether children were
20
+ // passed is fixed by the JSX, so the key is checked, not their value.
21
+ const shown = () => root.showsError() &&
22
+ (props.match === undefined || !!root.validity()?.[props.match]) &&
23
+ ("children" in props || !!root.message());
24
+ // Registered once mounted, as a popup's Title is, so that the control
25
+ // names it only while it is rendered, and in the same update.
26
+ onSettled(() => root.registerError({ id, shown }));
27
+ const ownProps = merge(root.attributes, {
28
+ get id() {
29
+ return id();
30
+ },
31
+ get children() {
32
+ return props.children ?? root.message();
33
+ },
34
+ });
35
+ const elementProps = mergeElementProps(ownProps, omit(props, "render", "id", "match", "children"));
36
+ return (<Show when={shown()}>
37
+ {renderElement(props, elementProps, (p) => (<p {...p}/>))}
38
+ </Show>);
39
+ }
@@ -0,0 +1 @@
1
+ export * as Field from "./index.parts.ts";
@@ -0,0 +1 @@
1
+ export * as Field from "./index.parts.js";
@@ -0,0 +1,5 @@
1
+ export { FieldRoot as Root } from "./root/FieldRoot.tsx";
2
+ export { FieldLabel as Label } from "./label/FieldLabel.tsx";
3
+ export { FieldControl as Control } from "./control/FieldControl.tsx";
4
+ export { FieldDescription as Description } from "./description/FieldDescription.tsx";
5
+ export { FieldError as Error } from "./error/FieldError.tsx";
@@ -0,0 +1,5 @@
1
+ export { FieldRoot as Root } from "./root/FieldRoot.jsx";
2
+ export { FieldLabel as Label } from "./label/FieldLabel.jsx";
3
+ export { FieldControl as Control } from "./control/FieldControl.jsx";
4
+ export { FieldDescription as Description } from "./description/FieldDescription.jsx";
5
+ export { FieldError as Error } from "./error/FieldError.jsx";
@@ -0,0 +1,11 @@
1
+ import type { JSX } from "@solidjs/web";
2
+ import type { RenderProp } from "../../core/renderElement.tsx";
3
+ export interface FieldLabelProps extends JSX.HTMLAttributes<HTMLElement> {
4
+ render?: RenderProp;
5
+ }
6
+ /**
7
+ * The control's label: a `<label>` whose `for` is the control's `id`, which
8
+ * the Root generates, so the server HTML is labelled before hydration.
9
+ * Place it inside `Field.Root`.
10
+ */
11
+ export declare function FieldLabel(props: FieldLabelProps): JSX.Element;
@@ -0,0 +1,19 @@
1
+ import { merge, omit } from "solid-js";
2
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
3
+ import { renderElement } from "../../core/renderElement.jsx";
4
+ import { useFieldRootContext } from "../root/FieldRootContext.js";
5
+ /**
6
+ * The control's label: a `<label>` whose `for` is the control's `id`, which
7
+ * the Root generates, so the server HTML is labelled before hydration.
8
+ * Place it inside `Field.Root`.
9
+ */
10
+ export function FieldLabel(props) {
11
+ const root = useFieldRootContext();
12
+ // `for` is a label's attribute, which `ElementProps`, typed for any
13
+ // element, does not list.
14
+ const ownProps = merge(root.attributes, {
15
+ for: root.controlId,
16
+ });
17
+ const elementProps = mergeElementProps(ownProps, omit(props, "render"));
18
+ return renderElement(props, elementProps, (p) => <label {...p}/>);
19
+ }
@@ -0,0 +1,25 @@
1
+ import type { JSX } from "@solidjs/web";
2
+ import type { RenderProp } from "../../core/renderElement.tsx";
3
+ export interface FieldRootProps extends JSX.HTMLAttributes<HTMLElement> {
4
+ /** The control's `name`, which the form submits it under. */
5
+ name?: string;
6
+ /** Whether the control is disabled. Every part gets `data-disabled`. */
7
+ disabled?: boolean;
8
+ /**
9
+ * Whether the field is invalid, from outside: a schema, a server's
10
+ * answer, the app's own form state. Shown once the field is touched.
11
+ */
12
+ invalid?: boolean;
13
+ /**
14
+ * The error message from outside, which makes the field invalid. An Error
15
+ * without children shows it, over the browser's message.
16
+ */
17
+ error?: string;
18
+ render?: RenderProp;
19
+ }
20
+ /**
21
+ * The field: a `<div>` that holds a control's label, description and error,
22
+ * and its validity. It generates the control's `id` and the Description's,
23
+ * and tracks whether the field was touched.
24
+ */
25
+ export declare function FieldRoot(props: FieldRootProps): JSX.Element;
@@ -0,0 +1,133 @@
1
+ import { createSignal, createUniqueId, merge, omit, onFlush, onSettled, } from "solid-js";
2
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
3
+ import { renderElement } from "../../core/renderElement.jsx";
4
+ import { FieldRootContext } from "./FieldRootContext.js";
5
+ const controlSelector = "input,select,textarea";
6
+ /**
7
+ * The field: a `<div>` that holds a control's label, description and error,
8
+ * and its validity. It generates the control's `id` and the Description's,
9
+ * and tracks whether the field was touched.
10
+ */
11
+ export function FieldRoot(props) {
12
+ const controlId = createUniqueId();
13
+ const descriptionId = createUniqueId();
14
+ const [touched, setTouched] = createSignal(false);
15
+ // The first control inside the Root that the browser finds invalid. Set
16
+ // again on each event, even to the same element, whose message may have
17
+ // changed.
18
+ const [invalidControl, setInvalidControl] = createSignal(undefined, { equals: false });
19
+ // False in the server HTML and the first render, when the control names
20
+ // the Description before it is rendered (README, "Decisions").
21
+ const [settled, setSettled] = createSignal(false);
22
+ const [descriptions, setDescriptions] = createSignal(0);
23
+ const [errors, setErrors] = createSignal([]);
24
+ let root;
25
+ const readValidity = () => {
26
+ if (!root)
27
+ return undefined;
28
+ for (const control of root.querySelectorAll(controlSelector)) {
29
+ if (control.willValidate && !control.validity.valid)
30
+ return control;
31
+ }
32
+ return undefined;
33
+ };
34
+ // Reads the controls' validity again, and sets touched if given. The
35
+ // Field's own state is written after the event's flush, so async work the
36
+ // event starts does not hold it (PLAN.md section 8, "API conventions").
37
+ // By then a form reset has reset the controls too.
38
+ const update = (touch) => onFlush(() => {
39
+ setInvalidControl(readValidity());
40
+ if (touch !== undefined)
41
+ setTouched(touch);
42
+ });
43
+ const invalid = () => !!props.invalid || !!props.error || invalidControl() !== undefined;
44
+ const showsError = () => touched() && invalid();
45
+ const attributes = {
46
+ get "data-invalid"() {
47
+ return showsError() ? "" : undefined;
48
+ },
49
+ get "data-touched"() {
50
+ return touched() ? "" : undefined;
51
+ },
52
+ get "data-disabled"() {
53
+ return props.disabled ? "" : undefined;
54
+ },
55
+ };
56
+ const context = {
57
+ controlId,
58
+ descriptionId,
59
+ name: () => props.name,
60
+ disabled: () => props.disabled,
61
+ showsError,
62
+ message: () => props.error || invalidControl()?.validationMessage,
63
+ validity: () => invalidControl()?.validity,
64
+ describedBy() {
65
+ const ids = [];
66
+ if (!settled() || descriptions() > 0)
67
+ ids.push(descriptionId);
68
+ for (const error of errors()) {
69
+ const id = error.shown() && error.id();
70
+ if (id)
71
+ ids.push(id);
72
+ }
73
+ return ids.join(" ") || undefined;
74
+ },
75
+ registerDescription() {
76
+ setDescriptions((count) => count + 1);
77
+ return () => setDescriptions((count) => count - 1);
78
+ },
79
+ registerError(entry) {
80
+ setErrors((list) => [...list, entry]);
81
+ return () => setErrors((list) => list.filter((e) => e !== entry));
82
+ },
83
+ attributes,
84
+ };
85
+ // A submission attempt, or a control found invalid: `invalid` does not
86
+ // bubble, so the Root and the form listen in the capture phase.
87
+ const touch = () => update(true);
88
+ onSettled(() => {
89
+ setSettled(true);
90
+ // A submission attempt touches every Field of the form, by its submit
91
+ // event or an `invalid` event of any control in it, and a reset clears
92
+ // it.
93
+ const form = root?.querySelector(controlSelector)?.form ??
94
+ root?.closest("form");
95
+ if (!form)
96
+ return;
97
+ // A reset the app cancels, after this listener, keeps it touched.
98
+ const onReset = (event) => onFlush(() => {
99
+ if (!event.defaultPrevented)
100
+ update(false);
101
+ });
102
+ form.addEventListener("submit", touch);
103
+ form.addEventListener("invalid", touch, true);
104
+ form.addEventListener("reset", onReset);
105
+ return () => {
106
+ form.removeEventListener("submit", touch);
107
+ form.removeEventListener("invalid", touch, true);
108
+ form.removeEventListener("reset", onReset);
109
+ };
110
+ });
111
+ const ownProps = merge(attributes, {
112
+ onFocusOut: (event) => {
113
+ const to = event.relatedTarget;
114
+ // Focus moving to nothing while the control keeps it is the window
115
+ // losing focus, to another window or tab, not the user leaving the
116
+ // field; a press on the page moves focus to the body.
117
+ const left = to
118
+ ? !root?.contains(to)
119
+ : document.activeElement !== event.target;
120
+ update(left || undefined);
121
+ },
122
+ onChange: touch,
123
+ onInput: () => update(),
124
+ ref: (element) => {
125
+ root = element;
126
+ element.addEventListener("invalid", touch, true);
127
+ },
128
+ });
129
+ const elementProps = mergeElementProps(ownProps, omit(props, "render", "name", "disabled", "invalid", "error"));
130
+ return (<FieldRootContext value={context}>
131
+ {renderElement(props, elementProps, (p) => (<div {...p}/>))}
132
+ </FieldRootContext>);
133
+ }
@@ -0,0 +1,50 @@
1
+ /**
2
+ * The field's state as `data-*` attributes, each `""` when it applies and
3
+ * absent otherwise. Every part merges the same object, so its getters are
4
+ * read where each element uses them.
5
+ */
6
+ export type FieldStateAttributes = {
7
+ readonly "data-invalid": "" | undefined;
8
+ readonly "data-touched": "" | undefined;
9
+ readonly "data-disabled": "" | undefined;
10
+ };
11
+ /** An Error part, as it registers with the Root once mounted. */
12
+ export interface FieldErrorEntry {
13
+ /** Its `id`, which the control names while it is rendered. */
14
+ id(): string | false;
15
+ /** Whether it is rendered. */
16
+ shown(): boolean;
17
+ }
18
+ /** What the Root shares with its parts. */
19
+ export interface FieldRootContextValue {
20
+ /** The control's `id`, generated by the Root. */
21
+ readonly controlId: string;
22
+ /** The Description's `id`, generated by the Root. */
23
+ readonly descriptionId: string;
24
+ /** The field's `name`, for the control. */
25
+ name(): string | undefined;
26
+ /** Whether the field is disabled, for the control. */
27
+ disabled(): boolean | undefined;
28
+ /** Whether the field shows an error: invalid, and touched. */
29
+ showsError(): boolean;
30
+ /** The message an Error without children shows. */
31
+ message(): string | undefined;
32
+ /** The browser's validity of the first invalid control, if any. */
33
+ validity(): ValidityState | undefined;
34
+ /** The `id`s the control names in `aria-describedby`, if any. */
35
+ describedBy(): string | undefined;
36
+ /**
37
+ * Counts a mounted Description, so the control names it. Returns what
38
+ * uncounts it, for the Description's disposal.
39
+ */
40
+ registerDescription(): () => void;
41
+ /**
42
+ * Registers a mounted Error. Returns what unregisters it, for the
43
+ * Error's disposal.
44
+ */
45
+ registerError(entry: FieldErrorEntry): () => void;
46
+ /** The `data-*` attributes every part carries. */
47
+ readonly attributes: FieldStateAttributes;
48
+ }
49
+ export declare const FieldRootContext: import("solid-js").Context<FieldRootContextValue | null>;
50
+ export declare function useFieldRootContext(): FieldRootContextValue;
@@ -0,0 +1,13 @@
1
+ import { createContext, useContext } from "solid-js";
2
+ import { isDev } from "@solidjs/web";
3
+ // `null` outside a Root, so that the part can name what is missing. Solid's
4
+ // own error for a context with no provider and no default does not.
5
+ export const FieldRootContext = createContext(null);
6
+ export function useFieldRootContext() {
7
+ const context = useContext(FieldRootContext);
8
+ // Production builds drop the check, and its message, with `isDev`.
9
+ if (isDev && !context) {
10
+ throw new Error("[menhir] Field parts must be placed within <Field.Root>.");
11
+ }
12
+ return context;
13
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "menhir",
3
- "version": "0.1.6",
3
+ "version": "0.1.8",
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",
@@ -69,6 +74,11 @@
69
74
  "solid": "./dist/source/dialog/index.js",
70
75
  "import": "./dist/dialog/index.js"
71
76
  },
77
+ "./field": {
78
+ "types": "./dist/source/field/index.d.ts",
79
+ "solid": "./dist/source/field/index.js",
80
+ "import": "./dist/field/index.js"
81
+ },
72
82
  "./menu": {
73
83
  "types": "./dist/source/menu/index.d.ts",
74
84
  "solid": "./dist/source/menu/index.js",