Overlays
ActionDialog
Asks before an action with an effect: the record at the top, then the choices, then “What happens”, which is read aloud when it changes. confirmName asks for the typed name, and reason for a reason (reason.maxLength limits its length). stays lists what does not change, and confirmLabel replaces the typed-name line. cancelLabel replaces the word on the cancel button. The first focus goes to the first choice, else to Cancel. returnFocusTo names the element that gets the focus when the dialog closes, for example a section heading after a link in the dialog moved the person there. Without it, the focus goes back to the element that had the focus when the dialog opened. The element must take the focus (a heading needs tabIndex={-1}).
import { ActionDialog } from "@percentsa/ui";Examples
Code
export function ActionDialogExample() {
const t = useT();
const [open, setOpen] = useState(false);
return (
<>
<Button variant="secondary" onClick={() => setOpen(true)}>{t("Redeploy", "إعادة النشر")}</Button>
<ActionDialog
open={open}
onOpenChange={setOpen}
title={t("Redeploy Caddy edge?", "إعادة نشر Caddy edge؟")}
subject={{ title: "Caddy edge", meta: "203.0.113.10", facts: [{ term: t("Status", "الحالة"), value: t("Live", "يعمل") }] }}
effects={[t("The service restarts and is down for about 10 seconds.", "يُعاد تشغيل الخدمة وتتوقف نحو 10 ثوانٍ."), t("Open connections end.", "تنتهي الاتصالات المفتوحة.")]}
action={t("Redeploy", "إعادة النشر")}
onAction={() => setOpen(false)}
/>
</>
);
}Code
export function ActionDialogChoiceExample() {
const t = useT();
const [open, setOpen] = useState(false);
const [kind, setKind] = useState<string>();
return (
<>
<Button variant="secondary" onClick={() => setOpen(true)}>{t("Refund", "استرداد")}</Button>
<ActionDialog
open={open}
onOpenChange={setOpen}
title={t("Refund this payment?", "استرداد هذه الدفعة؟")}
subject={{ title: "Al Noor", value: <Money amount={40} />, meta: t("Paid by card", "مدفوعة بالبطاقة") }}
effects={
kind === "twice"
? [t("The second payment goes back to the card.", "تعود الدفعة الثانية إلى البطاقة.")]
: kind === "request"
? [t("The full amount goes back to the card.", "يعود المبلغ كاملًا إلى البطاقة.")]
: null
}
effectsEmpty={t("Choose a reason first.", "اختر السبب أولًا.")}
reason={{ label: t("Note for the record", "ملاحظة للسجل"), required: true, maxLength: 200 }}
action={t("Refund", "استرداد")}
destructive
onAction={() => setOpen(false)}
>
<RadioGroup
aria-label={t("Refund reason", "سبب الاسترداد")}
value={kind}
onValueChange={setKind}
options={[
{ value: "twice", label: t("Paid twice", "دُفعت مرتين") },
{ value: "request", label: t("Customer asked", "طلب العميل") },
]}
/>
</ActionDialog>
</>
);
}Code
export function ActionDialogDeleteExample() {
const t = useT();
const [open, setOpen] = useState(false);
return (
<>
<Button variant="danger" onClick={() => setOpen(true)}>{t("Delete site", "حذف الموقع")}</Button>
<ActionDialog
open={open}
onOpenChange={setOpen}
title={t("Delete Main website?", "حذف Main website؟")}
subject={{ title: "Main website", meta: "shop.example.com" }}
effects={[t("The site and its files are deleted.", "يُحذف الموقع وملفاته."), t("This cannot be undone.", "لا يمكن التراجع.")]}
stays={[t("The backups stay for 30 days.", "تبقى النسخ الاحتياطية 30 يومًا.")]}
confirmName="Main website"
confirmLabel={t("Type the site name to delete it.", "اكتب اسم الموقع لحذفه.")}
action={t("Delete", "حذف")}
cancelLabel={t("Keep site", "إبقاء الموقع")}
destructive
onAction={() => setOpen(false)}
/>
</>
);
}Payments
Code
export function ActionDialogFocusExample() {
const t = useT();
const [open, setOpen] = useState(false);
const heading = useRef<HTMLHeadingElement>(null);
const back = useRef<HTMLElement | null>(null);
return (
<Stack gap={4}>
<Button variant="secondary" onClick={() => { back.current = null; setOpen(true); }}>{t("Refund", "استرداد")}</Button>
<ActionDialog
open={open}
onOpenChange={setOpen}
title={t("Refund this payment?", "استرداد هذه الدفعة؟")}
subject={{ title: "Al Noor", meta: "INV-1042" }}
notice={<a href="#payments-heading" className="underline" onClick={(e) => { e.preventDefault(); back.current = heading.current; setOpen(false); }}>{t("Go to Payments", "الانتقال إلى المدفوعات")}</a>}
effects={[t("The payment goes back to the card.", "يعود المبلغ إلى البطاقة.")]}
action={t("Refund", "استرداد")}
onAction={() => setOpen(false)}
returnFocusTo={back}
/>
<h3 id="payments-heading" ref={heading} tabIndex={-1} className="heading text-lg">{t("Payments", "المدفوعات")}</h3>
</Stack>
);
}