Alert Dialog
A modal dialog that interrupts the user with important content and expects a response.
Loading...
<twig:AlertDialog id="delete_account">
<twig:AlertDialog:Trigger>
<twig:Button {{ ...trigger_attrs }}>Open</twig:Button>
</twig:AlertDialog:Trigger>
<twig:AlertDialog:Content>
<twig:AlertDialog:Header>
<twig:AlertDialog:Title>Are you absolutely sure?</twig:AlertDialog:Title>
<twig:AlertDialog:Description>
This action cannot be undone. This will permanently delete your account
and remove your data from our servers.
</twig:AlertDialog:Description>
</twig:AlertDialog:Header>
<twig:AlertDialog:Footer>
<twig:AlertDialog:Cancel>Cancel</twig:AlertDialog:Cancel>
<twig:AlertDialog:Action>Continue</twig:AlertDialog:Action>
</twig:AlertDialog:Footer>
</twig:AlertDialog:Content>
</twig:AlertDialog>
Installation
Ensure the Symfony UX Toolkit is installed in your Symfony app:
composer require --dev symfony/ux-toolkit
Then, install the recipe and its dependencies by running:
bin/console ux:install Alert Dialog --kit shadcn
That's it!
Install the following Composer dependencies:
composer require twig/extra-bundle twig/html-extra:^3.12.0 tales-from-a-dev/twig-tailwind-extra:^1.0.0
Copy the following file(s) into your Symfony app:
import { Controller } from '@hotwired/stimulus';
export default class extends Controller {
static targets = ['trigger', 'dialog'];
async open() {
this.dialogTarget.showModal();
if (this.hasTriggerTarget) {
if (this.dialogTarget.getAnimations().length > 0) {
this.dialogTarget.addEventListener('transitionend', () => {
this.triggerTarget.setAttribute('aria-expanded', 'true');
})
} else {
this.triggerTarget.setAttribute('aria-expanded', 'true');
}
}
}
async close() {
this.dialogTarget.close();
if (this.hasTriggerTarget) {
if (this.dialogTarget.getAnimations().length > 0) {
this.dialogTarget.addEventListener('transitionend', () => {
this.triggerTarget.setAttribute('aria-expanded', 'false');
})
} else {
this.triggerTarget.setAttribute('aria-expanded', 'false');
}
}
}
}
{# @prop id string Unique suffix identifier for generating AlertDialog internal IDs #}
{# @block content The default block #}
{%- props id -%}
{%- set _alert_dialog_id = 'alert-dialog-' ~ id -%}
{%- set _alert_dialog_title_id = _alert_dialog_id ~ '-title' -%}
{%- set _alert_dialog_description_id = _alert_dialog_id ~ '-description' -%}
<div {{ attributes.defaults({
'data-controller': 'alert-dialog',
'aria-labelledby': _alert_dialog_title_id,
'aria-describedby': _alert_dialog_description_id,
}) }}>
{% block content %}{% endblock %}
</div>
{# @prop variant 'default'|'secondary'|'destructive'|'outline'|'ghost'|'link' The variant, default to `default` #}
{# @block content The default block #}
{%- props variant = 'default' -%}
<twig:Button variant="{{ variant }}" {{ ...attributes }}>
{{- block(outerBlocks.content) -}}
</twig:Button>
{# @block content The default block #}
<twig:Button variant="outline" data-action="click->alert-dialog#close" {{ ...attributes }}>
{{- block(outerBlocks.content) -}}
</twig:Button>
{# @block content The default block #}
<dialog
id="{{ _alert_dialog_id }}"
class="{{ 'bg-background fixed top-[50%] left-[50%] z-50 max-w-[calc(100%-2rem)] translate-x-[-50%] translate-y-[-50%] scale-95 gap-4 rounded-lg border p-6 opacity-0 shadow-lg transition-all transition-discrete duration-200 backdrop:transition-discrete backdrop:duration-150 open:grid open:scale-100 open:opacity-100 open:backdrop:bg-black/50 sm:max-w-lg starting:open:scale-95 starting:open:opacity-0 ' ~ attributes.render('class')|tailwind_merge }}"
data-alert-dialog-target="dialog"
data-action="keydown.esc->alert-dialog#close:prevent"
{{ attributes.without('id') }}
>
{%- block content %}{% endblock -%}
</dialog>
{# @block content The default block #}
<p
id="{{ _alert_dialog_description_id }}"
class="{{ 'text-muted-foreground text-sm ' ~ attributes.render('class')|tailwind_merge }}"
{{ attributes.without('id') }}
>
{%- block content %}{% endblock -%}
</p>
{# @block content The default block #}
<footer
class="{{ 'flex flex-col-reverse gap-2 sm:flex-row sm:justify-end ' ~ attributes.render('class')|tailwind_merge }}"
{{ attributes }}
>
{%- block content %}{% endblock -%}
</footer>
{# @block content The default block #}
<header
class="{{ 'flex flex-col gap-2 text-center sm:text-left ' ~ attributes.render('class')|tailwind_merge }}"
{{ attributes }}
>
{%- block content %}{% endblock -%}
</header>
{# @block content The default block #}
<h2
id="{{ _alert_dialog_title_id }}"
class="{{ 'text-lg font-semibold ' ~ attributes.render('class')|tailwind_merge }}"
{{ attributes.without('id') }}
>
{%- block content %}{% endblock -%}
</h2>
{# @block content The default block #}
{%- set trigger_attrs = {
'data-action': 'click->alert-dialog#open',
'data-alert-dialog-target': 'trigger',
'aria-haspopup': 'dialog',
'aria-expanded': 'false',
} -%}
{%- block content %}{% endblock -%}
{# @prop variant 'default'|'secondary'|'destructive'|'outline'|'ghost'|'link' The variant, default to `default` #}
{# @prop size 'default'|'sm'|'lg'|'icon'|'icon-sm'|'icon-lg' The size, default to `default` #}
{# @prop as 'button' The HTML tag to use, default to `button` #}
{# @block content The default block #}
{%- props variant = 'default', size = 'default', as = 'button' -%}
{%- set style = html_cva(
base: "inline-flex items-center justify-center gap-2 whitespace-nowrap rounded-md text-sm font-medium transition-all disabled:pointer-events-none disabled:opacity-50 [&_svg]:pointer-events-none [&_svg:not([class*='size-'])]:size-4 shrink-0 [&_svg]:shrink-0 outline-none focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[3px] aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive",
variants: {
variant: {
default: 'bg-primary text-primary-foreground hover:bg-primary/90',
secondary: 'bg-secondary text-secondary-foreground hover:bg-secondary/80',
destructive: 'bg-destructive text-white hover:bg-destructive/90 focus-visible:ring-destructive/20 dark:focus-visible:ring-destructive/40 dark:bg-destructive/60',
outline: 'border bg-background shadow-xs hover:bg-accent hover:text-accent-foreground dark:bg-input/30 dark:border-input dark:hover:bg-input/50',
ghost: 'hover:bg-accent hover:text-accent-foreground dark:hover:bg-accent/50',
link: 'text-primary underline-offset-4 hover:underline',
},
size: {
default: 'h-9 px-4 py-2 has-[>svg]:px-3',
sm: 'h-8 rounded-md gap-1.5 px-3 has-[>svg]:px-2.5',
lg: 'h-10 rounded-md px-6 has-[>svg]:px-4',
icon: 'size-9',
'icon-sm': 'size-8',
'icon-lg': 'size-10',
},
},
) -%}
<{{ as }}
data-slot="button"
class="{{ style.apply({variant: variant, size: size}, attributes.render('class'))|tailwind_merge }}"
{{ attributes }}
>
{%- block content %}{% endblock -%}
</{{ as }}>
Happy coding!
Usage
<twig:AlertDialog id="delete_account">
<twig:AlertDialog:Trigger>
<twig:Button {{ ...trigger_attrs }}>Open</twig:Button>
</twig:AlertDialog:Trigger>
<twig:AlertDialog:Content>
<twig:AlertDialog:Header>
<twig:AlertDialog:Title>Are you absolutely sure?</twig:AlertDialog:Title>
<twig:AlertDialog:Description>
This action cannot be undone. This will permanently delete your account
and remove your data from our servers.
</twig:AlertDialog:Description>
</twig:AlertDialog:Header>
<twig:AlertDialog:Footer>
<twig:AlertDialog:Cancel>Cancel</twig:AlertDialog:Cancel>
<twig:AlertDialog:Action>Continue</twig:AlertDialog:Action>
</twig:AlertDialog:Footer>
</twig:AlertDialog:Content>
</twig:AlertDialog>
Examples
Default
Loading...
<twig:AlertDialog id="delete_account">
<twig:AlertDialog:Trigger>
<twig:Button {{ ...trigger_attrs }}>Open</twig:Button>
</twig:AlertDialog:Trigger>
<twig:AlertDialog:Content>
<twig:AlertDialog:Header>
<twig:AlertDialog:Title>Are you absolutely sure?</twig:AlertDialog:Title>
<twig:AlertDialog:Description>
This action cannot be undone. This will permanently delete your account
and remove your data from our servers.
</twig:AlertDialog:Description>
</twig:AlertDialog:Header>
<twig:AlertDialog:Footer>
<twig:AlertDialog:Cancel>Cancel</twig:AlertDialog:Cancel>
<twig:AlertDialog:Action>Continue</twig:AlertDialog:Action>
</twig:AlertDialog:Footer>
</twig:AlertDialog:Content>
</twig:AlertDialog>
API Reference
AlertDialog
| Prop | Type | Description |
|---|---|---|
id |
string |
Unique suffix identifier for generating AlertDialog internal IDs |
| Block | Description |
|---|---|
content |
The default block |
AlertDialog:Action
| Prop | Type | Description |
|---|---|---|
variant |
'default'|'secondary'|'destructive'|'outline'|'ghost'|'link' |
The variant, default to default |
| Block | Description |
|---|---|
content |
The default block |
AlertDialog:Cancel
| Block | Description |
|---|---|
content |
The default block |
AlertDialog:Content
| Block | Description |
|---|---|
content |
The default block |
AlertDialog:Description
| Block | Description |
|---|---|
content |
The default block |
AlertDialog:Footer
| Block | Description |
|---|---|
content |
The default block |
AlertDialog:Header
| Block | Description |
|---|---|
content |
The default block |
AlertDialog:Title
| Block | Description |
|---|---|
content |
The default block |
AlertDialog:Trigger
| Block | Description |
|---|---|
content |
The default block |