menhir 0.1.8 → 0.1.9

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.
package/README.md CHANGED
@@ -62,7 +62,7 @@ For everything else, go to the docs:
62
62
 
63
63
  Early 0.x versions: the API may change between minor versions. The form components come next; the plan and roadmap are in the [repository](https://github.com/j13n/menhir). Built so far:
64
64
 
65
- - Components: Accordion, Alert Dialog, Autocomplete, Checkbox, Collapsible, Combobox, Context Menu, Dialog, Field, Menu, Menubar, Meter, Navigation Menu, Popover, Progress, Select, Separator, Switch, Tabs, Toggle, Toggle Group and Tooltip.
65
+ - Components: Accordion, Alert Dialog, Autocomplete, Checkbox, Collapsible, Combobox, Context Menu, Dialog, Field, Input, Menu, Menubar, Meter, Navigation Menu, Popover, Progress, Select, Separator, Switch, Tabs, Toggle, Toggle Group and Tooltip.
66
66
  - Utilities: color scheme and theme, each with local-storage and cookie storage.
67
67
 
68
68
  ## Licence
@@ -0,0 +1,2 @@
1
+ import * as Input from "./index.parts.js";
2
+ export { Input };
@@ -0,0 +1 @@
1
+ export { InputRoot as Root } from "./root/InputRoot.js";
@@ -0,0 +1,55 @@
1
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
2
+ import { renderElement } from "../../core/renderElement.js";
3
+ import { spread, template } from "@solidjs/web";
4
+ import { merge, omit, onFlush, onSettled, untrack } from "solid-js";
5
+ // src/input/root/InputRoot.tsx
6
+ var _tmpl$ = /* @__PURE__ */ template(`<input>`);
7
+ /**
8
+ * A text input: a native `<input>`, or what `render` renders. Name it with a
9
+ * `<label>`, or place it in `Field.Control`'s `render`.
10
+ */
11
+ function InputRoot(props) {
12
+ let element;
13
+ const restoreValue = () => {
14
+ const value = untrack(() => props.value);
15
+ if (element && value !== void 0 && element.value !== value) element.value = value;
16
+ };
17
+ onSettled(() => {
18
+ const form = element?.form;
19
+ if (!form) return;
20
+ const onReset = (event) => {
21
+ const text = element.value;
22
+ const defaultText = untrack(() => props.defaultValue) ?? "";
23
+ if (untrack(() => props.value) !== void 0) element.defaultValue = text;
24
+ setTimeout(() => {
25
+ if (event.defaultPrevented || text === defaultText) return;
26
+ props.onValueChange?.(defaultText, {
27
+ reason: "reset",
28
+ event
29
+ });
30
+ onFlush(restoreValue);
31
+ });
32
+ };
33
+ form.addEventListener("reset", onReset);
34
+ return () => form.removeEventListener("reset", onReset);
35
+ });
36
+ const elementProps = merge(mergeElementProps({
37
+ onInput: (event) => {
38
+ props.onValueChange?.(element.value, {
39
+ reason: "input-change",
40
+ event
41
+ });
42
+ if (!event.isComposing) onFlush(restoreValue);
43
+ },
44
+ onCompositionEnd: () => onFlush(restoreValue),
45
+ ref: (el) => {
46
+ element = el;
47
+ }
48
+ }, omit(props, "render", "value", "onValueChange")), () => props.value === void 0 ? {} : { value: props.value });
49
+ return renderElement(props, elementProps, (p) => (() => {
50
+ var _el$ = _tmpl$();
51
+ spread(_el$, p, false);
52
+ return _el$;
53
+ })());
54
+ }
55
+ export { InputRoot };
@@ -0,0 +1 @@
1
+ export * as Input from "./index.parts.ts";
@@ -0,0 +1 @@
1
+ export * as Input from "./index.parts.js";
@@ -0,0 +1 @@
1
+ export { InputRoot as Root } from "./root/InputRoot.tsx";
@@ -0,0 +1 @@
1
+ export { InputRoot as Root } from "./root/InputRoot.jsx";
@@ -0,0 +1,29 @@
1
+ import type { JSX } from "@solidjs/web";
2
+ import type { RenderProp } from "../../core/renderElement.tsx";
3
+ /** Why the text changed. */
4
+ export type InputValueChangeReason = "input-change" | "reset";
5
+ /** The second argument of `onValueChange`. */
6
+ export interface InputValueChangeDetails {
7
+ readonly reason: InputValueChangeReason;
8
+ /** The `input` event of an edit, or the form's `reset` event. */
9
+ readonly event: Event;
10
+ }
11
+ export interface InputRootProps extends Omit<JSX.InputHTMLAttributes<HTMLInputElement>, "ref" | "value" | "defaultValue"> {
12
+ /** Typed for any element, since `render` may render a `<textarea>`. */
13
+ ref?: JSX.Ref<HTMLElement>;
14
+ /** The text, when controlled. */
15
+ value?: string;
16
+ /** The text to start with when uncontrolled, and the text a form reset puts back. */
17
+ defaultValue?: string;
18
+ /**
19
+ * Called with the new text, and why, on each edit and on a form reset.
20
+ */
21
+ onValueChange?: (value: string, details: InputValueChangeDetails) => void;
22
+ /** Renders the input as another element or component, such as a `<textarea>`. Read once. */
23
+ render?: RenderProp;
24
+ }
25
+ /**
26
+ * A text input: a native `<input>`, or what `render` renders. Name it with a
27
+ * `<label>`, or place it in `Field.Control`'s `render`.
28
+ */
29
+ export declare function InputRoot(props: InputRootProps): JSX.Element;
@@ -0,0 +1,67 @@
1
+ import { merge, omit, onFlush, onSettled, untrack } from "solid-js";
2
+ import { mergeElementProps } from "../../core/mergeElementProps.js";
3
+ import { renderElement } from "../../core/renderElement.jsx";
4
+ /**
5
+ * A text input: a native `<input>`, or what `render` renders. Name it with a
6
+ * `<label>`, or place it in `Field.Control`'s `render`.
7
+ */
8
+ export function InputRoot(props) {
9
+ // An `<input>`, or a `<textarea>` rendered through `render`.
10
+ let element;
11
+ // Puts the element back to a controlled `value` once the consumer has
12
+ // had its say: Solid writes `value` only when it changes, so a declined
13
+ // edit would stay. Only when the two differ, since a write moves the
14
+ // caret to the end.
15
+ const restoreValue = () => {
16
+ const value = untrack(() => props.value);
17
+ if (element && value !== undefined && element.value !== value) {
18
+ element.value = value;
19
+ }
20
+ };
21
+ // A form reset puts a control back to its default, and so it does an
22
+ // Input: through `onValueChange`, so that a controlled one asks.
23
+ onSettled(() => {
24
+ const form = element?.form;
25
+ if (!form)
26
+ return;
27
+ const onReset = (event) => {
28
+ const text = element.value;
29
+ const defaultText = untrack(() => props.defaultValue) ?? "";
30
+ // The browser resets the element to its `value` attribute after this
31
+ // event. A controlled Input keeps its text through it, by making that
32
+ // its attribute, so that the element never shows a text its consumer
33
+ // does not hold; its consumer then decides.
34
+ if (untrack(() => props.value) !== undefined) {
35
+ element.defaultValue = text;
36
+ }
37
+ // In a task of its own, not `onFlush`: when the user presses a reset
38
+ // button, the browser runs microtasks as each listener returns,
39
+ // before later listeners could cancel the reset.
40
+ setTimeout(() => {
41
+ if (event.defaultPrevented || text === defaultText)
42
+ return;
43
+ props.onValueChange?.(defaultText, { reason: "reset", event });
44
+ onFlush(restoreValue);
45
+ });
46
+ };
47
+ form.addEventListener("reset", onReset);
48
+ return () => form.removeEventListener("reset", onReset);
49
+ });
50
+ const ownProps = {
51
+ onInput: (event) => {
52
+ props.onValueChange?.(element.value, { reason: "input-change", event });
53
+ // A write while an input method composes would end the composition.
54
+ if (!event.isComposing)
55
+ onFlush(restoreValue);
56
+ },
57
+ onCompositionEnd: () => onFlush(restoreValue),
58
+ ref: (el) => {
59
+ element = el;
60
+ },
61
+ };
62
+ const elementProps = merge(mergeElementProps(ownProps, omit(props, "render", "value", "onValueChange")),
63
+ // Solid writes `""` for a `value` of `undefined`, which would hide
64
+ // `defaultValue`: the key is there only while controlled.
65
+ () => (props.value === undefined ? {} : { value: props.value }));
66
+ return renderElement(props, elementProps, (p) => <input {...p}/>);
67
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "menhir",
3
- "version": "0.1.8",
3
+ "version": "0.1.9",
4
4
  "description": "Unstyled, accessible UI primitives for SolidJS 2.0, with small and measurable bundles",
5
5
  "keywords": [
6
6
  "solid",
@@ -79,6 +79,11 @@
79
79
  "solid": "./dist/source/field/index.js",
80
80
  "import": "./dist/field/index.js"
81
81
  },
82
+ "./input": {
83
+ "types": "./dist/source/input/index.d.ts",
84
+ "solid": "./dist/source/input/index.js",
85
+ "import": "./dist/input/index.js"
86
+ },
82
87
  "./menu": {
83
88
  "types": "./dist/source/menu/index.d.ts",
84
89
  "solid": "./dist/source/menu/index.js",