Components
Search
Search allows users to quickly find relevant content on a website or in an application. The component consists of a search field, with or without a search button.
React
Press Enter to start editing
const PreviewEn = () => { return ( <Search> <Search.Input aria-label='Søk' /> <Search.Clear /> <Button>Search</Button> </Search> ); }; render(<PreviewEn />)
Usage
Examples
Variants
Based on how sub-components, and the structure of the HTML, you can change how Search is displayed.
React
Press Enter to start editing
const VariantsEn = () => { return ( <> <Search> <Search.Input aria-label='Søk' /> <Search.Clear /> </Search> <Divider style={{ marginTop: 'var(--ds-size-4)' }} /> <Search> <Search.Input aria-label='Søk' /> <Search.Clear /> <Button>Search</Button> </Search> <Divider style={{ marginTop: 'var(--ds-size-4)' }} /> <Search> <Search.Input aria-label='Søk' /> <Search.Clear /> <Button variant='secondary'>Search</Button> </Search> </> ); }; render(<VariantsEn />)
With Label
If you want a label on the search field, you need to add a Label component that you connect with Search.Input. In the example, we have used Field to get a connection between Label and Search.Input.
React
Press Enter to start editing
const WithLabelEn = () => { return ( <Field> <Label>Search for cats</Label> <Search> <Search.Input name='cat-search' /> <Search.Clear /> <Button>Search</Button> </Search> </Field> ); }; render(<WithLabelEn />)
<form>
React
Press Enter to start editing
const Form = () => { const [value, setValue] = useState<string>(); const [submittedValue, setSubmittedValue] = useState<string>(); return ( <> <form onSubmit={(e) => { // Prevent navigation from Storybook e.preventDefault(); setSubmittedValue(value); }} > <Search> <Search.Input aria-label='Søk' value={value} onChange={(e) => setValue(e.target.value)} /> <Search.Clear /> <Search.Button /> </Search> </form> <Paragraph data-size='md' style={{ marginTop: 'var(--ds-size-2)' }}> Submitted value: {submittedValue} </Paragraph> </> ); }; render(<Form />)
HTML
You use the class name ds-search on a wrapper element around <input>.
If you want a "clear" button, attach a <button> with type="reset"
It is important that you add an empty placeholder to <input> if you have a "clear" button
CSS variables and data attributes
| Name | Value |
|---|---|
| --dsc-search-padding-inline | var(--ds-size-2) |
| --dsc-search-clear-padding | var(--ds-size-1) |
| --dsc-search-clear-size | var(--ds-size-9) |
| --dsc-search-clear-icon-url | url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24'%3E%3Cpath fill='currentColor' d='M6.53 5.47a.75.75 0 0 0-1.06 1.06L10.94 12l-5.47 5.47a.75.75 0 1 0 1.06 1.06L12 13.06l5.47 5.47a.75.75 0 1 0 1.06-1.06L13.06 12l5.47-5.47a.75.75 0 0 0-1.06-1.06L12 10.94z'/%3E%3C/svg%3E") |
| --dsc-search-magnifying-icon-url | url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24'%3E%3Cpath d='M10.5 3.25a7.25 7.25 0 1 0 4.57 12.88l5.41 5.41a.75.75 0 1 0 1.06-1.06l-5.41-5.41A7.25 7.25 0 0 0 10.5 3.25M4.75 10.5a5.75 5.75 0 1 1 11.5 0 5.75 5.75 0 0 1-11.5 0'/%3E%3C/svg%3E") |
| --dsc-search-magnifying-icon-size | var(--ds-size-7) |
| --dsc-button-size | var(--dsc-search-clear-size) |
No relevant data attributes found.
Edit this page on github.com (opens in a new tab)