Input

An input is a text field. Input is one line, InputTextArea several, and InputPassword, InputSearch and InputOTP are a password, a search and a one-time code. Each renders a native <input> or <textarea>.

Import
import { Input, InputTextArea, InputPassword, InputSearch, InputOTP } from 'svelte-ant-design';
Links
src/lib/Input Report an issue Component README

When to use

  • Use an input when the user has to type a short text: a name, an email, a query.
  • Use InputTextArea for longer text, InputPassword for a secret, InputSearch for a query with an action and InputOTP for a code.
  • To pick one or more items from a list, a checkbox is the better fit.

Examples

The name is "" no change yet

Basic

bind:value keeps a variable in sync with the field; onchange receives the new text and the event of the edit on every edit: the native input event, or compositionend when a browser (for example Firefox) ends an IME composition before its last input event. A field has no label of its own: name it (see Accessible names).

Sizes

large is 40px high, medium 32px, small 24px. middle and default are accepted as compatibility aliases of medium.

Variants

Four looks: outlined, filled, borderless and underlined. The borderless field draws a focus outline, because it has no border to change.

Status

error and warning change the look; error also sets aria-invalid. The statuses combine with every variant.

Disabled and read-only

A disabled field is natively disabled: it is not focusable and is not submitted. A read-only field can be focused and copied but not edited; it has no clear button.

$ USD kg

Prefix and suffix

prefix and suffix take plain text or a snippet and sit inside the border. The input keeps its place when they appear or disappear.

https:// .com https:// .org

Addons

addonBefore and addonAfter are cells outside the border, plain text or a snippet. React marks them deprecated in favor of Space.Compact, which this library does not have yet.

onclear called 0 times

Clear button

allowClear shows a native button while there is text. A press does not take focus from the field, and focus stays in it afterwards. The object form sets the accessible name, the tab index and the icon.

0 0 / 20 0 of 10 used

Character count

showCount shows the length; with maxlength it shows n / max and the browser stops the input at the limit. The object form formats the text. The count is joined to the field by aria-describedby.

24 / 10 6 words 0 / 8

Custom count logic

count.max is a soft limit: past it the count turns red and aria-invalid is set, the text is kept. strategy replaces the count, show can format it, exceedFormatter cuts the text past the limit.

Entered:

Controlled input and Enter

A function binding can refuse an edit: the field goes back to the last accepted text. onpressenter runs on Enter, but not on the Enter that confirms an IME candidate.

0 / 120 nothing yet; Enter pressed 0 times 44 / 20

TextArea

InputTextArea is a native <textarea> with the same value, clear, count, status, size and variant props. The clear button sits at the top corner and the count has its own row, so neither covers the text.

Auto size

autoSize sets the height to the content, within minRows and maxRows lines; past maxRows the field scrolls. It is measured again when the text, the width or the font changes.

The password is hidden; the first field toggled 0 times

Password

InputPassword toggles between type="password" and type="text" with a native button named "Show password"; aria-pressed follows visible. Use bind:visible, onvisiblechange, visibilityToggle and iconRender to control it.

Six cells, the default
The code is "" onchange (all filled): ""; oninput:
Four cells

One-time code

InputOTP is a group of single-character inputs. Typing moves focus on, Backspace goes back, the arrow keys move between cells and a pasted code is spread over the cells. onchange fires when every cell is filled, oninput on each change.

mask
mask with a string
formatter
separator as text
- - - - -
separator as a snippet
size, variant, status
size, variant
disabled

One-time code options

mask hides the characters, formatter changes what is shown, separator goes between cells, and size, variant, status and disabled apply to every cell. autocomplete="one-time-code" helps the browser fill a code.

$ 6 USD 15
/ / /

Semantic parts

Each component names its parts: classNames adds a class to a part and styles adds inline styles to it. Numbers are written as they are (no automatic px). class and style reach the root.

City The city you live in 0 / 80 A short line about you

Accessible names

A field has no label of its own. Name it with an external <label for>, aria-label or aria-labelledby; add hints with aria-describedby and a tooltip with title. Every attribute is forwarded to the native field.

Forms and native attributes

The fields are real inputs, so name, required and type work in a form and the form submits their text. A native reset does not change the state of the components: reset it in onreset.

API

PropertyDescriptionTypeDefault
valueThe text. Use bind:value for two-way binding; a plain value is the initial text. A function binding can refuse an edit; null counts as empty.string''
typeNative type. Text-like types are supported (text, search, email, tel, url, number). For a password use InputPassword.string'text'
disabledNative disabled: not focusable, not editable, not submitted.booleanfalse
readonlyNative readonly: focusable and copyable, not editable. The clear button is not shown.booleanfalse
sizeSize of the field: 40px, 32px or 24px high. middle and default are compatibility aliases of medium. In a SpaceCompact a control without a size takes the one of the block.'large' | 'medium' | 'middle' | 'small' | 'default''medium'
variantVisual variant. The borderless field draws a focus outline.'outlined' | 'filled' | 'borderless' | 'underlined''outlined'
statusValidation look. error also sets aria-invalid="true"; warning changes only the look.'error' | 'warning'—
prefixContent before the text, inside the border: plain text or a snippet.string | Snippet—
suffixContent after the text, inside the border, after the clear button and the count: plain text or a snippet.string | Snippet—
addonBeforeA cell before the field, outside the border: plain text or a snippet. While an addon is set, the field sits in a group element; adding the first addon or removing the last one creates the input again (it loses focus).string | Snippet—
addonAfterA cell after the field, outside the border: plain text or a snippet.string | Snippet—
allowClearShows a native clear button while there is text (not while disabled or read-only). A press keeps focus in the field and calls onchange("") then onclear. The object form sets the icon, the accessible name (default Clear) and the tab index.boolean | { clearIcon?: string | Snippet; label?: string; tabIndex?: number }false
showCountShows the length of the text. With maxlength it shows n / max. The object form formats the text; the result is a string.boolean | { formatter: (info: CountInfo) => string }false
countCharacter counting: a soft limit max (past it the count is red and the field is invalid, the text is kept), a strategy that replaces the count (the default counts code points), show (as showCount, and it wins over it) and an exceedFormatter that cuts the text.{ max?: number; strategy?: (value: string) => number; show?: boolean | ((info: CountInfo) => string); exceedFormatter?: (value: string, config: { max: number }) => string }—
maxlengthNative maxlength: the browser stops the input at the limit. For a limit that only warns use count.max.number—
onchangeCalled once per user edit with the new text and the event of the edit: the native input event, or compositionend when a browser (for example Firefox) ends an IME composition before its last input event. As onChange in React. Not called when the parent changes value, nor while disabled. The native oninput is forwarded as well and runs first.(value: string, event: Event) => void—
onpressenterCalled on Enter, but not on the Enter that confirms an IME candidate.(event: KeyboardEvent) => void—
onclearCalled after the clear button emptied the text.(event: MouseEvent) => void—
classNamesExtra classes for the parts, appended to the component's own.Partial<Record<'root' | 'prefix' | 'input' | 'suffix' | 'clear' | 'count', string>>—
stylesInline styles for the parts. Property names are kebab-case or custom properties, numbers are written as they are (no automatic px). They win over the component's styles.Partial<Record<'root' | 'prefix' | 'input' | 'suffix' | 'clear' | 'count', Record<string, string | number>>>—
classAn additional CSS class for the root.string—
styleInline style of the root; styles.root is written after it.string—
native attributesNative attributes of the input (id, name, placeholder, required, autocomplete, inputmode, title, aria-*, data-*, onfocus, onblur, onkeydown, oncompositionstart and others) are forwarded to the <input>. The component owns value, disabled, readonly, maxlength and onchange.HTMLInputAttributes—
Differences from Ant Design: onchange receives (value, event) on every edit (React passes the event); there is no defaultValue, because bind:value covers it; the parts are separate components (Svelte has no Input.TextArea); the formatters of showCount and count.show return a string (React: a node); visible and onvisiblechange of the password are props of their own (React nests them in visibilityToggle); bordered is not taken, use variant="borderless"; Input.Group and compact are not taken.

InputTextArea API

PropertyDescriptionTypeDefault
valueAs in Input.string''
disabledAs in Input.booleanfalse
readonlyAs in Input.booleanfalse
sizeAs in Input.'large' | 'medium' | 'middle' | 'small' | 'default''medium'
variantAs in Input.'outlined' | 'filled' | 'borderless' | 'underlined''outlined'
statusAs in Input.'error' | 'warning'—
allowClearAs in Input; the button sits at the top corner of the box.boolean | { clearIcon?: string | Snippet; label?: string; tabIndex?: number }false
showCountAs in Input; the count has its own row at the bottom of the box.boolean | { formatter: (info: CountInfo) => string }false
countAs in Input.{ max?: number; strategy?: (value: string) => number; show?: boolean | ((info: CountInfo) => string); exceedFormatter?: (value: string, config: { max: number }) => string }—
maxlengthAs in Input.number—
autoSizeThe height follows the content, within minRows and maxRows lines of the line height (a minimum of one line); past maxRows the field scrolls. It is measured again when the text, the width or the font changes. The manual resize handle is hidden while it is on.boolean | { minRows?: number; maxRows?: number }false
onchangeAs in Input.(value: string, event: Event) => void—
onpressenterAs in Input.(event: KeyboardEvent) => void—
onclearAs in Input.(event: MouseEvent) => void—
classNamesExtra classes for the parts, appended to the component's own.Partial<Record<'root' | 'textarea' | 'clear' | 'count', string>>—
stylesInline styles for the parts; they win over the component's styles.Partial<Record<'root' | 'textarea' | 'clear' | 'count', Record<string, string | number>>>—
classAs in Input.string—
styleAs in Input.string—
native attributesNative attributes of the textarea (rows, cols, placeholder, name, id, required, aria-*, data-*, onfocus and others) are forwarded to the <textarea>.HTMLTextareaAttributes—

InputPassword API

InputPassword takes every prop of Input except type (the component sets it) and adds these.

PropertyDescriptionTypeDefault
visibleWhether the text is shown. Use bind:visible for two-way binding; a plain value is the initial state.booleanfalse
visibilityToggleShows the toggle button. false hides it, so visible is the only control. The object form sets the accessible name (default Show password; the button has aria-pressed, so the name does not change) and the tab index.boolean | { tabIndex?: number; label?: string }true
onvisiblechangeCalled once per user toggle with the new visibility; not when the parent changes visible.(visible: boolean) => void—
iconRenderCustom content of the toggle button; it receives the current visibility.Snippet<[visible: boolean]>—

InputSearch takes every prop of Input and adds these. Its classNames and styles have one more part, button.

PropertyDescriptionTypeDefault
enterButtonA button after the field: true is a primary button with the search icon, a string or a snippet is its content. false leaves only the icon button inside the field. It conflicts with addonAfter: outside production using both throws, in production the button wins.boolean | string | Snippetfalse
loadingShows a spinner in place of the search icon; the search action (click and Enter) is ignored meanwhile.booleanfalse
onsearchCalled on a click of the icon or the button, on Enter (not while composing) and on the clear button. source is "clear" for the clear button and "input" otherwise; for the clear button the text is empty.(value: string, event: Event, info: { source: 'input' | 'clear' }) => void—
searchIconA custom search icon: plain text or a snippet.string | Snippet—
classNamesThe parts of Input and the search button.Partial<Record<'root' | 'prefix' | 'input' | 'suffix' | 'clear' | 'count' | 'button', string>>—
stylesInline styles for the same parts.Partial<Record<'root' | 'prefix' | 'input' | 'suffix' | 'clear' | 'count' | 'button', Record<string, string | number>>>—
class / styleThe root, which wraps the field and the button. type is search unless you set another.string—

InputOTP API

PropertyDescriptionTypeDefault
valueThe characters entered so far, without gaps. Use bind:value for two-way binding; a plain value is the initial text. Cell i shows the i-th character.string''
lengthNumber of cells. A value that is not a positive integer throws outside production and means 6 in production.number6
maskHides the characters: true shows a bullet, a string is the single character shown instead. The value is not changed.boolean | stringfalse
formatterMaps the whole text to the displayed text; the cell at index i shows the i-th character of the result. The mask wins over it.(value: string) => string—
separatorRendered after the cell at the given index (not after the last one): plain text, or a snippet that receives the index.string | Snippet<[index: number]>—
disabledDisables every cell.booleanfalse
sizeSize of the cells; a cell is as wide as the field is high. middle and default are aliases of medium.'large' | 'medium' | 'middle' | 'small' | 'default''medium'
variantVisual variant of the cells.'outlined' | 'filled' | 'borderless' | 'underlined''outlined'
statusValidation look of the cells; error sets aria-invalid on each.'error' | 'warning'—
autocompleteNative autocomplete of every cell; one-time-code enables autofill of a code.HTMLInputAttributes['autocomplete']—
onchangeCalled when every cell is filled, with the whole text; not for a change of value made by the parent.(value: string) => void—
oninputCalled on each change, with one entry per cell ("" for an empty one).(values: string[]) => void—
classNamesExtra classes for the root, the cells (input) and the separator.Partial<Record<'root' | 'input' | 'separator', string>>—
stylesInline styles for the same parts.Partial<Record<'root' | 'input' | 'separator', Record<string, string | number>>>—
classAn additional CSS class for the root.string—
styleInline style of the root; styles.root is written after it.string—
native attributesNative attributes of the group (id, aria-label, aria-labelledby, aria-describedby, data-*, handlers) are forwarded to the <div>, which keeps role="group". Each cell is named Character i of n (English; a locale mechanism is planned).HTMLAttributes<HTMLDivElement>—

Methods

All five components have these methods, through bind:this.

MethodDescriptionType
focusMoves focus to the field (to the first empty cell of InputOTP). cursor puts the caret at the start or the end, or selects all the text; a type with no selection (email, number) ignores it.(options?: { preventScroll?: boolean; cursor?: 'start' | 'end' | 'all' }) => void
blurRemoves focus from the field.() => void

Design tokens

TokenDescriptionDefault
--ads-input-font-sizeFont size of a medium field.14px
--ads-input-font-size-lgFont size of a large field.16px
--ads-input-font-size-smFont size of a small field.14px
--ads-input-padding-blockVertical padding of a medium field; with the line height and the border it gives the 32px height.4px
--ads-input-padding-block-lgVertical padding of a large field (40px high).7px
--ads-input-padding-block-smVertical padding of a small field (24px high).0px
--ads-input-padding-inlineHorizontal padding of a medium field.11px
--ads-input-padding-inline-lgHorizontal padding of a large field.11px
--ads-input-padding-inline-smHorizontal padding of a small field.7px
--ads-input-radiusCorner radius of a medium field.6px
--ads-input-radius-lgCorner radius of a large field.8px
--ads-input-radius-smCorner radius of a small field.4px
--ads-input-border-widthThickness of the border.1px
--ads-input-line-heightLine height of a medium and a small field.1.5714285714285714
--ads-input-line-height-lgLine height of a large field.1.5
--ads-input-gapSpace between the text and the prefix, the clear button, the count and the suffix.4px
--ads-input-icon-sizeSize of the clear, toggle and search icons.14px
--ads-input-addon-padding-inlineHorizontal padding of an addon cell.11px
--ads-input-otp-gapSpace between the cells of InputOTP.8px
--ads-input-bgBackground of the field.#ffffff
--ads-input-bg-hoverBackground on hover.#ffffff
--ads-input-bg-activeBackground while focused.#ffffff
--ads-input-borderBorder (the boundary of the field). tokens-accessible.css darkens it to #737373 to keep 3:1 against the page.#d9d9d9
--ads-input-border-hoverBorder on hover. tokens-accessible.css darkens it to #0958d9 (#4096ff is 2.99:1).#4096ff
--ads-input-border-activeBorder while focused. tokens-accessible.css darkens it to #1668dc.#1677ff
--ads-input-shadow-activeShadow ring while focused.0 0 0 2px rgba(5, 145, 255, 0.1)
--ads-input-placeholderColour of the placeholder (ours: the documentation lists none). tokens-accessible.css uses #6b6b6b for 4.5:1.rgba(0, 0, 0, 0.25)
--ads-input-icon-colorColour of the clear, toggle and search icons. tokens-accessible.css uses #737373.rgba(0, 0, 0, 0.45)
--ads-input-icon-color-hoverColour of those icons on hover.rgba(0, 0, 0, 0.65)
--ads-input-count-colorColour of the count. tokens-accessible.css uses #666666 for 4.5:1. Past a soft limit it is --ads-color-danger-text.rgba(0, 0, 0, 0.45)
--ads-input-addon-bgBackground of an addon cell.rgba(0, 0, 0, 0.02)
--ads-input-filled-bgBackground of the filled variant.rgba(0, 0, 0, 0.04)
--ads-input-filled-bg-hoverBackground of the filled variant on hover (ours).rgba(0, 0, 0, 0.06)
--ads-input-disabled-bgBackground of a disabled field.rgba(0, 0, 0, 0.04)
--ads-input-disabled-borderBorder of a disabled field.#d9d9d9
--ads-input-errorBorder of the error status.#ff4d4f
--ads-input-error-hoverBorder of the error status on hover. tokens-accessible.css darkens it to #d9363e.#ff7875
--ads-input-error-shadowShadow ring of the error status while focused.0 0 0 2px rgba(255, 38, 5, 0.06)
--ads-input-error-bgBackground of the filled variant with the error status.#fff2f0
--ads-input-warningBorder of the warning status. tokens-accessible.css darkens it to #ad6800 (1.9:1 otherwise).#faad14
--ads-input-warning-hoverBorder of the warning status on hover (ours). tokens-accessible.css uses #d46b08.#ffc53d
--ads-input-warning-shadowShadow ring of the warning status while focused.0 0 0 2px rgba(255, 215, 5, 0.1)
--ads-input-warning-bgBackground of the filled variant with the warning status.#fffbe6

Accessibility

  • The fields are native <input> and <textarea> elements: Tab, selection, autofill, spellcheck and forms come from the browser. A field has no label of its own: name it with <label for>, aria-label or aria-labelledby.
  • status="error" and a count past count.max set aria-invalid="true". The count is joined to the field by aria-describedby and is not a live region, so it does not announce every key press.
  • The clear, show-password and search buttons are native buttons with an accessible name (Clear, Show password and Search, in English; change them with the label options). They stay in the Tab order, and a press does not take focus from the field.
  • IME: onpressenter and onsearch do not fire on the Enter that confirms a candidate, and count.exceedFormatter cuts the text only after the composition ends.
  • InputOTP is a role="group" of cells named Character i of n: Backspace goes back, the arrow keys move between cells (mirrored in right-to-left layouts) and a paste fills the cells. A cell past the first empty one cannot take focus.
  • The default light colors are Ant Design's own and do not reach WCAG: the border is 1.41:1 and the placeholder 1.83:1. Import tokens-accessible.css for 3:1 on the border and 4.5:1 on the placeholder and the count; the dark theme needs no file. The filled and borderless variants have no boundary by design: use outlined or underlined where the boundary must reach 3:1.