Dialog
A modal that holds focus until it is answered or Escape dismisses it; as an alert dialog, only an answer closes it.
Previews are drawn in the theme you made.
Edit itConfirm #
import * as UiDialog from '@foldkit/ui/dialog'
import { Bundles, Button, Dialog, DialogSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const Danger = Recipes.Button.style({ tone: 'danger' })
const Quiet = Recipes.Button.style({ tone: 'neutral', variant: 'ghost' })
// A dialog that asks before something that cannot be undone. Escape, a press
// on the backdrop and Cancel close it; `isAnimated` keeps it drawn while it
// leaves, so it fades out as it faded in.
export default example({
title: 'Confirm',
slots: DialogSlots,
recipe: Recipes.Dialog,
state: Bundles.Dialog.with({ id: 'delete-dialog', isAnimated: true }),
view: ({ style, model, onMessage, h }) =>
h.div(
[],
[
Button.view(
{
label: 'Delete project',
onClick: onMessage(UiDialog.Message.RequestedOpen()),
style: Danger,
},
h,
),
h.submodel({
slotId: model.id,
model,
view: UiDialog.view,
viewInputs: {
hasDescription: true,
toView: Dialog.toView([style.mixin], { h }, dialog =>
h.dialog(
dialog.dialog,
dialog.isVisible
? [
h.div(dialog.backdrop),
h.div(dialog.panel, [
h.button(dialog.closeButton, ['✕']),
h.h2(dialog.title, ['Delete this project?']),
h.p(dialog.description, [
'Its pages and their history go for good. Escape keeps it.',
]),
h.div(dialog.footer, [
Button.view(
{
label: 'Cancel',
onClick: onMessage(UiDialog.Message.RequestedClose()),
style: Quiet,
},
h,
),
Button.view(
{
label: 'Delete',
onClick: onMessage(UiDialog.Message.RequestedClose()),
style: Danger,
},
h,
),
]),
]),
]
: [],
),
),
},
toParentMessage: onMessage,
}),
],
),
})In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.
An alert dialog #
import * as UiDialog from '@foldkit/ui/dialog'
import { Bundle } from 'foldkit-bundle'
import { Bundles, Button, Dialog, DialogSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const Danger = Recipes.Button.style({ tone: 'danger' })
const Quiet = Recipes.Button.style({ tone: 'neutral', variant: 'ghost' })
// An alert dialog asks for an answer: `role: 'alertdialog'`, no close button,
// and `dismissible: false`, so Escape and the backdrop send a Message the
// Bundle drops. Its answers close it through the placement's `close` helper,
// which a dismissal does not take. Focus opens on the answer that loses
// nothing, though it comes second.
const App = Bundle.compose({}).pipe(
Bundle.withMessages({ Answered: {} }),
Bundle.withChild('alert', Bundles.Dialog, {
args: { id: 'remove-member', dismissible: false, isAnimated: true },
onOut: Bundle.ignore,
}),
)
export default example({
title: 'An alert dialog',
slots: DialogSlots,
recipe: Recipes.Dialog,
state: App.placements.runtime({
Model: App.Model,
initial: {},
update: App.placements.update(model => App.children.alert.helpers.close()(model)),
}),
view: ({ style, model, onMessage, h }) =>
h.div(
[],
[
Button.view(
{
label: 'Remove member',
onClick: onMessage(
App.Message.GotAlertMessage({ message: UiDialog.Message.RequestedOpen() }),
),
style: Danger,
},
h,
),
h.submodel({
slotId: model.alert.id,
model: model.alert,
view: UiDialog.view,
viewInputs: {
hasDescription: true,
toView: Dialog.toView([style.mixin], { h, role: 'alertdialog' }, dialog =>
h.dialog(
dialog.dialog,
dialog.isVisible
? [
h.div(dialog.backdrop),
h.div(dialog.panel, [
h.h2(dialog.title, ['Remove Ada from the team?']),
h.p(dialog.description, [
'She loses access to every project at once. Only an answer closes this.',
]),
h.div(dialog.footer, [
Button.view(
{
label: 'Cancel',
onClick: onMessage(App.Message.Answered()),
style: Quiet,
attributes: { button: dialog.initialFocus },
},
h,
),
Button.view(
{
label: 'Remove',
onClick: onMessage(App.Message.Answered()),
style: Danger,
},
h,
),
]),
]),
]
: [],
),
),
},
toParentMessage: message => onMessage(App.Message.GotAlertMessage({ message })),
}),
],
),
})In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.
A long body #
import * as UiDialog from '@foldkit/ui/dialog'
import { Bundles, Button, Dialog, DialogSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const Primary = Recipes.Button.style({})
const Quiet = Recipes.Button.style({ tone: 'neutral', variant: 'ghost' })
const TERMS = [
'You keep the rights to what you make here. We keep a copy only to show it to the people you share it with.',
'We delete a closed account’s pages after thirty days, unless the law asks us to keep them longer.',
'We do not sell what we learn about you, and we tell you before a change to these terms takes effect.',
'A page you publish is public: anyone with its link can read it, and search engines may list it.',
'You may not use the service to break the law, or to send what no one asked for.',
'Either of us may end this agreement with notice; what you made stays yours to download for thirty days.',
]
// A body longer than the screen scrolls under the heading, its own bar
// drawn by `Dialog.overlayBars`, while the footer's answers stay in view.
export default example({
title: 'A long body',
slots: DialogSlots,
recipe: Recipes.Dialog,
state: Bundles.Dialog.with({ id: 'terms-dialog', isAnimated: true }),
view: ({ style, model, onMessage, h }) =>
h.div(
[],
[
Button.view(
{
label: 'Read the terms',
onClick: onMessage(UiDialog.Message.RequestedOpen()),
style: Quiet,
},
h,
),
h.submodel({
slotId: model.id,
model,
view: UiDialog.view,
viewInputs: {
toView: Dialog.toView([style.mixin, ...Dialog.overlayBars], { h }, dialog =>
h.dialog(
dialog.dialog,
dialog.isVisible
? [
h.div(dialog.backdrop),
h.div(dialog.panel, [
h.h2(dialog.title, ['Terms of service']),
h.div(dialog.body, [
h.div(
dialog.scroll,
// The reset zeroes a paragraph's margins: each term keeps a line between.
[...TERMS, ...TERMS].map(term =>
h.p([h.Style({ marginBlock: '0 0.75rem' })], [term]),
),
),
h.div(dialog.scrollBar, [h.div(dialog.scrollThumb)]),
]),
h.div(dialog.footer, [
Button.view(
{
label: 'Cancel',
onClick: onMessage(UiDialog.Message.RequestedClose()),
style: Quiet,
},
h,
),
Button.view(
{
label: 'Accept',
onClick: onMessage(UiDialog.Message.RequestedClose()),
style: Primary,
},
h,
),
]),
]),
]
: [],
),
),
},
toParentMessage: onMessage,
}),
],
),
})In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.
A form #
import * as UiDialog from '@foldkit/ui/dialog'
import { SlotView } from 'foldkit-mixins'
import {
Bundles,
Button,
ButtonSlots,
Dialog,
DialogSlots,
Input,
Recipes,
} from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const Primary = Recipes.Button.style({})
const Quiet = Recipes.Button.style({ tone: 'neutral', variant: 'ghost' })
const Field = Recipes.Input.style({})
// A form in the body, its submit button in the footer: `form` ties the two,
// so Enter in a field submits it too. `Dialog.keyboard` keeps the panel above
// the on-screen keyboard of a phone while a field has focus.
export default example({
title: 'A form',
slots: DialogSlots,
recipe: Recipes.Dialog,
state: Bundles.Dialog.with({ id: 'rename', isAnimated: true }),
view: ({ style, model, onMessage, h }) =>
h.div(
[],
[
Button.view(
{
label: 'Rename page',
onClick: onMessage(UiDialog.Message.RequestedOpen()),
style: Quiet,
},
h,
),
h.submodel({
slotId: model.id,
model,
view: UiDialog.view,
viewInputs: {
toView: Dialog.toView([style.mixin, ...Dialog.keyboard], { h }, dialog =>
h.dialog(
dialog.dialog,
dialog.isVisible
? [
h.div(dialog.backdrop),
h.div(dialog.panel, [
h.button(dialog.closeButton, ['✕']),
h.h2(dialog.title, ['Rename page']),
h.div(dialog.body, [
h.div(dialog.scroll, [
h.form(
[
h.Id('rename-form'),
h.OnSubmit(onMessage(UiDialog.Message.RequestedClose())),
],
[
Input.view(
{
id: 'rename-title',
value: 'Quarterly plan',
style: Field,
draw: (resolved, h) =>
h.div(
[],
[
h.label(resolved.label, ['Title']),
h.input(resolved.input),
],
),
},
h,
),
],
),
]),
]),
h.div(dialog.footer, [
Button.view(
{
label: 'Cancel',
onClick: onMessage(UiDialog.Message.RequestedClose()),
style: Quiet,
},
h,
),
h.button(
SlotView.buildersFor(ButtonSlots, [Primary.mixin], {
input: undefined,
h,
}).button.attrs([h.Type('submit'), h.FormAttr('rename-form')]),
['Rename'],
),
]),
]),
]
: [],
),
),
},
toParentMessage: onMessage,
}),
],
),
})In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.
Nested #
import * as UiDialog from '@foldkit/ui/dialog'
import { Bundle } from 'foldkit-bundle'
import { Bundles, Button, Dialog, DialogSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const Primary = Recipes.Button.style({})
const Quiet = Recipes.Button.style({ tone: 'neutral', variant: 'ghost' })
// A dialog opened from another: the inner one is drawn inside the outer
// `dialog`, beside its panel, and the outer panel steps back while it is
// open (`covered`). Escape closes the inner one first.
const App = Bundle.compose({}).pipe(
Bundle.withChild('share', Bundles.Dialog, {
args: { id: 'share', isAnimated: true },
onOut: Bundle.ignore,
}),
Bundle.withChild('link', Bundles.Dialog, {
args: { id: 'share-link', isAnimated: true },
onOut: Bundle.ignore,
}),
)
export default example({
title: 'Nested',
slots: DialogSlots,
recipe: Recipes.Dialog,
state: App.placements.runtime({ Model: App.Model, initial: {} }),
view: ({ style, model, onMessage, h }) => {
const toShare = (message: UiDialog.Message) =>
onMessage(App.Message.GotShareMessage({ message }))
const toLink = (message: UiDialog.Message) => onMessage(App.Message.GotLinkMessage({ message }))
const inner = h.submodel({
slotId: model.link.id,
model: model.link,
view: UiDialog.view,
viewInputs: {
toView: Dialog.toView([style.mixin], { h }, dialog =>
h.dialog(
dialog.dialog,
dialog.isVisible
? [
h.div(dialog.backdrop),
h.div(dialog.panel, [
h.h2(dialog.title, ['Invite by link']),
h.p([], ['Anyone with the link joins as a viewer.']),
h.div(dialog.footer, [
Button.view(
{
label: 'Done',
onClick: toLink(UiDialog.Message.RequestedClose()),
style: Primary,
},
h,
),
]),
]),
]
: [],
),
),
},
toParentMessage: toLink,
})
return h.div(
[],
[
Button.view(
{ label: 'Share', onClick: toShare(UiDialog.Message.RequestedOpen()), style: Quiet },
h,
),
h.submodel({
slotId: model.share.id,
model: model.share,
view: UiDialog.view,
viewInputs: {
toView: Dialog.toView([style.mixin], { h, covered: model.link.isOpen }, dialog =>
h.dialog(
dialog.dialog,
dialog.isVisible
? [
h.div(dialog.backdrop),
h.div(dialog.panel, [
h.button(dialog.closeButton, ['✕']),
h.h2(dialog.title, ['Share “Quarterly plan”']),
h.p([], ['Three people can edit it.']),
h.div(dialog.footer, [
Button.view(
{
label: 'Invite by link',
onClick: toLink(UiDialog.Message.RequestedOpen()),
style: Primary,
},
h,
),
]),
]),
inner,
]
: [],
),
),
},
toParentMessage: toShare,
}),
],
)
},
})In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.
Usage
Each example above is a module of its own, shown whole and run as shown, over the theme module Create a theme writes. The page hands it the style picked above.
Related
- Keeping focus when something is removed A control that leaves the page takes its focus with it: say where focus goes next, to a neighbour (
FocusRecovery), to the opener (focusReturn) or to a control (keepFocus).