<!--
  GENERATED FILE - do not edit by hand.
  Source of truth: src/components/mds-input/ in the magma monorepo (usage/*.md and the
  component's JSDoc). Regenerated on every stencil build (scripts/component-docs.ts).
-->

# mds-input

> `@maggioli-design-system/magma` 2.15.0. Shadow DOM: style it only through the public CSS custom properties and the parts below.

The `<mds-input>` web component is the primary single- and multi-line text entry control of the Magma Design System. It wraps a native `<input>` (or `<textarea>` when `type="textarea"`) and layers on form association, validation, inline tips, iconography, password masking, numeric steppers, colour codes with a swatch and the picker of the platform, datalist suggestions and speech-to-text dictation.

#### Semantic Behavior

- **Form association**: The component reports its value to the surrounding `<form>`, so it submits natively. On `disabled` the reported value is cleared. A form reset brings back the value the field had at load, both shown and submitted, and drops the `'success'` or `'error'` look of a past blur, like a native input.
- **Disabled fieldset**: A disabled `<fieldset>` around the component disables it as `disabled` does, like a native control: it cannot be changed and its value is left out of the form, until the fieldset is enabled.
- **Form validity**: The validators below also decide the validity reported to the form, on every change of the value or of the rules: like a native control, an invalid field matches `:invalid` and stops the submit, the browser shows a message in the page language next to the field and focuses it, and the field switches to `'error'` as on blur. Disabled and read-only fields are left out, and `novalidate` on the `<form>` turns the check off. `type="email"` and `type="url"` check the format of the value as a native input does.
- **Validation on blur**: Validation runs when the field loses focus (and re-runs on input once invalid). A `required` field waits for that first blur too: until then, typing only updates its required tip. It drives the `variant` automatically to `'success'` or `'error'`, emits `mdsInputValidation` with the boolean result, and reverts to `'primary'` when an optional field is emptied.
- **Type-aware validators**: The active validator set is derived from `type` - `email` and `url` install the format validators of the native types, `cf` the codice fiscale format and length validators, `isbn` a checksum validator, `piva` the partita IVA one (11 digits, the last one the check digit) and `cc` the card number one (13 to 19 digits, in groups split by spaces or dashes, that pass the Luhn check) and `color` the colour code one (see Colour field); no type applies an input mask, while `required`, `min`/`max` (any number, `0` included), `minlength`/`maxlength` and `pattern` add the corresponding constraint validators. As on a native input, `pattern` has to match the whole value, an expression that does not compile sets no rule, and `number`, `date`, `time`, `textarea` and `color` ignore it. The set is rebuilt whenever one of these props changes, also after load, as with the React wrappers under SSR: a pristine field only updates its required tip, a field already validated on blur is validated again. Custom validators can be attached at runtime through the `addValidator`/`removeValidator`/`hasValidator`/`getErrors` methods.
- **Localized messages**: The messages of the built-in validators, the ones `getErrors()` returns and `mds-input-field` shows, are in the language of the page when the value is validated (`el`, `en`, `es`, `it`, English for any other).
- **Error keys**: `getErrors()` reports each failed rule under its own key: `required`, `max`, `min`, `maxlength`, `minlength`, `pattern`, `email`, `url`, `piva`, `cc`, `color`, `isbn-error`, `cf-regex` and `cf-length`, plus the keys the custom validators return. Up to 2.5.0, `maxlength` and `minlength` were both reported as `minLenght`.
- **Disabled / read-only tips**: `disabled` and `readonly` surface a persistent top tip describing the state; read-only fields also auto-select their text on focus. The top tip (this badge, and the dot of `required`) stands on the top border of the field, half outside, clear of its rounded corner by its radius plus a margin, and stays there while the field has the focus: inside the field it covered the button at its end.
- **No action that changes the value on a field the user cannot change**: a read-only or disabled field (a disabled `<fieldset>` included) has none of the buttons at its end that change the value (the steppers of a number, the dictation button, the picker of a colour); its text takes their room and its end is left to the badge of its state. The password toggle stays, since it shows the value without changing it (disabled with the field).
- **Character counter**: When `maxlength` is set a bottom counter tip shows `current / max` and shifts through fullness variants as the value approaches the limit. A `maxlength` of `0` or less is treated as unset.
- **Password masking**: With `type="password"` a toggle button reveals/hides the value; it is disabled with the field.
- **Colour field**: `type="color"` keeps a text field, where a colour code is typed and validated, with a swatch of the colour at its start and a button at its end that opens the colour picker of the platform; it keeps the sizes, the variants, `readonly`, `required`, the form and the validation, and the label and message of `mds-input-field`. The value, in `mdsInputChange` and in the form, is the colour as `#rrggbb` in lower case, the valid lowercase simple colour of HTML, without alpha. The text also takes `#rgb` and the code without `#`, in upper or lower case (what Figma copies), and the field writes it back as `#rrggbb` when it is left. Any other text, a name or `rgb()` among them, is an error under the key `color` whose message says what to write: the text stays for the user to correct and the value becomes `''`, as a native number input does with a text that is no number (the form gets `''` and the field reports `badInput`), so the event and the form only ever carry a colour or `''`. A value set by code is read the same way: `#ABC` becomes `#aabbcc`, `red` is shown with its error and the value becomes `''`. A field that is not `required` can stay empty, where a native colour input always holds `#000000`.
- **The swatch and the picker button**: the swatch is a disc of the colour of the value, as large as the glyph of an icon and where the icon goes, so it scales with `size`; while the text names no colour it is empty, its ring alone. The ring is the `border-strong` role, the 3:1 contrast a graphic needs on the fill of the field to show a white or a black colour. The button at the end, where the microphone of a text field goes, has a palette icon and the name "Choose a colour for" the name of the field in the page language, and comes after the text in the order of the keyboard: it opens the colour picker of the platform (a native `<input type="color">` hidden under it) on the colour of the value, black for an empty field; the colour picked writes the text and the value and emits `mdsInputChange` as typing does. A click on the swatch opens it too, a shortcut for the pointer: the swatch is hidden from assistive technologies and out of the order of the keyboard, the button is the control. A read-only or disabled field (a disabled `<fieldset>` included) has no button: it offers no way to change the value, the swatch still shows the colour and opens nothing, and the end of the field is left to the badge of its state. Next to the other props: the swatch takes the place of `icon`, which a colour field ignores (one mark at the start, as on `mds-chip`), with the icon `variant="ai"` adds; `await` leaves the swatch, which shows the value, and puts its spinner at the end, before the button (at the very end of a read-only or disabled field); `mic` is ignored, since a colour code is typed or picked, not dictated, and the end holds the button.
- **Number steppers**: With `type="number"` increment/decrement buttons are rendered; their placement follows `controlsLayout` and their glyphs follow `controlsIcon`. The side buttons of the horizontal layout are as tall as the field, with the icon of an `sm` button at `md` and `sm`, as the other buttons inside the field. A read-only or disabled number has no steppers: it is a plain field, with the padding of any field and its text at the start (the centring of the horizontal layout belongs to the buttons on its sides). The arrow keys still step the value of a field the user can change.
- **A fill a step off its surface**: on a page the field paints `surface-overlay`; inside a surface that paints `overlay` (the window of a dialog, `mds-banner`, `mds-status-bar`, `mds-dropdown`, `mds-table`) it paints `surface-default`, the step below, passed down as `--magma-field-fill`, so the field never melts into the window. `--mds-input-background` still sets the fill outright.
- **Speech-to-text**: When `mic` is set a dictation button transcribes speech into the value, replacing what was there, and emits `mdsInputSpeechEnd` when the user stops the dictation; when the Web Speech API is unavailable the button switches to an error state and the value is left as it is. A disabled or read-only field (a disabled `<fieldset>` included), which the user cannot change, has no dictation button, and leaves the end of the field to the badge of its state; a dictation in progress stops at once, emitting `mdsInputSpeechEnd`, when the field becomes so.
- **Emitted events**: `mdsInputChange` carries the new value, `mdsInputKeydown` fires on every key pressed in the field with the native `KeyboardEvent` as detail (before the value changes: read the value from `mdsInputChange`), and `mdsInputFocus`/`mdsInputBlur` fire on focus transitions.

#### Properties & Visual Configurations

The shared `variant` ladder (color role) is defined in [`agents/variants.md`](../../../../agents/variants.md); `<mds-input>` consumes it for both intent styling and validation feedback, but does not expose a `tone` prop. It accepts `primary` (default), `ai`, `info`, `success`, `warning` and `error`.

- **`type`** selects both the rendered control and the validation profile: it covers the native HTML types (`color` as a text field with a swatch and a picker button, see Colour field) plus format types (`'cf'`, `'piva'`, `'isbn'`, `'cc'`) and switches the host to a multi-line `<textarea>` for `'textarea'`.
- **`variant="ai"`** auto-assigns the AI chatbot icon when no `icon` is otherwise provided, signposting AI-assisted fields.
- **`size`** sets the height of the field, the same as [`<mds-button>`](../mds-button/AGENTS.md): `sm` 28px, `md` 36px, `lg` 48px (default). Match it to the buttons next to the field, or go smaller in dense layouts (filter bars, table toolbars); the text style stays the same, the padding and the inner icon, stepper buttons and password and microphone toggles scale with it. A `textarea` keeps its own height (`--mds-input-textarea-min-height`).

#### Other behavioral props

- **`icon`** is an SVG filename slug from the Magma icon library shown at the leading (left) edge of the field; it is suppressed while `await` is active, since the spinner takes its place. A `color` field ignores it: its swatch stands there.
- **`controlsLayout`** chooses between stacked (`'vertical'`) and split (`'horizontal'`) stepper buttons, and **`controlsIcon`** picks arrow vs. plus/minus glyphs - both apply only to `type="number"`.
- **`datalist`** supplies suggestion options for `type="search"` style autocompletion.
- **`tip`** renders inline helper text below the field, and **`typography`** selects the field's text scale.

## Read next

- [`examples.md`](examples.md) - correct usage, from the most common case
- [`mistakes.md`](mistakes.md) - the wrong forms, each with the right one
- [Storybook](https://magma-design-system.github.io/magma/storybook/?path=/docs/form-input--docs) - the live examples, in the published Storybook of `main`
- [`conventions.md`](../../../../agents/conventions.md), [`variants.md`](../../../../agents/variants.md), [`mistakes.md`](../../../../agents/mistakes.md) - the rules shared by every component

## Props

| Prop | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `accessibleName` | `aria-label` | `string \| undefined` | - | The accessible name of the native control: the label a screen reader announces. An `mds-input-field` around the component passes its own label down here, so the attribute is only written by hand when the input stands on its own. The placeholder is deliberately not a fallback: it disappears as soon as the field is filled. |
| `autocomplete` | `autocomplete` | `"additional-name" \| "address" \| "address-level1" \| "address-level2" \| "address-level3" \| "address-level4" \| "address-line1" \| "address-line2" \| "address-line3" \| "bday" \| "bday-day" \| "bday-month" \| "bday-year" \| "cc-additional-name" \| "cc-csc" \| "cc-exp" \| "cc-exp-month" \| "cc-exp-year" \| "cc-family-name" \| "cc-given-name" \| "cc-name" \| "cc-number" \| "cc-type" \| "country" \| "country-name" \| "current-password" \| "email" \| "family-name" \| "given-name" \| "honorific-prefix" \| "honorific-suffix" \| "impp" \| "language" \| "name" \| "new-password" \| "nickname" \| "off" \| "on" \| "one-time-code" \| "organization" \| "organization-title" \| "photo" \| "postal-code" \| "sex" \| "street-address" \| "tel" \| "tel-area-code" \| "tel-country-code" \| "tel-extension" \| "tel-local" \| "tel-national" \| "transaction-amount" \| "transaction-currency" \| "url" \| "username" \| undefined` | `'off'` | Specifies whether the element should have autocomplete enabled |
| `autofocus` | `autofocus` | `boolean` | `false` | Specifies that the element should automatically get focus when the page loads |
| `await` | `await` | `boolean` | `false` | Specifies if the spinner icon is shown, replacing the icon if present. A `color` field keeps its swatch, which shows the value, and shows the spinner at its end, before the picker button (read-only or disabled, the field has no picker button and the spinner takes the end) |
| `controlsIcon` | `controls-icon` | `"arithmetic" \| "arrow" \| undefined` | `'arrow'` | Specifies the icon type of the counter buttons when the input type is set to `number` |
| `controlsLayout` | `controls-layout` | `"horizontal" \| "vertical" \| undefined` | `'vertical'` | Specifies the layout of the counter buttons when the input type is set to `number`: stacked at the end (`vertical`) or one at each end (`horizontal`). A read-only or disabled field has no counter buttons, and its text takes the room they leave |
| `datalist` | - | `string[] \| undefined` | - | A list of search terms to be searched from the input field, it should be used with type="search" input. |
| `disabled` | `disabled` | `boolean \| undefined` | `false` | If true, the element is displayed as disabled. The buttons that change the value (the counter buttons of a number, the microphone, the picker of a colour) are not shown: the end of the field is left to the badge of its state |
| `icon` | `icon` | `string \| undefined` | - | An icon displayed at the left of the input, inside the field; the spinner of `await` takes its place. A `color` field ignores it: its swatch stands there, one mark at the start |
| `max` | `max` | `number \| string \| undefined` | - | Specifies the maximum value, with `type="number"` or `type="date"`: a number, or a date in ISO format (YYYY-MM-DD) such as `2046-12-04` |
| `maxlength` | `maxlength` | `number \| undefined` | - | Specifies the maximum number of characters allowed in an element. Use it with input type="text". If maxlength is set to 0 or a negative number it will be considered as undefined. |
| `mic` | `mic` | `boolean \| undefined` | - | Toggles text recognition. A read-only or disabled field has no dictation button. A `color` field ignores it: a colour code is typed or picked, not dictated, and its end holds the button of the picker |
| `min` | `min` | `number \| string \| undefined` | - | Specifies the minimum value, with `type="number"` or `type="date"`: a number, or a date in ISO format (YYYY-MM-DD) such as `1988-04-15` |
| `minlength` | `minlength` | `number \| undefined` | - | Specifies the minimum number of characters allowed in an element use it with input type="number" |
| `name` | `name` | `string \| undefined` | - | Is needed to reference the form data after the form is submitted |
| `pattern` | `pattern` | `string \| undefined` | - | Specifies a regular expression the whole value has to match, as the pattern attribute of a native input: a value that does not match stops the submit of the form |
| `placeholder` | `placeholder` | `string \| undefined` | - | Specifies a short hint that describes the expected value of the element |
| `readonly` | `readonly` | `boolean \| undefined` | `false` | Specifies that the element is read-only. The buttons that change the value (the counter buttons of a number, the microphone, the picker of a colour) are not shown, as on a disabled field; the password toggle stays, since it shows the value without changing it |
| `required` | `required` | `boolean \| undefined` | `false` | Specifies that the element must be filled out before submitting the form |
| `size` | `size` | `"lg" \| "md" \| "sm" \| undefined` | `'lg'` | Specifies the height of the field, the same as `mds-button`: `sm` 28px, `md` 36px, `lg` 48px. A textarea keeps its own height (`--mds-input-textarea-min-height`) |
| `step` | `step` | `string \| undefined` | - | Specifies the interval between legal numbers in an input field |
| `tip` | `tip` | `string \| undefined` | - | Sets the word(s) of the tip of the input field |
| `type` | `type` | `"cc" \| "cf" \| "color" \| "date" \| "email" \| "isbn" \| "number" \| "password" \| "piva" \| "search" \| "tel" \| "text" \| "textarea" \| "time" \| "url" \| undefined` | `'text'` | Specifies the type of input element. `color` holds a colour: the field stays a text field, where the code is typed and validated, with a swatch of the colour at its start and a button at its end that opens the colour picker of the platform. Its value, in the change event and in the form, is the colour as `#rrggbb` in lower case, or `''` while the text is empty or names no colour, which is a validation error; the text also takes `#rgb` and the code without `#`, in any case, and is written back as `#rrggbb` when the field is left |
| `typography` | `typography` | `"detail" \| "snippet"` | `'detail'` | Specifies the typography of input element |
| `value` | `value` | `string` | `''` | Specifies the value of the input element. With `type="color"`, a colour as `#rrggbb`: a value set as `#rgb`, without `#` or in upper case is written so, and one that names no colour is shown as it is, with its error, while the value becomes `''` |
| `variant` | `variant` | `"ai" \| "error" \| "info" \| "primary" \| "success" \| "warning" \| undefined` | `'primary'` | Sets the variant of the input field |

## Events

| Event | Detail | Description |
| --- | --- | --- |
| `mdsInputBlur` | `void` | Emits a void event when input element is blurred |
| `mdsInputChange` | `MdsInputEventDetail` | Emits an InputChangeEventDetail when the value of the input element changes |
| `mdsInputFocus` | `void` | Emits a void event when input element is focused |
| `mdsInputKeydown` | `KeyboardEvent` | Emits a KeyboardEvent when a keyboard key is pressed on the focused input element |
| `mdsInputSpeechEnd` | `void` | Emits a void event when input speech recognition ends |
| `mdsInputValidation` | `boolean` | Emits a boolean event when a input execute validation |

## Methods

| Method | Signature | Description |
| --- | --- | --- |
| `addValidator` | `addValidator(validator: MdsValidatorFn) => Promise<void>` | Adds a validator to the input. |
| `getErrors` | `getErrors() => Promise<MdsValidationErrors \| null>` | Returns the current validation errors, or `null` if the value is valid. |
| `getInputElement` | `getInputElement() => Promise<HTMLInputElement \| HTMLTextAreaElement>` | Returns the native `<input>` element used under the hood. |
| `hasValidator` | `hasValidator(validator?: MdsValidatorFn) => Promise<boolean>` | Returns if validator is presen |
| `removeValidator` | `removeValidator(validator: MdsValidatorFn) => Promise<void>` | Removes a previously added validator from the input. |
| `setFocus` | `setFocus() => Promise<void>` | Sets focus on the specified `my-input`. Use this method instead of the global `input.focus()`. |

## Parts

| Part | Description |
| --- | --- |
| `color-picker-button` | Selects the button that opens the colour picker of a `color` field |
| `counter-button-decrease` | Selects the button used to decrease the input value |
| `counter-button-increase` | Selects the button used to increase the input value |
| `field` | Selects the native input field used by the component |
| `mic-toggle-button` | Selects the button that starts and stops the speech to text |
| `password-mask-item` | Selects the item used to mask the password |
| `password-mask-items` | Selects the items wrapper used to mask the password |
| `password-toggle-button` | Selects the button used to show or hide password |
| `tip-bottom` | Selects the verbose status of input on bottom of element |
| `tip-count` | Selects the mds-input-tip-item shown when maxlength attribute is set, can be used to hide it if needed |
| `tip-top` | Selects the verbose status of input on top of element |

## CSS custom properties

| Property | Description |
| --- | --- |
| `--mds-input-background` | The background color of the input field. |
| `--mds-input-ring` | The outline ring applied to the input on interactive states. |
| `--mds-input-shadow` | The box-shadow applied to the input component. |
| `--mds-input-textarea-max-height` | The max-height applied to the textarea. |
| `--mds-input-textarea-min-height` | The min-height applied to the textarea. |
| `--mds-input-textarea-resize` | Controls whether the textarea is resizable. |
| `--mds-input-tip-background` | The background color of the input tip/inline help. |
| `--mds-input-variant-color-rgb` | The raw RGB channels used for variant-driven styling. |

Only these are public. Its other 8 are internal: they may change in any release, do not set them.

## Dependencies

Uses: `mds-button`, `mds-input-tip`, `mds-input-tip-item`, `mds-icon`, `mds-spinner`.
Used by: `mds-input-otp`.
