Command palette

@yasinsensoy/web

Web · Components

Dialog

Modal window that opens above the page.

8 exports

Live example

Preview

Edit profile

Opens from the bottom on mobile; close it by pulling down or with the back button.

Code

import { Dialog, Button, TextField } from "@yasinsensoy/web";

<Dialog
  trigger={<Button variant="outline">Open dialog</Button>}
  title="Edit profile"
  icon="user"
  description="Opens from the bottom on mobile; close it by pulling down or with the back button."
  confirmLabel="Save"
  onConfirm={async (data) => await save(data.get("name"))}
>
  <TextField label="Ad" name="name" required />
</Dialog>

Events

  1. Events appear here when you interact with the preview.

confirm(): a confirmation question in one line

Custom footer: the footer function receives close

Version 2.0 is out

Discover what's new.

Import

import { Dialog, confirm } from "@yasinsensoy/web";

Dialog

componentClient component

Modal window that opens above the page. It focuses the user on a single task; the page behind becomes inert and does not scroll.

Title, description, icon, body and buttons are given as props; no nested parts are written. The body is a form: onConfirm receives the values of its fields as FormData. If the body needs to close the window, pass children as a function: {({ close }) => …}.

Don't write a component just for an "Are you sure?" question; use confirm.

Built on the native <dialog> element: no z-index wars, focus is kept inside the window, returns to the opening button on close, and Esc closes it. Animations are CSS only.

Mobile: On small screens it automatically becomes a bottom sheet; it can be closed by pulling down and with the phone's back button, moves above the keyboard when it opens, and respects the bottom inset of notched screens.

Dialog({ size, mobileSheet, className, showCloseButton, closeOnBackdropClick, closeOnBackButton, swipeToClose, effect, ...props }: DialogProps): JSX.Element

Examples

// Shows loading until "Save" finishes, closes on success.
<Dialog
  trigger={<Button variant="outline">Edit profile</Button>}
  title="Edit profile"
  description="Changes appear once saved."
  icon="user"
  confirmLabel="Save"
  onConfirm={async (data) => await save(data.get("name"))}
>
  <TextField label="Name" name="name" required />
</Dialog>

// Controlled, with its own footer:
<Dialog open={open} onOpenChange={setOpen} title="Info"
  footer={({ close }) => <Button onClick={close}>OK</Button>}>
  …
</Dialog>

confirm

function

Asks the user a confirmation question and waits for the answer. Returns true if confirmed, false if cancelled (Cancel, Esc, backdrop, back button).

The dialog has all the behaviors of Dialog (bottom sheet on mobile, closing with the back button…). If called several times at once, the questions are shown in order.

Requires UIProvider at the root of the app; without it, it falls back to the browser's own window.confirm dialog.

confirm(options: ConfirmOptions): Promise<boolean>

Examples

async function remove() {
  const ok = await confirm({
    title: "Delete the project?",
    description: "This action cannot be undone.",
    confirmLabel: "Delete",
    tone: "danger",
  });
  if (ok) await deleteProject();
}

// The dialog stays open and the button shows loading until deletion finishes:
await confirm({ title: "Delete?", tone: "danger", onConfirm: deleteProject });

Options of the confirm function.

NameTypeDefaultDescription
titleReactNodeThe question; becomes the dialog title.
description?ReactNodeDescription under the question (e.g. "This action cannot be undone.").
icon?IconPropIcon next to the title: a name or an element (e.g. "warning").
confirmLabel?ReactNode"Onayla"Text of the confirm button.
cancelLabel?ReactNode"İptal"Text of the cancel button.
tone?Tone"primary"Color of the confirm button. "danger" for irreversible actions such as deleting.
onConfirm?() => unknownRuns on confirm, before the dialog closes. If it returns a Promise, the confirm button shows loading until it settles; if it returns false or throws, the dialog stays open.

Maximum width of the Dialog window.

type DialogSize = "sm" | "md" | "lg" | "xl"

DialogProps

interface

Props of the Dialog component.

NameTypeDefaultDescription
size?DialogSize"md"Maximum width of the window: sm 384 px, md 512 px, lg 672 px, xl 896 px. Always full width as a mobile bottom sheet.
mobileSheet?booleantrueTurn into a bottom sheet on small screens (below 640 px)? If false, it opens centered on every screen.
effect?EffectPropEffect or effects of the window surface (e.g. "beam", ["border", "glow"]). If omitted, UIProvider appearance.defaults.dialog.effect. See BuiltinEffect.
open?booleanWhether it is open when controlled.
defaultOpen?booleanfalseIs it initially open when uncontrolled?
onOpenChange?(open: boolean) => voidCalled with the new state when it opens or closes.
trigger?ReactElementElement that opens the window, usually a Button. The window opens on click; aria-expanded and aria-controls are added automatically.
titleReactNodeTitle. Screen readers announce the window with this name, so it is required. To hide it, use classNames={{ title: "sr-only" }}.
description?ReactNodeDescription under the title; screen readers read it when the window opens.
icon?IconPropIcon next to the title: a name or an element.
children?ModalSlotBody. If a function is given it receives { close, pending }; for example to close the window when a link is clicked.
onConfirm?(data: FormData) => unknownCalled when the confirm button is pressed. The body works like a form: the values of its fields arrive as FormData, and it is not called until browser validation (required etc.) passes. - If it returns a Promise, the confirm button shows loading until it settles and the window does not close (Esc, backdrop and back button wait too). - If it returns false or throws, the window stays open. - Otherwise the window closes.
confirmLabel?ReactNodeText of the confirm button. If onConfirm is given but this isn't, the UIProvider text ("Onayla") is used.
cancelLabel?ReactNode{@link UIProvider} text ("İptal")Text of the cancel button. With null the button is not shown.
tone?Tone"primary"Color of the confirm button. "danger" for irreversible actions such as deleting.
footer?ModalSlotFooter shown instead of the ready-made buttons. If a function is given it receives { close, pending }. With null no footer is drawn at all.
showCloseButton?booleantrueShow a close (×) button in the top right corner?
closeOnBackdropClick?booleantrueClose on backdrop click? Pass false for forms where you don't want the user to lose data by accident.
closeOnBackButton?booleantrueShould the browser or phone back button close the window?
swipeToClose?booleantrueSwipe to close with a finger.
stickyHeader?booleantrueShould the header (title, description, icon) stay fixed? When true, only the body scrolls with long content and the header is always visible. When false, the header scrolls with the body.
stickyFooter?booleantrueShould the footer (buttons) stay fixed? When true, the buttons are always visible with long content and the body scrolls above them. When false, the footer is at the end of the content and scrolls with the body.
className?stringClasses added to the window itself.
classNames?ModalClassNamesClasses added to the inner parts.

Values received when children and footer are given as functions; so the window can be closed without writing nested components.

NameTypeDescription
close() => voidCloses the window.
pendingbooleanIs the confirmation (onConfirm) in progress?

Content that can be given as children or footer.

type ModalSlot = ReactNode | ((context: ModalRenderContext) => ReactNode)

Classes that can be passed to the inner parts of the ready-made window.

NameTypeDescription
header?stringTop area with the title, description and icon.
icon?stringIcon box.
title?stringTitle.
description?stringDescription.
body?stringBody wrapping children.
footer?stringFooter area with the buttons.