Hopp til hovedinnhold

Hva leter du etter?

Prøv å søke etter…

Komponenter

Dialog

Det er to typer dialoger, en modal og en ikke-modal. En modal dialog tvinger brukerne til å ta stilling til noe før de kan fortsette å bruke siden. En ikke-modal dialog lar brukerne fortsette å bruke siden, selv om dialogen er åpen.

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.

Å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.

Attributtet aria-haspopup="dialog" bør legges til på elementet som åpner dialogen, for å gjøre det tydelig for brukere av skjermlesere at det vil åpne en dialog. Vi gjør dette automatisk for deg når du bruker @digdir/designsystemet-web eller @digdir/designsystemet-react.

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.

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.

CSS-variabler
NavnVerdi
--dsc-dialog-backdrop-backgroundrgba(0,0,0,.5)
--dsc-dialog-backgroundvar(--ds-color-neutral-surface-default)
--dsc-dialog-icon-spacingvar(--ds-size-3)
--dsc-dialog-icon-urlurl("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' width='1em' height='1em' fill='none' viewBox='0 0 24 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-dialog-colorvar(--ds-color-neutral-text-default)
--dsc-dialog-divider-border-widthvar(--ds-border-width-default)
--dsc-dialog-divider-border-stylesolid
--dsc-dialog-divider-border-colorvar(--ds-color-neutral-border-subtle)
--dsc-dialog-border-widthvar(--ds-border-width-default)
--dsc-dialog-border-stylesolid
--dsc-dialog-border-colorvar(--ds-color-neutral-border-subtle)
--dsc-dialog-max-height80vh
--dsc-dialog-max-width40rem
--dsc-dialog-spacingvar(--ds-size-6)
--dsc-dialog-placement-inline-max-width40rem
--dsc-dialog-placement-block-max-width100%
--dsc-dialog-transition-duration300ms
Data-attributter
NavnVerdi
data-placementbottom, top, left, right
data-commandclose

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.

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.

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.

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

TriggerContext

Bruk Dialog.TriggerContext og Dialog.Trigger sammen, så kan Dialog kommunisere uten å måtte bruke id og commandfor.

Med ref og uten context

Dersom du ikke vil bruke Dialog.TriggerContext, kan du bruke ref for å åpne dialogen fra en ekstern trigger. Du bruker da native metoder på <dialog>-elementet, som showModal() eller show().

Props

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
NameTypeDefaultDescription
closeButtonstring | falseLukk 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.

modalbooleantrue

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)

openboolean-

@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.

asChildbooleanfalse

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
NameTypeDefaultDescription
asChildbooleanfalse

Change the default rendered element for the one passed as a child, merging their props and behavior.

DialogTrigger

command
Description

Native invoker commands. Specifies actions to perform on an element specified by commandfor. Polyfilled by designsystemet-web and includes a custom --show-non-modal command. "show-modal", "close", "request-close", "show-popover", "hide-popover", "toggle-popover", "--show-non-modal"

Type
string
commandfor
Description

Specifies the target element for "command". value is ID of target

Type
string
commandFor
Type
string
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
NameTypeDefaultDescription
commandstring-

Native invoker commands. Specifies actions to perform on an element specified by commandfor. Polyfilled by designsystemet-web and includes a custom --show-non-modal command. "show-modal", "close", "request-close", "show-popover", "hide-popover", "toggle-popover", "--show-non-modal"

commandforstring-

Specifies the target element for "command". value is ID of target

commandForstring-

-

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

iconbooleanfalse

Toggle icon only styling, pass icon as children When combined with loading, the loading-icon will be shown instead of the icon.

loadingReactNodefalse

Toggle loading state. Pass an element if you want to display a custom loader.

asChildbooleanfalse

Change the default rendered element for the one passed as a child, merging their props and behavior.

Rediger denne siden på github.com (åpnes i ny fane)