{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "credit-card-field",
  "type": "registry:ui",
  "title": "Credit Card Field",
  "description": "Credit Card Field is a compound UI control for card number, expiry, and CVC in one shell — visual pattern only, not a payment processor or PCI vault.",
  "author": "Skrewww",
  "dependencies": [
    "@phosphor-icons/react"
  ],
  "registryDependencies": [
    "@skrewww/validation-message",
    "@skrewww/foundation"
  ],
  "docs": "Host requirements (assumed already present, not installed by this command): react, react-dom.",
  "files": [
    {
      "path": "components/ui/CreditCardField.tsx",
      "content": "/** @skrewww-component credit-card-field */\n\"use client\";\n\nimport { CreditCard } from \"@phosphor-icons/react\";\nimport {\n  useId,\n  useRef,\n  type ChangeEvent,\n  type ClipboardEvent,\n} from \"react\";\nimport { cn } from \"@/lib/cn\";\nimport { useControllableState } from \"@/lib/use-controllable\";\nimport { ValidationMessage } from \"@/components/ui/ValidationMessage\";\nimport {\n  CREDIT_CARD_CVC_MAX_DIGITS,\n  CREDIT_CARD_EXPIRY_MAX_DIGITS,\n  CREDIT_CARD_NUMBER_MAX_DIGITS,\n  digitsOnly,\n  formatCardNumberDisplay,\n  formatCvcDisplay,\n  formatExpiryDisplay,\n  mapCaretAfterFormat,\n} from \"@/lib/credit-card-field-format\";\nimport styles from \"@/components/ui/credit-card-field.module.css\";\n\nexport type CreditCardFieldValue = {\n  /** Digit-only PAN (no spaces). */\n  number: string;\n  /** Digit-only expiry MMYY (no slash). */\n  expiry: string;\n  /** Digit-only CVC/CVV. */\n  cvc: string;\n};\n\nexport type CreditCardFieldProps = {\n  /** Visible group label (fieldset legend). */\n  label: string;\n  /** Accessible name for the card-number segment. Default: \"Card number\". */\n  numberLabel?: string;\n  /** Accessible name for the expiry segment. Default: \"Expiry\". */\n  expiryLabel?: string;\n  /** Accessible name for the CVC segment. Default: \"CVC\". */\n  cvcLabel?: string;\n  numberPlaceholder?: string;\n  expiryPlaceholder?: string;\n  cvcPlaceholder?: string;\n  value?: CreditCardFieldValue;\n  defaultValue?: CreditCardFieldValue;\n  onValueChange?: (value: CreditCardFieldValue) => void;\n  disabled?: boolean;\n  readOnly?: boolean;\n  required?: boolean;\n  supportingText?: string;\n  error?: string;\n  hideLabel?: boolean;\n  /** Prefix for native `name` attributes (`${name}-number`, etc.). */\n  name?: string;\n  id?: string;\n  className?: string;\n};\n\nconst EMPTY_VALUE: CreditCardFieldValue = { number: \"\", expiry: \"\", cvc: \"\" };\n\nfunction normalizeValue(value: CreditCardFieldValue): CreditCardFieldValue {\n  return {\n    number: digitsOnly(value.number, CREDIT_CARD_NUMBER_MAX_DIGITS),\n    expiry: digitsOnly(value.expiry, CREDIT_CARD_EXPIRY_MAX_DIGITS),\n    cvc: digitsOnly(value.cvc, CREDIT_CARD_CVC_MAX_DIGITS),\n  };\n}\n\n/**\n * Compound card-details control: number + expiry + CVC in one visual shell.\n *\n * This is a UI pattern only — not a payment processor, tokenizer, or PCI vault.\n * Prefer a payment provider’s hosted fields for production card capture.\n */\nexport function CreditCardField({\n  label,\n  numberLabel = \"Card number\",\n  expiryLabel = \"Expiry\",\n  cvcLabel = \"CVC\",\n  numberPlaceholder = \"Card number\",\n  expiryPlaceholder = \"MM/YY\",\n  cvcPlaceholder = \"CVC\",\n  value,\n  defaultValue = EMPTY_VALUE,\n  onValueChange,\n  disabled = false,\n  readOnly = false,\n  required = false,\n  supportingText,\n  error,\n  hideLabel = false,\n  name: nameProp,\n  id: idProp,\n  className,\n}: CreditCardFieldProps) {\n  const generatedId = useId();\n  const baseId = idProp ?? generatedId;\n  const nameBase = nameProp ?? baseId.replace(/:/g, \"\");\n  const supportingId = supportingText ? `${baseId}-supporting` : undefined;\n  const errorId = error ? `${baseId}-error` : undefined;\n  const describedBy = [errorId, !error ? supportingId : undefined].filter(Boolean).join(\" \");\n  const invalid = Boolean(error);\n\n  const numberRef = useRef<HTMLInputElement | null>(null);\n  const expiryRef = useRef<HTMLInputElement | null>(null);\n  const cvcRef = useRef<HTMLInputElement | null>(null);\n\n  const valueProvided = value !== undefined;\n  const [current, setCurrent] = useControllableState<CreditCardFieldValue>({\n    value: valueProvided ? normalizeValue(value as CreditCardFieldValue) : undefined,\n    defaultValue: normalizeValue(defaultValue),\n    onChange: onValueChange,\n    valueProvided,\n  });\n\n  function commit(next: CreditCardFieldValue) {\n    setCurrent(normalizeValue(next));\n  }\n\n  function handleNumberChange(event: ChangeEvent<HTMLInputElement>) {\n    const el = event.target;\n    const caret = el.selectionStart ?? el.value.length;\n    const prevDisplay = el.value;\n    const nextDigits = digitsOnly(el.value, CREDIT_CARD_NUMBER_MAX_DIGITS);\n    const nextDisplay = formatCardNumberDisplay(nextDigits);\n    commit({ ...current, number: nextDigits });\n    requestAnimationFrame(() => {\n      const node = numberRef.current;\n      if (!node) return;\n      const nextCaret = mapCaretAfterFormat(prevDisplay, caret, nextDisplay);\n      node.setSelectionRange(nextCaret, nextCaret);\n    });\n  }\n\n  function handleExpiryChange(event: ChangeEvent<HTMLInputElement>) {\n    const el = event.target;\n    const caret = el.selectionStart ?? el.value.length;\n    const prevDisplay = el.value;\n    const nextDigits = digitsOnly(el.value, CREDIT_CARD_EXPIRY_MAX_DIGITS);\n    const nextDisplay = formatExpiryDisplay(nextDigits);\n    commit({ ...current, expiry: nextDigits });\n    requestAnimationFrame(() => {\n      const node = expiryRef.current;\n      if (!node) return;\n      const nextCaret = mapCaretAfterFormat(prevDisplay, caret, nextDisplay);\n      node.setSelectionRange(nextCaret, nextCaret);\n    });\n  }\n\n  function handleCvcChange(event: ChangeEvent<HTMLInputElement>) {\n    const nextDigits = digitsOnly(event.target.value, CREDIT_CARD_CVC_MAX_DIGITS);\n    commit({ ...current, cvc: nextDigits });\n  }\n\n  function handlePaste(\n    field: keyof CreditCardFieldValue,\n    max: number,\n    event: ClipboardEvent<HTMLInputElement>,\n  ) {\n    event.preventDefault();\n    const pasted = digitsOnly(event.clipboardData.getData(\"text\"), max);\n    commit({ ...current, [field]: pasted });\n  }\n\n  const numberDisplay = formatCardNumberDisplay(current.number);\n  const expiryDisplay = formatExpiryDisplay(current.expiry);\n  const cvcDisplay = formatCvcDisplay(current.cvc);\n\n  return (\n    <fieldset\n      className={cn(styles.field, className)}\n      disabled={disabled || undefined}\n      aria-describedby={describedBy || undefined}\n      aria-invalid={invalid || undefined}\n      aria-required={required || undefined}\n    >\n      <legend className={hideLabel ? styles.srOnly : styles.legend}>\n        {label}\n        {required ? (\n          <>\n            <span className={styles.required} aria-hidden=\"true\">\n              *\n            </span>\n            <span className=\"sr-only\"> (required)</span>\n          </>\n        ) : null}\n      </legend>\n\n      <div\n        className={cn(\n          styles.shell,\n          invalid && styles.invalid,\n          disabled && styles.disabled,\n        )}\n      >\n        <span className={styles.icon} aria-hidden=\"true\">\n          <CreditCard size={20} weight=\"regular\" />\n        </span>\n\n        <input\n          ref={numberRef}\n          id={`${baseId}-number`}\n          name={`${nameBase}-number`}\n          className={cn(styles.segment, styles.number)}\n          type=\"text\"\n          inputMode=\"numeric\"\n          autoComplete=\"cc-number\"\n          spellCheck={false}\n          aria-label={numberLabel}\n          aria-invalid={invalid || undefined}\n          aria-describedby={describedBy || undefined}\n          placeholder={numberPlaceholder}\n          value={numberDisplay}\n          onChange={handleNumberChange}\n          onPaste={(event) => handlePaste(\"number\", CREDIT_CARD_NUMBER_MAX_DIGITS, event)}\n          disabled={disabled}\n          readOnly={readOnly}\n          required={required}\n        />\n\n        <span className={styles.divider} aria-hidden=\"true\" />\n\n        <input\n          ref={expiryRef}\n          id={`${baseId}-expiry`}\n          name={`${nameBase}-expiry`}\n          className={cn(styles.segment, styles.expiry)}\n          type=\"text\"\n          inputMode=\"numeric\"\n          autoComplete=\"cc-exp\"\n          spellCheck={false}\n          aria-label={expiryLabel}\n          aria-invalid={invalid || undefined}\n          placeholder={expiryPlaceholder}\n          value={expiryDisplay}\n          onChange={handleExpiryChange}\n          onPaste={(event) => handlePaste(\"expiry\", CREDIT_CARD_EXPIRY_MAX_DIGITS, event)}\n          disabled={disabled}\n          readOnly={readOnly}\n          required={required}\n        />\n\n        <span className={styles.divider} aria-hidden=\"true\" />\n\n        <input\n          ref={cvcRef}\n          id={`${baseId}-cvc`}\n          name={`${nameBase}-cvc`}\n          className={cn(styles.segment, styles.cvc)}\n          type=\"text\"\n          inputMode=\"numeric\"\n          autoComplete=\"cc-csc\"\n          spellCheck={false}\n          aria-label={cvcLabel}\n          aria-invalid={invalid || undefined}\n          placeholder={cvcPlaceholder}\n          value={cvcDisplay}\n          onChange={handleCvcChange}\n          onPaste={(event) => handlePaste(\"cvc\", CREDIT_CARD_CVC_MAX_DIGITS, event)}\n          disabled={disabled}\n          readOnly={readOnly}\n          required={required}\n        />\n      </div>\n\n      {error ? (\n        <ValidationMessage id={errorId} type=\"error\" announce=\"off\">\n          {error}\n        </ValidationMessage>\n      ) : supportingText ? (\n        <p id={supportingId} className={styles.supporting}>\n          {supportingText}\n        </p>\n      ) : null}\n    </fieldset>\n  );\n}\n",
      "type": "registry:ui",
      "target": "~/components/ui/CreditCardField.tsx"
    },
    {
      "path": "components/ui/credit-card-field.module.css",
      "content": ".field {\n  display: flex;\n  flex-direction: column;\n  gap: 0.375rem;\n  width: 100%;\n  margin: 0;\n  padding: 0;\n  border: 0;\n  min-width: 0;\n}\n\n.legend {\n  font-size: 0.875rem;\n  font-weight: 500;\n  line-height: 1.4;\n  color: var(--semantic-text-primary);\n  padding: 0;\n  margin-bottom: 0.375rem;\n}\n\n.required {\n  margin-left: 0.125rem;\n  color: var(--semantic-text-danger);\n}\n\n.srOnly {\n  position: absolute;\n  width: 1px;\n  height: 1px;\n  padding: 0;\n  margin: -1px;\n  overflow: hidden;\n  clip: rect(0, 0, 0, 0);\n  white-space: nowrap;\n  border: 0;\n}\n\n.supporting {\n  font-size: 0.8125rem;\n  line-height: 1.4;\n  color: var(--semantic-text-secondary);\n}\n\n/* Shared shell — mirrors Text Input chrome (same token family as Figma). */\n.shell {\n  box-sizing: border-box;\n  display: flex;\n  flex-direction: row;\n  align-items: center;\n  gap: 0.5rem; /* spacing/8 */\n  width: 100%;\n  min-height: var(--control-height-md);\n  padding: 0 0.75rem; /* spacing/12 */\n  border: 1px solid var(--semantic-border-default);\n  border-radius: var(--shape-radius-control);\n  clip-path: var(--squircle-clip-path-control, none);\n  background-color: var(--surface-fill-control);\n  background-image: var(--component-surface-gradient-overlay);\n  backdrop-filter: var(--glass-backdrop-filter-sm, none);\n  transition:\n    border-color 0.15s ease,\n    box-shadow 0.15s ease;\n}\n\n.shell:hover:not(.disabled):not(.invalid) {\n  border-color: var(--semantic-border-strong);\n}\n\n/* Focused state is shell-level (Figma State=Focused), not per-segment ring. */\n.shell:focus-within:not(.disabled) {\n  border-color: var(--semantic-focus-ring);\n  box-shadow: 0 0 0 2px var(--semantic-focus-ring);\n}\n\n[data-skrewww-shape=\"squircle\"] .shell:focus-within:not(.disabled) {\n  box-shadow: inset 0 0 0 2px var(--semantic-focus-ring);\n}\n\n.shell.invalid {\n  border-color: var(--semantic-action-danger);\n}\n\n.shell.invalid:focus-within:not(.disabled) {\n  border-color: var(--semantic-action-danger);\n  box-shadow: 0 0 0 2px var(--semantic-action-danger);\n}\n\n[data-skrewww-shape=\"squircle\"] .shell.invalid:focus-within:not(.disabled) {\n  box-shadow: inset 0 0 0 2px var(--semantic-action-danger);\n}\n\n.shell.disabled {\n  cursor: not-allowed;\n  pointer-events: none;\n  color: var(--semantic-text-disabled);\n  border-color: var(--semantic-border-disabled);\n  background-color: var(--semantic-surface-disabled);\n  opacity: var(--opacity-disabled);\n}\n\n.icon {\n  flex: 0 0 auto;\n  display: inline-flex;\n  align-items: center;\n  justify-content: center;\n  color: var(--semantic-icon-muted);\n  pointer-events: none;\n}\n\n.shell.disabled .icon {\n  color: var(--semantic-text-disabled);\n}\n\n.segment {\n  box-sizing: border-box;\n  margin: 0;\n  padding: 0;\n  border: 0;\n  background: transparent;\n  color: var(--semantic-text-primary);\n  font: inherit;\n  font-size: var(--control-font-size-md);\n  line-height: 1.2;\n  min-width: 0;\n}\n\n.segment:focus {\n  outline: none;\n}\n\n.segment::placeholder {\n  color: var(--semantic-text-secondary);\n}\n\n.shell.disabled .segment,\n.shell.disabled .segment::placeholder {\n  color: var(--semantic-text-disabled);\n}\n\n.number {\n  flex: 1 1 auto;\n  width: 100%;\n}\n\n.expiry {\n  flex: 0 0 auto;\n  width: 3.5rem;\n  text-align: left;\n  color: var(--semantic-text-secondary);\n}\n\n.expiry:not(:placeholder-shown) {\n  color: var(--semantic-text-primary);\n}\n\n.cvc {\n  flex: 0 0 auto;\n  width: 2.5rem;\n  text-align: left;\n  color: var(--semantic-text-secondary);\n}\n\n.cvc:not(:placeholder-shown) {\n  color: var(--semantic-text-primary);\n}\n\n.divider {\n  flex: 0 0 auto;\n  width: 1px;\n  height: 1.25rem; /* 20px */\n  background-color: var(--primitive-color-neutral-200, var(--semantic-border-default));\n  pointer-events: none;\n}\n\n@media (prefers-reduced-motion: reduce) {\n  .shell {\n    transition: none;\n  }\n}\n",
      "type": "registry:ui",
      "target": "~/components/ui/credit-card-field.module.css"
    },
    {
      "path": "lib/cn.ts",
      "content": "export function cn(...classes: Array<string | false | null | undefined>): string {\n  return classes.filter(Boolean).join(\" \");\n}\n",
      "type": "registry:lib",
      "target": "~/lib/cn.ts"
    },
    {
      "path": "lib/use-controllable.ts",
      "content": "import { useCallback, useEffect, useRef, useState } from \"react\";\n\nexport function useControllableState<T>({\n  value,\n  defaultValue,\n  onChange,\n  controlled: controlledProp,\n  valueProvided,\n}: {\n  value?: T;\n  defaultValue?: T;\n  onChange?: (next: T) => void;\n  /** Explicit controlled override — prefer `valueProvided` from the component root. */\n  controlled?: boolean;\n  /** True when the parent passed a `value` prop, including `undefined`. */\n  valueProvided?: boolean;\n}) {\n  const [uncontrolled, setUncontrolled] = useState(defaultValue as T);\n  const isControlled = controlledProp ?? valueProvided ?? value !== undefined;\n  const current = isControlled ? (value as T) : uncontrolled;\n  const wasControlledRef = useRef(isControlled);\n\n  useEffect(() => {\n    if (process.env.NODE_ENV !== \"development\") return;\n    if (wasControlledRef.current !== isControlled) {\n      console.warn(\n        \"[Skrewww] A component changed between controlled and uncontrolled modes. Keep `value` or `defaultValue` usage consistent across renders.\",\n      );\n    }\n    wasControlledRef.current = isControlled;\n  }, [isControlled]);\n\n  const setValue = useCallback(\n    (next: T) => {\n      if (!isControlled) setUncontrolled(next);\n      onChange?.(next);\n    },\n    [isControlled, onChange],\n  );\n\n  return [current, setValue] as const;\n}\n",
      "type": "registry:lib",
      "target": "~/lib/use-controllable.ts"
    },
    {
      "path": "lib/credit-card-field-format.ts",
      "content": "/**\n * Presentation helpers for Credit Card Field.\n * Values store digit-only strings; display may insert grouping separators.\n * Not payment validation and not network brand detection.\n */\n\n/** Max digit length covering common PAN ranges (incl. some 19-digit PANs). */\nexport const CREDIT_CARD_NUMBER_MAX_DIGITS = 19;\nexport const CREDIT_CARD_EXPIRY_MAX_DIGITS = 4;\nexport const CREDIT_CARD_CVC_MAX_DIGITS = 4;\n\nexport function digitsOnly(value: string, max: number): string {\n  return value.replace(/\\D/g, \"\").slice(0, max);\n}\n\n/** Groups digits in fours with spaces for display (Figma visual grouping). */\nexport function formatCardNumberDisplay(digits: string): string {\n  const clean = digitsOnly(digits, CREDIT_CARD_NUMBER_MAX_DIGITS);\n  return clean.replace(/(\\d{4})(?=\\d)/g, \"$1 \").trim();\n}\n\n/** MM/YY display from up to 4 digits. */\nexport function formatExpiryDisplay(digits: string): string {\n  const clean = digitsOnly(digits, CREDIT_CARD_EXPIRY_MAX_DIGITS);\n  if (clean.length <= 2) return clean;\n  return `${clean.slice(0, 2)}/${clean.slice(2)}`;\n}\n\nexport function formatCvcDisplay(digits: string): string {\n  return digitsOnly(digits, CREDIT_CARD_CVC_MAX_DIGITS);\n}\n\n/**\n * Map caret index in a formatted string back after reformatting.\n * Counts digits before the caret, then finds that digit offset in next.\n */\nexport function mapCaretAfterFormat(\n  previousFormatted: string,\n  caret: number,\n  nextFormatted: string,\n): number {\n  const digitsBefore = digitsOnly(previousFormatted.slice(0, caret), 64).length;\n  if (digitsBefore === 0) return 0;\n  let seen = 0;\n  for (let i = 0; i < nextFormatted.length; i += 1) {\n    if (/\\d/.test(nextFormatted[i]!)) {\n      seen += 1;\n      if (seen === digitsBefore) return i + 1;\n    }\n  }\n  return nextFormatted.length;\n}\n",
      "type": "registry:lib",
      "target": "~/lib/credit-card-field-format.ts"
    }
  ]
}
