@propellerads/input 0.1.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.
package/README.md ADDED
@@ -0,0 +1,204 @@
1
+ # Input
2
+
3
+ One field for text, passwords and numbers, and a textarea in the same box. Built the way
4
+ [Button](../Button/README.md) is: one stylesheet, the behaviour from
5
+ [Base UI](https://base-ui.com/react/components/input), every value from
6
+ [@propellerads/tokens](../Tokens/README.md). The parts follow
7
+ [shadcn's Input Group](https://ui.shadcn.com/docs/components/input-group).
8
+
9
+ It replaces `@propellerads/input-text`, `@propellerads/input-password`,
10
+ `@propellerads/input-number` and `@propellerads/textarea`.
11
+
12
+ ![](https://img.shields.io/npm/v/@propellerads/input.svg?style=flat-square)
13
+
14
+ [NPM](https://www.npmjs.com/package/@propellerads/input) |
15
+ [Github](https://github.com/propellerads/ui-components/tree/master/Components/Input)
16
+
17
+ ## Installation
18
+
19
+ ```sh
20
+ bun add @propellerads/input
21
+ ```
22
+
23
+ The built entry imports its own stylesheet. The tokens are the app's, the same
24
+ two files the button reads:
25
+
26
+ ```ts
27
+ import '@propellerads/tokens/common.css';
28
+ import '@propellerads/tokens/propellerads.css'; // or monetag / propush / zeydoo
29
+ ```
30
+
31
+ ## Use
32
+
33
+ ```tsx
34
+ import Input, {Field, FieldDescription, FieldLabel} from '@propellerads/input';
35
+
36
+ <Field isInvalid={errors.length > 0}>
37
+ <FieldLabel isRequired>Email</FieldLabel>
38
+ <Input type="email" value={email} onChange={(event) => setEmail(event.target.value)} errors={errors} required />
39
+ <FieldDescription>We'll send updates to this address.</FieldDescription>
40
+ </Field>
41
+
42
+ <Input type="password" autoComplete="current-password" aria-label="Password" />
43
+ <Input type="number" step={0.001} min={0} startAddon="$" aria-label="Bid" />
44
+ <Input tone="warning" errors={['The bid is 8× the market average']} aria-label="Bid" />
45
+ <Input characterLimit={24} aria-label="Title" />
46
+ ```
47
+
48
+ `type` picks the field:
49
+
50
+ - `password` gets the show-password button. It says its state with
51
+ `aria-pressed`.
52
+ - `number` stays the native number input. The value is the string the browser
53
+ holds, so a value on its way to `1,5` is not turned into a number half-typed.
54
+
55
+ The field is 36px tall with `--radius-8` corners, `--font-text` and the button's
56
+ focus ring, so an input and a button stand in one row without adjustment.
57
+
58
+ ## Textarea
59
+
60
+ ```tsx
61
+ import {Field, FieldLabel, Textarea} from '@propellerads/input';
62
+
63
+ <Field>
64
+ <FieldLabel>Message</FieldLabel>
65
+ <Textarea placeholder="Type your message here." characterLimit={500} />
66
+ </Field>
67
+ ```
68
+
69
+ The same box as `Input` — contour, focus ring, tone, `errors`,
70
+ `characterLimit`, `isDisabled` — on `--font-paragraph`. `rows` sets the height
71
+ it starts at (3 by default). The field grows with the text where the browser can
72
+ size a field by its content (`field-sizing`); in Safari, which cannot yet, it
73
+ keeps its height and the corner handle resizes it. The `!` mark sits in the top
74
+ end corner, beside the first line; the counter in the bottom one, beside the
75
+ typing.
76
+
77
+ ## Errors
78
+
79
+ A non-empty `errors` list draws the contour in the tone, sets `aria-invalid`,
80
+ and puts a `!` mark at the end of the field. The mark grows in from 80% as the
81
+ error appears.
82
+
83
+ The mark is [`@propellerads/error-label`](../ErrorLabel/README.md), the same
84
+ one every other field in the library uses. The list behind it is Base UI's
85
+ Popover:
86
+
87
+ - It opens on hover and on click, so it works under a finger as well as a
88
+ pointer.
89
+ - It appears the shadcn way — out of nothing, from 95%, and two pixels from the
90
+ side it grows from — in `--duration-200`, and leaves in `--duration-100`.
91
+ - It stands above the mark and flips below it at the edge of the screen.
92
+ - It closes on Escape and on a click outside.
93
+
94
+ The same text stays in the field for a screen reader: the input points at it
95
+ with `aria-describedby`. `showErrors={false}` hides the mark and keeps the
96
+ contour and the description.
97
+
98
+ `tone="warning"` is for a value that is allowed but worth a second look.
99
+
100
+ ## Field
101
+
102
+ The parts follow [shadcn's Field](https://ui.shadcn.com/docs/components/base/input),
103
+ on Base UI's. A `Field` wires itself: the label points at the input, and the
104
+ input is described by the description and by its errors. No ids by hand.
105
+
106
+ | Part | What it does |
107
+ |--------------------|-------------------------------------------------------------------------------|
108
+ | `Field` | Label, input and description, `--space-8` apart. `orientation="horizontal"` puts them in a row — an input and its button. `isDisabled` switches the input off and fades the words; `isInvalid` turns the contour and the label red and sets `aria-invalid` |
109
+ | `FieldLabel` | `isRequired` adds the asterisk. A badge inside stands beside the text. Outside a `Field`, pass `htmlFor` |
110
+ | `FieldDescription` | Quiet text under the input, `--font-description` |
111
+ | `FieldGroup` | The fields of one form, `--space-24` apart. `columns={2}` makes a grid; below 481px it goes back to one column |
112
+
113
+ `type="file"` keeps the field's box; the browser's button inside it reads as
114
+ bold text on the left.
115
+
116
+ ## Composing
117
+
118
+ For an icon, a unit or a button inside the box, compose the parts:
119
+
120
+ ```tsx
121
+ import {InputGroup, InputGroupAddon, InputGroupButton, InputGroupInput, InputGroupText} from '@propellerads/input';
122
+
123
+ <InputGroup>
124
+ <InputGroupAddon align="inline-start"><SearchIcon /></InputGroupAddon>
125
+ <InputGroupInput placeholder="Search zones" aria-label="Search zones" />
126
+ <InputGroupAddon>
127
+ <InputGroupButton aria-label="Clear" onClick={clear}><CloseIcon /></InputGroupButton>
128
+ </InputGroupAddon>
129
+ </InputGroup>
130
+
131
+ <InputGroup>
132
+ <InputGroupInput type="number" aria-label="Daily budget" />
133
+ <InputGroupAddon><InputGroupText>USD / day</InputGroupText></InputGroupAddon>
134
+ </InputGroup>
135
+ ```
136
+
137
+ `InputGroupTextarea` is the textarea for a group composed by hand. For the `!`
138
+ mark in such a group, use `ErrorLabel` from `@propellerads/error-label`.
139
+
140
+ ## Props
141
+
142
+ `Input` takes every `<input>` prop except `disabled` and `size`. `ref` reaches
143
+ the `<input>`. `className` and `style` land on the `<input>`; `groupClassName`
144
+ and `groupStyle` on the box around it.
145
+
146
+ | Prop | Type | Default | What it does |
147
+ |-----------------------|---------------------------|-------------------|----------------------------------------------------------------|
148
+ | `type` | `string` | `'text'` | `password` adds the eye, `number` is the native number field |
149
+ | `errors` | `readonly string[]` | `[]` | The list behind the `!` mark |
150
+ | `tone` | `'danger' \| 'warning'` | `'danger'` | The colour of the contour, the mark and the list |
151
+ | `showErrors` | `boolean` | `true` | `false` hides the mark and keeps the contour |
152
+ | `onErrorsClick` | `() => void` | — | Called when the mark is clicked |
153
+ | `characterLimit` | `number` | — | Shows how many characters are left. Typing past it is allowed |
154
+ | `countCharacters` | `(value) => number` | trimmed length | Counts for `characterLimit` — emoji, for one |
155
+ | `isDisabled` | `boolean` | `false` | Switches the field off. The native `disabled` is not accepted |
156
+ | `startAddon` | `ReactNode` | — | Before the text |
157
+ | `endAddon` | `ReactNode` | — | After the text, before the counter and the mark |
158
+ | `passwordToggleLabel` | `string` | `'Show password'` | Names the eye button |
159
+
160
+ ## Moving from the old packages
161
+
162
+ | Old | New |
163
+ |-----------------------------------------|------------------------------------------------------------|
164
+ | `elementId` | `id` |
165
+ | `label`, `isRequired` | `<Field><FieldLabel isRequired>…</FieldLabel><Input /></Field>` |
166
+ | `onChange(value, event)` | `onChange(event)` — `event.target.value` |
167
+ | `errors`, `showErrorLabel` | `errors`, `showErrors` |
168
+ | `onErrorClick` | `onErrorsClick` |
169
+ | `maxLength` (the counter) | `characterLimit` — the native `maxLength` stops typing |
170
+ | `onCalculateLength` | `countCharacters` |
171
+ | `inputRef` | `ref` |
172
+ | `customStyles` | `className`, or the `--input-placeholder-*` properties |
173
+ | `stepAmount`, `minAmount` | `step`, `min` |
174
+ | `errorColor` | `tone="warning"` |
175
+ | `InputNumber` disabled by default | Enabled by default |
176
+
177
+ ## Tokens
178
+
179
+ | What | Token |
180
+ |----------------------|-------------------------------------------------------------|
181
+ | Height | 36px, the button's |
182
+ | Contour | `--color-rich`, hover `--color-quiet` |
183
+ | Invalid contour | `--color-danger` / `--color-warning` |
184
+ | Focus ring | `--color-action`, 2px, offset 1px — the button's |
185
+ | Text, placeholder | `--font-text`, `--color-dark-inversion`, `--color-quiet-text` |
186
+ | Text padding | `--space-12` |
187
+ | Addons | `--color-quiet-text`, `--space-8` from the edge |
188
+ | Counter | `--font-description`, over the limit `--color-danger-text` |
189
+ | Error list | `--color-danger` pad, `--color-light` text, `--shadow-middle` |
190
+ | Motion | `--duration-200` in, `--duration-100` out |
191
+
192
+ Override points, on any ancestor:
193
+
194
+ ```css
195
+ .sign-in .input-group {
196
+ --input-height: 40px;
197
+ --input-placeholder-color: var(--color-rich);
198
+ --input-placeholder-font-weight: 700;
199
+ --input-placeholder-text-align: center;
200
+ }
201
+ ```
202
+
203
+ The list of errors is ErrorLabel's: `--error-label-z-index` is 3000 by default,
204
+ the layer of Dialog, so inside a dialog the list draws on top of it.
package/dist/index.css ADDED
@@ -0,0 +1 @@
1
+ .input-group{--input-height:36px;--input-contour:var(--color-rich);--input-ring:var(--color-action);--input-accent:var(--color-danger);--input-accent-on:var(--color-light);box-sizing:border-box;border-radius:var(--radius-8);background-color:var(--color-light-inversion);width:100%;min-width:0;box-shadow:inset 0 0 0 var(--space-1) var(--input-contour);color:var(--color-dark-inversion);outline:var(--space-2) solid transparent;outline-offset:var(--space-1);font:var(--font-text);font-family:var(--font-family);transition:box-shadow var(--duration-200) var(--ease-flat), outline-color var(--duration-200) var(--ease-flat);align-items:center;display:flex;position:relative}.input-group[data-tone=warning]{--input-accent:var(--color-warning);--input-accent-on:var(--color-dark)}.input-group:hover{--input-contour:var(--color-quiet)}.input-group[data-invalid]{--input-contour:var(--input-accent);--input-ring:var(--input-accent)}.input-group:has(.input-group__control[aria-invalid=true]){--input-contour:var(--input-accent);--input-ring:var(--input-accent)}.input-group:has(.input-group__control:focus-visible){outline-color:var(--input-ring)}.input-group:has(.input-group__control:disabled){--input-contour:var(--color-rich);opacity:var(--opacity-disabled);cursor:not-allowed}.input-group__control{box-sizing:border-box;width:100%;min-width:0;height:var(--input-height);margin:var(--space-0);padding:var(--space-0) var(--space-12);border-radius:inherit;color:inherit;font:inherit;background:0 0;border:0;outline:none;flex:1}.input-group__control:disabled{cursor:not-allowed}.input-group__control::placeholder{color:var(--input-placeholder-color,var(--color-quiet-text));font-size:var(--input-placeholder-font-size,inherit);font-weight:var(--input-placeholder-font-weight,inherit);text-align:var(--input-placeholder-text-align,inherit);opacity:1}.input-group__addon{align-items:center;gap:var(--space-4);color:var(--color-quiet-text);flex:none;display:flex}.input-group__addon--inline-start{order:-1;padding-inline-start:var(--space-12)}.input-group__addon--inline-start+.input-group__control,.input-group__addon--inline-start~.input-group__control{padding-inline-start:var(--space-8)}.input-group__addon--inline-end{padding-inline-end:var(--space-8)}.input-group__control:has(+.input-group__addon--inline-end){padding-inline-end:var(--space-4)}.input-group .error-label{margin-inline-end:var(--space-0)}.input-group__addon svg{flex:none}.input-group__text{white-space:nowrap}.input-group__counter{font:var(--font-description);font-family:var(--font-family);font-variant-numeric:tabular-nums}.input-group__counter[data-over]{color:var(--color-danger-text)}.input-group__button{box-sizing:border-box;width:24px;height:24px;padding:var(--space-0);border-radius:var(--radius-4);color:var(--color-quiet-text);outline:var(--space-2) solid transparent;outline-offset:calc(-1 * var(--space-2));cursor:pointer;transition:background-color var(--duration-200) var(--ease-flat), color var(--duration-200) var(--ease-flat), outline-color var(--duration-200) var(--ease-flat);background-color:#0000;border:0;justify-content:center;align-items:center;display:inline-flex}.input-group__button:hover{background-color:color-mix(in srgb, var(--color-action) var(--opacity-tint), transparent);color:var(--color-action)}.input-group__button[aria-pressed=true]{color:var(--color-action)}.input-group__button:focus-visible{outline-color:var(--color-action)}.input-group__button:disabled{cursor:not-allowed}.input-group__button:disabled:hover{color:var(--color-quiet-text);background-color:#0000}.field-label{color:var(--color-dark-inversion);font:var(--font-text);font-family:var(--font-family);margin-block-end:var(--space-8);display:inline-block}.field-label[data-required]:after{content:"*";color:var(--color-danger);margin-inline-start:var(--space-2)}.field>.field-label[data-required]:after{margin-inline-start:calc(var(--space-2) - var(--space-8))}.field{gap:var(--space-8);flex-direction:column;min-width:0;display:flex}.field[data-orientation=horizontal]{flex-direction:row;align-items:center}.field[data-orientation=horizontal]>.input-group{flex:1}.field>.field-label{align-items:center;gap:var(--space-8);margin-block-end:var(--space-0);display:inline-flex}.field-description{margin:var(--space-0);color:var(--color-quiet-text);font:var(--font-description);font-family:var(--font-family)}.field[data-invalid]>.field-label{color:var(--color-danger-text)}.field[data-disabled]>.field-label,.field[data-disabled]>.field-description{opacity:var(--opacity-disabled)}.field-group{gap:var(--space-24);flex-direction:column;display:flex}.field-group[data-columns]{grid-template-columns:repeat(var(--field-group-columns), minmax(0, 1fr));display:grid}@media (width<=480px){.field-group[data-columns]{grid-template-columns:minmax(0,1fr)}}.input-group__control[type=file]{padding-block:calc((var(--input-height) - 20px) / 2);color:var(--color-quiet-text);cursor:pointer}.input-group__control::file-selector-button{padding:var(--space-0);color:var(--color-dark-inversion);font:var(--font-h4);font-family:var(--font-family);cursor:pointer;background:0 0;border:0;margin-inline-end:var(--space-8)}.input-group--multiline{--input-multiline-mark-room:calc(var(--space-8) + 20px + var(--space-8));align-items:stretch}.input-group__control--multiline{height:auto;min-height:calc(var(--input-height) * 2);padding:var(--space-8) var(--space-12);font:var(--font-paragraph);font-family:var(--font-family);field-sizing:content;resize:vertical}.input-group--multiline[data-has-errors] .input-group__control--multiline{padding-inline-end:var(--input-multiline-mark-room)}.input-group--multiline[data-has-counter] .input-group__control--multiline{padding-block-end:calc(var(--space-8) + 20px)}.input-group__addon--block-start,.input-group__addon--block-end{padding:var(--space-0);position:absolute;inset-inline-end:var(--space-8)}.input-group__addon--block-start{inset-block-start:var(--space-8)}.input-group__addon--block-end{inset-block-end:var(--space-4)}
@@ -0,0 +1,133 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import { Button } from '@base-ui-components/react/button';
3
+ import { Field as Field$1 } from '@base-ui-components/react/field';
4
+ import { ComponentProps, ComponentPropsWithRef, ReactNode, Ref } from 'react';
5
+
6
+ /** Which colour the errors are shown in. */
7
+ declare const InputTone: {
8
+ /** Something is wrong and has to be fixed. */
9
+ readonly Danger: "danger";
10
+ /** Something is allowed but worth a second look — a bid far above the market. */
11
+ readonly Warning: "warning";
12
+ };
13
+ type InputTone = (typeof InputTone)[keyof typeof InputTone];
14
+ type InputGroupProps = ComponentPropsWithRef<'div'> & {
15
+ /** Colour of the contour when the group is invalid. @default 'danger' */
16
+ tone?: InputTone;
17
+ /** Draws the contour in the tone. Set by `Input` when it has errors. */
18
+ isInvalid?: boolean;
19
+ };
20
+ type InputGroupInputProps = ComponentPropsWithRef<'input'>;
21
+ type InputGroupAddonProps = ComponentPropsWithRef<'div'> & {
22
+ /**
23
+ * Where the addon stands. `inline-*` beside the text of an input;
24
+ * `block-*` in the top or bottom end corner of a textarea. @default 'inline-end'
25
+ */
26
+ align?: 'inline-start' | 'inline-end' | 'block-start' | 'block-end';
27
+ };
28
+ type InputGroupTextProps = ComponentPropsWithRef<'span'>;
29
+ type InputGroupButtonProps = Omit<ComponentProps<typeof Button>, 'className'> & {
30
+ className?: string;
31
+ };
32
+ type FieldProps = Omit<ComponentProps<typeof Field$1.Root>, 'className' | 'disabled' | 'invalid'> & {
33
+ className?: string;
34
+ /** `horizontal` puts the parts in a row: an input and its button, a checkbox and its label. @default 'vertical' */
35
+ orientation?: 'vertical' | 'horizontal';
36
+ /** Switches off the input inside and fades the label and the description. */
37
+ isDisabled?: boolean;
38
+ /** Marks the field invalid: the contour, the label and `aria-invalid` on the input. */
39
+ isInvalid?: boolean;
40
+ };
41
+ type FieldLabelProps = Omit<ComponentProps<typeof Field$1.Label>, 'className'> & {
42
+ className?: string;
43
+ /** Adds the asterisk. The input itself takes `required` separately. */
44
+ isRequired?: boolean;
45
+ };
46
+ type FieldDescriptionProps = Omit<ComponentProps<typeof Field$1.Description>, 'className'> & {
47
+ className?: string;
48
+ };
49
+ type FieldGroupProps = ComponentPropsWithRef<'div'> & {
50
+ /** Lays the fields out in this many equal columns. Stacked when omitted. */
51
+ columns?: number;
52
+ };
53
+ /** What `Input` and `Textarea` share: the errors, the counter, the switch and the box. */
54
+ type FieldControlOwnProps = {
55
+ /**
56
+ * Shown behind the `!` mark at the end of the field. A non-empty list also
57
+ * draws the contour in the tone and sets `aria-invalid`.
58
+ */
59
+ errors?: ReadonlyArray<string>;
60
+ /** Which colour the errors are shown in. @default 'danger' */
61
+ tone?: InputTone;
62
+ /** Hides the `!` mark and keeps the contour and the description. @default true */
63
+ showErrors?: boolean;
64
+ /** Called when the `!` mark is clicked. */
65
+ onErrorsClick?: () => void;
66
+ /**
67
+ * Shows how many characters are left. Typing past it is allowed and turns the
68
+ * counter red — unlike the native `maxLength`, which stops the keyboard.
69
+ */
70
+ characterLimit?: number;
71
+ /** Counts the value for `characterLimit`. @default trimmed length */
72
+ countCharacters?: (value: string) => number;
73
+ /** Switches the field off. The native `disabled` is not accepted, as on Button. */
74
+ isDisabled?: boolean;
75
+ /** On the group, the box that draws the contour. `className` stays on the control. */
76
+ groupClassName?: string;
77
+ groupStyle?: InputGroupProps['style'];
78
+ };
79
+ type InputProps = Omit<ComponentPropsWithRef<'input'>, 'disabled' | 'size'> & FieldControlOwnProps & {
80
+ /** Before the text: an icon, a currency sign. */
81
+ startAddon?: ReactNode;
82
+ /** After the text, before the counter and the `!` mark. */
83
+ endAddon?: ReactNode;
84
+ /** Names the show-password button. @default 'Show password' */
85
+ passwordToggleLabel?: string;
86
+ ref?: Ref<HTMLInputElement>;
87
+ };
88
+ type InputGroupTextareaProps = ComponentPropsWithRef<'textarea'>;
89
+ type TextareaProps = Omit<ComponentPropsWithRef<'textarea'>, 'disabled'> & FieldControlOwnProps & {
90
+ /**
91
+ * The height it starts at, in lines. The field grows with the text past it
92
+ * where the browser can size a field by its content. @default 3
93
+ */
94
+ rows?: number;
95
+ ref?: Ref<HTMLTextAreaElement>;
96
+ };
97
+
98
+ /** A label, a control and a description, stacked or in a row. */
99
+ declare const Field: ({ orientation, isDisabled, isInvalid, className, ...props }: FieldProps) => react_jsx_runtime.JSX.Element;
100
+ /** The label. Inside a `Field` it finds the input itself; outside one, pass `htmlFor`. */
101
+ declare const FieldLabel: ({ isRequired, className, ...props }: FieldLabelProps) => react_jsx_runtime.JSX.Element;
102
+ /** Quiet text under the input. The input is described by it. */
103
+ declare const FieldDescription: ({ className, ...props }: FieldDescriptionProps) => react_jsx_runtime.JSX.Element;
104
+ /** Fields of one form, spaced apart; `columns` lays them out in a grid. */
105
+ declare const FieldGroup: ({ columns, className, style, ...props }: FieldGroupProps) => react_jsx_runtime.JSX.Element;
106
+
107
+ /** The box. Draws the contour and the focus ring for the control inside it. */
108
+ declare const InputGroup: ({ tone, isInvalid, className, ...props }: InputGroupProps) => react_jsx_runtime.JSX.Element;
109
+ /** The `<input>` inside a group. Takes every native prop. */
110
+ declare const InputGroupInput: ({ className, ...props }: InputGroupInputProps) => react_jsx_runtime.JSX.Element;
111
+ /** The `<textarea>` inside a group. Takes every native prop. */
112
+ declare const InputGroupTextarea: ({ className, ...props }: InputGroupTextareaProps) => react_jsx_runtime.JSX.Element;
113
+ /** Something that stands beside the text: an icon, a unit, a button. */
114
+ declare const InputGroupAddon: ({ align, className, ...props }: InputGroupAddonProps) => react_jsx_runtime.JSX.Element;
115
+ /** Quiet text inside the box: a unit, a prefix, a counter. */
116
+ declare const InputGroupText: ({ className, ...props }: InputGroupTextProps) => react_jsx_runtime.JSX.Element;
117
+ /** A small square button inside the box. Name it with `aria-label` when it is only an icon. */
118
+ declare const InputGroupButton: ({ className, ...props }: InputGroupButtonProps) => react_jsx_runtime.JSX.Element;
119
+ /**
120
+ * A text field, a password field or a number field, by `type`. A password gets
121
+ * the show-password button; a number stays the native input, so its value is
122
+ * the string the user typed, a comma included.
123
+ */
124
+ declare const Input: ({ type, id, errors, tone, showErrors, onErrorsClick, characterLimit, countCharacters, isDisabled, startAddon, endAddon, passwordToggleLabel, groupClassName, groupStyle, value, defaultValue, onChange, ...props }: InputProps) => react_jsx_runtime.JSX.Element;
125
+ /**
126
+ * A field for more than a line, the same box as `Input`. The `!` mark sits in
127
+ * the top corner, next to the first line; the counter in the bottom one, next to
128
+ * where the typing is.
129
+ */
130
+ declare const Textarea: ({ id, rows, errors, tone, showErrors, onErrorsClick, characterLimit, countCharacters, isDisabled, groupClassName, groupStyle, value, defaultValue, onChange, ...props }: TextareaProps) => react_jsx_runtime.JSX.Element;
131
+
132
+ export { Field, FieldDescription, FieldGroup, FieldLabel, Input, InputGroup, InputGroupAddon, InputGroupButton, InputGroupInput, InputGroupText, InputGroupTextarea, InputTone, Textarea, Input as default };
133
+ export type { FieldDescriptionProps, FieldGroupProps, FieldLabelProps, FieldProps, InputGroupAddonProps, InputGroupButtonProps, InputGroupInputProps, InputGroupProps, InputGroupTextProps, InputGroupTextareaProps, InputProps, TextareaProps };
package/dist/index.js ADDED
@@ -0,0 +1,223 @@
1
+ import { Button as e } from "@base-ui-components/react/button";
2
+ import { Field as t } from "@base-ui-components/react/field";
3
+ import { Input as n } from "@base-ui-components/react/input";
4
+ import r, { errorLabelTextId as i } from "@propellerads/error-label";
5
+ import { createContext as a, useContext as o, useId as s, useState as c } from "react";
6
+ import { clsx as l } from "clsx";
7
+ import { jsx as u, jsxs as d } from "react/jsx-runtime";
8
+ import './index.css';//#region src/cn.ts
9
+ var f = (...e) => l(e), p = a(!1), m = () => o(p), h = ({ orientation: e = "vertical", isDisabled: n, isInvalid: r, className: i, ...a }) => /* @__PURE__ */ u(p.Provider, {
10
+ value: !0,
11
+ children: /* @__PURE__ */ u(t.Root, {
12
+ ...a,
13
+ disabled: n,
14
+ invalid: r,
15
+ "data-orientation": e,
16
+ className: f("field", i)
17
+ })
18
+ }), g = ({ isRequired: e = !1, className: n, ...r }) => /* @__PURE__ */ u(t.Label, {
19
+ ...r,
20
+ "data-required": e || void 0,
21
+ className: f("field-label", n)
22
+ }), _ = ({ className: e, ...n }) => /* @__PURE__ */ u(t.Description, {
23
+ ...n,
24
+ className: f("field-description", e)
25
+ }), v = ({ columns: e, className: t, style: n, ...r }) => /* @__PURE__ */ u("div", {
26
+ ...r,
27
+ "data-columns": e,
28
+ className: f("field-group", t),
29
+ style: e === void 0 ? n : {
30
+ "--field-group-columns": e,
31
+ ...n
32
+ }
33
+ }), y = {
34
+ Danger: "danger",
35
+ Warning: "warning"
36
+ }, b = ({ tone: e = y.Danger, isInvalid: t = !1, className: n, ...r }) => /* @__PURE__ */ u("div", {
37
+ ...r,
38
+ "data-tone": e,
39
+ "data-invalid": t || void 0,
40
+ className: f("input-group", n)
41
+ }), x = ({ className: e, ...t }) => /* @__PURE__ */ u(n, {
42
+ ...t,
43
+ className: f("input-group__control", e)
44
+ }), S = ({ className: e, ...n }) => /* @__PURE__ */ u(t.Control, {
45
+ ...n,
46
+ render: /* @__PURE__ */ u("textarea", {}),
47
+ className: f("input-group__control", "input-group__control--multiline", e)
48
+ }), C = ({ align: e = "inline-end", className: t, ...n }) => /* @__PURE__ */ u("div", {
49
+ ...n,
50
+ "data-align": e,
51
+ className: f("input-group__addon", `input-group__addon--${e}`, t)
52
+ }), w = ({ className: e, ...t }) => /* @__PURE__ */ u("span", {
53
+ ...t,
54
+ className: f("input-group__text", e)
55
+ }), T = ({ className: t, ...n }) => /* @__PURE__ */ u(e, {
56
+ type: "button",
57
+ ...n,
58
+ className: f("input-group__button", t)
59
+ }), E = ({ isOpen: e }) => /* @__PURE__ */ d("svg", {
60
+ viewBox: "0 0 16 16",
61
+ width: "16",
62
+ height: "16",
63
+ fill: "none",
64
+ "aria-hidden": "true",
65
+ children: [
66
+ /* @__PURE__ */ u("path", {
67
+ d: "M1.5 8S4 3.5 8 3.5 14.5 8 14.5 8 12 12.5 8 12.5 1.5 8 1.5 8Z",
68
+ stroke: "currentColor",
69
+ strokeWidth: "1.5",
70
+ strokeLinejoin: "round"
71
+ }),
72
+ /* @__PURE__ */ u("circle", {
73
+ cx: "8",
74
+ cy: "8",
75
+ r: "2",
76
+ stroke: "currentColor",
77
+ strokeWidth: "1.5"
78
+ }),
79
+ !e && /* @__PURE__ */ u("path", {
80
+ d: "M2.5 13.5l11-11",
81
+ stroke: "currentColor",
82
+ strokeWidth: "1.5",
83
+ strokeLinecap: "round"
84
+ })
85
+ ]
86
+ }), D = (e) => e.trim().length, O = ({ id: e, errors: t, characterLimit: n, countCharacters: r, value: a, defaultValue: o, onChange: l, describedBy: u }) => {
87
+ let d = m(), p = s(), h = e ?? p, [g, _] = c(String(o ?? "")), v = t.length > 0;
88
+ return {
89
+ errorsKey: h,
90
+ isInField: d,
91
+ isInvalid: v,
92
+ remaining: n === void 0 ? void 0 : n - r(a === void 0 ? g : String(a)),
93
+ handleChange: (e) => {
94
+ a === void 0 && _(e.target.value), l?.(e);
95
+ },
96
+ ariaProps: {
97
+ ...v ? { "aria-invalid": !0 } : {},
98
+ ...v && !d ? { "aria-describedby": f(u, i(h)) } : {}
99
+ }
100
+ };
101
+ }, k = ({ remaining: e }) => /* @__PURE__ */ u(w, {
102
+ className: "input-group__counter",
103
+ "data-over": e < 0 || void 0,
104
+ children: e
105
+ }), A = ({ errorsKey: e, errors: n }) => {
106
+ let r = m(), a = n.join(". ");
107
+ return u(r ? t.Description : "span", {
108
+ id: i(e),
109
+ hidden: !0,
110
+ children: a
111
+ });
112
+ }, j = ({ type: e = "text", id: t, errors: n = [], tone: i = y.Danger, showErrors: a = !0, onErrorsClick: o, characterLimit: s, countCharacters: l = D, isDisabled: f = !1, startAddon: p, endAddon: m, passwordToggleLabel: h = "Show password", groupClassName: g, groupStyle: _, value: v, defaultValue: S, onChange: w, ...j }) => {
113
+ let [M, N] = c(!1), { errorsKey: P, isInField: F, isInvalid: I, remaining: L, handleChange: R, ariaProps: z } = O({
114
+ id: t,
115
+ errors: n,
116
+ characterLimit: s,
117
+ countCharacters: l,
118
+ value: v,
119
+ defaultValue: S,
120
+ onChange: w,
121
+ describedBy: j["aria-describedby"]
122
+ }), B = e === "password";
123
+ return /* @__PURE__ */ d(b, {
124
+ tone: i,
125
+ isInvalid: I,
126
+ className: g,
127
+ style: _,
128
+ children: [
129
+ p !== void 0 && /* @__PURE__ */ u(C, {
130
+ align: "inline-start",
131
+ children: p
132
+ }),
133
+ /* @__PURE__ */ u(x, {
134
+ ...j,
135
+ id: t,
136
+ type: B && M ? "text" : e,
137
+ value: v,
138
+ defaultValue: S,
139
+ onChange: R,
140
+ disabled: f,
141
+ ...z
142
+ }),
143
+ (m !== void 0 || B || L !== void 0 || I && a) && /* @__PURE__ */ d(C, {
144
+ align: "inline-end",
145
+ children: [
146
+ m,
147
+ B && /* @__PURE__ */ u(T, {
148
+ "aria-label": h,
149
+ "aria-pressed": M,
150
+ disabled: f,
151
+ onClick: () => N((e) => !e),
152
+ children: /* @__PURE__ */ u(E, { isOpen: M })
153
+ }),
154
+ L !== void 0 && /* @__PURE__ */ u(k, { remaining: L }),
155
+ I && a && /* @__PURE__ */ u(r, {
156
+ elementId: P,
157
+ errors: n,
158
+ tone: i,
159
+ onClick: o,
160
+ hasDescription: !F
161
+ })
162
+ ]
163
+ }),
164
+ I && (F || !a) && /* @__PURE__ */ u(A, {
165
+ errorsKey: P,
166
+ errors: n
167
+ })
168
+ ]
169
+ });
170
+ }, M = ({ id: e, rows: t = 3, errors: n = [], tone: i = y.Danger, showErrors: a = !0, onErrorsClick: o, characterLimit: s, countCharacters: c = D, isDisabled: l = !1, groupClassName: p, groupStyle: m, value: h, defaultValue: g, onChange: _, ...v }) => {
171
+ let { errorsKey: x, isInField: w, isInvalid: T, remaining: E, handleChange: j, ariaProps: M } = O({
172
+ id: e,
173
+ errors: n,
174
+ characterLimit: s,
175
+ countCharacters: c,
176
+ value: h,
177
+ defaultValue: g,
178
+ onChange: _,
179
+ describedBy: v["aria-describedby"]
180
+ });
181
+ return /* @__PURE__ */ d(b, {
182
+ tone: i,
183
+ isInvalid: T,
184
+ className: f("input-group--multiline", p),
185
+ style: m,
186
+ "data-has-errors": T && a || void 0,
187
+ "data-has-counter": E !== void 0 || void 0,
188
+ children: [
189
+ /* @__PURE__ */ u(S, {
190
+ ...v,
191
+ id: e,
192
+ rows: t,
193
+ value: h,
194
+ defaultValue: g,
195
+ onChange: j,
196
+ disabled: l,
197
+ ...M
198
+ }),
199
+ T && a && /* @__PURE__ */ u(C, {
200
+ align: "block-start",
201
+ children: /* @__PURE__ */ u(r, {
202
+ elementId: x,
203
+ errors: n,
204
+ tone: i,
205
+ onClick: o,
206
+ hasDescription: !w
207
+ })
208
+ }),
209
+ E !== void 0 && /* @__PURE__ */ u(C, {
210
+ align: "block-end",
211
+ children: /* @__PURE__ */ u(k, { remaining: E })
212
+ }),
213
+ T && (w || !a) && /* @__PURE__ */ u(A, {
214
+ errorsKey: x,
215
+ errors: n
216
+ })
217
+ ]
218
+ });
219
+ };
220
+ //#endregion
221
+ export { h as Field, _ as FieldDescription, v as FieldGroup, g as FieldLabel, j as Input, j as default, b as InputGroup, C as InputGroupAddon, T as InputGroupButton, x as InputGroupInput, w as InputGroupText, S as InputGroupTextarea, y as InputTone, M as Textarea };
222
+
223
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/cn.ts","../src/field.tsx","../src/types.ts","../src/index.tsx"],"sourcesContent":["import {type ClassValue, clsx} from 'clsx';\n\n/**\n * Joins class names. There is no tailwind-merge here on purpose: the input\n * ships its own stylesheet instead of utility classes, so there is nothing to\n * deduplicate.\n */\nconst cn = (...inputs: ClassValue[]): string => clsx(inputs);\n\nexport {cn};\n","import {Field as BaseField} from '@base-ui-components/react/field';\nimport {type CSSProperties, createContext, useContext} from 'react';\nimport {cn} from './cn';\nimport type {FieldDescriptionProps, FieldGroupProps, FieldLabelProps, FieldProps} from './types';\n\n/*\n * The parts follow shadcn's Field, on Base UI's. The field wires itself: the\n * label points at the input, and the input is described by the description and\n * by its errors, with no ids written by hand.\n */\n\n/** Whether a part is standing inside a `Field`, which then does the wiring. */\nconst FieldContext = createContext<boolean>(false);\n\nconst useIsInField = (): boolean => useContext(FieldContext);\n\n/** A label, a control and a description, stacked or in a row. */\nconst Field = ({orientation = 'vertical', isDisabled, isInvalid, className, ...props}: FieldProps) => (\n <FieldContext.Provider value={true}>\n <BaseField.Root\n {...props}\n disabled={isDisabled}\n invalid={isInvalid}\n data-orientation={orientation}\n className={cn('field', className)}\n />\n </FieldContext.Provider>\n);\n\n/** The label. Inside a `Field` it finds the input itself; outside one, pass `htmlFor`. */\nconst FieldLabel = ({isRequired = false, className, ...props}: FieldLabelProps) => (\n <BaseField.Label {...props} data-required={isRequired || undefined} className={cn('field-label', className)} />\n);\n\n/** Quiet text under the input. The input is described by it. */\nconst FieldDescription = ({className, ...props}: FieldDescriptionProps) => (\n <BaseField.Description {...props} className={cn('field-description', className)} />\n);\n\n/** Fields of one form, spaced apart; `columns` lays them out in a grid. */\nconst FieldGroup = ({columns, className, style, ...props}: FieldGroupProps) => (\n <div\n {...props}\n data-columns={columns}\n className={cn('field-group', className)}\n style={columns === undefined ? style : ({'--field-group-columns': columns, ...style} as CSSProperties)}\n />\n);\n\nexport {Field, FieldDescription, FieldGroup, FieldLabel, useIsInField};\n","import type {Button as BaseButton} from '@base-ui-components/react/button';\nimport type {Field as BaseField} from '@base-ui-components/react/field';\nimport type {ComponentProps, ComponentPropsWithRef, ReactNode, Ref} from 'react';\n\n/** Which colour the errors are shown in. */\nexport const InputTone = {\n /** Something is wrong and has to be fixed. */\n Danger: 'danger',\n /** Something is allowed but worth a second look — a bid far above the market. */\n Warning: 'warning',\n} as const;\nexport type InputTone = (typeof InputTone)[keyof typeof InputTone];\n\nexport type InputGroupProps = ComponentPropsWithRef<'div'> & {\n /** Colour of the contour when the group is invalid. @default 'danger' */\n tone?: InputTone;\n /** Draws the contour in the tone. Set by `Input` when it has errors. */\n isInvalid?: boolean;\n};\n\nexport type InputGroupInputProps = ComponentPropsWithRef<'input'>;\n\nexport type InputGroupAddonProps = ComponentPropsWithRef<'div'> & {\n /**\n * Where the addon stands. `inline-*` beside the text of an input;\n * `block-*` in the top or bottom end corner of a textarea. @default 'inline-end'\n */\n align?: 'inline-start' | 'inline-end' | 'block-start' | 'block-end';\n};\n\nexport type InputGroupTextProps = ComponentPropsWithRef<'span'>;\n\nexport type InputGroupButtonProps = Omit<ComponentProps<typeof BaseButton>, 'className'> & {\n className?: string;\n};\n\nexport type FieldProps = Omit<ComponentProps<typeof BaseField.Root>, 'className' | 'disabled' | 'invalid'> & {\n className?: string;\n /** `horizontal` puts the parts in a row: an input and its button, a checkbox and its label. @default 'vertical' */\n orientation?: 'vertical' | 'horizontal';\n /** Switches off the input inside and fades the label and the description. */\n isDisabled?: boolean;\n /** Marks the field invalid: the contour, the label and `aria-invalid` on the input. */\n isInvalid?: boolean;\n};\n\nexport type FieldLabelProps = Omit<ComponentProps<typeof BaseField.Label>, 'className'> & {\n className?: string;\n /** Adds the asterisk. The input itself takes `required` separately. */\n isRequired?: boolean;\n};\n\nexport type FieldDescriptionProps = Omit<ComponentProps<typeof BaseField.Description>, 'className'> & {\n className?: string;\n};\n\nexport type FieldGroupProps = ComponentPropsWithRef<'div'> & {\n /** Lays the fields out in this many equal columns. Stacked when omitted. */\n columns?: number;\n};\n\n/** What `Input` and `Textarea` share: the errors, the counter, the switch and the box. */\ntype FieldControlOwnProps = {\n /**\n * Shown behind the `!` mark at the end of the field. A non-empty list also\n * draws the contour in the tone and sets `aria-invalid`.\n */\n errors?: ReadonlyArray<string>;\n /** Which colour the errors are shown in. @default 'danger' */\n tone?: InputTone;\n /** Hides the `!` mark and keeps the contour and the description. @default true */\n showErrors?: boolean;\n /** Called when the `!` mark is clicked. */\n onErrorsClick?: () => void;\n /**\n * Shows how many characters are left. Typing past it is allowed and turns the\n * counter red — unlike the native `maxLength`, which stops the keyboard.\n */\n characterLimit?: number;\n /** Counts the value for `characterLimit`. @default trimmed length */\n countCharacters?: (value: string) => number;\n /** Switches the field off. The native `disabled` is not accepted, as on Button. */\n isDisabled?: boolean;\n /** On the group, the box that draws the contour. `className` stays on the control. */\n groupClassName?: string;\n groupStyle?: InputGroupProps['style'];\n};\n\nexport type InputProps = Omit<ComponentPropsWithRef<'input'>, 'disabled' | 'size'> &\n FieldControlOwnProps & {\n /** Before the text: an icon, a currency sign. */\n startAddon?: ReactNode;\n /** After the text, before the counter and the `!` mark. */\n endAddon?: ReactNode;\n /** Names the show-password button. @default 'Show password' */\n passwordToggleLabel?: string;\n ref?: Ref<HTMLInputElement>;\n };\n\nexport type InputGroupTextareaProps = ComponentPropsWithRef<'textarea'>;\n\nexport type TextareaProps = Omit<ComponentPropsWithRef<'textarea'>, 'disabled'> &\n FieldControlOwnProps & {\n /**\n * The height it starts at, in lines. The field grows with the text past it\n * where the browser can size a field by its content. @default 3\n */\n rows?: number;\n ref?: Ref<HTMLTextAreaElement>;\n };\n","import {Button as BaseButton} from '@base-ui-components/react/button';\nimport {Field as BaseField} from '@base-ui-components/react/field';\nimport {Input as BaseInput} from '@base-ui-components/react/input';\nimport ErrorLabel, {errorLabelTextId} from '@propellerads/error-label';\nimport {type ChangeEvent, type ComponentProps, useId, useState} from 'react';\nimport './input.css';\nimport {cn} from './cn';\nimport {Field, FieldDescription, FieldGroup, FieldLabel, useIsInField} from './field';\nimport {\n type FieldDescriptionProps,\n type FieldGroupProps,\n type FieldLabelProps,\n type FieldProps,\n type InputGroupAddonProps,\n type InputGroupButtonProps,\n type InputGroupInputProps,\n type InputGroupProps,\n type InputGroupTextareaProps,\n type InputGroupTextProps,\n type InputProps,\n InputTone,\n type TextareaProps,\n} from './types';\n\n/*\n * The parts follow shadcn's Input Group, on Base UI instead of Radix. The group is\n * the box — contour, corners, focus ring — and the control inside it draws\n * nothing, so an icon, a counter or a button can stand in the same box. `Input`\n * and `Textarea` below are the group with the common parts filled in.\n */\n\n/** The box. Draws the contour and the focus ring for the control inside it. */\nconst InputGroup = ({tone = InputTone.Danger, isInvalid = false, className, ...props}: InputGroupProps) => (\n <div {...props} data-tone={tone} data-invalid={isInvalid || undefined} className={cn('input-group', className)} />\n);\n\n/** The `<input>` inside a group. Takes every native prop. */\nconst InputGroupInput = ({className, ...props}: InputGroupInputProps) => (\n <BaseInput {...props} className={cn('input-group__control', className)} />\n);\n\n/** The `<textarea>` inside a group. Takes every native prop. */\nconst InputGroupTextarea = ({className, ...props}: InputGroupTextareaProps) => (\n <BaseField.Control\n // Base UI types the control as an <input>, and renders a textarea through\n // `render` the way its own docs do; the props are the textarea's.\n {...(props as unknown as ComponentProps<typeof BaseField.Control>)}\n render={<textarea />}\n className={cn('input-group__control', 'input-group__control--multiline', className)}\n />\n);\n\n/** Something that stands beside the text: an icon, a unit, a button. */\nconst InputGroupAddon = ({align = 'inline-end', className, ...props}: InputGroupAddonProps) => (\n <div {...props} data-align={align} className={cn('input-group__addon', `input-group__addon--${align}`, className)} />\n);\n\n/** Quiet text inside the box: a unit, a prefix, a counter. */\nconst InputGroupText = ({className, ...props}: InputGroupTextProps) => (\n <span {...props} className={cn('input-group__text', className)} />\n);\n\n/** A small square button inside the box. Name it with `aria-label` when it is only an icon. */\nconst InputGroupButton = ({className, ...props}: InputGroupButtonProps) => (\n <BaseButton type=\"button\" {...props} className={cn('input-group__button', className)} />\n);\n\nconst EyeIcon = ({isOpen}: {isOpen: boolean}) => (\n <svg viewBox=\"0 0 16 16\" width=\"16\" height=\"16\" fill=\"none\" aria-hidden=\"true\">\n <path\n d=\"M1.5 8S4 3.5 8 3.5 14.5 8 14.5 8 12 12.5 8 12.5 1.5 8 1.5 8Z\"\n stroke=\"currentColor\"\n strokeWidth=\"1.5\"\n strokeLinejoin=\"round\"\n />\n <circle cx=\"8\" cy=\"8\" r=\"2\" stroke=\"currentColor\" strokeWidth=\"1.5\" />\n {!isOpen && <path d=\"M2.5 13.5l11-11\" stroke=\"currentColor\" strokeWidth=\"1.5\" strokeLinecap=\"round\" />}\n </svg>\n);\n\nconst defaultCount = (value: string): number => value.trim().length;\n\ntype ControlState<E extends HTMLInputElement | HTMLTextAreaElement> = {\n id?: string;\n errors: ReadonlyArray<string>;\n characterLimit?: number;\n countCharacters: (value: string) => number;\n value?: ChangeEvent<E>['target']['value'] | number | ReadonlyArray<string>;\n defaultValue?: ChangeEvent<E>['target']['value'] | number | ReadonlyArray<string>;\n onChange?: (event: ChangeEvent<E>) => void;\n describedBy?: string;\n};\n\n/**\n * What `Input` and `Textarea` work out the same way: the key the error label is\n * named by, whether the control has to be pointed at the errors by hand, and\n * what is left under the character limit.\n */\nconst useControlState = <E extends HTMLInputElement | HTMLTextAreaElement>({\n id,\n errors,\n characterLimit,\n countCharacters,\n value,\n defaultValue,\n onChange,\n describedBy,\n}: ControlState<E>) => {\n const isInField = useIsInField();\n const fallbackKey = useId();\n const errorsKey = id ?? fallbackKey;\n // The counter needs the text even when the control is not controlled.\n const [uncontrolledValue, setUncontrolledValue] = useState<string>(String(defaultValue ?? ''));\n\n const isInvalid = errors.length > 0;\n const text = value === undefined ? uncontrolledValue : String(value);\n const remaining = characterLimit === undefined ? undefined : characterLimit - countCharacters(text);\n\n const handleChange = (event: ChangeEvent<E>): void => {\n if (value === undefined) {\n setUncontrolledValue(event.target.value);\n }\n\n onChange?.(event);\n };\n\n const ariaProps = {\n // Left out rather than `undefined` when valid: a Field marked invalid writes its own.\n ...(isInvalid ? {'aria-invalid': true} : {}),\n // Inside a Field the description registers itself; outside one it is pointed at here.\n ...(isInvalid && !isInField ? {'aria-describedby': cn(describedBy, errorLabelTextId(errorsKey))} : {}),\n };\n\n return {errorsKey, isInField, isInvalid, remaining, handleChange, ariaProps};\n};\n\nconst Counter = ({remaining}: {remaining: number}) => (\n <InputGroupText className=\"input-group__counter\" data-over={remaining < 0 || undefined}>\n {remaining}\n </InputGroupText>\n);\n\n/**\n * The errors as hidden text inside a Field, as a Base UI description, so the\n * control is described by them next to the field's own description. Outside a\n * Field the error label writes this text and the control is pointed at it.\n */\nconst HiddenErrors = ({errorsKey, errors}: {errorsKey: string; errors: ReadonlyArray<string>}) => {\n const isInField = useIsInField();\n const text = errors.join('. ');\n\n // Base UI throws for a description with no Field around it.\n return isInField ? (\n <BaseField.Description id={errorLabelTextId(errorsKey)} hidden>\n {text}\n </BaseField.Description>\n ) : (\n <span id={errorLabelTextId(errorsKey)} hidden>\n {text}\n </span>\n );\n};\n\n/**\n * A text field, a password field or a number field, by `type`. A password gets\n * the show-password button; a number stays the native input, so its value is\n * the string the user typed, a comma included.\n */\nconst Input = ({\n type = 'text',\n id,\n errors = [],\n tone = InputTone.Danger,\n showErrors = true,\n onErrorsClick,\n characterLimit,\n countCharacters = defaultCount,\n isDisabled = false,\n startAddon,\n endAddon,\n passwordToggleLabel = 'Show password',\n groupClassName,\n groupStyle,\n value,\n defaultValue,\n onChange,\n ...props\n}: InputProps) => {\n const [isRevealed, setRevealed] = useState<boolean>(false);\n const {errorsKey, isInField, isInvalid, remaining, handleChange, ariaProps} = useControlState<HTMLInputElement>({\n id,\n errors,\n characterLimit,\n countCharacters,\n value,\n defaultValue,\n onChange,\n describedBy: props['aria-describedby'],\n });\n\n const isPassword = type === 'password';\n\n return (\n <InputGroup tone={tone} isInvalid={isInvalid} className={groupClassName} style={groupStyle}>\n {startAddon !== undefined && <InputGroupAddon align=\"inline-start\">{startAddon}</InputGroupAddon>}\n <InputGroupInput\n {...props}\n // Inside a Field the id is Base UI's, so the label finds the input.\n id={id}\n type={isPassword && isRevealed ? 'text' : type}\n value={value}\n defaultValue={defaultValue}\n onChange={handleChange}\n disabled={isDisabled}\n {...ariaProps}\n />\n {(endAddon !== undefined || isPassword || remaining !== undefined || (isInvalid && showErrors)) && (\n <InputGroupAddon align=\"inline-end\">\n {endAddon}\n {isPassword && (\n <InputGroupButton\n aria-label={passwordToggleLabel}\n aria-pressed={isRevealed}\n disabled={isDisabled}\n onClick={() => setRevealed((previous) => !previous)}\n >\n <EyeIcon isOpen={isRevealed} />\n </InputGroupButton>\n )}\n {remaining !== undefined && <Counter remaining={remaining} />}\n {isInvalid && showErrors && (\n <ErrorLabel\n elementId={errorsKey}\n errors={errors}\n tone={tone}\n onClick={onErrorsClick}\n hasDescription={!isInField}\n />\n )}\n </InputGroupAddon>\n )}\n {isInvalid && (isInField || !showErrors) && <HiddenErrors errorsKey={errorsKey} errors={errors} />}\n </InputGroup>\n );\n};\n\n/**\n * A field for more than a line, the same box as `Input`. The `!` mark sits in\n * the top corner, next to the first line; the counter in the bottom one, next to\n * where the typing is.\n */\nconst Textarea = ({\n id,\n rows = 3,\n errors = [],\n tone = InputTone.Danger,\n showErrors = true,\n onErrorsClick,\n characterLimit,\n countCharacters = defaultCount,\n isDisabled = false,\n groupClassName,\n groupStyle,\n value,\n defaultValue,\n onChange,\n ...props\n}: TextareaProps) => {\n const {errorsKey, isInField, isInvalid, remaining, handleChange, ariaProps} = useControlState<HTMLTextAreaElement>({\n id,\n errors,\n characterLimit,\n countCharacters,\n value,\n defaultValue,\n onChange,\n describedBy: props['aria-describedby'],\n });\n\n return (\n <InputGroup\n tone={tone}\n isInvalid={isInvalid}\n className={cn('input-group--multiline', groupClassName)}\n style={groupStyle}\n data-has-errors={(isInvalid && showErrors) || undefined}\n data-has-counter={remaining !== undefined || undefined}\n >\n <InputGroupTextarea\n {...props}\n id={id}\n rows={rows}\n value={value}\n defaultValue={defaultValue}\n onChange={handleChange}\n disabled={isDisabled}\n {...ariaProps}\n />\n {isInvalid && showErrors && (\n <InputGroupAddon align=\"block-start\">\n <ErrorLabel\n elementId={errorsKey}\n errors={errors}\n tone={tone}\n onClick={onErrorsClick}\n hasDescription={!isInField}\n />\n </InputGroupAddon>\n )}\n {remaining !== undefined && (\n <InputGroupAddon align=\"block-end\">\n <Counter remaining={remaining} />\n </InputGroupAddon>\n )}\n {isInvalid && (isInField || !showErrors) && <HiddenErrors errorsKey={errorsKey} errors={errors} />}\n </InputGroup>\n );\n};\n\nexport default Input;\n\nexport type {\n FieldDescriptionProps,\n FieldGroupProps,\n FieldLabelProps,\n FieldProps,\n InputGroupAddonProps,\n InputGroupButtonProps,\n InputGroupInputProps,\n InputGroupProps,\n InputGroupTextareaProps,\n InputGroupTextProps,\n InputProps,\n TextareaProps,\n};\nexport {\n Field,\n FieldDescription,\n FieldGroup,\n FieldLabel,\n Input,\n InputGroup,\n InputGroupAddon,\n InputGroupButton,\n InputGroupInput,\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AAa,CAAC,MAAe,kBAAC,GAAD;IAAyB;IAAmB;IAAU,CAAA;GACvF"}
package/package.json ADDED
@@ -0,0 +1,57 @@
1
+ {
2
+ "name": "@propellerads/input",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "types": "dist/index.d.ts",
6
+ "style": "dist/index.css",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "import": "./dist/index.js"
11
+ },
12
+ "./index.css": "./dist/index.css"
13
+ },
14
+ "sideEffects": [
15
+ "**/*.css"
16
+ ],
17
+ "files": [
18
+ "dist"
19
+ ],
20
+ "repository": "https://github.com/propellerads/ui-components.git",
21
+ "author": "i.pasyuk@propellerads.net",
22
+ "license": "MIT",
23
+ "bugs": {
24
+ "url": "https://github.com/propellerads/ui-components/issues"
25
+ },
26
+ "scripts": {
27
+ "build": "tsc && vite build && bunx rollup -c rollup.dts.config.mjs"
28
+ },
29
+ "homepage": "https://github.com/propellerads/ui-components#readme",
30
+ "private": false,
31
+ "dependencies": {
32
+ "@base-ui-components/react": "1.0.0-rc.0",
33
+ "@propellerads/error-label": "^6.0.0",
34
+ "class-variance-authority": "^0.7.1",
35
+ "clsx": "^2.1.1"
36
+ },
37
+ "peerDependencies": {
38
+ "@propellerads/tokens": ">=0.2.0",
39
+ "react": "^18.0.0 || ^19.0.0",
40
+ "react-dom": "^18.0.0 || ^19.0.0"
41
+ },
42
+ "peerDependenciesMeta": {
43
+ "@propellerads/tokens": {
44
+ "optional": true
45
+ }
46
+ },
47
+ "devDependencies": {
48
+ "@propellerads/tokens": "workspace:^",
49
+ "@types/react": "^19.2.14",
50
+ "@vitejs/plugin-react": "^6.0.1",
51
+ "rollup": "^4.60.3",
52
+ "rollup-plugin-dts": "^6.4.1",
53
+ "typescript": "^6.0.3",
54
+ "vite": "^8.0.10",
55
+ "vite-plugin-lib-inject-css": "^2.2.2"
56
+ }
57
+ }