A dialog over the page, for a task or message the user must deal with before continuing.
Mirrors Dialog from @digdir/designsystemet-react.
import Designsystemet.Button as Button
import Designsystemet.Dialog as Dialog
confirmDelete : Dialog.Id
confirmDelete =
Dialog.id "confirm-delete"
view : Html Msg
view =
Html.div []
[ Dialog.trigger confirmDelete [] [ Html.text "Slett sak" ]
, Dialog.new confirmDelete
|> Dialog.withCloseButton { label = "Lukk dialogvindu" }
|> Dialog.view [ Dialog.onClose DeleteDialogClosed ]
[ Dialog.block [] [ Heading.h2 [] [ Html.text "Slette saken?" ] ]
, Dialog.block []
[ Button.view [ Events.onClick Delete ] [ Html.text "Slett" ]
, Button.view (Button.secondary :: Dialog.close confirmDelete)
[ Html.text "Avbryt" ]
]
]
]The dialog is a native <dialog>.
Open it in one of two ways:
command/commandfor,
polyfilled by designsystemet-web). No message or model field needed.open and a Bool. This is React's open
prop, for dialogs opened by an event (a timeout, a failed save) rather
than a click.<Dialog id="x"> → Dialog.new (Dialog.id "x") |> Dialog.view [] […]closeButton="Lukk" (the default) → Dialog.withCloseButton { label = "Lukk" }closeButton={false} → leave out withCloseButton<Dialog.Block> → Dialog.block []<Dialog.TriggerContext> → not needed: share a Dialog.Id<Dialog.Trigger> → Dialog.trigger id []<Button command="show-modal" commandfor="x"> → Button.view (Dialog.showModal id)modal={false} → Dialog.nonModal, or open with Dialog.showNonModal idplacement="right" → Dialog.drawer Dialog.Rightclosedby="any" → Dialog.closedByAnyonClose → Dialog.onClose msgopen={isOpen} + onClose → Dialog.open model.isOpen, Dialog.onClose Closedaria-haspopup="dialog" on every button that opens a dialog, set on
focus. trigger also renders it up front, as React's
Dialog.Trigger does. The value is the same, so they don't conflict.closedby="any" light dismiss, in browsers without native support.Opening a dialog takes a method call (showModal()), which Elm can't make.
open renders the state as data-open, and the package's
JavaScript syncs the element:
import "@ensolabs/designsystemet-elm"; // once, in your entry pointDialog.new sessionExpired
|> Dialog.view
[ Dialog.open model.sessionExpired
, Dialog.onClose SessionDialogClosed
]
[{- … -}]When the user closes the dialog (Esc, a close button), it closes and sends
onClose. Set your state back to closed there. Until you do,
open True won't reopen it, since the attribute hasn't changed.
Name a dialog. The string becomes its DOM id, so it must be unique on
the page.
confirmDelete : Dialog.Id
confirmDelete =
Dialog.id "confirm-delete"The DOM id, for ports and tests.
Add an "×" close button in the top corner. It has no visible text, so
label is what screen readers announce.
Dialog.new confirmDelete
|> Dialog.withCloseButton { label = "Lukk dialogvindu" }
|> Dialog.view [] [{- … -}]A section of the dialog, like a header, the body or a footer of buttons.
Dialog.view []
[ Dialog.block [] [ Heading.h2 [] [ Html.text "Endre adresse" ] ]
, Dialog.block [] [ addressForm ]
, Dialog.block [] [ saveButton, cancelButton ]
]
dialogShow the dialog as a drawer along one edge of the screen instead of in the center.
Dialog.view [ Dialog.drawer Dialog.Right ] [ filterForm ] filtersThe edge of the screen a drawer slides in from.
Whether the dialog is open, from your model. Needs the companion script
(see Opening from your model), and an
onClose that sets your state back when the user closes it.
Dialog.view [ Dialog.open model.confirming, Dialog.onClose ConfirmClosed ] [{- … -}]No backdrop, and the page behind stays usable, when opened with
open. React's modal={false}. Invoker buttons choose for
themselves with showNonModal.
How the user can close the dialog without a close button. The default is
closedByCloseRequest for modal dialogs (Esc closes it).
Close on Esc and on a backdrop click. Good for dialogs that only show information.
Close on Esc (or the Android back gesture), but not on a click outside.
Close only from a button in the dialog. Use sparingly, since the user has no other way out.
Sent when the dialog closes, by a close button, Esc, a backdrop click or code.
Dialog.view [ Dialog.onClose EditDialogClosed ] [{- … -}] editDialogThe native close event. It doesn't bubble, so a nested dialog never
triggers it.
A Button that opens the dialog as a modal
(React's Dialog.Trigger).
Dialog.trigger editAddress [ Button.secondary ] [ Html.text "Endre adresse" ]For a non-modal dialog, use showNonModal on a button
instead.
Open the dialog as a modal: the rest of the page is inert behind a backdrop until it closes.
Open the dialog without a backdrop, leaving the rest of the page usable.
Ask the dialog to close, as Esc does. It fires a cancelable cancel event
first, for example to warn about unsaved changes.
The
idthat links a dialog to the buttons that open and close it.Make one per dialog, as a top-level value, and use it in both places. A
Dialog.Idcan't be given to aPopoverby mistake.