Komponenter
Search
Search lar brukere raskt finne relevant innhold på et nettsted eller i en applikasjon. Komponenten består av et søkefelt, med eller uten en søkeknapp.
Search er avviklet og fjernes i neste hovedversjon. Les Search fjernes, hva gjør du? for å se hvordan du kan lage ditt eget søkefelt.
<input class="ds-input" type="search" aria-label="Søk"/>const Preview = () => { return <Input type='search' aria-label='Søk' />; }; render(<Preview />)
Bruk
Bruk klassenavnet ds-suggestion på en <ds-suggestion> rundt <input>.
Vil du ha en tøm-knapp, legger du til en <button type="reset"> etter <input>. Denne vises og skjules automatisk basert på verdien i input.
Du kan endre variant på button for å tilpasse visningen.
Forstørrelsesglass-ikonet vises når input har type="search", og skjules når input har type="text".
CSS variabler og data-attributter
Størrelser styres med data-size og farger med data-color. Komponenten vil arve nærmeste forelder med disse satt.
| Navn | Verdi |
|---|---|
| --dsc-button-size | var(--dsc-suggestion-clear-size) |
| --dsc-search-clear-icon-url | var(--dsc-suggestion-clear-icon-url) |
| --dsc-search-clear-padding | var(--dsc-suggestion-clear-padding) |
| --dsc-search-clear-size | var(--dsc-suggestion-clear-size) |
| --dsc-search-magnifying-icon-size | var(--dsc-suggestion-search-icon-size) |
| --dsc-search-magnifying-icon-url | var(--dsc-suggestion-search-icon-url) |
| --dsc-search-padding-inline | var(--dsc-suggestion-clear-gap) |
| --dsc-suggestion-border-color | var(--ds-color-neutral-border-subtle) |
| --dsc-suggestion-border-style | solid |
| --dsc-suggestion-border-width | var(--ds-border-width-default) |
| --dsc-suggestion-chip-background | var(--ds-color-base-default) |
| --dsc-suggestion-chip-background--hover | var(--ds-color-base-hover) |
| --dsc-suggestion-chip-border-color | transparent |
| --dsc-suggestion-chip-border-radius | var(--ds-border-radius-full) |
| --dsc-suggestion-chip-border-style | solid |
| --dsc-suggestion-chip-border-width | var(--ds-border-width-default) |
| --dsc-suggestion-chip-color | var(--ds-color-base-contrast-default) |
| --dsc-suggestion-chip-font-size | var(--ds-body-sm-font-size) |
| --dsc-suggestion-chip-gap | var(--ds-size-1) |
| --dsc-suggestion-chip-height | var(--ds-size-8) |
| --dsc-suggestion-chip-icon-size | var(--ds-icon-size) |
| --dsc-suggestion-chip-icon-url | url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='1em' height='1em' fill='none' viewBox='0 0 24 24' focusable='false' role='img'%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/path%3E%3C/svg%3E") |
| --dsc-suggestion-chip-input-size | var(--ds-size-5) |
| --dsc-suggestion-chip-padding | 0 var(--ds-size-3) |
| --dsc-suggestion-chip-spacing | calc(.5 * (var(--dsc-suggestion-chip-height) - var(--dsc-suggestion-chip-input-size))) |
| --dsc-suggestion-clear-background--active | var(--ds-color-surface-active) |
| --dsc-suggestion-clear-background--hover | var(--ds-color-surface-hover) |
| --dsc-suggestion-clear-border-radius | var(--ds-border-radius-md) |
| --dsc-suggestion-clear-gap | var(--ds-size-2) |
| --dsc-suggestion-clear-icon-url | url("data:image/svg+xml,%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-suggestion-clear-padding | var(--ds-size-1) |
| --dsc-suggestion-clear-size | var(--ds-size-9) |
| --dsc-suggestion-create-icon-url | url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24'%3E%3Cpath d='M12.75 5.5a.75.75 0 0 0-1.5 0v5.75H5.5a.75.75 0 0 0 0 1.5h5.75v5.75a.75.75 0 0 0 1.5 0v-5.75h5.75a.75.75 0 0 0 0-1.5h-5.75z'/%3E%3C/svg%3E") |
| --dsc-suggestion-create-text | 'Legg til "{value}"' |
| --dsc-suggestion-empty-text | "Tomt" |
| --dsc-suggestion-list-background | var(--ds-color-neutral-surface-default) |
| --dsc-suggestion-list-border-radius | var(--ds-border-radius-md) |
| --dsc-suggestion-list-box-shadow | var(--ds-shadow-md) |
| --dsc-suggestion-list-color | var(--ds-color-text-default) |
| --dsc-suggestion-list-gap | var(--ds-size-1) |
| --dsc-suggestion-list-offset | var(--ds-size-1) |
| --dsc-suggestion-list-padding | var(--ds-size-3) var(--ds-size-2) |
| --dsc-suggestion-list-placement | bottom |
| --dsc-suggestion-option-background--hover | var(--ds-color-surface-hover) |
| --dsc-suggestion-option-background--selected | var(--ds-color-surface-active) |
| --dsc-suggestion-option-border-radius | var(--ds-border-radius-md) |
| --dsc-suggestion-option-checkmark-border | calc(max(2px, .125rem)) |
| --dsc-suggestion-option-checkmark-border-color | var(--ds-color-neutral-border-default) |
| --dsc-suggestion-option-checkmark-size | var(--ds-size-6) |
| --dsc-suggestion-option-checkmark-url | url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%221em%22%20height%3D%221em%22%20fill%3D%22none%22%20viewBox%3D%220%200%2024%2024%22%20focusable%3D%22false%22%20role%3D%22img%22%3E%3Cpath%20fill%3D%22currentColor%22%20fill-rule%3D%22evenodd%22%20d%3D%22M18.998%206.94a.75.75%200%200%201%20.063%201.058l-8%209a.75.75%200%200%201-1.091.032l-5-5a.75.75%200%201%201%201.06-1.06l4.438%204.437%207.471-8.405A.75.75%200%200%201%2019%206.939%22%20clip-rule%3D%22evenodd%22%3E%3C%2Fpath%3E%3C%2Fsvg%3E") |
| --dsc-suggestion-option-gap | var(--ds-size-2) |
| --dsc-suggestion-option-padding | var(--ds-size-3) |
| --dsc-suggestion-search-icon-size | var(--ds-icon-size) |
| --dsc-suggestion-search-icon-spacing | var(--ds-size-3) |
| --dsc-suggestion-search-icon-url | url("data:image/svg+xml,%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-suggestion-sr-added | "La til" |
| --dsc-suggestion-sr-clear | "Fjern tekst" |
| --dsc-suggestion-sr-empty | "Ingen valgte" |
| --dsc-suggestion-sr-found | "Naviger til venstre for å finne %d valgte" |
| --dsc-suggestion-sr-invalid | "Ugyldig valg" |
| --dsc-suggestion-sr-items | "Valgte" |
| --dsc-suggestion-sr-of | "av" |
| --dsc-suggestion-sr-plural | "%d forslag" |
| --dsc-suggestion-sr-remove | "Trykk for å fjerne" |
| --dsc-suggestion-sr-removed | "Fjernet" |
| --dsc-suggestion-sr-singular | "%d forslag" |
| --dsc-suggestion-sr-toggle | "Forslag" |
| --dsc-suggestion-toggle-icon-url | url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M5.97 9.47a.75.75 0 0 1 1.06 0L12 14.44l4.97-4.97a.75.75 0 1 1 1.06 1.06l-5.5 5.5a.75.75 0 0 1-1.06 0l-5.5-5.5a.75.75 0 0 1 0-1.06'/%3E%3C/svg%3E") |
| Navn | Verdi |
|---|---|
| data-activedescendant | |
| data-creatable | |
| data-empty | |
| data-multiple | |
| data-overscroll | contain |
Eksempel
Med Input
I de fleste tilfeller holder det med en Input med type="search" i en Field.
<ds-field class="ds-field">
<label class="ds-label">Søk</label>
<input class="ds-input" type="search"/>
</ds-field>const WithInput = () => { return ( <Field> <Label>Søk</Label> <Input type='search' /> </Field> ); }; render(<WithInput />)
Med label
Dersom du vil ha label på søkefeltet, må du legge til label som du kobler sammen med Suggestion.Input. I eksempelet har vi brukt Field for å koble sammen label og Suggestion.Input.
<ds-field class="ds-field">
<label class="ds-label">Søk etter katter</label>
<ds-suggestion class="ds-suggestion">
<input
class="ds-input"
type="text"
placeholder=""
name="cat-search"
/>
<button hidden="" type="reset"></button>
<button class="ds-button" type="submit">Søk</button>
</ds-suggestion>
</ds-field>const WithLabel = () => { return ( <Field> <Label>Søk etter katter</Label> <EXPERIMENTAL_Suggestion> <EXPERIMENTAL_Suggestion.Input type='text' name='cat-search' /> <EXPERIMENTAL_Suggestion.Clear /> <Button type='submit'>Søk</Button> </EXPERIMENTAL_Suggestion> </Field> ); }; render(<WithLabel />)
<form>
<form>
<ds-suggestion class="ds-suggestion">
<input
class="ds-input"
type="text"
placeholder=""
aria-label="Søk"
name="search"
/>
<button hidden="" type="reset"></button>
<button class="ds-button" type="submit">Søk</button>
</ds-suggestion>
</form>
<p
class="ds-paragraph"
data-size="md"
style="margin-top:var(--ds-size-2)"
>Submitted value:</p>const Form = () => { const [submittedValue, setSubmittedValue] = useState<string>(); return ( <> <form onSubmit={(e) => { // Prevent navigation from Storybook e.preventDefault(); setSubmittedValue( new FormData(e.currentTarget).get('search')?.toString(), ); }} > <EXPERIMENTAL_Suggestion> <EXPERIMENTAL_Suggestion.Input type='text' name='search' aria-label='Søk' /> <EXPERIMENTAL_Suggestion.Clear /> <Button type='submit'>Søk</Button> </EXPERIMENTAL_Suggestion> </form> <Paragraph data-size='md' style={{ marginTop: 'var(--ds-size-2)' }}> Submitted value: {submittedValue} </Paragraph> </> ); }; render(<Form />)
React
Search er avviklet og blir fjernet i neste hovedversjon. Bruk Input med type="search" i en Field i stedet, eller Suggestion hvis du trenger en tøm-knapp.
import {
Button,
EXPERIMENTAL_Suggestion as Suggestion,
Field,
Input,
Label,
} from '@digdir/designsystemet-react';
// Søkefelt
<Field>
<Label>Søk</Label>
<Input type="search" />
</Field>
// Med tøm-knapp og søkeknapp
<Suggestion>
<Suggestion.Input type="search" aria-label="Søk" />
<Suggestion.Clear />
<Button type="submit">Søk</Button>
</Suggestion>
SearchButton
- variant
- Description
Specify which variant to use
- Type
"primary" | "secondary" | "tertiary"- Default
'primary'
- icon
- Description
Toggle icon only styling, pass icon as children When combined with loading, the loading-icon will be shown instead of the icon.
- Type
boolean- Default
false
- loading
- Description
Toggle loading state. Pass an element if you want to display a custom loader.
- Type
ReactNode- Default
false
- asChild
- Description
Change the default rendered element for the one passed as a child, merging their props and behavior.
- Type
boolean- Default
false
- type
- Description
Specify the type of button. Unset when `asChild` is true
- Type
"button" | "submit" | "reset"- Default
'button'
| Name | Type | Default | Description |
|---|---|---|---|
| variant | "primary" | "secondary" | "tertiary" | 'primary' | Specify which variant to use |
| icon | boolean | false | Toggle icon only styling, pass icon as children When combined with loading, the loading-icon will be shown instead of the icon. |
| loading | ReactNode | false | Toggle loading state. Pass an element if you want to display a custom loader. |
| asChild | boolean | false | Change the default rendered element for the one passed as a child, merging their props and behavior. |
| type | "button" | "submit" | "reset" | 'button' | Specify the type of button. Unset when `asChild` is true |
SearchClear
- aria-label
- Description
Aria label for the clear button @deprecated Please use `--dsc-suggestion-sr-clear` or `data-sr-clear` on Suggestion to set label.
- Type
string
| Name | Type | Default | Description |
|---|---|---|---|
| aria-label | string | - | Aria label for the clear button @deprecated Please use `--dsc-suggestion-sr-clear` or `data-sr-clear` on Suggestion to set label. |
SearchInput
- type
- Description
Type of input, affects if magnifying glass is shown or not. Use 'text' to hide magnifying glass.
- Type
"text" | "search"- Default
search
| Name | Type | Default | Description |
|---|---|---|---|
| type | "text" | "search" | search | Type of input, affects if magnifying glass is shown or not. Use 'text' to hide magnifying glass. |