# Dialog

<div class="not-content sl-bejamas-component-preview flex justify-center px-4 md:px-10 py-12 border border-border rounded-t-lg min-h-72 items-center">
<Dialog> <DialogTrigger asChild> <Button>Manage Cookies</Button> </DialogTrigger> <DialogContent class="sm:max-w-lg"> <DialogHeader> <DialogTitle>Manage Cookies</DialogTitle> <DialogDescription> Using websites and apps involves storing and retrieving information from your device, including cookies and other identifiers. </DialogDescription> </DialogHeader> <div class="mt-3"> <div class="flex items-center gap-3"> <Checkbox id="cookies-necessary" name="cookies-necessary" disabled checked /> <Label for="cookies-necessary">Strictly Necessary Cookies (always active)</Label> </div> <p class="text-sm text-muted-foreground ml-8 mt-1">These cookies are essential for the site to function and cannot be toggled off. They assist with security, user authentication, customer support, etc.</p> </div> <div class="mt-3"> <div class="flex items-center gap-3"> <Checkbox id="cookies-analytics" name="cookies-analytics" /> <Label for="cookies-analytics">Analytics Cookies</Label> </div> <p class="text-sm text-muted-foreground ml-8 mt-1">These cookies help us understand how visitors interact with our site. They allow us to measure traffic and improve site performance.</p> </div> <div class="mt-3"> <div class="flex items-center gap-3"> <Checkbox id="cookies-marketing-performance" name="cookies-marketing-performance" /> <Label for="cookies-marketing-performance">Marketing Performance Cookies</Label> </div> <p class="text-sm text-muted-foreground ml-8 mt-1">These cookies help us measure the effectiveness of our marketing campaigns.</p> </div> <DialogFooter> <Button data-slot="dialog-close" class="cn-dialog-close mt-4" variant="outline">Close</Button> <Button data-dialog-close class="mt-4">Save changes</Button> </DialogFooter> </DialogContent> </Dialog>
</div>

```astro
---
---

<Dialog>
  <DialogTrigger asChild>
    <Button>Manage Cookies</Button>
  </DialogTrigger>

  <DialogContent class="sm:max-w-lg">
    <DialogHeader>
      <DialogTitle>Manage Cookies</DialogTitle>
      <DialogDescription>
        Using websites and apps involves storing and retrieving information from your device, including cookies and other identifiers.
      </DialogDescription>
    </DialogHeader>
    <div class="mt-3">
      <div class="flex items-center gap-3">
        <Checkbox id="cookies-necessary" name="cookies-necessary" disabled checked />
        <Label for="cookies-necessary">Strictly Necessary Cookies (always active)</Label>
      </div>
      <p class="text-sm text-muted-foreground ml-8 mt-1">These cookies are essential for the site to function and cannot be toggled off. They assist with security, user authentication, customer support, etc.</p>
    </div>
    <div class="mt-3">
      <div class="flex items-center gap-3">
        <Checkbox id="cookies-analytics" name="cookies-analytics" />
        <Label for="cookies-analytics">Analytics Cookies</Label>
      </div>
      <p class="text-sm text-muted-foreground ml-8 mt-1">These cookies help us understand how visitors interact with our site. They allow us to measure traffic and improve site performance.</p>
    </div>
    <div class="mt-3">
      <div class="flex items-center gap-3">
        <Checkbox id="cookies-marketing-performance" name="cookies-marketing-performance" />
        <Label for="cookies-marketing-performance">Marketing Performance Cookies</Label>
      </div>
      <p class="text-sm text-muted-foreground ml-8 mt-1">These cookies help us measure the effectiveness of our marketing campaigns.</p>
    </div>
    <DialogFooter>
      <Button data-slot="dialog-close" class="cn-dialog-close mt-4" variant="outline">Close</Button>
      <Button data-dialog-close class="mt-4">Save changes</Button>
    </DialogFooter>
  </DialogContent>
</Dialog>
```

## Installation

<DocsTabs syncKey="pkg">
  <DocsTabItem label="bun">
  ```bash
  bunx bejamas add dialog
  ```
  </DocsTabItem>
  <DocsTabItem label="npm">
  ```bash
  npx bejamas add dialog
  ```
  </DocsTabItem>
  <DocsTabItem label="pnpm">
  ```bash
  pnpm dlx bejamas add dialog
  ```
  </DocsTabItem>
  <DocsTabItem label="yarn">
  ```bash
  yarn dlx bejamas add dialog
  ```
  </DocsTabItem>
</DocsTabs>

## Usage

```astro nocollapse
---
---

<Dialog id="myDialog">
  <DialogTrigger>Open Dialog</DialogTrigger>
  <DialogContent>
    <DialogTitle>Dialog Title</DialogTitle>
    <DialogDescription>Dialog description.</DialogDescription>
    Content here
    <DialogFooter>
      <DialogClose>Close</DialogClose>
    </DialogFooter>
  </DialogContent>
</Dialog>
```

## Props

| Prop | Type | Default |
|---|---|---|
| <code>defaultOpen</code> | `boolean` | `false` |
| <code>class</code> | `string` | `""` |
| <code>closeOnClickOutside</code> | `boolean` | `true` |
| <code>closeOnEscape</code> | `boolean` | `true` |
| <code>lockScroll</code> | `boolean` | `true` |

## Examples

### Nested Dialogs

<div class="not-content sl-bejamas-component-preview flex justify-center px-4 md:px-10 py-12 border border-border rounded-t-lg min-h-72 items-center">
<Dialog> <Button data-slot="dialog-trigger" class="cn-dialog-trigger">{"Open Nested Dialog"}</Button> <DialogContent class="sm:max-w-lg"> <DialogHeader> <DialogTitle>{"Nested Dialog"}</DialogTitle> <DialogDescription>{" Using websites and apps involves storing and retrieving information from your device, including cookies and other identifiers. "}</DialogDescription> </DialogHeader> <DialogFooter> <Button data-slot="dialog-close" class="cn-dialog-close mt-4" variant="outline">{"Close"}</Button> <Dialog> <Button data-slot="dialog-trigger" class="cn-dialog-trigger mt-4">{"Open Nested Dialog"}</Button> <DialogContent class="sm:max-w-md border-red-800"> <DialogHeader> <DialogTitle>{"Nested Dialog"}</DialogTitle> <DialogDescription>{" Using websites and apps involves storing and retrieving information from your device, including cookies and other identifiers. "}</DialogDescription> </DialogHeader> </DialogContent> </Dialog> </DialogFooter> </DialogContent> </Dialog>
</div>

```astro
---
---

<Dialog>
  <Button data-slot="dialog-trigger" class="cn-dialog-trigger">Open Nested Dialog</Button>
  <DialogContent class="sm:max-w-lg">
    <DialogHeader>
      <DialogTitle>Nested Dialog</DialogTitle>
      <DialogDescription>
        Using websites and apps involves storing and retrieving information from your device, including cookies and other identifiers.
      </DialogDescription>
    </DialogHeader>
    <DialogFooter>
      <Button data-slot="dialog-close" class="cn-dialog-close mt-4" variant="outline">Close</Button>
      <Dialog>
        <Button data-slot="dialog-trigger" class="cn-dialog-trigger mt-4">Open Nested Dialog</Button>
        <DialogContent class="sm:max-w-md border-red-800">
          <DialogHeader>
            <DialogTitle>Nested Dialog</DialogTitle>
            <DialogDescription>
              Using websites and apps involves storing and retrieving information from your device, including cookies and other identifiers.
            </DialogDescription>
          </DialogHeader>
        </DialogContent>
      </Dialog>
    </DialogFooter>
  </DialogContent>
</Dialog>
```

## API Reference

### Events

The dialog emits custom events that you can listen to:

| Event | Detail | Description |
|-------|--------|-------------|
| `dialog:change` | `{ open: boolean }` | Fired when the open state changes |

<div class="not-content sl-bejamas-component-preview flex justify-center px-4 md:px-10 py-12 border border-border rounded-t-lg min-h-72 items-center">
<Dialog id="my-dialog"> <DialogTrigger>{"Open Dialog"}</DialogTrigger> <DialogContent> <DialogTitle>{"Title"}</DialogTitle> <DialogDescription>{"Description"}</DialogDescription> </DialogContent> </Dialog>
</div>

```astro nocollapse
<Dialog id="my-dialog">
  <DialogTrigger>Open Dialog</DialogTrigger>
  <DialogContent>
    <DialogTitle>Title</DialogTitle>
    <DialogDescription>Description</DialogDescription>
  </DialogContent>
</Dialog>
```

```js nocollapse
  const dialog = document.getElementById('my-dialog');

  dialog.addEventListener('dialog:change', (e) => {
    console.log('Is open:', e.detail.open);
  });
```

### Programmatic Control

You can control the dialog programmatically by dispatching a `dialog:set` event:

```js nocollapse
const dialog = document.getElementById('my-dialog');

// Open the dialog
dialog.dispatchEvent(new CustomEvent('dialog:set', {
  detail: { open: true }
}));

// Close the dialog
dialog.dispatchEvent(new CustomEvent('dialog:set', {
  detail: { open: false }
}));
```

### Data Attributes

The dialog sets these data attributes that you can use for styling or querying state:

| Attribute | Element | Description |
|-----------|---------|-------------|
| `data-state` | dialog | Current state (`open` or `closed`) |