0.04K

A window overlaid on either the primary window or another dialog window, rendering the content underneath inert.

import { on } from '@ember/modifier';
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';

import { Button } from '@/components/ui/button';
import {
  Dialog,
  DialogClose,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
  DialogTrigger,
} from '@/components/ui/dialog';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';

export default class DialogDemo extends Component {
  @tracked name = 'Pedro Duarte';
  @tracked username = '@peduarte';
  @tracked isOpen = false;

  setIsOpen = (open: boolean) => {
    this.isOpen = open;
  };

  handleSubmit = (event: SubmitEvent) => {
    event.preventDefault();
    const formData = new FormData(event.target as HTMLFormElement);
    this.name = formData.get('name') as string;
    this.username = formData.get('username') as string;
    this.isOpen = false;
  };

  <template>
    <Dialog @onOpenChange={{this.setIsOpen}} @open={{this.isOpen}}>
      <form {{on "submit" this.handleSubmit}}>
        <DialogTrigger>
          <Button @variant="outline">Open Dialog</Button>
        </DialogTrigger>
        <DialogContent @class="sm:max-w-[425px]">
          <DialogHeader>
            <DialogTitle>Edit profile</DialogTitle>
            <DialogDescription>
              Make changes to your profile here. Click save when you're done.
            </DialogDescription>
          </DialogHeader>
          <div class="grid gap-4">
            <div class="grid gap-3">
              <Label @for="name-1">Name</Label>
              <Input id="name-1" name="name" value={{this.name}} />
            </div>
            <div class="grid gap-3">
              <Label @for="username-1">Username</Label>
              <Input id="username-1" name="username" value={{this.username}} />
            </div>
          </div>
          <DialogFooter>
            <DialogClose @asChild={{true}}>
              <Button @variant="outline">Cancel</Button>
            </DialogClose>
            <Button type="submit">Save changes</Button>
          </DialogFooter>
        </DialogContent>
      </form>
    </Dialog>
  </template>
}

Installation

pnpm dlx shadcn-ember@latest add dialog

Usage

import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogHeader,
  DialogTitle,
  DialogTrigger,
} from '@/components/ui/dialog';
<Dialog>
  <DialogTrigger>Open</DialogTrigger>
  <DialogContent>
    <DialogHeader>
      <DialogTitle>Are you absolutely sure?</DialogTitle>
      <DialogDescription>
        This action cannot be undone. This will permanently delete your account
        and remove your data from our servers.
      </DialogDescription>
    </DialogHeader>
  </DialogContent>
</Dialog>

Examples

Custom close button

import { Button } from '@/components/ui/button';
import {
  Dialog,
  DialogClose,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
  DialogTrigger,
} from '@/components/ui/dialog';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';

<template>
  <Dialog>
    <DialogTrigger>
      <Button @variant="outline">Share</Button>
    </DialogTrigger>
    <DialogContent @class="sm:max-w-md">
      <DialogHeader>
        <DialogTitle>Share link</DialogTitle>
        <DialogDescription>
          Anyone who has this link will be able to view this.
        </DialogDescription>
      </DialogHeader>
      <div class="flex items-center gap-2">
        <div class="grid flex-1 gap-2">
          <Label @class="sr-only" @for="link">
            Link
          </Label>
          <Input
            id="link"
            readonly
            value="https://ui.shadcn.com/docs/installation"
          />
        </div>
      </div>
      <DialogFooter @class="sm:justify-start">
        <DialogClose>
          <Button @variant="secondary" type="button">
            Close
          </Button>
        </DialogClose>
      </DialogFooter>
    </DialogContent>
  </Dialog>
</template>

Notes

To use the Dialog component from within a Context Menu or Dropdown Menu , you must encase the Context Menu or Dropdown Menu component in the Dialog component.

<Dialog>
  <ContextMenu>
    <ContextMenuTrigger>Right click</ContextMenuTrigger>
    <ContextMenuContent>
      <ContextMenuItem>Open</ContextMenuItem>
      <ContextMenuItem>Download</ContextMenuItem>
      <DialogTrigger @asChild={{true}}>
        <ContextMenuItem>
          <span>Delete</span>
        </ContextMenuItem>
      </DialogTrigger>
    </ContextMenuContent>
  </ContextMenu>
  <DialogContent>
    <DialogHeader>
      <DialogTitle>Are you absolutely sure?</DialogTitle>
      <DialogDescription>
        This action cannot be undone. Are you sure you want to permanently
        delete this file from our servers?
      </DialogDescription>
    </DialogHeader>
    <DialogFooter>
      <Button type="submit">Confirm</Button>
    </DialogFooter>
  </DialogContent>
</Dialog>