Komponenter
Dialog
Det er to typer dialoger, en modal og en ikke-modal. Når en modal dialog er åpen, dempes resten av siden, og brukerne kan kun interagere med innhold inni dialogen. En ikke-modal dialog lar brukerne fortsette å bruke siden, selv om dialogen er åpen.
<button
class="ds-button"
commandfor="my-dialog-preview"
type="button"
command="show-modal"
>Open modal Dialog</button>
<button
class="ds-button"
commandfor="my-dialog-preview"
type="button"
command="--show-non-modal"
>Open non-modal Dialog</button>
<dialog
class="ds-dialog"
data-placement="center"
data-modal="true"
id="my-dialog-preview"
>
<button
class="ds-button"
data-icon="true"
commandfor="my-dialog-preview"
data-variant="tertiary"
type="button"
aria-label="Lukk dialogvindu"
data-color="neutral"
command="close"
></button>
<h2 class="ds-heading" style="margin-bottom:var(--ds-size-2)">Dialog header</h2>
<p class="ds-paragraph" style="margin-bottom:var(--ds-size-2)">
Lorem ipsum dolor sit, amet consectetur adipisicing elit. Blanditiis
doloremque obcaecati assumenda odio ducimus sunt et.
</p>
</dialog>const Preview = () => { return ( <> <Button command='show-modal' commandfor='my-dialog-preview'> Open modal Dialog </Button> <Button command='--show-non-modal' commandfor='my-dialog-preview'> Open non-modal Dialog </Button> <Dialog id='my-dialog-preview'> <Heading style={{ marginBottom: 'var(--ds-size-2)' }}> Dialog header </Heading> <Paragraph style={{ marginBottom: 'var(--ds-size-2)' }}> Lorem ipsum dolor sit, amet consectetur adipisicing elit. Blanditiis doloremque obcaecati assumenda odio ducimus sunt et. </Paragraph> </Dialog> </> ); }; render(<Preview />)
Bruk
Dialog har to klassenavn du kan bruke.
ds-dialog: Hovedklassen til bruk på<dialog>-elementet.ds-dialog__block: Klassen som brukes for hver seksjon i dialogen.
<dialog class="ds-dialog">
Content
</dialog>
<!-- med inndeling -->
<dialog class="ds-dialog">
<div class="ds-dialog__block">
Header
</div>
<div class="ds-dialog__block">
Content
</div>
<div class="ds-dialog__block">
Footer
</div>
</dialog>
Åpne og lukke dialogen
Bruk command og commandfor for å koble <dialog>-elementet til en trigger.
Benytt verdiene command="show-modal" eller command="--show-non-modal" for modal med eller uten bakgrunnsdimming og commandfor="dialog-id" for å koble triggeren til dialogen.
Designsystemet legger automatisk til aria-haspopup="dialog" på knapper som åpner en dialog.
Hvis dialogen har en overskrift, brukes den som dialogens tilgjengelige navn.
Hvis dialogen er tom eller laster innhold når den åpnes, må du selv gi dialogelementet et tilgjengelig navn med aria-label.
<button type="button" aria-haspopup="dialog" command="show-modal" commandfor="my-dialog" >Open Dialog</button>
<dialog id="my-dialog" class="ds-dialog">
<div>Content</div>
<button command="close" commandfor="my-dialog">Close</button>
</dialog>
Polyfill
Dialog er avhengig av attributtene command og commandfor (mozilla.org), som er en relativt ny del av webstandarden og lar button utføre handlinger på andre elementer deklarativt. Vi legger automatisk til invokers-polyfill (npmjs.com) for å støtte eldre nettlesere ved behov.
Vi legger også til en polyfill for closedby="any" for å støtte eldre Safari, som ikke støtter dette nativt.
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.
Alle CSS variabler brukt av ds-dialog__block er satt på ds-dialog.
| Navn | Verdi |
|---|---|
| --dsc-dialog-backdrop-background | rgba(0,0,0,.5) |
| --dsc-dialog-background | var(--ds-color-neutral-surface-default) |
| --dsc-dialog-border-color | var(--ds-color-neutral-border-subtle) |
| --dsc-dialog-border-style | solid |
| --dsc-dialog-border-width | var(--ds-border-width-default) |
| --dsc-dialog-close-label | "Lukk" |
| --dsc-dialog-color | var(--ds-color-neutral-text-default) |
| --dsc-dialog-divider-border-color | var(--ds-color-neutral-border-subtle) |
| --dsc-dialog-divider-border-style | solid |
| --dsc-dialog-divider-border-width | var(--ds-border-width-default) |
| --dsc-dialog-icon-size | var(--ds-icon-size) |
| --dsc-dialog-icon-spacing | var(--ds-size-3) |
| --dsc-dialog-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-dialog-max-height | 80vh |
| --dsc-dialog-max-width | 40rem |
| --dsc-dialog-placement-block-max-width | 100% |
| --dsc-dialog-placement-inline-max-width | 40rem |
| --dsc-dialog-spacing | var(--ds-size-6) |
| --dsc-dialog-transition-duration | 300ms |
| Navn | Verdi |
|---|---|
| data-command | close |
| data-placement | bottom, top, left, right, center |
Eksempel
Med skjema og fokus
Dersom du vil at et felt i et skjema inni dialogen skal få fokus når dialogen åpnes, kan du bruke native autofocus-attributtet på input-elementet.
Når du bruker dette med React, må du skrive det med små bokstaver som autofocus, ikke autoFocus.
Denne prop-en eksistere ikke i React sine typedefinisjoner, så vi har ignorert feilen med en @ts-expect-error-kommentar i eksempelet under.
<button
class="ds-button"
commandfor="my-dialog-form"
type="button"
command="show-modal"
>Open Dialog with command</button>
<dialog
class="ds-dialog"
data-placement="center"
data-modal="true"
id="my-dialog-form"
closedby="any"
>
<button
class="ds-button"
data-icon="true"
commandfor="my-dialog-form"
data-variant="tertiary"
type="button"
aria-label="Lukk dialogvindu"
data-color="neutral"
command="close"
></button>
<h2 class="ds-heading" style="margin-bottom:var(--ds-size-2)">Dialog med skjema</h2>
<ds-field class="ds-field">
<label class="ds-label">Navn</label>
<div class="ds-field-affixes">
<input class="ds-input" type="text" autofocus="true" value=""/>
</div>
</ds-field>
<div
style="display:flex;gap:var(--ds-size-4);margin-top:var(--ds-size-4)"
>
<button
class="ds-button"
commandfor="my-dialog-form"
type="button"
command="close"
>Send inn skjema</button>
<button
class="ds-button"
commandfor="my-dialog-form"
data-variant="secondary"
type="button"
data-color="danger"
command="close"
>Avbryt</button>
</div>
</dialog>const WithForm = () => { const [input, setInput] = useState(''); return ( <> <Button command='show-modal' commandfor='my-dialog-form'> Open Dialog with command </Button> <Dialog onClose={() => setInput('')} closedby='any' id='my-dialog-form'> <Heading style={{ marginBottom: 'var(--ds-size-2)' }}> Dialog med skjema </Heading> <Textfield label='Navn' value={input} onChange={(e) => setInput(e.target.value)} // @ts-expect-error We want the native "autofocus" and Reacts onMount smartness (see https://react.dev/reference/react-dom/components/input#input) autofocus='true' /> <div style={{ display: 'flex', gap: 'var(--ds-size-4)', marginTop: 'var(--ds-size-4)', }} > <Button command='close' commandfor='my-dialog-form' onClick={() => alert(`Du har sendt inn skjema med navn: ${input}`)} > Send inn skjema </Button> <Button variant='secondary' data-color='danger' command='close' commandfor='my-dialog-form' > Avbryt </Button> </div> </Dialog> </> ); }; render(<WithForm />)
Med seksjoner
Bruk flere ds-dialog__block hvis du vil dele opp dialogen med skillelinjer til for eksempel topp- og bunn-område.
Merk at innhold ikke kan plasseres direkte i dialog dersom du bruker ds-dialog__block; da burde alt innholdet være inni en av dialogens ds-dialog__block-seksjoner.
<button
class="ds-button"
commandfor="my-dialog-blocks"
type="button"
command="show-modal"
>Open Dialog with command</button>
<dialog
class="ds-dialog"
data-placement="center"
data-modal="true"
id="my-dialog-blocks"
>
<button
class="ds-button"
data-icon="true"
commandfor="my-dialog-blocks"
data-variant="tertiary"
type="button"
aria-label="Lukk dialogvindu"
data-color="neutral"
command="close"
></button>
<div class="ds-dialog__block">
<p class="ds-paragraph" data-size="sm">Dialog subtitle</p>
<h2 class="ds-heading">Dialog with dividers</h2>
</div>
<div class="ds-dialog__block">
<p class="ds-paragraph">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur
sodales eros justo.
</p>
</div>
<div class="ds-dialog__block">
<button
class="ds-button"
commandfor="my-dialog-blocks"
data-variant="secondary"
type="button"
command="close"
>Lukk</button>
</div>
</dialog>const WithBlocks = () => ( <> <Button command='show-modal' commandfor='my-dialog-blocks'> Open Dialog with command </Button> <Dialog id='my-dialog-blocks'> <Dialog.Block> <Paragraph data-size='sm'>Dialog subtitle</Paragraph> <Heading>Dialog with dividers</Heading> </Dialog.Block> <Dialog.Block> <Paragraph> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur sodales eros justo. </Paragraph> </Dialog.Block> <Dialog.Block> <Button variant='secondary' command='close' commandfor='my-dialog-blocks' > Lukk </Button> </Dialog.Block> </Dialog> </> ); render(<WithBlocks />)
Lukk ved klikk utenfor
Bruk closedby="any" for å lukke dialogen når brukeren klikker utenfor.
Vi legger til polyfill så closedby="any" også fungerer i eldre Safari.
<button
class="ds-button"
commandfor="my-dialog-close-outside"
type="button"
command="show-modal"
>Open Dialog with command</button>
<dialog
class="ds-dialog"
data-placement="center"
data-modal="true"
id="my-dialog-close-outside"
closedby="any"
>
<button
class="ds-button"
data-icon="true"
commandfor="my-dialog-close-outside"
data-variant="tertiary"
type="button"
aria-label="Lukk dialogvindu"
data-color="neutral"
command="close"
></button>
<h2 class="ds-heading">Click outside to close</h2>
</dialog>const CloseWithClickOutside = () => ( <> <Button command='show-modal' commandfor='my-dialog-close-outside'> Open Dialog with command </Button> <Dialog id='my-dialog-close-outside' closedby='any'> <Heading>Click outside to close</Heading> </Dialog> </> ); render(<CloseWithClickOutside />)
Lukk med knapp
For å legge til egen lukkeknapp, kan du enten bruke command="close" commandfor="DIALOG-ID" eller ref og Dialog.close() på en knapp du lager selv.
Dersom knappen er tom vil den få et ikon.
Dersom knappen er direkte barn av dialogen og første element, vil den flyte til topp høyre.
React
import { Button, Dialog } from '@digdir/designsystemet-react';
<Button command="show-modal" commandfor="my-dialog">Open Dialog</Button>
<Dialog id="my-dialog">
Dialog content
</Dialog>
Med ref
Du kan også bruke ref for å åpne dialogen fra en ekstern trigger.
Du bruker da native metoder på <dialog>-elementet, som showModal() eller show().
<button class="ds-button" type="button" aria-haspopup="dialog">Open Dialog with ref</button>
<dialog
class="ds-dialog"
data-placement="center"
data-modal="true"
id="_R_2_"
>
<button
class="ds-button"
data-icon="true"
commandfor="_R_2_"
data-variant="tertiary"
type="button"
aria-label="Lukk dialogvindu"
data-color="neutral"
command="close"
></button>
<h2 class="ds-heading" style="margin-bottom:var(--ds-size-2)">Dialog header</h2>
<p class="ds-paragraph" style="margin-bottom:var(--ds-size-2)">
Lorem ipsum dolor sit, amet consectetur adipisicing elit. Blanditiis
doloremque obcaecati assumenda odio ducimus sunt et.
</p>
</dialog>const WithRef = () => { const dialogRef = useRef<HTMLDialogElement>(null); return ( <> <Button aria-haspopup='dialog' onClick={() => dialogRef.current?.showModal()} > Open Dialog with ref </Button> <Dialog ref={dialogRef}> <Heading style={{ marginBottom: 'var(--ds-size-2)' }}> Dialog header </Heading> <Paragraph style={{ marginBottom: 'var(--ds-size-2)' }}> Lorem ipsum dolor sit, amet consectetur adipisicing elit. Blanditiis doloremque obcaecati assumenda odio ducimus sunt et. </Paragraph> </Dialog> </> ); }; render(<WithRef />)
Props
Dialog
Dialog
- closeButton
- Description
Screen reader label of close button. Set false to hide the close button.
- Type
string | false- Default
Lukk dialogvindu
- closedby
- Description
Light dismiss behavior, allowing to close on backdrop click by setting `closedby="any"`. @see [mdn closedBy](https://developer.mozilla.org/en-US/docs/Web/API/HTMLDialogElement/closedBy)
- Type
"none" | "closerequest" | "any"- Default
'closerequest'
- placement
- Description
When not center, displays dialog as a "drawer" from the specified side.
- Type
"center" | "left" | "right" | "top" | "bottom"- Default
center
- modal
- Description
Toogle modal and non-modal dialog. @see [mdn modal dialog](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/dialog#creating_a_modal_dialog)
- Type
boolean- Default
true
- open
- Description
@note Unlike standard html, where the open attribute always opens a non-modal dialog, Dialog's open prop uses the `modal` prop to determine whether the Dialog is modal or non-modal
- Type
boolean
- onClose
- Description
Callback that is called when the dialog is closed.
- Type
((event: Event) => void)
- asChild
- Description
Change the default rendered element for the one passed as a child, merging their props and behavior. @deprecated Will be removed in the next major version. Should always be a `<dialog>` element
- Type
boolean- Default
false
| Name | Type | Default | Description |
|---|---|---|---|
| closeButton | string | false | Lukk dialogvindu | Screen reader label of close button. Set false to hide the close button. |
| closedby | "none" | "closerequest" | "any" | 'closerequest' | Light dismiss behavior, allowing to close on backdrop click by setting `closedby="any"`. @see [mdn closedBy](https://developer.mozilla.org/en-US/docs/Web/API/HTMLDialogElement/closedBy) |
| placement | "center" | "left" | "right" | "top" | "bottom" | center | When not center, displays dialog as a "drawer" from the specified side. |
| modal | boolean | true | Toogle modal and non-modal dialog. @see [mdn modal dialog](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/dialog#creating_a_modal_dialog) |
| open | boolean | - | @note Unlike standard html, where the open attribute always opens a non-modal dialog, Dialog's open prop uses the `modal` prop to determine whether the Dialog is modal or non-modal |
| onClose | ((event: Event) => void) | - | Callback that is called when the dialog is closed. |
| asChild | boolean | false | Change the default rendered element for the one passed as a child, merging their props and behavior. @deprecated Will be removed in the next major version. Should always be a `<dialog>` element |
DialogBlock
- asChild
- Description
Change the default rendered element for the one passed as a child, merging their props and behavior.
- Type
boolean- Default
false
| Name | Type | Default | Description |
|---|---|---|---|
| asChild | boolean | false | Change the default rendered element for the one passed as a child, merging their props and behavior. |
DialogTrigger
- type
- Description
Specify the type of button. Unset when `asChild` is true
- Type
"submit" | "reset" | "button"- Default
'button'
- 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
| Name | Type | Default | Description |
|---|---|---|---|
| type | "submit" | "reset" | "button" | 'button' | Specify the type of button. Unset when `asChild` is true |
| 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. |