@volvel/ui 0.0.0-stage → 0.7.0

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 (137) hide show
  1. package/LICENSE +202 -0
  2. package/THIRD_PARTY_NOTICES.md +23036 -0
  3. package/dist/Badge.d.ts +20 -0
  4. package/dist/Badge.js +35 -0
  5. package/dist/Button.d.ts +56 -0
  6. package/dist/Button.js +52 -0
  7. package/dist/Chart.d.ts +23 -0
  8. package/dist/Chart.js +98 -0
  9. package/dist/Combobox.d.ts +45 -0
  10. package/dist/Combobox.js +42 -0
  11. package/dist/ContextMenu.d.ts +16 -0
  12. package/dist/ContextMenu.js +38 -0
  13. package/dist/DateInput.d.ts +31 -0
  14. package/dist/DateInput.js +329 -0
  15. package/dist/Details.d.ts +12 -0
  16. package/dist/Details.js +9 -0
  17. package/dist/Dialog.d.ts +44 -0
  18. package/dist/Dialog.js +95 -0
  19. package/dist/Dialogs.d.ts +56 -0
  20. package/dist/Dialogs.js +95 -0
  21. package/dist/Doc.d.ts +25 -0
  22. package/dist/Doc.js +118 -0
  23. package/dist/DocServed.d.ts +6 -0
  24. package/dist/DocServed.js +154 -0
  25. package/dist/DocZoom.d.ts +11 -0
  26. package/dist/DocZoom.js +133 -0
  27. package/dist/Facts.d.ts +12 -0
  28. package/dist/Facts.js +10 -0
  29. package/dist/Fields.d.ts +184 -0
  30. package/dist/Fields.js +249 -0
  31. package/dist/Form.d.ts +47 -0
  32. package/dist/Form.js +189 -0
  33. package/dist/Grid.d.ts +118 -0
  34. package/dist/Grid.js +475 -0
  35. package/dist/HeadingSlot.d.ts +13 -0
  36. package/dist/HeadingSlot.js +19 -0
  37. package/dist/JsonView.d.ts +11 -0
  38. package/dist/JsonView.js +68 -0
  39. package/dist/LogViewer.d.ts +45 -0
  40. package/dist/LogViewer.js +299 -0
  41. package/dist/Markdown.d.ts +21 -0
  42. package/dist/Markdown.js +27 -0
  43. package/dist/MarkdownBody.d.ts +6 -0
  44. package/dist/MarkdownBody.js +14 -0
  45. package/dist/Menu.d.ts +29 -0
  46. package/dist/Menu.js +8 -0
  47. package/dist/MoveButtons.d.ts +19 -0
  48. package/dist/MoveButtons.js +22 -0
  49. package/dist/Notice.d.ts +47 -0
  50. package/dist/Notice.js +50 -0
  51. package/dist/PageShell.d.ts +42 -0
  52. package/dist/PageShell.js +14 -0
  53. package/dist/Pagination.d.ts +13 -0
  54. package/dist/Pagination.js +9 -0
  55. package/dist/Panel.d.ts +31 -0
  56. package/dist/Panel.js +33 -0
  57. package/dist/Pickers.d.ts +37 -0
  58. package/dist/Pickers.js +44 -0
  59. package/dist/Popover.d.ts +24 -0
  60. package/dist/Popover.js +12 -0
  61. package/dist/ResultView.d.ts +28 -0
  62. package/dist/ResultView.js +140 -0
  63. package/dist/SchemaForm.d.ts +45 -0
  64. package/dist/SchemaForm.js +626 -0
  65. package/dist/SideSheet.d.ts +31 -0
  66. package/dist/SideSheet.js +136 -0
  67. package/dist/Sparkline.d.ts +14 -0
  68. package/dist/Sparkline.js +23 -0
  69. package/dist/Surfaces.d.ts +30 -0
  70. package/dist/Surfaces.js +34 -0
  71. package/dist/Switch.d.ts +18 -0
  72. package/dist/Switch.js +11 -0
  73. package/dist/Table.d.ts +15 -0
  74. package/dist/Table.js +6 -0
  75. package/dist/Tabs.d.ts +32 -0
  76. package/dist/Tabs.js +11 -0
  77. package/dist/Term.d.ts +82 -0
  78. package/dist/Term.js +153 -0
  79. package/dist/Timeline.d.ts +29 -0
  80. package/dist/Timeline.js +39 -0
  81. package/dist/Toast.d.ts +43 -0
  82. package/dist/Toast.js +140 -0
  83. package/dist/Tooltip.d.ts +34 -0
  84. package/dist/Tooltip.js +70 -0
  85. package/dist/Tree.d.ts +51 -0
  86. package/dist/Tree.js +233 -0
  87. package/dist/chartEngine.d.ts +32 -0
  88. package/dist/chartEngine.js +72 -0
  89. package/dist/chartTheme.d.ts +41 -0
  90. package/dist/chartTheme.js +131 -0
  91. package/dist/contrast.d.ts +27 -0
  92. package/dist/contrast.js +110 -0
  93. package/dist/dates.d.ts +61 -0
  94. package/dist/dates.js +294 -0
  95. package/dist/drawer/Chips.d.ts +37 -0
  96. package/dist/drawer/Chips.js +32 -0
  97. package/dist/drawer/Choices.d.ts +47 -0
  98. package/dist/drawer/Choices.js +81 -0
  99. package/dist/drawer/Drawer.d.ts +125 -0
  100. package/dist/drawer/Drawer.js +150 -0
  101. package/dist/drawer/EditorBodies.d.ts +95 -0
  102. package/dist/drawer/EditorBodies.js +169 -0
  103. package/dist/drawer/NumberFormat.d.ts +36 -0
  104. package/dist/drawer/NumberFormat.js +98 -0
  105. package/dist/drawer/SortableRows.d.ts +35 -0
  106. package/dist/drawer/SortableRows.js +23 -0
  107. package/dist/drawer/edgeShadows.d.ts +7 -0
  108. package/dist/drawer/edgeShadows.js +17 -0
  109. package/dist/drawer/index.d.ts +8 -0
  110. package/dist/drawer/index.js +8 -0
  111. package/dist/drawer/useSortable.d.ts +53 -0
  112. package/dist/drawer/useSortable.js +323 -0
  113. package/dist/format.d.ts +6 -0
  114. package/dist/format.js +17 -0
  115. package/dist/icons.d.ts +20 -0
  116. package/dist/icons.js +16 -0
  117. package/dist/index.d.ts +48 -0
  118. package/dist/index.js +48 -0
  119. package/dist/markdownKnowledge.d.ts +45 -0
  120. package/dist/markdownKnowledge.js +37 -0
  121. package/dist/overlay.d.ts +7 -0
  122. package/dist/overlay.js +10 -0
  123. package/dist/pageIcons.d.ts +12 -0
  124. package/dist/pageIcons.js +32 -0
  125. package/dist/presence.d.ts +41 -0
  126. package/dist/presence.js +108 -0
  127. package/dist/returnFocus.d.ts +9 -0
  128. package/dist/returnFocus.js +44 -0
  129. package/dist/tokens.css +437 -0
  130. package/dist/type-scale.css +34 -0
  131. package/dist/useFlip.d.ts +32 -0
  132. package/dist/useFlip.js +118 -0
  133. package/fonts/OFL.txt +193 -0
  134. package/fonts/geist-latin.woff2 +0 -0
  135. package/fonts/geist-mono-latin.woff2 +0 -0
  136. package/package.json +52 -3
  137. package/README.md +0 -3
@@ -0,0 +1,184 @@
1
+ import type { KeyboardEvent, ReactNode } from "react";
2
+ /** A control's own box. An invalid one has a danger edge, beside the words that say why. */
3
+ export declare const control = "h-control w-full rounded-control border border-line-control bg-field px-3 text-ink placeholder:text-ink-muted/70 disabled:opacity-60 data-[invalid]:border-danger aria-[invalid=true]:border-danger";
4
+ interface Common {
5
+ label: string;
6
+ /** Shown under the control. Says what is expected, not what went wrong. It stays beside an error. */
7
+ hint?: string;
8
+ error?: string | undefined;
9
+ /**
10
+ * For a field in a toolbar, beside others whose purpose its placeholder or
11
+ * value makes plain: the label is read, not shown (P4-1).
12
+ */
13
+ labelHidden?: boolean;
14
+ /**
15
+ * The label sits inside the box, before the value, as in a filter bar:
16
+ * "Clinic All". The hint and the error stay under it.
17
+ */
18
+ inline?: boolean;
19
+ }
20
+ export declare const FieldMarks: import("react").Provider<"both" | "required">;
21
+ /**
22
+ * Puts something beside the label of the field inside it: the ⓘ that says
23
+ * more about the field. It sits next to the label and not in it, since a
24
+ * label may hold only the control it names.
25
+ */
26
+ export declare function FieldHelp({ help, children }: {
27
+ help: ReactNode;
28
+ children: ReactNode;
29
+ }): import("react").JSX.Element;
30
+ /**
31
+ * A field's label, its control, what is expected and what went wrong (08 §3.30).
32
+ * The error and the hint are tied to the control by `aria-describedby`, so a
33
+ * screen reader reads them on reaching it. A required field is marked; one
34
+ * marked `required={false}` says it is optional. The marks are for the eye: the
35
+ * control's `required` is what assistive technology announces, so a field's
36
+ * name stays its label.
37
+ */
38
+ export declare function FieldFrame({ label, hint, error, required, labelHidden, children, }: Common & {
39
+ required?: boolean | undefined;
40
+ children: ReactNode;
41
+ }): import("react").JSX.Element;
42
+ /** The count and the refusal of a limit, for a box that is not a `TextField`: said under or beside it. */
43
+ export declare function LimitNote({ value, max, always, className, }: {
44
+ value: string;
45
+ max: number;
46
+ always?: boolean;
47
+ className?: string;
48
+ }): import("react").JSX.Element | null;
49
+ /** Whether a text is past its limit: what a save of it refuses. */
50
+ export declare const pastLimit: (value: string, max: number) => boolean;
51
+ export interface TextFieldProps extends Common {
52
+ name: string;
53
+ type?: "text" | "email" | "password" | "number" | "date";
54
+ value?: string;
55
+ placeholder?: string;
56
+ required?: boolean;
57
+ minLength?: number;
58
+ maxLength?: number;
59
+ /** With a `maxLength`: says how many letters are typed of how many, under the box. */
60
+ showCount?: boolean;
61
+ autoComplete?: string;
62
+ disabled?: boolean;
63
+ /** Shown and selectable, not editable. */
64
+ readOnly?: boolean;
65
+ onChange?: (value: string) => void;
66
+ /** For a field whose Enter does something of its own, such as adding what is typed to a list. */
67
+ onKeyDown?: (event: KeyboardEvent<HTMLInputElement>) => void;
68
+ }
69
+ export declare function TextField({ label, hint, error, onChange, labelHidden, inline, showCount, maxLength, ...input }: TextFieldProps): import("react").JSX.Element;
70
+ export interface SelectFieldProps extends Common {
71
+ name: string;
72
+ value?: string;
73
+ required?: boolean;
74
+ disabled?: boolean;
75
+ options: {
76
+ value: string;
77
+ label: string;
78
+ }[];
79
+ onChange?: (value: string) => void;
80
+ /** Drawn inside the box before the value: the table's icon, as the boards' selects have it. */
81
+ icon?: ReactNode;
82
+ }
83
+ /** A native select: it works with every keyboard, screen reader and phone as it is. */
84
+ export declare function SelectField({ label, hint, error, options, onChange, labelHidden, inline, icon, ...select }: SelectFieldProps): import("react").JSX.Element;
85
+ export interface TextAreaFieldProps extends Common {
86
+ name: string;
87
+ value?: string;
88
+ /** The fewest lines it shows. It grows from there with what it holds. */
89
+ rows?: number;
90
+ /** The most lines it grows to before its text scrolls. */
91
+ maxRows?: number;
92
+ placeholder?: string;
93
+ required?: boolean;
94
+ minLength?: number;
95
+ maxLength?: number;
96
+ disabled?: boolean;
97
+ onChange?: (value: string) => void;
98
+ /** The field was left. */
99
+ onBlur?: () => void;
100
+ }
101
+ /** How tall a text area grows by itself, in lines. */
102
+ export declare const TEXT_AREA_MAX_ROWS = 12;
103
+ /** Text that runs over several lines. */
104
+ export declare function TextAreaField({ label, hint, error, rows, maxRows, onChange, maxLength, ...area }: TextAreaFieldProps): import("react").JSX.Element;
105
+ export interface NumberFieldProps extends Common {
106
+ name: string;
107
+ /** The number in the box. Undefined for an empty box. Leave it out to let the box keep its own. */
108
+ value?: number | undefined;
109
+ min?: number;
110
+ max?: number;
111
+ /** The size of one step. Use 1 for whole numbers. */
112
+ step?: number;
113
+ placeholder?: string;
114
+ required?: boolean;
115
+ disabled?: boolean;
116
+ /** Called with the number typed, or undefined when the box holds no number. */
117
+ onChange?: (value: number | undefined) => void;
118
+ }
119
+ export declare function NumberField(props: NumberFieldProps): import("react").JSX.Element;
120
+ export interface MoneyFieldProps extends Common {
121
+ name: string;
122
+ /** The amount as the column keeps it: cents, or whole units when `minorUnits` is false. */
123
+ value?: number | undefined;
124
+ /** An ISO 4217 code: shown beside the box, and used in the words of a problem. Default USD. */
125
+ currency?: string;
126
+ /** The column keeps cents (default). `false`: whole units. */
127
+ minorUnits?: boolean;
128
+ placeholder?: string;
129
+ required?: boolean;
130
+ disabled?: boolean;
131
+ /** Called with the amount as kept (exact, no float error), and the problem in words when what is typed is not an amount. */
132
+ onChange?: (value: number | undefined, problem?: string) => void;
133
+ }
134
+ /**
135
+ * An amount of money: typed as the person writes it ("123.45", or "123,45" where a comma is
136
+ * written), with the currency beside the box. It keeps the amount as the column holds it, exactly
137
+ * (12345 for 123.45 in cents), and says in words what is wrong with text that is not an amount
138
+ * or has more than two decimals.
139
+ */
140
+ export declare function MoneyField(props: MoneyFieldProps): import("react").JSX.Element;
141
+ export interface DateFieldProps extends Common {
142
+ name: string;
143
+ /**
144
+ * `2026-03-09`, or with `withTime` a date and time: `2026-03-09T14:30` in the
145
+ * person's local time, or with `timeZone: "UTC"` an instant such as
146
+ * `2026-03-09T14:30:00.000Z`.
147
+ */
148
+ value?: string;
149
+ /** Asks for a time of day as well. */
150
+ withTime?: boolean;
151
+ /**
152
+ * Which clock a time is read and written in. `local`, the default, is the
153
+ * browser's. With `UTC` the field shows UTC times and `value`, `min`, `max`
154
+ * and `onChange` are ISO instants ending in `Z`, so what is stored never
155
+ * depends on where the person is.
156
+ */
157
+ timeZone?: "local" | "UTC";
158
+ /** The earliest and latest values allowed, in the same form as `value`. */
159
+ min?: string;
160
+ max?: string;
161
+ required?: boolean;
162
+ disabled?: boolean;
163
+ onChange?: (value: string) => void;
164
+ }
165
+ /** An ISO instant as a UTC wall-clock time for a `datetime-local` input. */
166
+ export declare function utcInputValue(instant: string | undefined): string | undefined;
167
+ /** A `datetime-local` input's value, read as UTC, as an ISO instant. */
168
+ export declare function utcInstant(wallClock: string): string;
169
+ /**
170
+ * A date, or a date and a time, in the kit's own box: the person's date as words ("Nov 21, 2026"),
171
+ * typed or picked from a calendar (`DateInput`). On a touch screen the button opens the phone's
172
+ * own picker. The value in and out is the string described on `DateFieldProps`.
173
+ */
174
+ export declare function DateField({ label, hint, error, withTime, timeZone, onChange, labelHidden, ...input }: DateFieldProps): import("react").JSX.Element;
175
+ export interface CheckboxFieldProps extends Common {
176
+ name: string;
177
+ checked?: boolean;
178
+ required?: boolean;
179
+ disabled?: boolean;
180
+ onChange?: (checked: boolean) => void;
181
+ }
182
+ /** A yes-or-no choice. The label sits beside the box and can be pressed too. */
183
+ export declare function CheckboxField({ label, hint, error, onChange, ...input }: CheckboxFieldProps): import("react").JSX.Element;
184
+ export {};
package/dist/Fields.js ADDED
@@ -0,0 +1,249 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { Field } from "@base-ui/react/field";
3
+ import { moneyText, parseMoney } from "@volvel/widgets";
4
+ import { ChevronDown } from "lucide-react";
5
+ import { createContext, useCallback, useContext, useId, useLayoutEffect, useRef, useState, } from "react";
6
+ import { DateInput } from "./DateInput.js";
7
+ import { TermText } from "./Term.js";
8
+ /** A control's own box. An invalid one has a danger edge, beside the words that say why. */
9
+ export const control = "h-control w-full rounded-control border border-line-control bg-field px-3 text-ink placeholder:text-ink-muted/70 disabled:opacity-60 data-[invalid]:border-danger aria-[invalid=true]:border-danger";
10
+ /** A select's own box: the native control, drawn flat so the chevron beside it is the kit's. */
11
+ const selectControl = `${control} appearance-none pr-8`;
12
+ /** The chevron a select shows at its end. It takes no pointer: the select under it does. */
13
+ function Chevron() {
14
+ return (_jsx(ChevronDown, { "aria-hidden": "true", size: 14, className: "pointer-events-none absolute top-1/2 right-3 -translate-y-1/2 text-ink-muted" }));
15
+ }
16
+ /**
17
+ * A field whose label sits inside its box, before the value: "Clinic All".
18
+ * For a filter bar and other rows of small controls. The label is a real
19
+ * label, so the control keeps its name; the box takes the focus ring.
20
+ */
21
+ function InlineFrame({ label, hint, error, children, chevron = false, }) {
22
+ return (_jsxs(Field.Root, { invalid: error !== undefined, className: "flex flex-col gap-1", children: [_jsxs("div", { className: "relative flex h-control items-center gap-2 rounded-control border border-line-control bg-field px-3 text-body-sm has-[:focus-visible]:outline-2 has-[:focus-visible]:outline-offset-2 has-[:focus-visible]:outline-focus has-[[data-invalid]]:border-danger", children: [_jsx(Field.Label, { className: "shrink-0 font-normal text-ink-muted", children: label }), children, chevron ? _jsx(Chevron, {}) : null] }), hint ? (_jsx(Field.Description, { className: "m-0 text-body-sm text-ink-muted", children: _jsx(TermText, { children: hint }) })) : null, error ? (_jsx(Field.Error, { match: true, role: "alert", className: "m-0 text-body-sm text-danger", children: error })) : null] }));
23
+ }
24
+ /** What a control inside an `InlineFrame` is drawn with: no box of its own. */
25
+ const bare = "h-full min-w-0 flex-1 appearance-none bg-transparent text-ink outline-none! placeholder:text-ink-muted/70 focus-visible:outline-none! disabled:opacity-60";
26
+ const Beside = createContext(null);
27
+ /**
28
+ * Which marks a field's label carries. The console marks both ("optional" on what may be left
29
+ * out, as the pattern library has it). A form made for the people who use an app marks only what
30
+ * is required, with an asterisk, as board 5 draws it.
31
+ */
32
+ const Marks = createContext("both");
33
+ export const FieldMarks = Marks.Provider;
34
+ /**
35
+ * Puts something beside the label of the field inside it: the ⓘ that says
36
+ * more about the field. It sits next to the label and not in it, since a
37
+ * label may hold only the control it names.
38
+ */
39
+ export function FieldHelp({ help, children }) {
40
+ return _jsx(Beside.Provider, { value: help, children: children });
41
+ }
42
+ /**
43
+ * A field's label, its control, what is expected and what went wrong (08 §3.30).
44
+ * The error and the hint are tied to the control by `aria-describedby`, so a
45
+ * screen reader reads them on reaching it. A required field is marked; one
46
+ * marked `required={false}` says it is optional. The marks are for the eye: the
47
+ * control's `required` is what assistive technology announces, so a field's
48
+ * name stays its label.
49
+ */
50
+ export function FieldFrame({ label, hint, error, required, labelHidden = false, children, }) {
51
+ const beside = useContext(Beside);
52
+ const marks = useContext(Marks);
53
+ // A label that says "(optional)" in its words is not marked again: the console showed both.
54
+ const says = typeof label === "string" && /\(optional\)/i.test(label);
55
+ return (_jsxs(Field.Root, { invalid: error !== undefined, className: "flex flex-col gap-1", children: [_jsxs("div", { className: labelHidden ? "sr-only" : "flex items-baseline gap-1 text-body-sm", children: [_jsx(Field.Label, { className: "font-medium text-ink", children: label }), beside && !labelHidden ? (_jsx(Beside.Provider, { value: null, children: _jsx("span", { className: "self-center", children: beside }) })) : null, required === true ? (_jsx("span", { "aria-hidden": "true", className: "text-danger", children: "*" })) : required === false && marks === "both" && !says ? (_jsx("span", { "aria-hidden": "true", className: "text-ink-subtle", children: "optional" })) : null] }), children, hint ? (_jsx(Field.Description, { className: "m-0 text-body-sm text-ink-muted", children: _jsx(TermText, { children: hint }) })) : null, error ? (_jsx(Field.Error, { match: true, role: "alert", className: "m-0 text-body-sm text-danger", children: error })) : null] }));
56
+ }
57
+ /**
58
+ * A limit on length is said at the field and refused, never cut: the box takes what is typed or
59
+ * pasted, the count shows near the limit, and past it the field says how far over it is. The
60
+ * native `maxLength` is not set, since it drops the rest of a paste without a word.
61
+ */
62
+ function lengthLimit(value, max, always) {
63
+ if (max === undefined)
64
+ return { error: undefined, count: undefined };
65
+ const length = (value ?? "").length;
66
+ const over = length > max;
67
+ return {
68
+ error: over ? `Use at most ${max} characters. This is ${length}.` : undefined,
69
+ count: always || over || length >= max * 0.8 ? `${length} of ${max}` : undefined,
70
+ };
71
+ }
72
+ /** The count and the refusal of a limit, for a box that is not a `TextField`: said under or beside it. */
73
+ export function LimitNote({ value, max, always = false, className = "", }) {
74
+ const limit = lengthLimit(value, max, always);
75
+ if (!limit.count)
76
+ return null;
77
+ return (_jsx("span", { "data-field-count": "", ...(limit.error ? { role: "alert" } : {}), className: `${countClass} ${limit.error ? "text-danger" : ""} ${className}`, children: limit.error ?? limit.count }));
78
+ }
79
+ /** Whether a text is past its limit: what a save of it refuses. */
80
+ export const pastLimit = (value, max) => value.length > max;
81
+ const countClass = "text-right text-meta text-ink-muted tabular-nums";
82
+ export function TextField({ label, hint, error, onChange, labelHidden = false, inline = false, showCount = false, maxLength, ...input }) {
83
+ const limit = lengthLimit(input.value, maxLength, showCount);
84
+ error = error ?? limit.error;
85
+ if (labelHidden && input.placeholder === undefined)
86
+ input.placeholder = label;
87
+ if (inline) {
88
+ return (_jsx(InlineFrame, { label: label, hint: hint, error: error, children: _jsx(Field.Control, { ...input, className: bare, onChange: (event) => onChange?.(event.currentTarget.value) }) }));
89
+ }
90
+ return (_jsxs(FieldFrame, { label: label, ...(hint ? { hint } : {}), error: error, required: input.required, labelHidden: labelHidden, children: [_jsx(Field.Control, { ...input, className: control, onChange: (event) => onChange?.(event.currentTarget.value) }), limit.count ? (_jsx("span", { "data-field-count": "", className: countClass, children: limit.count })) : null] }));
91
+ }
92
+ /** A native select: it works with every keyboard, screen reader and phone as it is. */
93
+ export function SelectField({ label, hint, error, options, onChange, labelHidden = false, inline = false, icon, ...select }) {
94
+ const choices = options.map((option) => (_jsx("option", { value: option.value, children: option.label }, option.value)));
95
+ if (inline) {
96
+ return (_jsx(InlineFrame, { label: label, hint: hint, error: error, chevron: true, children: _jsx(Field.Control, { render: _jsx("select", { ...select, className: `${bare} pr-6`, onChange: (event) => onChange?.(event.currentTarget.value), children: choices }) }) }));
97
+ }
98
+ return (_jsx(FieldFrame, { label: label, ...(hint ? { hint } : {}), error: error, required: select.required, labelHidden: labelHidden, children: _jsxs("div", { className: "relative", children: [icon ? (_jsx("span", { "aria-hidden": "true", className: "pointer-events-none absolute top-1/2 left-3 flex -translate-y-1/2 text-ink-muted", children: icon })) : null, _jsx(Field.Control, { render: _jsx("select", { ...select, className: icon ? `${selectControl} pl-10` : selectControl, onChange: (event) => onChange?.(event.currentTarget.value), children: choices }) }), _jsx(Chevron, {})] }) }));
99
+ }
100
+ /** How tall a text area grows by itself, in lines. */
101
+ export const TEXT_AREA_MAX_ROWS = 12;
102
+ /**
103
+ * Makes a text area as tall as its text, between its `rows` and `maxRows`
104
+ * lines. The handle in its corner still drags it to any height, and a field
105
+ * its reader has dragged is left at the height they gave it.
106
+ */
107
+ function useGrowing(value, maxRows) {
108
+ const area = useRef(null);
109
+ // The height last given here. Any other height is one the reader dragged it to.
110
+ const given = useRef(undefined);
111
+ const fit = useCallback(() => {
112
+ const node = area.current;
113
+ if (!node)
114
+ return;
115
+ if (given.current !== undefined && Math.abs(node.offsetHeight - given.current) > 1)
116
+ return;
117
+ node.style.height = "auto";
118
+ const style = getComputedStyle(node);
119
+ const line = Number.parseFloat(style.lineHeight);
120
+ // Nothing is laid out, as in a test with no browser: leave it to its rows.
121
+ if (node.scrollHeight === 0 || Number.isNaN(line))
122
+ return;
123
+ const edges = node.offsetHeight - node.clientHeight;
124
+ const padding = Number.parseFloat(style.paddingTop) + Number.parseFloat(style.paddingBottom);
125
+ const most = line * maxRows + padding;
126
+ const wanted = Math.min(node.scrollHeight, most);
127
+ // Never shorter than its rows, which is the height it has with none set.
128
+ const height = Math.max(wanted + edges, node.offsetHeight);
129
+ node.style.height = `${height}px`;
130
+ node.style.overflowY = node.scrollHeight > most ? "auto" : "hidden";
131
+ given.current = node.offsetHeight;
132
+ }, [maxRows]);
133
+ // biome-ignore lint/correctness/useExhaustiveDependencies: new text is the reason to measure again
134
+ useLayoutEffect(fit, [fit, value]);
135
+ return { area, fit };
136
+ }
137
+ /** Text that runs over several lines. */
138
+ export function TextAreaField({ label, hint, error, rows = 4, maxRows = TEXT_AREA_MAX_ROWS, onChange, maxLength, ...area }) {
139
+ const limit = lengthLimit(area.value, maxLength, false);
140
+ error = error ?? limit.error;
141
+ const growing = useGrowing(area.value, Math.max(rows, maxRows));
142
+ return (_jsxs(FieldFrame, { label: label, ...(hint ? { hint } : {}), error: error, required: area.required, children: [_jsx(Field.Control, { render: _jsx("textarea", { ...area, ref: growing.area, rows: rows, className: `${control} h-auto resize-y py-1.5`, onInput: growing.fit, onChange: (event) => onChange?.(event.currentTarget.value) }) }), limit.count ? (_jsx("span", { "data-field-count": "", className: countClass, children: limit.count })) : null] }));
143
+ }
144
+ /** What the box shows for a number. Nothing for no number. */
145
+ function numberText(value) {
146
+ return value === undefined ? "" : String(value);
147
+ }
148
+ /** The number typed in the box. Undefined for an empty box or text that is not a number. */
149
+ function typedNumber(text) {
150
+ if (text.trim() === "")
151
+ return undefined;
152
+ const number = Number(text);
153
+ return Number.isNaN(number) ? undefined : number;
154
+ }
155
+ export function NumberField(props) {
156
+ const { label, hint, error, value, onChange, ...input } = props;
157
+ // The typed text is kept as it is, so that "1." or "-" is not rewritten half-way through.
158
+ // It gives way only when a value is passed in that is not the number it spells.
159
+ const [text, setText] = useState(() => numberText(value));
160
+ const held = "value" in props && typedNumber(text) !== value;
161
+ return (_jsx(FieldFrame, { label: label, ...(hint ? { hint } : {}), error: error, required: input.required, children: _jsx(Field.Control, { ...input, type: "number", value: held ? numberText(value) : text, className: `${control} text-right`, onChange: (event) => {
162
+ setText(event.currentTarget.value);
163
+ onChange?.(typedNumber(event.currentTarget.value));
164
+ } }) }));
165
+ }
166
+ /** A symbol for a currency: "$", "€". The code itself where there is none. */
167
+ function symbolOf(currency) {
168
+ try {
169
+ return (new Intl.NumberFormat(undefined, {
170
+ style: "currency",
171
+ currency,
172
+ currencyDisplay: "narrowSymbol",
173
+ })
174
+ .formatToParts(0)
175
+ .find((part) => part.type === "currency")?.value ?? currency);
176
+ }
177
+ catch {
178
+ return currency;
179
+ }
180
+ }
181
+ /**
182
+ * An amount of money: typed as the person writes it ("123.45", or "123,45" where a comma is
183
+ * written), with the currency beside the box. It keeps the amount as the column holds it, exactly
184
+ * (12345 for 123.45 in cents), and says in words what is wrong with text that is not an amount
185
+ * or has more than two decimals.
186
+ */
187
+ export function MoneyField(props) {
188
+ const { label, hint, error, value, onChange, currency = "USD", minorUnits = true, ...input } = props;
189
+ const options = { minorUnits, currency };
190
+ const [text, setText] = useState(() => (value === undefined ? "" : moneyText(value, options)));
191
+ const [left, setLeft] = useState(false);
192
+ const read = parseMoney(text, options);
193
+ const typed = read.ok ? read.value : undefined;
194
+ // A value passed in that is not what the box spells replaces it (a reset, another record).
195
+ const held = "value" in props && read.ok && typed !== value;
196
+ const shown = held ? (value === undefined ? "" : moneyText(value, options)) : text;
197
+ const problem = read.ok ? undefined : read.message;
198
+ const say = error ?? (left || text.length > 0 ? problem : undefined);
199
+ return (_jsx(FieldFrame, { label: label, ...(hint ? { hint } : {}), error: say, required: input.required, children: _jsxs("div", { className: "flex items-center gap-2", children: [_jsx("span", { "aria-hidden": "true", className: "text-body-sm text-ink-muted", children: symbolOf(currency) }), _jsx(Field.Control, { ...input, type: "text", inputMode: "decimal", autoComplete: "off", value: shown, className: `${control} min-w-0 flex-1 text-right tabular-nums`, onBlur: () => setLeft(true), onChange: (event) => {
200
+ const next = event.currentTarget.value;
201
+ setText(next);
202
+ const parsed = parseMoney(next, options);
203
+ if (parsed.ok)
204
+ onChange?.(parsed.value);
205
+ else
206
+ onChange?.(undefined, parsed.message);
207
+ } }), _jsx("span", { className: "w-9 shrink-0 text-body-sm text-ink-muted", children: currency })] }) }));
208
+ }
209
+ /** An ISO instant as a UTC wall-clock time for a `datetime-local` input. */
210
+ export function utcInputValue(instant) {
211
+ if (!instant)
212
+ return instant;
213
+ const date = new Date(instant);
214
+ return Number.isNaN(date.getTime()) ? instant : date.toISOString().slice(0, 16);
215
+ }
216
+ /** A `datetime-local` input's value, read as UTC, as an ISO instant. */
217
+ export function utcInstant(wallClock) {
218
+ if (!wallClock)
219
+ return "";
220
+ const date = new Date(`${wallClock.length === 16 ? `${wallClock}:00` : wallClock}Z`);
221
+ return Number.isNaN(date.getTime()) ? "" : date.toISOString();
222
+ }
223
+ /**
224
+ * A date, or a date and a time, in the kit's own box: the person's date as words ("Nov 21, 2026"),
225
+ * typed or picked from a calendar (`DateInput`). On a touch screen the button opens the phone's
226
+ * own picker. The value in and out is the string described on `DateFieldProps`.
227
+ */
228
+ export function DateField({ label, hint, error, withTime, timeZone = "local", onChange, labelHidden = false, ...input }) {
229
+ const utc = withTime && timeZone === "UTC";
230
+ const [problem, setProblem] = useState();
231
+ const [own, setOwn] = useState("");
232
+ const controlled = input.value !== undefined;
233
+ const given = controlled ? (input.value ?? "") : own;
234
+ const wall = utc ? (utcInputValue(given) ?? "") : given;
235
+ const range = (edge) => (utc ? utcInputValue(edge) : edge);
236
+ const note = utc ? (hint ? `${hint} Times are in UTC.` : "Times are in UTC.") : hint;
237
+ return (_jsx(FieldFrame, { label: label, ...(note ? { hint: note } : {}), error: error ?? problem, required: input.required, labelHidden: labelHidden, children: _jsx(Field.Control, { render: (props) => (_jsx(DateInput, { boxProps: props, name: input.name, value: wall, formValue: utc ? utcInstant(wall) : wall, ...(withTime ? { withTime } : {}), min: range(input.min), max: range(input.max), required: input.required, disabled: input.disabled, onProblem: setProblem, onChange: (next) => {
238
+ const value = utc ? utcInstant(next) : next;
239
+ if (!controlled)
240
+ setOwn(value);
241
+ onChange?.(value);
242
+ } })) }) }));
243
+ }
244
+ /** A yes-or-no choice. The label sits beside the box and can be pressed too. */
245
+ export function CheckboxField({ label, hint, error, onChange, ...input }) {
246
+ const id = useId();
247
+ const note = error || hint ? `${id}-note` : undefined;
248
+ return (_jsxs("div", { className: "flex flex-col gap-1", children: [_jsxs("label", { className: "flex items-center gap-2 text-body-sm font-medium text-ink", children: [_jsx("input", { ...input, type: "checkbox", className: "size-4 accent-(--vx-action) disabled:opacity-60", "aria-invalid": error !== undefined, "aria-describedby": note, onChange: (event) => onChange?.(event.currentTarget.checked) }), label] }), error ? (_jsx("p", { id: note, role: "alert", className: "m-0 text-body-sm text-danger", children: error })) : hint ? (_jsx("p", { id: note, className: "m-0 text-body-sm text-ink-muted", children: hint })) : null] }));
249
+ }
package/dist/Form.d.ts ADDED
@@ -0,0 +1,47 @@
1
+ /** One field of a form: the part of JSON Schema the form understands. */
2
+ export interface FormProperty {
3
+ type: "string" | "number" | "integer" | "boolean";
4
+ /** The field's label. Without one, the label is made from the property's name. */
5
+ title?: string;
6
+ /** Shown under the field as its hint. */
7
+ description?: string;
8
+ default?: string | number | boolean;
9
+ /** The values to choose from. The field becomes a select. */
10
+ enum?: readonly (string | number)[];
11
+ /** `date`, `date-time`, `email` and `password` change the control. Any other format is plain text. */
12
+ format?: string;
13
+ minLength?: number;
14
+ maxLength?: number;
15
+ minimum?: number;
16
+ maximum?: number;
17
+ }
18
+ /** A JSON Schema for a flat object. The fields are shown in the order of `properties`. */
19
+ export interface FormSchema {
20
+ type: "object";
21
+ title?: string;
22
+ description?: string;
23
+ properties: Record<string, FormProperty>;
24
+ required?: readonly string[];
25
+ }
26
+ /**
27
+ * Checks what was typed against the schema and turns it into values of the
28
+ * schema's types. An optional field left empty is left out of the values.
29
+ * Returns a message for each field that does not pass.
30
+ */
31
+ export declare function checkForm(schema: FormSchema, draft: Record<string, string | number | boolean | undefined>): {
32
+ values: Record<string, unknown>;
33
+ errors: Record<string, string>;
34
+ };
35
+ export interface FormProps {
36
+ schema: FormSchema;
37
+ /** What the fields start with. Read once: give the form a new `key` to start again. */
38
+ value?: Record<string, unknown>;
39
+ /** Called with the checked values. While its promise is pending the button is disabled. */
40
+ onSubmit: (values: Record<string, unknown>) => void | Promise<void>;
41
+ submitLabel: string;
42
+ /** Messages from the server, by field name. One the form found itself is shown first. */
43
+ errors?: Record<string, string>;
44
+ disabled?: boolean;
45
+ }
46
+ /** A form drawn from a JSON Schema. It checks what was typed before it submits. */
47
+ export declare function Form({ schema, value, onSubmit, submitLabel, errors, disabled }: FormProps): import("react").JSX.Element;