Buttons
Inline Confirm
A considered archive action that opens its confirmation in place, with cancel, pending, error, and completion states.
#confirmation#button#disclosure#interaction
Original by Plinth · MIT
Report a problem or suggest an improvementInstall
npx shadcn@latest add https://plinthui.com/r/inline-confirm.json"use client";
import { useEffect, useRef, useState } from "react";
const css = String.raw`
.ic-root {
--ic-ink: #28312b;
--ic-muted: #6d746b;
--ic-surface: #f5f5ef;
--ic-line: #dce0d5;
--ic-accent: #a63e21;
width: min(310px, 100%);
color: var(--ic-ink);
font:
14px/1.45 system-ui,
sans-serif;
}
.ic-root * {
box-sizing: border-box;
}
.ic-box {
padding: 18px;
border: 1px solid var(--ic-line);
border-radius: 22px;
background:
linear-gradient(145deg, #ffffff12, transparent), var(--ic-surface);
box-shadow:
inset 0 1px 0 #ffffff32,
0 12px 28px #111b1412;
}
.ic-caption {
display: flex;
justify-content: space-between;
margin: 0 0 17px;
color: var(--ic-muted);
font:
10px/1.4 ui-monospace,
monospace;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.ic-dot {
width: 6px;
height: 6px;
margin: 3px 0;
border-radius: 50%;
background: #93a378;
}
.ic-heading {
display: flex;
align-items: center;
gap: 12px;
}
.ic-icon {
display: grid;
place-items: center;
flex: none;
width: 42px;
height: 42px;
border-radius: 13px;
border: 1px solid var(--ic-line);
background: linear-gradient(145deg, #ffffff24, #7c89600d);
}
.ic-icon svg {
width: 21px;
height: 21px;
}
.ic-title {
margin: 0;
font-size: 15px;
font-weight: 600;
letter-spacing: -0.025em;
}
.ic-subtitle {
margin: 3px 0 0;
font-size: 11px;
color: var(--ic-muted);
}
.ic-actions {
display: flex;
gap: 7px;
margin-top: 18px;
}
.ic-root button {
min-height: 38px;
cursor: pointer;
border: 1px solid var(--ic-line);
border-radius: 11px;
padding: 8px 13px;
background: transparent;
color: var(--ic-ink);
font:
500 12px/1.4 system-ui,
sans-serif;
transition:
background 160ms,
transform 160ms;
}
.ic-primary {
flex: 1;
text-align: left !important;
display: flex;
align-items: center;
justify-content: space-between;
}
.ic-primary span {
font-size: 16px;
}
.ic-root button:hover {
background: #81956818;
}
.ic-root button:active {
transform: translateY(1px);
}
.ic-root .ic-commit {
flex: 1;
color: #fff8ed;
background: #a63e21;
border-color: #a63e21;
}
.ic-root .ic-commit:hover {
background: #89331c;
}
.ic-root button:disabled {
cursor: wait;
opacity: 0.65;
}
.ic-root button:focus-visible {
outline: 2px solid var(--ic-accent);
outline-offset: 3px;
}
.ic-state {
animation: ic-enter 220ms cubic-bezier(0.2, 0.8, 0.2, 1);
}
.ic-state[hidden] {
display: none;
}
.ic-error {
margin: 12px 0 0;
font-size: 11px;
color: var(--ic-accent);
}
.ic-error:empty {
display: none;
}
.ic-sr {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
}
.ic-done .ic-icon {
color: #597a36;
background: #b3cf8222;
}
.ic-footnote {
margin: 10px 0 0;
text-align: center;
color: var(--ic-muted);
font-size: 10px;
}
:root[data-theme="dark"] .ic-root {
--ic-ink: #e9eddf;
--ic-muted: #a1aa98;
--ic-surface: #262c24;
--ic-line: #3d4637;
--ic-accent: #ffa283;
}
@media (prefers-color-scheme: dark) {
:root:not([data-theme="light"]) .ic-root {
--ic-ink: #e9eddf;
--ic-muted: #a1aa98;
--ic-surface: #262c24;
--ic-line: #3d4637;
--ic-accent: #ffa283;
}
}
@keyframes ic-enter {
from {
opacity: 0.35;
transform: translateY(5px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
@media (prefers-reduced-motion: reduce) {
.ic-state {
animation: none;
}
.ic-root button {
transition: none;
}
}
`;
/** Supply onConfirm to perform the action. Rejections keep the confirmation open. */
export function InlineConfirm({
onConfirm,
}: {
onConfirm?: () => void | Promise<void>;
}) {
const [state, setState] = useState<"idle" | "confirm" | "done">("idle");
const [busy, setBusy] = useState(false);
const [error, setError] = useState("");
const trigger = useRef<HTMLButtonElement>(null);
const cancel = useRef<HTMLButtonElement>(null);
const reset = useRef<HTMLButtonElement>(null);
const mounted = useRef(false);
const pending = useRef(false);
useEffect(() => {
mounted.current = true;
return () => {
mounted.current = false;
};
}, []);
const moveTo = (next: "idle" | "confirm" | "done") => {
setError("");
setState(next);
};
const previous = useRef(state);
useEffect(() => {
if (previous.current === state) return;
previous.current = state;
(state === "confirm"
? cancel
: state === "done"
? reset
: trigger
).current?.focus();
}, [state]);
const confirm = async () => {
if (pending.current) return;
pending.current = true;
setBusy(true);
setError("");
try {
await onConfirm?.();
if (mounted.current) setState("done");
} catch {
if (mounted.current) setError("Could not archive. Try again.");
} finally {
pending.current = false;
if (mounted.current) setBusy(false);
}
};
return (
<>
<style>{css}</style>
<div
className="ic-root"
onKeyDown={(event) => {
if (event.key === "Escape" && state === "confirm" && !busy) {
event.preventDefault();
moveTo("idle");
}
}}
>
<div className="ic-box">
<p className="ic-caption">
<span>Draft management</span>
<span className="ic-dot" aria-hidden="true" />
</p>
<div className="ic-state" hidden={state !== "idle"}>
<div className="ic-heading">
<span className="ic-icon" aria-hidden="true">
<svg
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="1.5"
strokeLinejoin="round"
>
<path d="M5 8v12h14V8M3 4h18v4H3zM9 12h6" />
</svg>
</span>
<div>
<h3 className="ic-title">Clear a little space.</h3>
<p className="ic-subtitle">Keep the work. Lose the clutter.</p>
</div>
</div>
<div className="ic-actions">
<button
className="ic-primary"
type="button"
ref={trigger}
onClick={() => moveTo("confirm")}
>
Archive draft <span aria-hidden="true">↗</span>
</button>
</div>
</div>
<div className="ic-state" hidden={state !== "confirm"}>
<h3 className="ic-title">Archive this draft?</h3>
<p className="ic-subtitle">
You can find it in your archive later.
</p>
<div className="ic-actions" aria-busy={busy}>
<button
type="button"
ref={cancel}
disabled={busy}
onClick={() => moveTo("idle")}
>
Cancel
</button>
<button
className="ic-commit"
type="button"
disabled={busy}
onClick={confirm}
>
{busy ? "Archiving…" : "Archive"}
</button>
</div>
<p className="ic-error" role="alert">
{error}
</p>
</div>
<div className="ic-state ic-done" hidden={state !== "done"}>
<div className="ic-heading">
<span className="ic-icon" aria-hidden="true">
<svg
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="1.8"
strokeLinecap="round"
strokeLinejoin="round"
>
<path d="m5 12 4 4L19 6" />
</svg>
</span>
<div>
<h3 className="ic-title">Draft archived.</h3>
<p className="ic-subtitle">
A little less noise. All still there.
</p>
</div>
</div>
<div className="ic-actions">
<button
className="ic-primary"
type="button"
ref={reset}
onClick={() => moveTo("idle")}
>
Done <span aria-hidden="true">✓</span>
</button>
</div>
</div>
</div>
<p className="ic-sr" role="status">
{busy ? "Archiving draft" : state === "done" ? "Draft archived" : ""}
</p>
</div>
</>
);
}