-
-
Notifications
You must be signed in to change notification settings - Fork 383
[Toolkit][Shadcn] Add AlertDialog recipe #3073
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Examples | ||
|
||
## Default | ||
|
||
```twig {"preview":true,"height":"500px"} | ||
<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> | ||
``` | ||
|
||
## Opened by default | ||
|
||
```twig {"preview":true,"height":"500px"} | ||
<twig:AlertDialog id="delete_account" open> | ||
<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> | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Controller } from '@hotwired/stimulus'; | ||
import { enter, leave } from 'el-transition'; | ||
|
||
export default class extends Controller { | ||
|
||
static targets = ['trigger', 'overlay', 'dialog', 'content']; | ||
|
||
async open() { | ||
this.dialogTarget.showModal(); | ||
|
||
await Promise.all([enter(this.overlayTarget), enter(this.contentTarget)]); | ||
|
||
if (this.hasTriggerTarget) { | ||
this.triggerTarget.setAttribute('aria-expanded', 'true'); | ||
} | ||
} | ||
|
||
async close() { | ||
await Promise.all([leave(this.overlayTarget), leave(this.contentTarget)]); | ||
|
||
this.dialogTarget.close(); | ||
|
||
if (this.hasTriggerTarget) { | ||
this.triggerTarget.setAttribute('aria-expanded', 'false'); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{ | ||
"$schema": "../../../schema-kit-recipe-v1.json", | ||
"type": "component", | ||
"name": "AlertDialog", | ||
"description": "A modal dialog that interrupts the user with important content and expects a response.", | ||
"copy-files": { | ||
"templates/": "templates/" | ||
}, | ||
"dependencies": [ | ||
{ | ||
"type": "php", | ||
"name": "twig/extra-bundle" | ||
}, | ||
{ | ||
"type": "php", | ||
"name": "twig/html-extra", | ||
"version": "^3.12.0" | ||
}, | ||
{ | ||
"type": "php", | ||
"name": "tales-from-a-dev/twig-tailwind-extra" | ||
}, | ||
{ | ||
"type": "npm", | ||
"name": "el-transition" | ||
}, | ||
{ | ||
"type": "importmap", | ||
"package": "el-transition" | ||
}, | ||
{ | ||
"type": "recipe", | ||
"name": "Button" | ||
} | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{%- props open = false, id -%} | ||
|
||
{%- set _alert_dialog_open = open %} | ||
{%- 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> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{% props variant = 'default' %} | ||
<twig:Button variant="{{ variant }}" {{ ...attributes }}> | ||
{{ block(outerBlocks.content) }} | ||
</twig:Button> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<twig:Button | ||
variant="outline" | ||
data-action="click->alert-dialog#close" | ||
{{ ...attributes }} | ||
> | ||
{{- block(outerBlocks.content) -}} | ||
</twig:Button> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<dialog | ||
id="{{ _alert_dialog_id }}" | ||
{{ _alert_dialog_open ? 'open' }} | ||
class="{{ 'fixed inset-0 size-auto max-h-none max-w-none overflow-y-auto bg-transparent backdrop:bg-transparent ' ~ attributes.render('class')|tailwind_merge }}" | ||
data-alert-dialog-target="dialog" | ||
data-action="keydown.esc->alert-dialog#close:prevent" | ||
{{ attributes.without('id') }} | ||
> | ||
<div | ||
data-alert-dialog-target="overlay" | ||
data-transition-enter="transition ease-out duration-100" | ||
data-transition-enter-start="transform opacity-0" | ||
data-transition-enter-end="transform opacity-100" | ||
data-transition-leave="transition ease-in duration-75" | ||
data-transition-leave-start="transform opacity-100" | ||
data-transition-leave-end="transform opacity-0" | ||
Comment on lines
+12
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This FYI it's not possible to use https://stimulus-use.github.io/stimulus-use/#/use-transition because it supports only one target, but we need to transition the overlay and content targets. |
||
class="{{ _alert_dialog_open ? '' : 'hidden' }} fixed inset-0 z-50 bg-black/50" | ||
></div> | ||
|
||
<section | ||
tabindex="0" | ||
class="flex min-h-full items-end justify-center p-4 text-center focus:outline-none sm:items-center sm:p-0" | ||
> | ||
<div | ||
data-transition-enter="transition ease-out duration-200" | ||
data-transition-enter-start="transform opacity-0 scale-95" | ||
data-transition-enter-end="transform opacity-100 scale-100" | ||
data-transition-leave="transition ease-in duration-75" | ||
data-transition-leave-start="transform opacity-100 scale-100" | ||
data-transition-leave-end="transform opacity-0 scale-95" | ||
class="{{ _alert_dialog_open ? '' : 'hidden' }} bg-background fixed top-[50%] left-[50%] z-50 grid w-full max-w-[calc(100%-2rem)] translate-x-[-50%] translate-y-[-50%] gap-4 rounded-lg border p-6 shadow-lg sm:max-w-lg" | ||
data-alert-dialog-target="content" | ||
> | ||
{%- block content %}{% endblock -%} | ||
</div> | ||
</section> | ||
</dialog> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<p | ||
id="{{ _alert_dialog_description_id }}" | ||
class="{{ 'text-muted-foreground text-sm ' ~ attributes.render('class')|tailwind_merge }}" | ||
{{ attributes.without('id') }} | ||
> | ||
{%- block content %}{% endblock -%} | ||
</p> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<footer | ||
class="{{ 'flex flex-col-reverse gap-2 sm:flex-row sm:justify-end ' ~ attributes.render('class')|tailwind_merge }}" | ||
{{ attributes }} | ||
> | ||
{%- block content %}{% endblock -%} | ||
</footer> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<header | ||
class="{{ 'flex flex-col gap-2 text-center sm:text-left ' ~ attributes.render('class')|tailwind_merge }}" | ||
{{ attributes }} | ||
> | ||
{%- block content %}{% endblock -%} | ||
</header> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<h2 | ||
id="{{ _alert_dialog_title_id }}" | ||
class="{{ 'text-lg font-semibold ' ~ attributes.render('class')|tailwind_merge }}" | ||
{{ attributes.without('id') }} | ||
> | ||
{%- block content %}{% endblock -%} | ||
</h2> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{%- set trigger_attrs = { | ||
'data-action': 'click->alert-dialog#open', | ||
'data-alert-dialog-target': 'trigger', | ||
'aria-haspopup': 'dialog', | ||
'aria-expanded': _alert_dialog_open ? 'true' : 'false', | ||
} -%} | ||
{%- block content %}{% endblock -%} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like this schema structure anymore, I will refactor it in another PR