Elements

Dialog

A dialog presents content over the rest of the app in its own native window layer: a presented view controller on iOS and Mac Catalyst, a Dialog on Android, a layer over the whole window on the desktops and the web. It is used for full-screen flows, sheets and dialogs. The system’s own gestures (swiping a sheet down, the Android back button, Escape) work, and are decided natively. Rust makes one with nsh_base::dialog(), TSX with <Dialog> from nsh/base.

el-dialog.tsx
el-dialog.tsxLive
import { mount, signal, Text, View } from "nsh";
import { Button, Dialog, Switch } from "nsh/base";
import { backdrop, button, buttonText, desktop, dialogCard, dialogText, dialogTitle, Inspector, Panel, Stage, switchLook, ui, wide } from "./palette";


function App() {
  const card = signal(false);
  const sheet = signal(false);
  const backdropCloses = signal(true);
  const cancels = signal(0);
  const log = signal("–");
  const openCard = () => (
    <Button onPress={() => card.set(true)} {...button("primary")}>
      <Text style={buttonText("primary")}>Open a card</Text>
    </Button>
  );
  const openSheet = () => (
    <Button onPress={() => sheet.set(true)} {...button()}>
      <Text style={buttonText()}>Open as a page sheet</Text>
    </Button>
  );
  const backdropRow = () => (
    <Switch
      checked={backdropCloses()}
      onChange={({ checked }) => backdropCloses.set(checked)}
      class={wide() ? "flex-row items-center gap-2 self-center active:opacity-70" : "flex-row items-center gap-3 rounded-xl p-3 active:opacity-70"}
      style={wide() ? undefined : ui().row}
      {...switchLook(() => backdropCloses())}
    >
      <Text style={{ color: ui().text, flex: wide() ? undefined : 1, fontSize: desktop ? 13 : undefined }}>closeOnBackdropPress {String(backdropCloses())}</Text>
    </Switch>
  );
  const hint = desktop
    ? "Close with Escape or a click on the backdrop."
    : "Close with a tap on the backdrop, a swipe down or the back button. On iOS the sheet is a system pageSheet.";
  const state = () => `open: card ${String(card())} · sheet ${String(sheet())} · onCancel ${cancels()}×`;
  return (
    <View style={{ flex: 1, backgroundColor: ui().page, padding: 20, paddingTop: 24, gap: 12 }}>
      <Text style={{ fontSize: 22, fontWeight: "700", color: ui().text }}>dialog</Text>

      {() =>
        wide() ? (
          <>
            <Text style={{ color: ui().muted }}>{hint}</Text>
            <View style={{ flex: 1, flexDirection: "row", gap: 16 }}>
              <Stage gap={16}>
                <View style={{ flexDirection: "row", justifyContent: "center", gap: 8 }}>
                  {openCard()}
                  {openSheet()}
                </View>
                {backdropRow()}
              </Stage>
              <Inspector>
                <Panel title="open · onCancel">
                  <Text style={{ fontSize: 13, color: ui().text }}>{state()}</Text>
                </Panel>
                <Panel title="onOpenChange({ open, reason }) · onOpen" grow>
                  <Text style={{ fontSize: 13, fontWeight: "600", color: ui().accent }}>{log()}</Text>
                </Panel>
              </Inspector>
            </View>
          </>
        ) : (
          <View style={{ gap: 10 }}>
            {openCard()}
            {openSheet()}

            {backdropRow()}

            <View style={{ ...ui().inset, borderRadius: 12, padding: 14, gap: 4 }}>
              <Text style={{ color: ui().muted, fontSize: 13 }}>{state()}</Text>
              <Text style={{ color: ui().accent, fontSize: 15, fontWeight: "600" }}>{log()}</Text>
            </View>
            <Text style={{ color: ui().faint, fontSize: 13 }}>{hint}</Text>
          </View>
        )
      }

      <Dialog
        open={card()}
        animation="fade"
        closeOnBackdropPress={backdropCloses()}
        onOpenChange={({ open, reason }) => {
          card.set(open);
          log.set(`onOpenChange({ open: ${open}, reason: "${reason}" })`);
        }}
        onCancel={() => cancels.update((n) => n + 1)}
        onOpen={() => log.set("onOpen: card")}
        backdropTestID="card-backdrop"
        backdropStyle={backdrop()}
        style={dialogCard(desktop ? 360 : 320)}
      >
        <Text style={dialogTitle()}>Delete this item?</Text>
        <Text style={dialogText()}>
          {backdropCloses()
            ? "A press on the dimmed backdrop closes it."
            : "The backdrop only calls onCancel: close with a button."}
        </Text>
        <View style={{ flexDirection: "row", gap: 8, paddingTop: 4, justifyContent: desktop ? "flex-end" : undefined }}>
          <Button
            onPress={() => {
              card.set(false);
              log.set("closed by the app: Keep");
            }}
            {...button("secondary", { style: desktop ? {} : { flex: 1 } })}
          >
            <Text style={buttonText()}>Keep</Text>
          </Button>
          <Button
            onPress={() => {
              card.set(false);
              log.set("closed by the app: Delete");
            }}
            {...button("danger", { style: desktop ? {} : { flex: 1 } })}
          >
            <Text style={buttonText("danger")}>Delete</Text>
          </Button>
        </View>
      </Dialog>

      <Dialog
        open={sheet()}
        presentation="pageSheet"
        onOpenChange={({ open, reason }) => {
          sheet.set(open);
          log.set(`onOpenChange({ open: ${open}, reason: "${reason}" })`);
        }}
        onCancel={() => cancels.update((n) => n + 1)}
        onOpen={() => log.set("onOpen: sheet")}
        backdropStyle={backdrop()}
        // As tall as its content: a panel in the middle on the desktop, a
        // sheet along the bottom edge on a phone.
        style={
          desktop
            ? { backgroundColor: ui().page, margin: "auto", width: 480, maxWidth: "90%", maxHeight: "90%", borderRadius: 16 }
            : {
                backgroundColor: ui().page,
                marginTop: "auto",
                width: "100%",
                maxWidth: 560,
                maxHeight: "90%",
                alignSelf: "center",
                borderTopLeftRadius: 16,
                borderTopRightRadius: 16,
              }
        }
      >
        <View style={{ padding: 20, paddingTop: "calc(env(safe-area-inset-top) + 20px)", paddingRight: "calc(env(safe-area-inset-right) + 20px)", paddingBottom: "calc(env(safe-area-inset-bottom) + 20px)", paddingLeft: "calc(env(safe-area-inset-left) + 20px)", gap: 12 }}>
          <Text style={{ color: ui().text, fontSize: 22, fontWeight: "700" }}>A sheet</Text>
          <Text style={{ color: ui().muted }}>
            presentation="pageSheet": a system sheet on iOS that follows the finger when swiped down. Elsewhere the
            dialog is a layer over the window, and this panel is the app's own style.
          </Text>
          <Button onPress={() => sheet.set(false)} {...button("primary", { align: "flex-end" })}>
            <Text style={buttonText("primary")}>Close</Text>
          </Button>
        </View>
      </Dialog>
    </View>
  );
}

mount(App);

The dialog owns its behavior and leaves its look to the app: its styles style the popup (the view holding the children), and in TSX backdropClass / backdropStyle add a backdrop behind it.

On macOS, closing a dialog returns keyboard focus to the input or control that had it before opening, including Editor and RichEditor, while that control remains visible in the current scope. If another control has already taken focus, it keeps it.

use nsh_base::{button, dialog};
use nsh_core::prelude::*;

/// A blue button with a white label.
fn primary(label: &'static str, on_press: impl Fn() + 'static) -> impl IntoElement {
    button()
        .on_press(on_press)
        .items_center()
        .rounded_lg()
        .bg(rgb(0x2563eb))
        .px_4()
        .py_3()
        .active(|s| s.opacity(0.7))
        .child(text(label).font_weight(FontWeight::SEMIBOLD).text_color(white()))
}

fn example() -> impl IntoElement {
    let open = signal(false);
    view()
        .flex_1()
        .justify_center()
        .p_6()
        .child(primary("Open", move || open.set(true)))
        .child(when(open, move || {
            dialog()
                .presentation(ModalPresentation::PageSheet)
                .on_cancel(move || open.set(false))
                .flex_1()
                .bg(white())
                .child(
                    view()
                        .flex_1()
                        .px_5()
                        .pt_safe_area_plus(px(20.0))
                        .pb_safe_area_plus(px(20.0))
                        .gap_3()
                        .child(text("A sheet").text_2xl().font_weight(FontWeight::BOLD))
                        .child(primary("Close", move || open.set(false))),
                )
        }))
}

In TSX, the dialog is shown while open is not false and closed when it becomes false. In Rust, it is presented while it is in the view and dismissed when it leaves it, so when holds it. Either way it gives its content the dialog’s safe-area insets, which the env(safe-area-inset-*) variables and safeAreaInsets() (cx.insets() in a Rust component) inside it read. Button, also from nsh/base, is a press target styled by the app.

nsh_core::prelude also has an older, deprecated dialog function: import nsh_base::dialog by name, as above, and the explicit import takes its place. For the system’s alert or action sheet, use AlertDialog.

#Props

Prop Rust Type Default Description
open in the view while open (when(open, ..)) boolean true Shown while true.
animation .animation(ModalAnimation::Fade) "slide" | "fade" | "none" "slide" How it comes in and goes.
transitionShader .transition_shader(TransitionShader::new(program)) { source: string; uniforms?: Record<string, ShaderUniform>; duration?: number } none It comes in and goes through a WGSL shader in place of its animation: shade(uv) reads what is behind as sample_from and the dialog as sample_to while nsh.progress runs from 0 to 1 over duration milliseconds (350 by default), and from 1 back to 0 as it closes. When the system asks for less motion, it takes its own animation. See Shader.
presentation .presentation(ModalPresentation::PageSheet) "fullScreen" | "pageSheet" | "formSheet" "fullScreen" Its form on iOS: fullScreen covers the screen with a clear background; pageSheet and formSheet are system sheets that keep what is behind visible (formSheet is a smaller centered sheet on large screens).
closeOnEscape .close_on_escape(..) boolean true Escape and the Android back button close it natively.
closeOnBackdropPress .close_on_backdrop_press(..) boolean true A press on the backdrop closes it.
closeOnSwipe .close_on_swipe(..) boolean true Swiping a sheet down (iOS, phone browsers) closes it natively.
detents .detents(vec![SheetDetent::Medium, SheetDetent::Large]) SheetDetent[] [] iOS sheets (pageSheet, formSheet): the heights it rests at, "medium" (about half the screen), "large", { height } in points or { fraction } of the large one (the SheetDetent values Sheet takes). None: the system’s full height. See A sheet at half height.
detent .detent(..) SheetDetent The detent it rests at: it moves there when the value changes. The user’s moves stand even when the app does not follow them.
defaultDetent .default_detent(..) SheetDetent the smallest The detent it opens at without detent.
grabber .grabber(..) boolean false The system’s grabber at the top of the sheet.
largestUndimmedDetent .largest_undimmed_detent(..) SheetDetent none Up to this detent, what is behind the sheet stays undimmed and takes touches.
statusBar .status_bar(..) { foreground?, hidden?, animated? } none iOS and Android: the status bar while the dialog is open, over the pages’ (Status bar).
accessibilityLabel .label(..) string none What screen readers call the dialog as the focus enters it: its title.
accessibilityDescription .accessibility_description(..) string none What screen readers read after its name: its message.
role .role(AccessibilityRole::AlertDialog) "dialog" | "alertdialog" "dialog" What it is for screen readers: "alertdialog" for one that asks the user to confirm something or tells them something urgent.
onOpen .on_open(..) () => void It finished appearing.
onClose .on_close(..) () => void It went away: the user closed it, or the app removed it.
onCancel .on_cancel(..) () => void The user tried to close it (Escape, back, a backdrop press, a swipe down), whether or not a closeOn* flag let it close.
onDetentChange .on_detent_change(..) (e: { detent: SheetDetent }) => void The user moved the sheet to another detent (a drag, the grabber, a scroll at its edge, a screen reader).
onOpenChange .on_cancel(..) (e: { open: boolean; reason: "cancel" | "backdropPress" | "swipe" }) => void The user closed it: keep open in sync here. In Rust, on_cancel removes the dialog from the view.
class, style Styled methods string, StyleProp<ViewStyle> The popup: the view holding the children.
backdropClass, backdropStyle a child view filling the dialog string, StyleProp<ViewStyle> A backdrop filling the dialog behind the popup, present when either is set.
children .child(..) JSX.Element The content.
ios key Rust Type Default Description
supportedOrientations .supported_orientations(..) ("portrait" | "portrait-upside-down" | "landscape" | "landscape-left" | "landscape-right")[] the presenter’s The orientations the dialog allows. landscape-left is the device turned clockwise, its home side on the left (UIKit’s landscapeLeft).
android key Rust Type Default Description
statusBarTranslucent .status_bar_translucent(..) boolean false The content reaches under the status bar; otherwise it starts below it.

#Accessibility

Screen readers hear a dialog as one: as the focus enters it they say that it is a dialog and its name, then read what is inside. Give it its title as its name, accessibilityLabel in TSX and .label(..) in Rust, and its message as its description, accessibilityDescription and .accessibility_description(..), which screen readers read right after the name, so the user hears what the dialog is about before reaching its buttons. A dialog that asks the user to confirm something, such as a deletion, is an alert dialog: role="alertdialog", .role(AccessibilityRole::AlertDialog). Each platform presents the role as its own dialogs: VoiceOver’s dialog on macOS, a semantic group named by the title on iOS, UI Automation’s dialog pane on Windows, AT-SPI’s dialog on Linux, a dialog with aria-modal on the web.

dialog()
    .role(AccessibilityRole::AlertDialog)
    .label("Delete this item?")
    .accessibility_description("This can't be undone.")
    .on_cancel(move || confirm.set(false))
    .child(card())

#Opening and closing

The dialog’s state is the app’s. When the user closes the dialog (Escape, the Android back button, a press on the backdrop, a sheet swiped down), it closes natively and tells the app, which sets its state to match: TSX’s onOpenChange({ open: false, reason }), Rust’s on_cancel. In TSX, while open stays true the dialog stays closed, and it opens again when open goes from false to true.

Each closeOn* flag is read natively when the user acts, since the main thread never waits for the app’s code. To confirm before closing, set the flags to false: the attempt then calls onCancel, and the app closes the dialog itself (in TSX by setting open to false, in Rust by taking it out of the view):

dialog()
    .presentation(ModalPresentation::PageSheet)
    .close_on_escape(false)
    .close_on_backdrop_press(false)
    .close_on_swipe(false)
    .on_cancel(move || ask_to_discard.set(true))
    .flex_1()
    .bg(white())
    .child(form())

onCancel is called on every attempt, whether the flags let the dialog close or not.

#A sheet at half height

On iOS, a pageSheet or formSheet dialog with detents rests at those heights: the user drags it between them, the system way, and past the smallest it closes when closeOnSwipe allows. A NavStack inside it pushes pages with the system’s header, back button and swipe back, inside the sheet:

dialog()
    .presentation(ModalPresentation::PageSheet)
    .detents(vec![SheetDetent::Medium, SheetDetent::Large])
    .detent(move || Some(detent.get()))
    .on_detent_change(move |d: &SheetDetent| detent.set(*d))
    .grabber(true)
    .on_cancel(move || open.set(false))
    .flex_1()
    .child(nav_stack().flex_1().child(nav_page().key("settings").child(settings())))

detent moves the sheet when its value changes, and follows the user with onDetentChange as above; a drag the app does not follow stays where the user left it.

The content is laid out at the height the sheet rests at. Elsewhere the detents are ignored and the dialog keeps its form there; for a bottom sheet that rests at detents on every platform, use Sheet.

#A dialog with a backdrop

A dialog with the default fullScreen presentation shows what the app draws: here a dimmed backdrop and a card centered over it. A press on the backdrop closes it:

when(confirm, move || {
    dialog()
        .animation(ModalAnimation::Fade)
        .on_cancel(move || confirm.set(false))
        .flex_1()
        .child(
            // The backdrop: a press on it closes the dialog.
            view()
                .absolute()
                .inset_0()
                .bg(rgba(0x00000066))
                .on_press(move || confirm.set(false)),
        )
        .child(
            view()
                .m(auto())
                .w_80()
                .gap_3()
                .rounded_2xl()
                .bg(white())
                .p_5()
                .child(text("Delete this item?").text_xl().font_weight(FontWeight::BOLD))
                .child(
                    button()
                        .on_press(remove)
                        .items_center()
                        .rounded_lg()
                        .bg(rgb(0xdc2626))
                        .px_4()
                        .py_3()
                        .active(|s| s.opacity(0.7))
                        .child(text("Delete").font_weight(FontWeight::SEMIBOLD).text_color(white())),
                ),
        )
})

For the system’s alert or action sheet, use AlertDialog from nsh/base.

#Ref methods

None.

#Platform notes

  • iOS and Mac Catalyst: presentation picks the form. fullScreen presents over the full screen with a clear background; pageSheet and formSheet are system sheets with the system background, which follow the finger when swiped down (closeOnSwipe). A sheet rests at its detents, with the system’s grabber when grabber is set; custom heights need iOS 16, and iOS 15 takes the nearer of medium and large.
  • Android: the dialog fills the window and shows the app’s popup and backdrop. The back button goes to the topmost dialog first, before stacks and BackHandler listeners.
  • Desktops (macOS, Windows, Linux, GPUI) and the web: the dialog is a transparent layer over the whole window that shows the app’s popup and backdrop. Escape is a close attempt.
  • detents, detent, defaultDetent, grabber, largestUndimmedDetent and onDetentChange are iOS’s: Android, the desktops and the web have no system sheet, so they are ignored there (use Sheet).
  • supportedOrientations is iOS’s; statusBarTranslucent is Android’s. An Android dialog’s content starts below the status bar unless statusBarTranslucent is set.