Modal dialog
A confirm dialog built on the native <dialog> element that grows out of the button you pressed and asks you to type the project name before deleting.
"use client";
import {
useRef,
useState,
type CSSProperties,
type MouseEvent as ReactMouseEvent,
} from "react";
import "./confirm-dialog.css";
const PROJECT = "summer-campaign";
/**
* ConfirmDialog
* - Native <dialog> + showModal(): focus trap, Escape to close, inert background and top layer for free.
* - The dialog's transform-origin is set to the trigger's position so it grows out of the button.
* - Clicking the backdrop closes it (the click target is the <dialog> itself, not its content).
*/
export function ConfirmDialog({
theme = "light",
}: {
theme?: "light" | "dark";
}) {
const dialog = useRef<HTMLDialogElement>(null);
const [origin, setOrigin] = useState("50% 50%");
const [typed, setTyped] = useState("");
const [deleted, setDeleted] = useState(false);
const open = (e: ReactMouseEvent<HTMLButtonElement>) => {
const r = e.currentTarget.getBoundingClientRect();
// Origin relative to the viewport center, where the dialog is placed
setOrigin(
`${r.left + r.width / 2 - innerWidth / 2 + 220}px ${r.top + r.height / 2 - innerHeight / 2 + 150}px`,
);
setTyped("");
dialog.current?.showModal();
};
const close = () => dialog.current?.close();
return (
<div className="gu-dlg" data-theme={theme}>
{deleted ? (
<p className="gu-dlg__gone" role="status">
“{PROJECT}” was deleted.{" "}
<button type="button" onClick={() => setDeleted(false)}>
Undo
</button>
</p>
) : (
<div className="gu-dlg__row">
<span className="gu-dlg__file" aria-hidden="true">
▦
</span>
<span>
<b>{PROJECT}</b>
<small>48 files · edited 2 days ago</small>
</span>
<button type="button" className="gu-dlg__danger" onClick={open}>
Delete project
</button>
</div>
)}
<dialog
ref={dialog}
className="gu-dlg__box"
data-theme={theme}
style={{ "--origin": origin } as CSSProperties}
aria-labelledby="gu-dlg-title"
aria-describedby="gu-dlg-desc"
onClick={(e) => e.target === e.currentTarget && close()}
>
<form
method="dialog"
onSubmit={(e) => {
if (typed !== PROJECT) return e.preventDefault();
setDeleted(true);
}}
>
<span className="gu-dlg__icon" aria-hidden="true">
!
</span>
<h2 id="gu-dlg-title">Delete “{PROJECT}”?</h2>
<p id="gu-dlg-desc">
This removes all 48 files and their history for everyone on the
team. You can't undo this after 30 days.
</p>
<label htmlFor="gu-dlg-confirm">
Type <code>{PROJECT}</code> to confirm
</label>
<input
id="gu-dlg-confirm"
value={typed}
onChange={(e) => setTyped(e.target.value)}
autoComplete="off"
spellCheck={false}
/>
<div className="gu-dlg__actions">
<button
type="button"
className="gu-dlg__cancel"
onClick={close}
autoFocus
>
Cancel
</button>
<button
type="submit"
className="gu-dlg__confirm"
disabled={typed !== PROJECT}
>
Delete forever
</button>
</div>
</form>
</dialog>
</div>
);
}
Paste this into ChatGPT, Claude, Cursor, v0, Lovable or Bolt. It describes the layout, motion, colors and accessibility and includes the code, so the AI can rebuild it in any framework.
# Build this UI piece: Modal dialog
You are a senior front-end engineer. Recreate the piece below in my project. If I don't say which framework I use, ask once, then build it in that framework (React, Vue, Svelte, Angular, Astro, plain HTML/CSS/JS or anything else). Keep the behavior, motion, colors, spacing and accessibility exactly as described. Keep the code clean and commented for beginners.
## What it is
A destructive-action confirm dialog using the native <dialog> element. It grows out of the "Delete project" button, blurs the page behind it, and only enables "Delete forever" once you type the project name.
## Layout and look
- Trigger row: file tile (orange), project name and meta, and an outlined red "Delete project" pill that fills red on hover.
- Dialog: 440px, radius 26px, card background, deep shadow. Red-tinted 48px "!" tile, title "Delete “summer-campaign”?" (23px, weight 900), muted description, a label with the name in a code chip, a 50px monospace input, then Cancel (outline) and Delete forever (red) pill buttons.
- Backdrop: 55% Ink with a 4px blur.
- Phones (≤520px): the dialog docks to the bottom as a sheet with stacked full-width buttons.
## Behavior and motion
1. showModal() gives focus trapping, Escape, top layer and inert background. Cancel gets autofocus.
2. transform-origin is set from the trigger's position, so the dialog scales from 0.2 out of the button (500ms cubic-bezier(0.16, 1, 0.3, 1)).
3. Backdrop clicks close (target === dialog). The form uses method="dialog"; submit is blocked until the typed text matches.
4. After deleting, a status line with an Undo link replaces the row.
## Accessibility
- aria-labelledby and aria-describedby on the dialog, a real label on the input, disabled confirm until valid.
- prefers-reduced-motion: plain fade.
## Props
theme. Change PROJECT.
## Reference implementation
The original is written in React with one plain CSS file. Dependencies: none (plain React and CSS). Translate it faithfully; do not add new libraries unless my framework needs them.
### ConfirmDialog.tsx
```tsx
"use client";
import {
useRef,
useState,
type CSSProperties,
type MouseEvent as ReactMouseEvent,
} from "react";
import "./confirm-dialog.css";
const PROJECT = "summer-campaign";
/**
* ConfirmDialog
* - Native <dialog> + showModal(): focus trap, Escape to close, inert background and top layer for free.
* - The dialog's transform-origin is set to the trigger's position so it grows out of the button.
* - Clicking the backdrop closes it (the click target is the <dialog> itself, not its content).
*/
export function ConfirmDialog({
theme = "light",
}: {
theme?: "light" | "dark";
}) {
const dialog = useRef<HTMLDialogElement>(null);
const [origin, setOrigin] = useState("50% 50%");
const [typed, setTyped] = useState("");
const [deleted, setDeleted] = useState(false);
const open = (e: ReactMouseEvent<HTMLButtonElement>) => {
const r = e.currentTarget.getBoundingClientRect();
// Origin relative to the viewport center, where the dialog is placed
setOrigin(
`${r.left + r.width / 2 - innerWidth / 2 + 220}px ${r.top + r.height / 2 - innerHeight / 2 + 150}px`,
);
setTyped("");
dialog.current?.showModal();
};
const close = () => dialog.current?.close();
return (
<div className="gu-dlg" data-theme={theme}>
{deleted ? (
<p className="gu-dlg__gone" role="status">
“{PROJECT}” was deleted.{" "}
<button type="button" onClick={() => setDeleted(false)}>
Undo
</button>
</p>
) : (
<div className="gu-dlg__row">
<span className="gu-dlg__file" aria-hidden="true">
▦
</span>
<span>
<b>{PROJECT}</b>
<small>48 files · edited 2 days ago</small>
</span>
<button type="button" className="gu-dlg__danger" onClick={open}>
Delete project
</button>
</div>
)}
<dialog
ref={dialog}
className="gu-dlg__box"
data-theme={theme}
style={{ "--origin": origin } as CSSProperties}
aria-labelledby="gu-dlg-title"
aria-describedby="gu-dlg-desc"
onClick={(e) => e.target === e.currentTarget && close()}
>
<form
method="dialog"
onSubmit={(e) => {
if (typed !== PROJECT) return e.preventDefault();
setDeleted(true);
}}
>
<span className="gu-dlg__icon" aria-hidden="true">
!
</span>
<h2 id="gu-dlg-title">Delete “{PROJECT}”?</h2>
<p id="gu-dlg-desc">
This removes all 48 files and their history for everyone on the
team. You can't undo this after 30 days.
</p>
<label htmlFor="gu-dlg-confirm">
Type <code>{PROJECT}</code> to confirm
</label>
<input
id="gu-dlg-confirm"
value={typed}
onChange={(e) => setTyped(e.target.value)}
autoComplete="off"
spellCheck={false}
/>
<div className="gu-dlg__actions">
<button
type="button"
className="gu-dlg__cancel"
onClick={close}
autoFocus
>
Cancel
</button>
<button
type="submit"
className="gu-dlg__confirm"
disabled={typed !== PROJECT}
>
Delete forever
</button>
</div>
</form>
</dialog>
</div>
);
}
```
### confirm-dialog.css
```css
/* ConfirmDialog: change these colors to match your brand. */
.gu-dlg,
.gu-dlg__box {
--card: #fbf8f2;
--field: #f3eee3;
--text: #0e0e10;
--muted: #57534b;
--line: rgb(14 14 16 / 0.14);
--danger: #dc2626;
--danger-text: #ffffff;
--focus: #2b35ff;
--ease: cubic-bezier(0.16, 1, 0.3, 1);
font-family: system-ui, sans-serif;
color: var(--text);
}
.gu-dlg[data-theme="dark"],
.gu-dlg__box[data-theme="dark"] {
--card: #18181b;
--field: #0e0e10;
--text: #f3eee3;
--muted: #aea89c;
--line: rgb(243 238 227 / 0.14);
--danger: #f87171;
--danger-text: #0e0e10;
--focus: #d7ff3a;
}
.gu-dlg {
width: min(100%, 460px);
}
.gu-dlg__row {
display: grid;
grid-template-columns: 44px 1fr auto;
align-items: center;
gap: 14px;
padding: 14px;
border: 1px solid var(--line);
border-radius: 20px;
background: var(--card);
}
.gu-dlg__row > span:nth-child(2) {
display: grid;
min-width: 0;
}
.gu-dlg__row small {
color: var(--muted);
}
.gu-dlg__file {
display: grid;
place-items: center;
height: 44px;
border-radius: 12px;
background: #ff5a1f;
color: #0e0e10;
font-size: 1.3rem;
}
.gu-dlg button {
font: inherit;
cursor: pointer;
}
.gu-dlg__danger {
min-height: 44px;
padding: 0 16px;
border: 1.5px solid color-mix(in srgb, var(--danger) 50%, transparent);
border-radius: 999px;
background: none;
color: var(--danger);
font-weight: 800 !important;
}
.gu-dlg__danger:hover {
background: var(--danger);
color: var(--danger-text);
}
.gu-dlg :focus-visible,
.gu-dlg__box :focus-visible {
outline: 3px solid var(--focus);
outline-offset: 2px;
}
.gu-dlg__gone {
margin: 0;
padding: 16px;
border-radius: 16px;
background: var(--card);
text-align: center;
font-weight: 600;
}
.gu-dlg__gone button {
border: 0;
background: none;
color: inherit;
font-weight: 800;
text-decoration: underline;
}
/* The dialog */
.gu-dlg__box {
width: min(440px, calc(100vw - 32px));
max-width: none;
padding: 0;
border: 1px solid var(--line);
border-radius: 26px;
background: var(--card);
box-shadow: 0 40px 80px -30px rgb(0 0 0 / 0.5);
transform-origin: var(--origin);
}
.gu-dlg__box[open] {
animation: gu-dlg-grow 0.5s var(--ease);
}
.gu-dlg__box::backdrop {
background: rgb(14 14 16 / 0.55);
backdrop-filter: blur(4px);
animation: gu-dlg-fade 0.3s;
}
@keyframes gu-dlg-grow {
from {
opacity: 0;
transform: scale(0.2);
}
}
@keyframes gu-dlg-fade {
from {
opacity: 0;
}
}
.gu-dlg__box form {
display: grid;
gap: 10px;
padding: 26px;
}
.gu-dlg__icon {
display: grid;
place-items: center;
width: 48px;
height: 48px;
margin-bottom: 4px;
border-radius: 16px;
background: color-mix(in srgb, var(--danger) 15%, transparent);
color: var(--danger);
font-size: 1.5rem;
font-weight: 900;
}
.gu-dlg__box h2 {
margin: 0;
font-size: 1.45rem;
font-weight: 900;
letter-spacing: -0.03em;
overflow-wrap: anywhere;
}
.gu-dlg__box p {
margin: 0 0 8px;
color: var(--muted);
line-height: 1.5;
}
.gu-dlg__box label {
font-size: 0.88rem;
font-weight: 600;
}
.gu-dlg__box code {
padding: 1px 6px;
border-radius: 6px;
background: var(--field);
font-weight: 800;
}
.gu-dlg__box input {
height: 50px;
padding: 0 14px;
border: 1.5px solid var(--line);
border-radius: 12px;
outline: none;
background: var(--field);
color: inherit;
font:
600 1rem ui-monospace,
monospace;
}
.gu-dlg__box input:focus {
border-color: var(--focus);
}
.gu-dlg__actions {
display: flex;
justify-content: flex-end;
gap: 8px;
margin-top: 10px;
}
.gu-dlg__actions button {
min-height: 48px;
padding: 0 20px;
border-radius: 999px;
font: inherit;
font-weight: 800;
cursor: pointer;
}
.gu-dlg__cancel {
border: 1.5px solid var(--line);
background: none;
color: inherit;
}
.gu-dlg__confirm {
border: 0;
background: var(--danger);
color: var(--danger-text);
transition:
opacity 0.2s,
transform 0.2s;
}
.gu-dlg__confirm:disabled {
opacity: 0.35;
cursor: not-allowed;
}
.gu-dlg__confirm:not(:disabled):active {
transform: scale(0.97);
}
/* Phones: bottom sheet style */
@media (max-width: 520px) {
.gu-dlg__box {
width: 100vw;
margin: auto 0 0;
border-radius: 26px 26px 0 0;
}
.gu-dlg__box[open] {
animation-name: gu-dlg-up;
}
@keyframes gu-dlg-up {
from {
transform: translateY(100%);
}
}
.gu-dlg__actions {
flex-direction: column-reverse;
}
.gu-dlg__row {
grid-template-columns: 44px 1fr;
}
.gu-dlg__danger {
grid-column: 1 / -1;
}
}
@media (prefers-reduced-motion: reduce) {
.gu-dlg__box[open] {
animation: gu-dlg-fade 0.15s;
}
.gu-dlg__box::backdrop {
animation: none;
}
}
```
You might also want
Next piece: Motion designer portfolioCopy field
Components, beginner
Newsletter signup
Components, beginner
Cart drawer
Components, advanced