Skip to content
العربية
Overlays

OnceValueDialog

Shows a new value one time, for example a token. It closes only with Done. 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 { OnceValueDialog } from "@percentsa/ui";

Examples

Code
export function OnceValueDialogExample() {
  const t = useT();
  const [value, setValue] = useState<string | null>(null);
  const [confirm, setConfirm] = useState(false);
  const rotate = useRef<HTMLButtonElement>(null);
  return (
    <>
      <Button ref={rotate} onClick={() => setConfirm(true)}>{t("Rotate token", "تدوير الرمز")}</Button>
      {/* The confirm dialog closes when the once dialog opens, so the focus at open is lost: returnFocusTo gives it back to the button. */}
      <AlertDialog
        open={confirm}
        onOpenChange={setConfirm}
        title={t("Rotate the token?", "تدوير الرمز؟")}
        description={t("The old token stops working.", "يتوقف الرمز القديم عن العمل.")}
        action={t("Rotate", "تدوير")}
        onAction={() => (setConfirm(false), setValue("pt_example_1234"))}
      />
      <OnceValueDialog
        returnFocusTo={rotate}
        open={value !== null}
        title={t("Server token", "رمز الخادم")}
        description={t("Copy the new token now.", "انسخ الرمز الجديد الآن.")}
        note={t("You cannot see this value again.", "لا يمكنك رؤية هذه القيمة مرة أخرى.")}
        value={value ?? ""}
        onDone={() => setValue(null)}
      />
    </>
  );
}