RendyUI English

组件 / 审批请求

审批请求

AI 智能体 · React / TypeScript Free

录制演示

审批请求 实现预览

源码

approval-request.tsx
/**
 * Approval request - original UI Stacks free component, MIT licensed.
 * Source: public page https://uistacks.dev/components/approval-request (adapted to support localized interface labels).
 * Upstream project: https://github.com/nazmijavier/uistacks
 */
"use client";

import { useEffect, useRef, useState, useSyncExternalStore, type ReactNode } from "react";
import { AnimatePresence, motion, useReducedMotion } from "motion/react";
import { CircleAlert, Landmark, ShieldCheck, Timer, TriangleAlert } from "lucide-react";

export type ApprovalStatus = "pending" | "approved" | "always" | "denied";

export interface ApprovalRequestProps {
  /** The agent and the app, above the title. */
  overline?: string;
  title: string;
  description?: string;
  /** A short red line about what can't be undone (high risk). */
  warning?: string;
  risk?: "low" | "high";
  /** Shown with a timer under the details while the request is pending, e.g. "Expires in 9 min". */
  expiry?: string;
  /** `mono` sets the value in the mono face, for a tool name such as `payments.create`. */
  details?: { label: string; value: string; mono?: boolean }[];
  icon?: ReactNode;
  /** Show the "Always allow" button. */
  allowAlways?: boolean;
  status?: ApprovalStatus;
  defaultStatus?: ApprovalStatus;
  onApprove?: (scope: "once" | "always") => void;
  onDeny?: () => void;
  locale?: "en" | "zh-CN";
  className?: string;
}

const spring = { type: "spring", stiffness: 420, damping: 34 } as const;

/** A key that stays unique when two entries share a name. */
const keyAt = (ids: string[], i: number) => `${ids[i]}\u0000${ids.slice(0, i).filter((x) => x === ids[i]).length}`;

/** Reduced motion, read only after hydration so the server and the first client render match. */
const noSubscribe = () => () => {};
function useReduce() {
  const prefers = useReducedMotion();
  const hydrated = useSyncExternalStore(noSubscribe, () => true, () => false);
  return hydrated && Boolean(prefers);
}

/** The decision mark: a check (allowed) or a cross (denied) that draws itself in. */
function DrawnMark({ kind, reduce }: { kind: "check" | "cross"; reduce: boolean }) {
  const draw = { initial: { pathLength: reduce ? 1 : 0 }, animate: { pathLength: 1 }, transition: { duration: reduce ? 0 : 0.34, ease: "easeOut" as const, delay: reduce ? 0 : 0.12 } };
  return (
    <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={2.4} strokeLinecap="round" strokeLinejoin="round" aria-hidden>
      {kind === "check" ? (
        <motion.path d="M5 12.5l4.5 4.5L19 7.5" {...draw} />
      ) : (
        <>
          <motion.path d="M6.5 6.5l11 11" {...draw} />
          <motion.path d="M17.5 6.5l-11 11" {...draw} />
        </>
      )}
    </svg>
  );
}
// Hover and press tint a ::before layer under the label (the kit button language); focus gets the select-blue ring.
const btn =
  "relative isolate h-9 shrink-0 whitespace-nowrap rounded-full px-3 text-[14px] @[400px]:px-3.5 font-medium outline-none transition active:scale-[0.97] before:pointer-events-none before:absolute before:inset-0 before:-z-10 before:rounded-full before:opacity-0 before:transition-opacity focus-visible:ring-2 focus-visible:ring-[var(--select)] focus-visible:ring-offset-2 focus-visible:ring-offset-[var(--surface)]";
const secondary = `${btn} bg-surface text-fg shadow-[var(--shadow-btn-secondary)] before:bg-fg hover:before:opacity-[0.04] active:before:opacity-[0.08]`;
const primary = `${btn} text-[var(--inverse-fg)] shadow-[var(--shadow-btn-primary)] before:bg-[var(--inverse-fg)] hover:before:opacity-[0.08] active:before:opacity-[0.14]`;

export function ApprovalRequest({
  overline,
  title,
  description,
  warning,
  risk = "low",
  expiry,
  details,
  icon,
  allowAlways = true,
  status: statusProp,
  defaultStatus = "pending",
  onApprove,
  onDeny,
  locale = "en",
  className = "",
}: ApprovalRequestProps) {
  const [inner, setInner] = useState<ApprovalStatus>(defaultStatus);
  const status = statusProp ?? inner;
  const high = risk === "high";
  const zh = locale === "zh-CN";
  const reduce = useReduce();
  const result = {
    pending: "",
    approved: zh ? "已允许本次操作" : "Allowed once",
    always: zh ? "已始终允许此工具" : "Always allowed for this tool",
    denied: zh ? "已拒绝" : "Denied",
  }[status];
  // The pressed button unmounts with the pending row: focus moves to the result line (only after a click here).
  const decided = useRef(false);
  const resultRef = useRef<HTMLDivElement>(null);
  useEffect(() => {
    if (!decided.current || status === "pending") return;
    decided.current = false;
    resultRef.current?.focus();
  }, [status]);

  const approve = (scope: "once" | "always") => {
    decided.current = true;
    setInner(scope === "once" ? "approved" : "always");
    onApprove?.(scope);
  };

  const riskPill = (place: string) => (
    <span
      className={`h-7 shrink-0 items-center gap-1.5 rounded-full px-2.5 text-sm ${high ? "bg-danger-soft text-danger" : "bg-muted text-fg-muted"} ${place}`}
    >
      {high ? <TriangleAlert size={16} strokeWidth={1.5} /> : <ShieldCheck size={16} strokeWidth={1.5} />}
      {high ? (zh ? "高风险" : "High risk") : (zh ? "低风险" : "Low risk")}
    </span>
  );

  return (
    <motion.div
      data-approval-risk={risk}
      layout
      initial={reduce ? { opacity: 0 } : { opacity: 0, y: 10, scale: 0.98 }}
      animate={{ opacity: 1, y: 0, scale: 1 }}
      transition={spring}
      className={`@container w-full max-w-[460px] rounded-[20px] bg-surface p-5 shadow-[var(--shadow-card)] ${className}`}
    >
      <div className="flex items-start gap-3">
        <div className={`grid size-10 shrink-0 place-items-center rounded-[14px] ${high ? "bg-danger-soft text-danger" : "bg-surface shadow-[inset_0_0_0_1px_color-mix(in_srgb,var(--fg)_10%,transparent)] text-fg"}`}>
          {icon ?? <Landmark size={20} strokeWidth={1.5} />}
        </div>
        <div className="min-w-0 flex-1 pt-0.5">
          {overline && <p className="text-[13px] font-medium text-fg-muted">{overline}</p>}
          <p className="mt-0.5 text-[17px] font-medium leading-6 text-fg">{title}</p>
          {/* A narrow card moves the risk pill under the title, so the overline and the title keep their words. */}
          {riskPill("mt-2 inline-flex @[400px]:hidden")}
        </div>
        {riskPill("mt-2 hidden @[400px]:inline-flex")}
      </div>

      {description && <p className="mt-3 text-base leading-[26px] text-fg-muted">{description}</p>}

      {warning && (
        <div className="mt-3 flex items-center gap-2.5 rounded-[14px] bg-danger-soft px-4 py-3 text-[15px] text-danger">
          <CircleAlert size={16} strokeWidth={1.5} className="shrink-0" />
          {warning}
        </div>
      )}

      {details && details.length > 0 && (
        // A narrow card tightens the label column; values wrap rather than cut a payee or an amount.
        <dl className="mt-3 grid grid-cols-[auto_1fr] gap-x-4 gap-y-2.5 rounded-[18px] bg-subtle p-4 text-[15px] @[400px]:grid-cols-[minmax(96px,auto)_1fr] @[400px]:gap-x-6">
          {/* Rows settle in one after another as the request arrives; a changed value rolls in place. */}
          {details.map((d, i) => {
            const enter = { initial: reduce ? { opacity: 0 } : { opacity: 0, y: 6 }, animate: { opacity: 1, y: 0 }, transition: { ...spring, delay: reduce ? 0 : 0.08 + i * 0.06 } };
            return (
              <div key={keyAt(details.map((x) => x.label), i)} className="contents">
                <motion.dt {...enter} className="text-fg-muted">
                  {d.label}
                </motion.dt>
                <motion.dd {...enter} className={`relative min-w-0 break-words text-fg ${d.mono ? "font-mono text-[14px]" : ""}`}>
                  <AnimatePresence mode="popLayout" initial={false}>
                    <motion.span key={d.value} className="block" initial={reduce ? { opacity: 0 } : { opacity: 0, y: 8 }} animate={{ opacity: 1, y: 0 }} exit={reduce ? { opacity: 0 } : { opacity: 0, y: -8 }} transition={spring}>
                      {d.value}
                    </motion.span>
                  </AnimatePresence>
                </motion.dd>
              </div>
            );
          })}
        </dl>
      )}

      {/* The expiry is a meta line of its own, so the three buttons always share one row. */}
      {/* A ticking expiry ("Expires in 8:41") rolls its text each time it changes. */}
      {expiry && status === "pending" && (
        <p className="mt-3 flex items-center gap-1 text-[13px] text-fg-muted">
          <Timer size={16} strokeWidth={1.5} className="shrink-0" />
          <span className="relative h-5 overflow-hidden leading-5">
            <AnimatePresence mode="popLayout" initial={false}>
              <motion.span key={expiry} className="block tabular-nums" initial={reduce ? { opacity: 0 } : { opacity: 0, y: 10 }} animate={{ opacity: 1, y: 0 }} exit={reduce ? { opacity: 0 } : { opacity: 0, y: -10 }} transition={spring}>
                {expiry}
              </motion.span>
            </AnimatePresence>
          </span>
        </p>
      )}

      <div className="mt-4 flex min-h-9 items-center gap-3">
        <AnimatePresence mode="popLayout" initial={false}>
          {status === "pending" ? (
            // Deny on the left, the approvals on the right.
            <motion.div key="pending" className="flex w-full items-center gap-1.5" initial={reduce ? { opacity: 0 } : { opacity: 0, y: 6 }} animate={{ opacity: 1, y: 0 }} exit={reduce ? { opacity: 0 } : { opacity: 0, y: -6 }} transition={spring}>
              <button
                type="button"
                data-approval-action="deny"
                className={`${secondary} mr-auto`}
                onClick={() => {
                  decided.current = true;
                  setInner("denied");
                  onDeny?.();
                }}
              >
                {zh ? "拒绝" : "Deny"}
              </button>
              {allowAlways && !high && (
                <button type="button" data-approval-action="always" className={secondary} onClick={() => approve("always")}>
                  {zh ? "始终允许" : "Always allow"}
                </button>
              )}
              <button
                type="button"
                data-approval-action="once"
                onClick={() => approve("once")}
                className={primary}
                style={{ backgroundImage: "var(--grad-primary)" }}
              >
                {zh ? "仅允许一次" : "Allow once"}
              </button>
            </motion.div>
          ) : (
            <motion.div
              key={status}
              ref={resultRef}
              tabIndex={-1}
              className={`flex items-center gap-2 rounded-full text-[15px] font-medium outline-none ${status === "denied" ? "text-fg-muted" : "text-success"}`}
              initial={reduce ? { opacity: 0 } : { opacity: 0, y: 6 }}
              animate={{ opacity: 1, y: 0 }}
              exit={reduce ? { opacity: 0 } : { opacity: 0, y: -6 }}
              transition={spring}
            >
              <motion.span
                className={`grid size-7 place-items-center rounded-full ${status === "denied" ? "bg-muted" : "bg-success-soft"}`}
                initial={reduce ? false : { scale: 0.4, rotate: -30 }}
                animate={{ scale: 1, rotate: 0 }}
                transition={{ type: "spring", stiffness: 520, damping: 18 }}
              >
                <DrawnMark kind={status === "denied" ? "cross" : "check"} reduce={reduce} />
              </motion.span>
              {result}
            </motion.div>
          )}
        </AnimatePresence>
      </div>
      <span className="sr-only" role="status" aria-live="polite">
        {result}
      </span>
    </motion.div>
  );
}

使用说明

把 TSX 加入 React 项目,并安装组件引用的依赖。页面样式依赖 Tailwind CSS 与 主题变量。

包依赖 · lucide-react、motion、react

参数

名称类型说明
`title / overline``string`What the agent wants to do, and which agent.
`description``string`One line of context.
`risk``"low" \| "high"`High risk turns the icon and badge red and hides Always allow.
`warning``string`A red line about what can't be undone.
`details``{ label; value }[]`Rows the person should read first.
`expiry``string`Shown with a timer.
`allowAlways``boolean`Show Always allow (default true).
`onApprove / onDeny``(scope: "once" \| "always") => void / () => void`Called when the person answers.
locale"en" | "zh-CN"Optional interface language. Defaults to English.