Input
Examples
Validation errors are announced
When a field is invalid it carries aria-invalid and points at its message through aria-describedby; the message itself is a role="alert" live region, so it
is spoken when it appears rather than only drawn on screen.
An action-style field never announces an error it does not show
actionInput hides the label, the error message and the error styling so the field can
sit inside a composite control. aria-invalid follows that same gate: a field that shows
no error must not announce one.
Helper text is associated too
infoMessage is part of the field's description, so it is referenced by aria-describedby even when the field is valid.
Basic
Multi-line (useTextArea)
Set useTextArea to render a <textarea> instead of an <input>. Use rows for the initial height.
Auto-resize
With autoResize the textarea grows with its content between minRows and maxRows (then scrolls).
Character counter
Pass showCount with a maxLength to show a live counter.
Manual resize
Control the resize handle with resize (default 'none') — 'vertical', 'horizontal', or 'both'. Drag the corner/edge
of each field below.
Independent leading icon colour
--input-left-icon-color overrides only the leading icon. When it is unset, both icons
retain --input-icon-color.
Native input types (dataType)
dataType is passed straight through as the native type. Beyond the
original text/tel/password/email/number it also accepts time, date, search and url.
tel validation (dataType="tel")
Before 4.20, dataType="tel" with no validationPattern silently required
a ten-digit Indian mobile number (leading 6-9). It now checks only the characters and length a
phone number may hold — the two fields below validate the identical UK number differently: the
default rule accepts it, the legacy TEL_PRESET_IN_MOBILE pattern still rejects it, matching
what it did before.
spellcheck
Defaults to unset, so the browser default is untouched. Pass false for fields holding code,
JSON or identifiers.
readonly
readonly keeps the field focusable and selectable but not editable — unlike disable, which removes it from the focus order entirely and so cannot carry a
select-all-to-copy affordance.
onPaste on a non-tel field
The paste callback fires for every dataType, not just tel — which is what
lets a consumer intercept pasted files or images in a multi-line field.
maxLength= — no limit
maxLength defaults to 1000 and is rendered as a native maxlength unconditionally. A composer or paste target that silently truncates long input is worse than an unbounded
one, so pass null to omit the attribute entirely.
line-height
A textarea/input computes line-height: normal from the UA sheet regardless of what
its container inherits, so a consumer cannot reach it by inheritance. This is the hook; the
default is the same normal, so existing fields are unchanged.
min-height / max-height
A textarea that grows with its content needs a ceiling before it can scroll, and one used as a paste target needs a floor. Both default to the CSS initial value, so a field that sets neither is unchanged.
autoResize with a CSS ceiling
autoResize grows the field to fit its content. When the ceiling comes from --input-max-height rather than maxRows, the field must still become
scrollable at the ceiling instead of clipping what it cannot show.
paste events seen: 0
Required field, and the deprecated mandatory alias
required is the platform's name and the one every other form control here uses. mandatory is the original spelling and still works, so existing call sites need no change.
Documentation
A text input field with built-in validation for email, phone (tel), password, and text patterns. Validates automatically using validationPattern and inProgressPattern RegExp props, plus optional custom validator functions. Shows error messages when validation fails and info messages below the input. For tel dataType, automatically strips non-digit characters and enforces maxLength. Supports text transformers that modify the raw input value and view presentation transformers that format the displayed value (e.g., adding spaces to a card number). The validation state (Valid / InProgress / Invalid) is computed reactively and reported via onstatechange. Can render as a <textarea> when useTextArea is true, with multi-line options: rows, auto-resize (autoResize / minRows / maxRows), a resize handle, and a showCount character counter.
Usage
<script>
import { Input } from '@juspay/svelte-ui-components';
</script>
<Input value={'...'} />
Multi-line (textarea)
Set useTextArea to render a <textarea>. It supports the same label/validation/error
machinery as the single-line input, plus textarea-specific options: rows, autoResize
(minRows/maxRows), a resize handle, and a showCount character counter.
<script>
let notes = $state('');
</script>
<!-- Fixed height -->
<Input bind:value={notes} useTextArea label="Notes" rows={4} />
<!-- Auto-grow between 2 and 8 rows -->
<Input bind:value={notes} useTextArea autoResize minRows={2} maxRows={8} />
<!-- Character counter -->
<Input bind:value={notes} useTextArea showCount maxLength={140} label="Bio" />
<!-- User-resizable -->
<Input bind:value={notes} useTextArea resize="vertical" />
Props
| Prop | Type | Required | Default | Description |
|---|---|---|---|---|
| value | string |
Yes | '' |
Bindable. The current input value. Two-way bound to the underlying <input> or <textarea> element. |
| placeholder | string | null |
No | '' |
Placeholder text shown when the input is empty. |
| dataType | InputDataType |
No | 'text' |
The native input type, and the switch validateInput runs on. Only text, tel, password and email are validated — number, time, date, search and url have no branch at all and are always reported Valid, so onErrorMessage never shows for them and the browser's own constraint validation is the only check; supply validators if you need more. 'tel' strips non-digits and applies textTransformers; 'email' uses RFC 5322 validation; 'password' and 'text' use pattern-based validation. See Phone numbers below. |
| label | string | null |
No | '' |
Label text shown above the input field. Hidden when actionInput is true. |
| labelSuffix | Snippet |
No | - |
Content beside the visible label, right-aligned in the label row (for example a status Pill, character count, or short hint). It sits outside the <label>, so it does not join the field's accessible name. Ignored when label is absent, or when actionInput is set. |
| autofocus | boolean |
No | false |
Focuses the native field once it mounts, without scrolling it into view. Prefer this for a field that opens already visible in a dialog or sheet. |
| onErrorMessage | string | null |
No | '' |
Error message text displayed below the input when validation state is 'Invalid'. Hidden when actionInput is true. |
| infoMessage | string | null |
No | '' |
Informational text displayed below the input regardless of validation state. Hidden when actionInput is true. |
| validators | CustomValidator[] |
No | [] |
Array of custom validator functions. Each receives the input value and current validation state, and returns a new ValidationState. Validators run after the built-in validation. |
| disable | boolean |
No | false |
Whether the input is disabled (greyed out and non-interactive). |
| readonly | boolean |
No | false |
Renders the field read-only: the value can be focused, selected and copied but not edited. Deliberately distinct from disable, which also removes the element from the focus order and so cannot serve a select-all-to-copy affordance. |
| spellcheck | boolean | null |
No | null |
Native spellcheck. null renders as attribute-absent, so the browser default is unchanged for existing consumers. Pass false for fields holding code, JSON, or identifiers, where red squiggles are noise. |
| validationPattern | RegExp | null |
No | null |
RegExp that the input value must match to be considered 'Valid'. If null, no pattern validation is applied. |
| inProgressPattern | RegExp | null |
No | null |
RegExp that matches partial/incomplete input. If the value matches this pattern (but not validationPattern), the state is 'InProgress' instead of 'Invalid'. |
| addFocusColor | boolean |
No | false |
When true, adds a 1px focus border to the input. Used with actionInput mode. |
| maxLength | number |
No | 1000 |
Maximum number of characters allowed. For dataType='tel', this limits the digit count (excess digits are trimmed from the start). |
| minLength | number |
No | 0 |
Minimum number of characters required (HTML minlength attribute). |
| min | number |
No | - |
Minimum value for numeric inputs (HTML min attribute). Only applies to <input>, not <textarea>. |
| max | number |
No | - |
Maximum value for numeric inputs (HTML max attribute). Only applies to <input>, not <textarea>. |
| actionInput | boolean |
No | false |
When true, hides the label, error message, and info message, and adjusts border-radius/shadow for seamless integration inside InputButton. |
| useTextArea | boolean |
No | false |
When true, renders a <textarea> instead of an <input>. Useful for multi-line text entry. |
| rows | number |
No | - |
Initial visible rows for the textarea (only applies when useTextArea). |
| autoResize | boolean |
No | false |
When useTextArea, grows/shrinks the textarea to fit its content between minRows and maxRows; disables manual resizing while active. |
| minRows | number |
No | rows |
Lower bound (in rows) when autoResize is on. |
| maxRows | number |
No | - |
Upper bound (in rows) when autoResize is on; beyond this the textarea scrolls. |
| resize | 'none' | 'vertical' | 'horizontal' | 'both' |
No | 'none' |
Manual resize-handle behaviour for the textarea. Forced to 'none' when autoResize is on. |
| showCount | boolean |
No | false |
When useTextArea, shows a live current / maxLength character counter beneath the field. |
| autoComplete | HTMLInputAttributes['autocomplete'] |
No | 'on' |
The HTML autocomplete attribute value. Controls browser autofill behavior. Accepts any string for non-standard values (e.g., 'off', 'new-password'). |
| inputMode | HTMLInputAttributes['inputmode'] |
No | - |
Virtual-keyboard hint rendered as the native inputmode attribute (e.g. 'numeric' for OTP/PIN fields). Left off by default. |
| name | string |
No | '' |
The HTML name attribute for the input. Used for form submission and label association. |
| id | string |
No | - |
Explicit id for the underlying <input>/<textarea>, and the value the label's for points at. Wins over the auto-derived id. Optional: the fallback appends a per-instance $props.id() suffix, so fields sharing a name still get unique ids. Names the field, not the wrapper <div>. |
| textTransformers | TextTransformer[] |
No | [] |
Array of functions applied to the raw input value before digit extraction (tel mode only). Use for stripping country codes or formatting. |
| textViewPresentation | TextTransformer[] |
No | [] |
Array of functions applied to the value for display purposes. The underlying value stays clean but the displayed text is transformed (e.g., adding spaces every 4 digits for card numbers). |
| testId | string |
No | '' |
Value for the data-pw attribute, used for end-to-end testing selectors. |
| classes | string |
No | - |
CSS class string applied to the component's top-level element. Useful for theming — define classes with CSS variable overrides and pass them to create variant styles. |
| role | string |
No | - |
Sets the ARIA role attribute on the underlying <input>/<textarea>. Use 'combobox' when building autocomplete patterns. |
| ariaLabel | string | null |
No | - |
Accessible name applied as aria-label on the underlying <input>/<textarea>. Use whenever the field has no visible label (e.g. inline table cells, icon-only search fields). Ignored when a visible label is rendered, so the visible text is never overridden (WCAG 2.5.3). |
| ariaExpanded | boolean |
No | - |
Sets aria-expanded on the input element. Use when the input controls a dropdown or listbox that can be open or closed. |
| ariaAutocomplete | 'none' | 'inline' | 'list' | 'both' |
No | - |
Sets aria-autocomplete on the input element. Indicates whether the input provides autocomplete suggestions inline, as a list, both, or neither. |
| ariaControls | string | null |
No | - |
Sets aria-controls on the input element. Should reference the id of the listbox/dropdown element that this input controls. |
| ariaActivedescendant | string | null |
No | - |
Sets aria-activedescendant on the input element. Should reference the id of the currently focused option in the controlled listbox, enabling screen readers to announce the active option without moving DOM focus. |
| leftIcon | Snippet |
No | - |
Passive or clickable icon rendered inside the field on the leading edge (e.g. a search icon). Renders inside the field border; non-icon consumers keep the original DOM unchanged. |
| rightIcon | Snippet |
No | - |
Passive or clickable icon rendered inside the field on the trailing edge (e.g. a clear button). |
| onlefticonclick | () => void |
No | - |
When set, leftIcon renders as a focusable <button> invoking this handler (search / clear-on-click pattern); otherwise the icon is a passive <span>. |
| onrighticonclick | () => void |
No | - |
When set, rightIcon renders as a focusable <button> invoking this handler. |
| leftIconLabel | string |
No | 'Leading action' |
Accessible aria-label for the clickable leftIcon button. |
| rightIconLabel | string |
No | 'Trailing action' |
Accessible aria-label for the clickable rightIcon button. |
| required | boolean |
No | - |
Appends a required asterisk beside the label and sets both aria-required and the native required attribute, enabling native form validation alongside assistive-technology support. The platform's own name, and the spelling every other form control in this library uses. Wins over mandatory when both are supplied. |
| mandatory | boolean |
No | false |
Deprecated, use required. The original spelling of the same concept. It keeps working unchanged and is only consulted when required is not supplied. The --input-mandatory-* tokens and the .input-mandatory-asterisk class keep their names. |
| forceError | boolean |
No | false |
Forces the error state (red border and error message text when onErrorMessage is set) independent of validationPattern — for server/runtime-driven errors. Combines with the existing client-side validation error state. |
Methods
Exported methods that can be called via bind:this on the component instance.
| Method | Signature | Description |
|---|---|---|
focus() |
() => void |
Programmatically focuses the underlying <input> or <textarea> element. |
blur() |
() => void |
Programmatically removes focus from the underlying <input> or <textarea> element. |
getInputRef() |
() => HTMLInputElement | HTMLTextAreaElement | null |
Returns a reference to the underlying DOM element. Use for custom focus management or third-party library integration. |
Events
| Event | Type | Description |
|---|---|---|
| oninput | (value: string, event: Event) => void |
Fires on every input change. Receives the current input value (after any text transformer processing for tel type) and the original DOM Event. |
| onfocus | (event: FocusEvent) => void |
Fires when the input element gains focus. |
| onfocusout | (event: FocusEvent) => void |
Fires when the input element loses focus. Internally, if the validation state is 'InProgress' and the value is non-empty, the state transitions to 'Invalid' on blur. |
| onblur | (event: FocusEvent) => void |
Fires when the input element loses focus, alongside onfocusout. Provided as a convenience alias for consumers who prefer the blur event naming convention. |
| onpaste | (event: ClipboardEvent) => void |
Fires when content is pasted into the input. For dataType='tel', the pasted text is filtered to digits only and trimmed to maxLength. |
| onclick | (event: MouseEvent) => void |
Fires when the input element is clicked. |
| onstatechange | (state: ValidationState) => void |
Fires whenever the validation state changes. Receives the new ValidationState ('Valid', 'InProgress', or 'Invalid'). Runs as a reactive $effect so it fires on initial render and every state change. |
| onkeydown | (event: KeyboardEvent) => void |
Fires when a key is pressed while the input has focus. |
CSS Variables
Override these custom properties to theme the component.
| Variable | Default | CSS Property | Description |
|---|---|---|---|
--input-box-sizing |
border-box |
box-sizing | Box sizing model for the input element. |
--input-height |
fit-content |
height | Height of the input element. |
--input-background |
white |
background-color | Background color of the input. |
--input-font-size |
16px |
font-size | Font size of the input text. |
--input-font-family |
inherit |
font-family | Font family of the input text. |
--input-radius |
4px |
border-radius | Corner rounding of the input. |
--input-padding |
16px |
padding | Inner padding of the input. |
--input-font-weight |
500 |
font-weight | Font weight of the input text. |
--input-width |
fit-content |
width | Width of the input element. |
--input-margin |
0px 0px 12px 0px |
margin | Outer margin of the input element. |
--input-box-shadow |
0px 1px 8px #2f537733 |
box-shadow | Box shadow around the input. |
--input-border |
1px solid transparent |
border | Border of the input in its normal state. |
--input-visibility |
visible |
visibility | Controls input visibility (visible/hidden). |
--input-text-align |
left |
text-align | Text alignment inside the input. |
--input-text-transform |
none |
text-transform | Text transform for the input value (e.g. uppercase). |
--input-text-color |
- |
color | Color of the input text. |
--input-focus-border |
1px solid transparent |
border | Border of the input when focused. |
--input-container-margin |
- |
margin | Outer margin of the input container. |
--input-container-padding |
- |
padding | Inner padding of the input container. |
--input-container-width |
- |
width | Width of the input container. |
--input-label-msg-text-weight |
400 |
font-weight | Font weight of the label text. |
--input-label-msg-text-size |
12px |
font-size | Font size of the label text. |
--input-label-msg-text-color |
#4d6174 |
color | Color of the label text. |
--input-label-msg-margin |
0px 0px 6px 0px |
margin | Margin around the label. |
--input-label-msg-padding |
- |
padding | Padding inside the label. |
--input-error-msg-text-weight |
400 |
font-weight | Font weight of the error message. |
--input-error-msg-text-size |
12px |
font-size | Font size of the error message. |
--input-error-msg-text-color |
#c5120a |
color | Color used for the error border and error message text (6.06:1 on white, AA). |
--input-mandatory-color |
var(--input-error-msg-text-color, #c5120a) |
color | Color of the mandatory asterisk. |
--input-mandatory-gap |
2px |
margin-left | Gap between the label text and the mandatory asterisk. |
--input-icon-size |
20px |
width / height | Box size of the leftIcon / rightIcon. |
--input-icon-gap |
12px |
left / right | Icon inset from the field edge (also drives the field's icon-side padding). |
--input-icon-color |
inherit |
color | Color of both rendered icons unless the leading icon has its own override. |
--input-left-icon-color |
var(--input-icon-color, inherit) |
color | Leading-icon-only colour override; takes precedence over --input-icon-color. |
--input-icon-focus-outline |
2px solid var(--input-focus-border-color, #005fcc) |
outline | Outline shown on icon buttons when focused via keyboard (:focus-visible). |
--input-icon-focus-outline-offset |
2px |
outline-offset | Offset of the focus outline from the icon button edge. |
--input-icon-focus-radius |
2px |
border-radius | Border-radius of the focus outline on icon buttons. |
--input-error-msg-margin |
- |
margin | Margin around the error message. |
--input-error-msg-padding |
- |
padding | Padding inside the error message. |
--input-info-msg-text-weight |
400 |
font-weight | Font weight of the info message. |
--input-info-msg-text-size |
12px |
font-size | Font size of the info message. |
--input-info-msg-text-color |
#52525b |
color | Color of the info message text. Deliberately neutral, not the error color — see Accessibility. |
--input-info-msg-margin |
- |
margin | Margin around the info message. |
--input-info-msg-padding |
- |
padding | Padding inside the info message. |
--input-placeholder-color |
- |
color | Color of placeholder text. |
--input-error-border |
1px solid var(--input-error-msg-text-color, #c5120a) |
border | Border of the input when in error state. |
--input-char-count-size |
12px |
font-size | Font size of the textarea character counter. |
--input-char-count-color |
#98a2b3 |
color | Color of the character counter. |
--input-char-count-limit-color |
#c5120a |
color | Counter color when the value reaches maxLength. |
--input-char-count-margin |
4px 0 0 |
margin | Margin around the character counter. |
Phone numbers
dataType="tel" with no validationPattern checks only the characters a phone number
may contain — digits, spaces, +, -, ., () — and at most the 15 digits E.164
allows. It asserts nothing about any national format, so +33 6 12 34 56 78,
(555) 123-4567 and 9876543210 are all accepted, and 555-CALL-NOW is not.
This changed. tel previously required ten digits beginning 6-9 — an Indian mobile
number — unconditionally, so a consumer in any other market had correct input silently
marked Invalid by a rule they never chose. That rule is still shipped, as a preset you
ask for:
<script>
import { Input, TEL_PRESET_IN_MOBILE } from '@juspay/svelte-ui-components';
</script>
<Input
bind:value
dataType="tel"
validationPattern={TEL_PRESET_IN_MOBILE.valid}
inProgressPattern={TEL_PRESET_IN_MOBILE.inProgress}
onErrorMessage="Enter a 10-digit Indian mobile number"
/>
Calling the validator directly takes the preset as an option instead:
import { validateInput, TEL_PRESET_IN_MOBILE } from '@juspay/svelte-ui-components';
validateInput(value, 'tel', null, null, [], { telPreset: TEL_PRESET_IN_MOBILE });
TEL_PRESETS holds every preset the library ships, keyed by name ('in-mobile').
Accessibility
- The error message is a
role="alert"live region, and both it andinfoMessageare referenced by the field'saria-describedby(error first, then helper text) — a screen-reader user hears the same description a sighted user sees, in the same order. infoMessagerenders in a neutral color (--input-info-msg-text-color), distinct from the error message's red (--input-error-msg-text-color). They used to share the same default, so helper text with no error present visually read as a failed field — color was the only thing telling them apart, which is what WCAG 1.4.1 (Use of Color) exists to prevent. Consumers who want helper text to look red can still set--input-info-msg-text-colorthemselves.- Both message colors default to values that clear WCAG AA contrast (4.5:1) against a white background at their 12px size.
Type Reference
Custom types used by this component's props and events:
InputDataType
type InputDataType =
| 'text'
| 'tel'
| 'password'
| 'email'
| 'number'
| 'time'
| 'date'
| 'search'
| 'url';
Only text, tel, password and email are validated by validateInput. The other
five are rendered and submitted normally and are never reported Invalid by it.
CustomValidator
type CustomValidator = (
inputValue: string,
currentValidationState: ValidationState
) => ValidationState;
TextTransformer
type TextTransformer = (text: string) => string;
ValidationState
type ValidationState = 'Valid' | 'InProgress' | 'Invalid';
Web Component
Tag: <sui-input>
<sui-input placeholder="Enter email" data-type="email" label="Email"></sui-input>
Validation errors are announced
When a field is invalid, Input marks it aria-invalid="true" and links it to its message with
aria-describedby; the message element is a role="alert" live region, so assistive technology
speaks it as it appears instead of leaving it as pixels on screen. Both attributes are absent
while the field is valid, so a healthy form does not announce every field as broken.
<Input
bind:value={email}
label="Work email"
forceError={serverRejected}
onErrorMessage="Enter a valid email address"
/>
forceError drives this from server-side or runtime validation; validationPattern drives it
from the field's own contents. Either route produces the same announced message.
infoMessage is treated the same way: helper text is part of the field's description, so it
is referenced by aria-describedby whether or not the field is currently invalid. When both
are shown the field references them in reading order — the error first, then the advice —
since aria-describedby takes a space-separated id list and assistive technology announces
them in the order given.
Slots
| Slot Name | Maps to Snippet | Description |
|---|---|---|
left-icon |
leftIcon |
Icon inside the field on the leading edge. Nothing renders when the slot is empty, and the field keeps its normal padding — there is no built-in icon. |
right-icon |
rightIcon |
Icon inside the field on the trailing edge. Nothing renders when the slot is empty, and the field keeps its normal padding — there is no built-in icon. |
label-suffix |
labelSuffix |
Content beside the visible label. Nothing renders, and the label stays a bare label, when both label-suffix and labelSuffix are absent. Ignored when label is absent or actionInput is set, matching the Svelte prop. |
The element claims the snippet only when the slot is actually filled, which matters here beyond
the glyph: supplying leftIcon/rightIcon is also what adds
calc(var(--input-icon-size, 20px) + var(--input-icon-gap, 12px) * 2) of padding to the control,
so an always-claimed snippet would indent every field around an empty box. For the same reason a
JavaScript-assigned leftIcon/rightIcon property keeps working when the slot is empty, while
slotted markup wins when it is not.
onlefticonclick/onrighticonclick apply to slotted content too: with a handler set, the icon
becomes a real <button> wrapping whatever the slot projects.
Web Component Events
onfocusout, onstatechange, onlefticonclick, and onrighticonclick are available as JS
properties, and each also dispatches a same-named DOM custom event (bubbles, composed) for a
consumer who only calls addEventListener — each carries its own single argument as detail
(focusout's is the FocusEvent, statechange's is the ValidationState, and the two icon
click events carry no argument, since neither callback takes one):
const input = document.querySelector('sui-input');
input.addEventListener('focusout', (e) => console.log(e.detail.target));
input.addEventListener('statechange', (e) => console.log(e.detail));
onfocus, onblur, oninput, onpaste, onclick, and onkeydown are JS-property callbacks
only and do not dispatch DOM events: all six names are already native HTMLElement/form
handlers, so a listener for any of them already sees the real event that bubbles out of the
shadow root on its own, and a synthetic duplicate under the same name would double-deliver it.
Native consumer integration
nativeAttrs forwards typed native attributes to the actual control/media element.
Use dedicated props for component-owned state, source, classes, events, and test
hooks; those props retain precedence over an attribute spread. element is a
bindable native reference for Svelte controllers that measure, focus, animate, or
manage media. unstyled renders the native element without layout wrappers or
component appearance, so a host's centralized recipe can preserve its composition.
Default rendering and appearance remain unchanged when these props are omitted.
Native pattern and maxlength constraints can coexist with library validation.
An external aria-describedby is combined with generated hint/error IDs, and a
caller-supplied aria-invalid takes precedence. In unstyled mode the host owns
labels, hints, and error presentation; supply their relationships explicitly.
native preserves browser input and paste behavior without telephone digit
normalization or library validation callbacks. Native constraints (required,
pattern, lengths, and the input type) still apply; the host owns validation and
error presentation, with forceError available for explicit server errors.
unstyled and native are independent: one controls appearance, the other form
mechanics. This lets a consumer replace a native field without silently changing
the value it submits.
Native form resets
Input and textarea bindings synchronize to the native defaultValue after a
permitted form reset. Cancelled resets preserve the edited value. Native undo
continues to use the browser's edit history, and reset synchronization does not
synthesize an input callback.