Skip to main content

Modal

Modal is an overlay that focuses attention on a single task, such as discarding a draft or renaming a project.

Import

import { Modal } from '@vortexlabs/vortex-native';
import type { ModalProps } from '@vortexlabs/vortex-native';

Other links

import { Button, Modal, Stack, Text } from "@vortexlabs/vortex-native";
import { useState } from "react";

export const ConfirmDiscard = () => {
const [open, setOpen] = useState(false);
const [discarded, setDiscarded] = useState(false);

const discard = () => {
  setDiscarded(true);
  setOpen(false);
};

return (
  <Stack align="center" gap={2}>
    <Button variant="secondary" onPress={() => { setDiscarded(false); setOpen(true); }}>Discard draft</Button>
    {discarded && <Text color="subtle">Draft discarded</Text>}
    <Modal active={open} onClose={() => setOpen(false)}>
      <Stack gap={5}>
        <Stack gap={1}>
          <Modal.Title>Discard this draft?</Modal.Title>
          <Modal.Description>Your unsent changes will be lost.</Modal.Description>
        </Stack>
        <Stack gap={2}>
          <Button tone="critical" fullWidth onPress={discard}>Discard</Button>
          <Button variant="secondary" fullWidth onPress={() => setOpen(false)}>Keep editing</Button>
        </Stack>
      </Stack>
    </Modal>
  </Stack>
);
};

Features

  • Keeps touches inside the open dialog or on its backdrop.
  • Uses the title to name the dialog and focus its heading on open.
  • Supports dim, blurred, and transparent backdrops.
  • Reports why the dialog requested dismissal and when transitions finish.

Modal.Dismissible

Place the title inside Modal.Dismissible to put the close button beside it. Render it on its own for a close button in the panel’s top end corner.

<Modal.Dismissible closeAriaLabel="Close delivery details">
  <Modal.Title>Delivery details</Modal.Title>
</Modal.Dismissible>

Variants

Use dim to darken the screen or transparent to leave it visible. The blurred variant uses the provider blur adapter and falls back to dim.

<Modal active={open} variant="blurred" onClose={() => setOpen(false)}>
  <Modal.Dismissible closeAriaLabel="Close preview">
    <Modal.Title>Backdrop preview</Modal.Title>
  </Modal.Dismissible>
</Modal>

Backdrop Presses

Set disableCloseOnOutsideClick when tapping outside should leave the dialog open. The close button and Android Back can still dismiss the dialog.

<Modal active={open} onClose={close} disableCloseOnOutsideClick>
  <Stack gap={4}>
    <Modal.Title>Collect your order</Modal.Title>
    <Modal.Description>Bring your confirmation to the service desk.</Modal.Description>
    <Button fullWidth onPress={close}>Got it</Button>
  </Stack>
</Modal>

Padding

Set padding={0} to bring an image to the panel edges. Use a rounded overlay close button over the image.

<Modal active={open} onClose={close} ariaLabel="Sydney harbour photo" padding={0}>
  <Image
    src="https://cdn.vortex.design/docs/components/image/sydney.webp"
    alt="Sydney harbour"
    width="100%"
    aspectRatio={1.5}
  />
  <Modal.Dismissible closeAriaLabel="Close photo" rounded variant="overlay" />
</Modal>

Scrollable Content

Wrap the body in ScrollArea to keep the title and footer visible while people scroll.

<Stack gap={4}>
  <Modal.Title>Harbour walk</Modal.Title>
  <ScrollArea maxHeight={144}>
    <Stack gap={3}>
      {stops.map((stop, index) => <Text key={stop}>{index + 1}. {stop}</Text>)}
    </Stack>
  </ScrollArea>
  <Button fullWidth onPress={close}>Done</Button>
</Stack>

Form Fields

Keep the field’s draft separate from the saved value so Cancel can discard edits.

import { Button, Modal, Stack, Text, TextField } from "@vortexlabs/vortex-native";
import { useState } from "react";

export const RenameProject = () => {
  const [open, setOpen] = useState(false);
  const [name, setName] = useState("Harbour walk");
  const [draft, setDraft] = useState("");
  const close = () => setOpen(false);

  const save = () => {
    setName(draft.trim());
    close();
  };

  return (
    <Stack align="center" gap={2}>
      <Text>{name}</Text>
      <Button onPress={() => { setDraft(name); setOpen(true); }}>Rename project</Button>
      <Modal active={open} onClose={close} disableCloseOnOutsideClick onAfterClose={() => setDraft("")}>
        <Stack gap={4}>
          <Modal.Title>Rename project</Modal.Title>
          <TextField.Root>
            <TextField.Label>Project name</TextField.Label>
            <TextField.Input name="projectName" value={draft} onChange={({ value }) => setDraft(value)} />
          </TextField.Root>
          <Stack direction="row" gap={2} justify="end">
            <Button variant="secondary" onPress={close}>Cancel</Button>
            <Button disabled={!draft.trim()} onPress={save}>Save</Button>
          </Stack>
        </Stack>
      </Modal>
    </Stack>
  );
};

Use onAfterClose to clear unsaved input after the exit animation. Use onAfterOpen for work that needs the panel fully visible.

Overflow

Set overflow="visible" when an element extends beyond the panel, such as an avatar above a profile card.

<Modal active={open} onClose={close} overflow="visible">
  <Stack gap={2} align="center">
    <Avatar initials="AL" size="lg" rounded style={{ marginTop: -36 }} />
    <Modal.Title>Ada Lee</Modal.Title>
    <Modal.Description>Trip organizer</Modal.Description>
    <Button variant="secondary" onPress={close}>Close profile</Button>
  </Stack>
</Modal>

Accessibility

Description
Modal.Title names the dialog and receives screen reader focus when it opens. Supply ariaLabel when you omit the title.
Give the close button an accessible name with closeAriaLabel.