Command palette

@yasinsensoy/native

React Native · Components

Dialog

A modal window that opens on top of the screen.

8 exports

Live example

The preview runs in the browser with the web component; the native component takes the same props and looks the same on iOS and Android.

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/native";

<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/native";

Dialog

component

A modal window that opens on top of the screen. Same API as Dialog on web: the title, description, icon, body and buttons are given as props; while an onConfirm returning a Promise runs, the confirm button shows loading and the window does not close.

  • Phone: Opens as a sheet sliding up from the bottom (mobileSheet); closes by pulling down, tapping the backdrop and the Android back button. When the keyboard opens it moves above the keyboard and respects the bottom inset of notched screens.
  • Tablet: Opens centered.
  • Look and animations are the same on iOS and Android: not RN's Modal but the library's own layer is used (see UIProvider).

Do not write a component just for an "Are you sure?" question; use confirm.

Dialog({ size, mobileSheet, trigger, open, defaultOpen, onOpenChange, onConfirm, closeOnBackdropClick, closeOnBackButton, swipeToClose, style, testID, ...layout }: DialogProps): JSX.Element

Examples

<Dialog
  trigger={<Button variant="outline">Edit profile</Button>}
  title="Edit profile"
  description="Changes appear once saved."
  confirmLabel="Save"
  onConfirm={async () => await save(name)}
>
  <TextField label="Name" value={name} onChangeText={setName} />
</Dialog>

confirm

function

Asks the user a confirmation question and waits for the answer. Returns true if confirmed, false if dismissed (Cancel, backdrop, pull down, back button). Same as confirm on web.

The window has all of Dialog's behavior (a bottom sheet on a phone, closing with the back button…). When called several times at once the questions are shown in order. It can also be called from outside components (services, stores).

It needs a UIProvider at the root of the app; without one it falls back to the platform's own alert (Alert).

confirm(options: ConfirmOptions): Promise<boolean>

Examples

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

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

Options of the confirm function (same as web).

NameTypeDefaultDescription
titleReactNodeThe question; becomes the window title.
description?ReactNodeDescription below the question (e.g. "This cannot be undone.").
icon?IconPropIcon next to the title: a name or an element.
confirmLabel?ReactNode"Onayla"Confirm button text.
cancelLabel?ReactNode"İptal"Cancel button text.
tone?Tone"primary"Confirm button color. "danger" for actions like deleting.
onConfirm?() => unknownRuns on confirm, before the window closes. If it returns a Promise the confirm button shows loading until it finishes; if it returns false or throws, the window stays open.

Maximum width of the dialog window (same as web).

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

DialogProps

interface

Props of the Dialog component.

NameTypeDefaultDescription
size?DialogSize"md"Maximum width of the window: sm 384, md 512, lg 672, xl 896 pixels. When it opens as a bottom sheet on a phone it is always full width.
mobileSheet?booleantrueTurn into a sheet sliding up from the bottom on narrow screens (below 640 pixels, i.e. phones)? With false it opens centered on every screen.
open?booleanWhether it is open in controlled use.
defaultOpen?booleanfalseOpen initially in uncontrolled use?
onOpenChange?(open: boolean) => voidCalled with the new state when it opens or closes.
trigger?ReactElementElement that opens the window, usually a Button. Pressing it opens the window.
titleReactNodeTitle. Screen readers announce the window by this name; so it is required.
description?ReactNodeDescription below the title.
icon?IconPropIcon next to the title: a name or an element.
children?ModalSlotBody. When a function, it receives { close, pending }.
onConfirm?() => unknownCalled when the confirm button is pressed (unlike web there is no form, so no FormData is passed; read field values from your own state). - If it returns a Promise, the confirm button shows loading until it finishes and the window does not close (the backdrop, dragging and the back button wait too). - If it returns false or throws, the window stays open. - Otherwise the window closes.
confirmLabel?ReactNodeConfirm button text. "Onayla" when onConfirm is given and this is not.
cancelLabel?ReactNode"İptal"Cancel button text. With null the button is not shown.
tone?Tone"primary"Confirm button color. "danger" for actions like deleting.
footer?ModalSlotFooter instead of the built-in buttons. When a function, it receives { close, pending }; null draws nothing.
showCloseButton?booleantrueClose (×) button in the top right corner.
closeOnBackdropClick?booleantrueClose when the backdrop is tapped?
closeOnBackButton?booleantrueShould the Android back button (and back gesture) close the window?
swipeToClose?booleantrueClosing by swiping with a finger.
stickyHeader?booleantrueKeep the header fixed in place? With false it scrolls with the body.
stickyFooter?booleantrueKeep the footer (buttons) fixed in place?
style?ViewStyleStyle of the window itself.
testID?stringTest id (the window surface).

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

NameTypeDescription
close() => voidCloses the window.
pendingbooleanWhether the confirm (onConfirm) is in progress.

Content that can be given as children or footer.

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

ModalOwnProps

interface

Common props of Dialog and Sheet (ModalOwnProps on web).

NameTypeDefaultDescription
open?booleanWhether it is open in controlled use.
defaultOpen?booleanfalseOpen initially in uncontrolled use?
onOpenChange?(open: boolean) => voidCalled with the new state when it opens or closes.
trigger?ReactElementElement that opens the window, usually a Button. Pressing it opens the window.
titleReactNodeTitle. Screen readers announce the window by this name; so it is required.
description?ReactNodeDescription below the title.
icon?IconPropIcon next to the title: a name or an element.
children?ModalSlotBody. When a function, it receives { close, pending }.
onConfirm?() => unknownCalled when the confirm button is pressed (unlike web there is no form, so no FormData is passed; read field values from your own state). - If it returns a Promise, the confirm button shows loading until it finishes and the window does not close (the backdrop, dragging and the back button wait too). - If it returns false or throws, the window stays open. - Otherwise the window closes.
confirmLabel?ReactNodeConfirm button text. "Onayla" when onConfirm is given and this is not.
cancelLabel?ReactNode"İptal"Cancel button text. With null the button is not shown.
tone?Tone"primary"Confirm button color. "danger" for actions like deleting.
footer?ModalSlotFooter instead of the built-in buttons. When a function, it receives { close, pending }; null draws nothing.
showCloseButton?booleantrueClose (×) button in the top right corner.
closeOnBackdropClick?booleantrueClose when the backdrop is tapped?
closeOnBackButton?booleantrueShould the Android back button (and back gesture) close the window?
swipeToClose?booleantrueClosing by swiping with a finger.
stickyHeader?booleantrueKeep the header fixed in place? With false it scrolls with the body.
stickyFooter?booleantrueKeep the footer (buttons) fixed in place?
style?ViewStyleStyle of the window itself.
testID?stringTest id (the window surface).