Components
Input
Input is a form element used to collect user data. It offers basic functionality and is ideal when you need full control over the component's layout and validation, making it suitable for building custom elements.
<input class="ds-input" type="text" aria-label="input"/>const Preview = () => { return <Input aria-label='input' />; }; render(<Preview />)
Usage
The class ds-input is used on most form controls, such as <input>, <textarea>, and <select>.
<input class="ds-input" />
<textarea class="ds-input" />
<select class="ds-input" />
For a valid form element, we recommend using <ds-field> together with a <label>.
<ds-field class="ds-field">
<label class="ds-label">Checkbox</label>
<input class="ds-input" />
</ds-field>
CSS variables and data attributes
Sizes are controlled with data-size and colors with data-color. The component will inherit from the closest parent where these are set.
| Name | Value |
|---|---|
| --dsc-input-accent-color | var(--ds-color-base-default) |
| --dsc-input-accent-color--invalid | var(--ds-color-danger-text-subtle) |
| --dsc-input-background | var(--ds-color-neutral-surface-default) |
| --dsc-input-background--readonly | var(--ds-color-neutral-surface-tinted) |
| --dsc-input-border-color | var(--ds-color-neutral-border-default) |
| --dsc-input-border-color--readonly | var(--ds-color-neutral-border-subtle) |
| --dsc-input-border-radius | var(--ds-border-radius-md) |
| --dsc-input-border-style | solid |
| --dsc-input-border-width | var(--ds-border-width-default) |
| --dsc-input-border-width--toggle | max(var(--ds-border-width-default), var(--ds-size-1) / 2) |
| --dsc-input-color | var(--ds-color-neutral-text-default) |
| --dsc-input-color--readonly | var(--ds-color-neutral-text-default) |
| --dsc-input-color-placeholder | color-mix(in srgb, var(--dsc-input-color), transparent 30%) |
| --dsc-input-font-size | calc(max(1rem, 1em)) |
| --dsc-input-line-height | var(--ds-line-height-md) |
| --dsc-input-outline-color--hover | var(--ds-color-neutral-border-default) |
| --dsc-input-outline-color--toggle--hover | var(--dsc-input-accent-color) |
| --dsc-input-outline-style--hover | solid |
| --dsc-input-outline-width--hover | var(--ds-border-width-default) |
| --dsc-input-padding | var(--dsc-input-padding-block) var(--dsc-input-padding-inline) |
| --dsc-input-padding-block | var(--ds-size-2) |
| --dsc-input-padding-inline | var(--ds-size-3) |
| --dsc-input-size | var(--ds-size-12) |
| --dsc-input-size--toggle | var(--ds-size-6) |
| --dsc-input-stroke-color | var(--ds-color-base-contrast-default) |
| --dsc-input-stroke-color--invalid | var(--ds-color-danger-base-contrast-default) |
| --dsc-input-stroke-width | 0.05em |
| Name | Value |
|---|---|
| data-width | auto |
Validation and error state
By design, the Input does not automatically receive error styling based on the browser's built-in :invalid state.
To give you control over when the error state is displayed, we recommend instead showing and hiding an associated Validation message with the hidden attribute.
When the Validation message and the Input are placed within the same Field, the Input automatically receives aria-invalid="true" when necessary.
Examples
ReadOnly
<ds-field class="ds-field">
<label class="ds-label">Fødselsnummer</label>
<input
class="ds-input"
type="text"
readOnly=""
value="12345678901"
/>
</ds-field>const ReadOnly = () => { return ( <Field> <Label>Fødselsnummer</Label> <Input readOnly value='12345678901' /> </Field> ); }; render(<ReadOnly />)
React
import { Input, Label } from "@digdir/designsystemet-react";
<>
<Label htmlFor="my-input">Label</Label>
<Input id="my-input" />
</>;
Input
- type
- Description
Supported `input` types
- Type
"number" | "hidden" | "color" | "checkbox" | "date" | "datetime-local" | "email" | "file" | "month" | "password" | "radio" | "search" | "tel" | "text" | "time" | "url" | "week"- Default
text
- size
- Description
Defines the width of `Input` in count of characters.
- Type
number
- disabled
- Description
Disables element @note Avoid using if possible for accessibility purposes
- Type
boolean
- readOnly
- Description
Toggle `readOnly`
- Type
boolean
- role
- Description
Set role, i.e. `switch` when `checkbox` or `radio`
- Type
AriaRole
- data-indeterminate
- Description
Indeterminate state for checkbox inputs Only works when used inside `Field` component
- Type
boolean- Default
false
| Name | Type | Default | Description |
|---|---|---|---|
| type | "number" | "hidden" | "color" | "checkbox" | "date" | "datetime-local" | "email" | "file" | "month" | "password" | "radio" | "search" | "tel" | "text" | "time" | "url" | "week" | text | Supported `input` types |
| size | number | - | Defines the width of `Input` in count of characters. |
| disabled | boolean | - | Disables element @note Avoid using if possible for accessibility purposes |
| readOnly | boolean | - | Toggle `readOnly` |
| role | AriaRole | - | Set role, i.e. `switch` when `checkbox` or `radio` |
| data-indeterminate | boolean | false | Indeterminate state for checkbox inputs Only works when used inside `Field` component |
Type values
Input exposes only supported type values. You can see the valid type values in the dropdown below.
<div
style="display:flex;gap:var(--ds-size-4);align-items:end;flex-wrap:wrap"
>
<ds-field class="ds-field">
<label class="ds-label">
<span data-lang="no">Velg</span>
<span data-lang="en">Choose</span>
type
</label>
<select class="ds-input" lang="en">
<option value="text" selected="">text</option>
<option value="color">color</option>
<option value="date">date</option>
<option value="datetime-local">datetime-local</option>
<option value="email">email</option>
<option value="file">file</option>
<option value="month">month</option>
<option value="hidden">hidden</option>
<option value="number">number</option>
<option value="password">password</option>
<option value="search">search</option>
<option value="tel">tel</option>
<option value="time">time</option>
<option value="url">url</option>
<option value="week">week</option>
</select>
</ds-field>
<svg aria-hidden="true" role="img"></svg>
<ds-field class="ds-field">
<label class="ds-label">type="text"</label>
<input class="ds-input" type="text"/>
</ds-field>
</div>const InputType = () => { const [type, setType] = useState<TextfieldProps['type']>('text'); return ( <div style={{ display: 'flex', gap: 'var(--ds-size-4)', alignItems: 'end', flexWrap: 'wrap', }} > <Field> <Label> <span data-lang='no'>Velg</span> <span data-lang='en'>Choose</span> type </Label> <Select defaultValue='text' lang='en' onChange={(e) => setType(e.target.value as TextfieldProps['type'])} > <Select.Option value='text'>text</Select.Option> <Select.Option value='color'>color</Select.Option> <Select.Option value='date'>date</Select.Option> <Select.Option value='datetime-local'>datetime-local</Select.Option> <Select.Option value='email'>email</Select.Option> <Select.Option value='file'>file</Select.Option> <Select.Option value='month'>month</Select.Option> <Select.Option value='hidden'>hidden</Select.Option> <Select.Option value='number'>number</Select.Option> <Select.Option value='password'>password</Select.Option> <Select.Option value='search'>search</Select.Option> <Select.Option value='tel'>tel</Select.Option> <Select.Option value='time'>time</Select.Option> <Select.Option value='url'>url</Select.Option> <Select.Option value='week'>week</Select.Option> </Select> </Field> <ArrowRightIcon aria-hidden width='3rem' height='3rem' /> <Field> <Label>{`type="${type}"`}</Label> <Input type={type} /> </Field> </div> ); }; render(<InputType />)