{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "pix-payment",
  "title": "Pix Payment",
  "description": "A Pix charge with QR code, copy-and-paste code, expiry countdown, payment status, and card, plain, and compact variants.",
  "dependencies": [
    "class-variance-authority",
    "lucide-react"
  ],
  "registryDependencies": [
    "badge",
    "button",
    "https://uaiblocks.dev/r/uai-theme.json",
    "https://uaiblocks.dev/r/uai-utils.json"
  ],
  "files": [
    {
      "path": "src/registry/uai/components/pix-payment.tsx",
      "content": "\"use client\";\n\nimport { cva } from \"class-variance-authority\";\nimport { Check, CheckCircle2, Copy, TimerOff } from \"lucide-react\";\nimport {\n  type ComponentProps,\n  createContext,\n  useContext,\n  useEffect,\n  useId,\n  useRef,\n  useState,\n} from \"react\";\n\nimport { Badge } from \"@/components/ui/badge\";\nimport { Button } from \"@/components/ui/button\";\nimport { cn } from \"@/lib/uai-utils\";\n\nexport const PIX_PAYMENT_VARIANTS = [\"card\", \"plain\", \"compact\"] as const;\nexport const PIX_PAYMENT_STATUSES = [\"pending\", \"paid\", \"expired\"] as const;\n\nexport type PixPaymentVariant = (typeof PIX_PAYMENT_VARIANTS)[number];\nexport type PixPaymentStatus = (typeof PIX_PAYMENT_STATUSES)[number];\n\nexport function formatBrl(value: number) {\n  return new Intl.NumberFormat(\"pt-BR\", { style: \"currency\", currency: \"BRL\" }).format(value);\n}\n\nexport type PixPaymentProps = ComponentProps<\"section\"> & {\n  variant?: PixPaymentVariant;\n  /** Payment state owned by the application, usually from a webhook or polling. */\n  status?: PixPaymentStatus;\n  /** The Pix copy-and-paste payload (BR Code). */\n  code: string;\n  /** When the charge expires. The countdown ticks locally while pending. */\n  expiresAt?: Date | string | number;\n  /** Fixes the starting clock so server and client render the same countdown. */\n  now?: Date | string | number;\n  onExpire?: () => void;\n  onCodeCopy?: (code: string) => void;\n};\n\nconst pixPaymentVariants = cva(\"@container/pix grid w-full text-card-foreground\", {\n  variants: {\n    variant: {\n      card: \"gap-5 rounded-[14px] border border-border bg-card p-5\",\n      plain: \"gap-5\",\n      compact: \"gap-3.5 rounded-xl border border-border bg-card p-3.5\",\n    },\n  },\n});\n\ntype PixPaymentContextValue = {\n  variant: PixPaymentVariant;\n  status: PixPaymentStatus;\n  code: string;\n  secondsLeft: number | null;\n  copied: boolean;\n  copyFailed: boolean;\n  copy: () => Promise<void>;\n};\n\nconst PixPaymentContext = createContext<PixPaymentContextValue | null>(null);\n\nfunction usePixPayment(name: string) {\n  const context = useContext(PixPaymentContext);\n  if (!context) throw new Error(`${name} must be used within PixPayment`);\n  return context;\n}\n\nfunction toTime(value: Date | string | number) {\n  return new Date(value).getTime();\n}\n\nexport function PixPayment({\n  variant = \"card\",\n  status = \"pending\",\n  code,\n  expiresAt,\n  now,\n  onExpire,\n  onCodeCopy,\n  children,\n  className,\n  ...props\n}: PixPaymentProps) {\n  const deadline = expiresAt === undefined ? null : toTime(expiresAt);\n  // A fixed `now` becomes an offset from the real clock, so the countdown keeps ticking.\n  const [offset] = useState(() => (now === undefined ? 0 : toTime(now) - Date.now()));\n  const [clock, setClock] = useState(() => Date.now() + offset);\n  const [copied, setCopied] = useState(false);\n  const [copyFailed, setCopyFailed] = useState(false);\n  const expiredNotified = useRef(false);\n  const onExpireRef = useRef(onExpire);\n  onExpireRef.current = onExpire;\n\n  const secondsLeft = deadline === null ? null : Math.max(0, Math.ceil((deadline - clock) / 1000));\n\n  useEffect(() => {\n    if (status !== \"pending\" || deadline === null) return;\n    const timer = window.setInterval(() => setClock(Date.now() + offset), 1000);\n    return () => window.clearInterval(timer);\n  }, [deadline, offset, status]);\n\n  useEffect(() => {\n    if (status !== \"pending\" || secondsLeft !== 0 || expiredNotified.current) return;\n    expiredNotified.current = true;\n    onExpireRef.current?.();\n  }, [secondsLeft, status]);\n\n  useEffect(() => {\n    if (!copied) return;\n    const timer = window.setTimeout(() => setCopied(false), 2000);\n    return () => window.clearTimeout(timer);\n  }, [copied]);\n\n  const copy = async () => {\n    try {\n      await navigator.clipboard.writeText(code);\n      setCopyFailed(false);\n      setCopied(true);\n      onCodeCopy?.(code);\n    } catch {\n      // Without clipboard access the code stays selectable, and the hint says so.\n      setCopied(false);\n      setCopyFailed(true);\n    }\n  };\n\n  return (\n    <PixPaymentContext.Provider\n      value={{ variant, status, code, secondsLeft, copied, copyFailed, copy }}\n    >\n      <section\n        data-slot=\"pix-payment\"\n        data-variant={variant}\n        data-status={status}\n        className={cn(pixPaymentVariants({ variant }), className)}\n        {...props}\n      >\n        {children}\n      </section>\n    </PixPaymentContext.Provider>\n  );\n}\n\nexport type PixPaymentHeaderProps = ComponentProps<\"header\">;\n\nexport function PixPaymentHeader({ className, ...props }: PixPaymentHeaderProps) {\n  return (\n    <header\n      data-slot=\"pix-payment-header\"\n      className={cn(\n        \"flex min-w-0 flex-wrap items-start justify-between gap-x-4 gap-y-2\",\n        className,\n      )}\n      {...props}\n    />\n  );\n}\n\nexport type PixPaymentTitleProps = ComponentProps<\"h3\">;\n\nexport function PixPaymentTitle({\n  children = \"Pague com Pix\",\n  className,\n  ...props\n}: PixPaymentTitleProps) {\n  return (\n    <h3\n      data-slot=\"pix-payment-title\"\n      className={cn(\"text-[13px] leading-[18px] font-medium text-muted-foreground\", className)}\n      {...props}\n    >\n      {children}\n    </h3>\n  );\n}\n\nexport type PixPaymentAmountProps = Omit<ComponentProps<\"p\">, \"children\"> & {\n  value: number;\n};\n\nexport function PixPaymentAmount({ value, className, ...props }: PixPaymentAmountProps) {\n  const context = usePixPayment(\"PixPaymentAmount\");\n  return (\n    <p\n      data-slot=\"pix-payment-amount\"\n      className={cn(\n        \"font-semibold tracking-[-0.01em] text-foreground tabular-nums\",\n        context.variant === \"compact\" ? \"text-[20px] leading-6\" : \"text-[28px] leading-8\",\n        className,\n      )}\n      {...props}\n    >\n      {formatBrl(value)}\n    </p>\n  );\n}\n\nconst statusCopy: Record<PixPaymentStatus, string> = {\n  pending: \"Aguardando pagamento\",\n  paid: \"Pagamento confirmado\",\n  expired: \"Código expirado\",\n};\n\nexport type PixPaymentStatusBadgeProps = ComponentProps<\"span\">;\n\n/** A polite live region, so confirmation is announced when the status changes. */\nexport function PixPaymentStatusBadge({\n  children,\n  className,\n  ...props\n}: PixPaymentStatusBadgeProps) {\n  const context = usePixPayment(\"PixPaymentStatusBadge\");\n  return (\n    <Badge\n      variant=\"secondary\"\n      data-slot=\"pix-payment-status-badge\"\n      data-status={context.status}\n      className={cn(\n        \"h-6 shrink-0 gap-1.5 rounded-full border-0 px-2.5 py-0 text-[11.5px] font-medium\",\n        context.status === \"pending\" && \"bg-warning/14 text-warning-ink\",\n        context.status === \"paid\" && \"bg-success/14 text-success-ink\",\n        context.status === \"expired\" && \"bg-muted text-muted-foreground\",\n        className,\n      )}\n      {...props}\n      role=\"status\"\n      aria-live=\"polite\"\n    >\n      <span\n        aria-hidden=\"true\"\n        className={cn(\n          \"size-1.5 rounded-full bg-current\",\n          context.status === \"pending\" && \"animate-pulse motion-reduce:animate-none\",\n        )}\n      />\n      {children ?? statusCopy[context.status]}\n    </Badge>\n  );\n}\n\nexport type PixPaymentBodyProps = ComponentProps<\"div\">;\n\n/** Places the QR code beside the payment details, stacking on narrow containers. */\nexport function PixPaymentBody({ className, ...props }: PixPaymentBodyProps) {\n  return (\n    <div\n      data-slot=\"pix-payment-body\"\n      className={cn(\n        \"grid items-start gap-5 @min-[520px]/pix:grid-cols-[auto_minmax(0,1fr)]\",\n        className,\n      )}\n      {...props}\n    />\n  );\n}\n\nexport type PixPaymentQrCodeProps = ComponentProps<\"figure\">;\n\n/** Frames the consumer's QR image. The tile stays white so every camera can read it. */\nexport function PixPaymentQrCode({ children, className, ...props }: PixPaymentQrCodeProps) {\n  const context = usePixPayment(\"PixPaymentQrCode\");\n  const compact = context.variant === \"compact\";\n  return (\n    <figure\n      data-slot=\"pix-payment-qr-code\"\n      className={cn(\n        \"relative isolate m-0 grid shrink-0 place-items-center justify-self-center overflow-hidden bg-white shadow-[0_0_0_1px_var(--border)] @min-[520px]/pix:justify-self-start\",\n        compact ? \"size-32 rounded-[10px] p-2.5\" : \"size-44 rounded-xl p-3.5\",\n        \"[&_img]:size-full [&_svg]:size-full\",\n        className,\n      )}\n      {...props}\n    >\n      <div\n        className={cn(\n          \"size-full transition-[filter,opacity] duration-240 ease-out-quint motion-reduce:transition-none\",\n          context.status !== \"pending\" && \"opacity-25 blur-[3px]\",\n        )}\n        aria-hidden={context.status !== \"pending\" || undefined}\n      >\n        {children}\n      </div>\n      {context.status !== \"pending\" ? (\n        <figcaption\n          className={cn(\n            \"absolute inset-0 grid animate-in place-content-center justify-items-center gap-1.5 text-center text-[12px] leading-4 font-medium text-[oklch(0.247_0.006_258)] duration-240 ease-out-quint fade-in-0 zoom-in-96 motion-reduce:animate-none\",\n          )}\n        >\n          {context.status === \"paid\" ? (\n            <CheckCircle2\n              className=\"size-7 animate-in text-success-ink delay-60 duration-240 ease-out-quint fill-mode-both fade-in-0 zoom-in-50 motion-reduce:animate-none\"\n              strokeWidth={1.75}\n              aria-hidden=\"true\"\n            />\n          ) : (\n            <TimerOff className=\"size-7 opacity-70\" strokeWidth={1.75} aria-hidden=\"true\" />\n          )}\n          {context.status === \"paid\" ? \"Pago\" : \"QR Code expirado\"}\n        </figcaption>\n      ) : null}\n    </figure>\n  );\n}\n\nexport type PixPaymentDetailsProps = ComponentProps<\"div\">;\n\nexport function PixPaymentDetails({ className, ...props }: PixPaymentDetailsProps) {\n  return (\n    <div\n      data-slot=\"pix-payment-details\"\n      className={cn(\"grid min-w-0 content-start gap-3.5\", className)}\n      {...props}\n    />\n  );\n}\n\nexport type PixPaymentCodeProps = Omit<ComponentProps<\"div\">, \"children\"> & {\n  label?: string;\n};\n\n/** The copy-and-paste code with a copy button and a spoken confirmation. */\nexport function PixPaymentCode({\n  label = \"Pix copia e cola\",\n  className,\n  ...props\n}: PixPaymentCodeProps) {\n  const context = usePixPayment(\"PixPaymentCode\");\n  const compact = context.variant === \"compact\";\n  const disabled = context.status !== \"pending\";\n  const labelId = useId();\n  return (\n    // biome-ignore lint/a11y/useSemanticElements: a fieldset would imply form controls the code does not have.\n    <div\n      data-slot=\"pix-payment-code\"\n      className={cn(\"grid min-w-0 gap-1.5\", className)}\n      {...props}\n      role=\"group\"\n      aria-labelledby={labelId}\n    >\n      <span id={labelId} className=\"text-[12px] leading-4 font-medium text-muted-foreground\">\n        {label}\n      </span>\n      <div\n        className={cn(\n          \"flex min-w-0 items-center gap-1 bg-background\",\n          compact ? \"rounded-[10px] p-0.5 pl-2.5\" : \"rounded-xl p-1 pl-3\",\n        )}\n      >\n        <code\n          className={cn(\n            \"min-w-0 flex-1 truncate font-mono text-[12px] leading-4 text-muted-foreground select-all\",\n            disabled && \"line-through decoration-subtle-foreground/60\",\n          )}\n          title={context.code}\n        >\n          {context.code}\n        </code>\n        <Button\n          type=\"button\"\n          data-slot=\"pix-payment-copy\"\n          className={cn(\n            \"shrink-0 gap-1.5 rounded-full py-0 font-medium transition-[scale,background-color,color,filter] duration-[140ms] ease-out-quint focus-visible:ring-0 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring focus-visible:outline-solid active:scale-[0.97] disabled:pointer-events-auto disabled:cursor-not-allowed disabled:bg-secondary disabled:text-subtle-foreground disabled:opacity-100 motion-reduce:transition-none motion-reduce:active:scale-100\",\n            compact\n              ? \"h-7 px-3 text-[12px] has-[>svg]:px-3\"\n              : \"h-8 px-3.5 text-[12.5px] has-[>svg]:px-3.5\",\n            context.copied\n              ? \"bg-success/14 text-success-ink hover:bg-success/14\"\n              : \"bg-primary text-primary-foreground hover:bg-primary hover:brightness-[1.08]\",\n          )}\n          disabled={disabled}\n          onClick={() => void context.copy()}\n        >\n          {context.copied ? (\n            <Check\n              key=\"copied\"\n              className=\"size-3.5 animate-in duration-200 ease-out-quint fade-in-0 zoom-in-50 motion-reduce:animate-none\"\n              strokeWidth={2.2}\n              aria-hidden=\"true\"\n            />\n          ) : (\n            <Copy key=\"copy\" className=\"size-3.5\" strokeWidth={2} aria-hidden=\"true\" />\n          )}\n          {/* Both labels share one cell, so the button keeps its width when the label swaps. */}\n          <span className=\"grid [&>*]:[grid-area:1/1]\">\n            <span className={cn(context.copied && \"invisible\")} aria-hidden={context.copied}>\n              Copiar código\n            </span>\n            <span className={cn(!context.copied && \"invisible\")} aria-hidden={!context.copied}>\n              Copiado\n            </span>\n          </span>\n        </Button>\n      </div>\n      {context.copyFailed ? (\n        <p className=\"m-0 text-[11.5px] leading-4 text-subtle-foreground\">\n          Selecione e copie o código\n        </p>\n      ) : null}\n      <span role=\"status\" className=\"sr-only\">\n        {context.copied\n          ? \"Código Pix copiado\"\n          : context.copyFailed\n            ? \"Não foi possível copiar o código Pix\"\n            : \"\"}\n      </span>\n    </div>\n  );\n}\n\nfunction formatClock(seconds: number) {\n  const minutes = Math.floor(seconds / 60);\n  return `${String(minutes).padStart(2, \"0\")}:${String(seconds % 60).padStart(2, \"0\")}`;\n}\n\nexport type PixPaymentCountdownProps = Omit<ComponentProps<\"p\">, \"children\">;\n\n/** Time left before the charge expires. Hidden once the payment is confirmed. */\nexport function PixPaymentCountdown({ className, ...props }: PixPaymentCountdownProps) {\n  const context = usePixPayment(\"PixPaymentCountdown\");\n  if (context.secondsLeft === null || context.status === \"paid\") return null;\n  const expired = context.status === \"expired\" || context.secondsLeft === 0;\n  const urgent = !expired && context.secondsLeft <= 60;\n  return (\n    <p\n      data-slot=\"pix-payment-countdown\"\n      className={cn(\n        \"text-[12px] leading-4 text-subtle-foreground transition-colors duration-240 motion-reduce:transition-none\",\n        urgent && \"text-warning-ink\",\n        className,\n      )}\n      {...props}\n    >\n      {expired ? (\n        \"O prazo para pagar terminou.\"\n      ) : (\n        <>\n          Expira em{\" \"}\n          <time\n            className=\"font-medium text-foreground tabular-nums\"\n            dateTime={`PT${context.secondsLeft}S`}\n          >\n            {formatClock(context.secondsLeft)}\n          </time>\n        </>\n      )}\n    </p>\n  );\n}\n\nexport type PixPaymentStepsProps = ComponentProps<\"ol\">;\n\nexport function PixPaymentSteps({ className, ...props }: PixPaymentStepsProps) {\n  return (\n    <ol\n      data-slot=\"pix-payment-steps\"\n      className={cn(\n        \"m-0 grid list-none gap-2 p-0 text-[12.5px] leading-[18px] text-muted-foreground [counter-reset:pix-step]\",\n        className,\n      )}\n      {...props}\n    />\n  );\n}\n\nexport type PixPaymentStepProps = ComponentProps<\"li\">;\n\nexport function PixPaymentStep({ className, ...props }: PixPaymentStepProps) {\n  return (\n    <li\n      data-slot=\"pix-payment-step\"\n      className={cn(\n        \"flex gap-2.5 [counter-increment:pix-step] before:grid before:size-[18px] before:shrink-0 before:place-items-center before:rounded-full before:bg-muted before:text-[10.5px] before:font-medium before:text-muted-foreground before:tabular-nums before:content-[counter(pix-step)]\",\n        className,\n      )}\n      {...props}\n    />\n  );\n}\n",
      "type": "registry:ui",
      "target": "@ui/uai/pix-payment.tsx"
    }
  ],
  "categories": [
    "brazil",
    "commerce",
    "checkout"
  ],
  "type": "registry:ui"
}