{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "order-summary",
  "title": "Order Summary",
  "description": "Order summary with priced rows, a total, an optional promo code form and a checkout button.",
  "dependencies": [
    "@vllnt/ui@^0.3.0"
  ],
  "registryDependencies": [],
  "files": [
    {
      "path": "registry/default/order-summary/order-summary.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nimport { cva } from \"class-variance-authority\";\n\nimport { cn } from \"@vllnt/ui\";\nimport { Button } from \"@vllnt/ui\";\nimport { Card } from \"@vllnt/ui\";\nimport { Input } from \"@vllnt/ui\";\nimport { Label } from \"@vllnt/ui\";\nimport { Separator } from \"@vllnt/ui\";\nimport { Spinner } from \"@vllnt/ui\";\n\nconst DEFAULT_APPLY_LABEL = \"Apply\";\nconst DEFAULT_CHECKOUT_LABEL = \"Checkout\";\nconst DEFAULT_PROMO_ERROR = \"This code can't be applied\";\nconst DEFAULT_PROMO_TITLE = \"Promo code\";\n\nconst surfaceVariants = cva(\"flex w-full flex-col\", {\n  defaultVariants: { layout: \"desktop\" },\n  variants: {\n    layout: {\n      desktop: \"gap-4 p-6\",\n      phone: \"gap-3 p-4\",\n    },\n  },\n});\n\n/**\n * Density of {@link OrderSummary}. Maps to the Penpot `Layout` axis\n * (`desktop` | `phone`) and changes padding and spacing, nothing else.\n *\n * @public\n */\nexport type OrderSummaryLayout = \"desktop\" | \"phone\";\n\n/**\n * One priced row. The host preformats both strings, so the component never\n * formats currency.\n *\n * @public\n */\nexport type OrderSummaryLine = {\n  /** Row label, for example `\"Shipping\"`. */\n  label: string;\n  /** Preformatted value, for example `\"$8.00\"`. */\n  value: string;\n};\n\n/**\n * Promo code state. Maps to the Penpot `Status` axis: `default` is `idle`,\n * `applying-promo` is `applying`, `error` is `error`.\n *\n * @public\n */\nexport type OrderSummaryPromoStatus = \"applying\" | \"error\" | \"idle\";\n\n/**\n * Localizable strings for the promo code area of {@link OrderSummary}.\n *\n * @public\n */\nexport type OrderSummaryPromoLabels = {\n  /** Apply button word, also shown next to the spinner. Defaults to `\"Apply\"`. */\n  apply?: string;\n  /**\n   * Error words announced as an alert when `status=\"error\"` and no `hint` is\n   * given, so the error never rests on colour alone. Defaults to English words\n   * saying the code does not apply.\n   */\n  error?: string;\n  /** Input placeholder. No placeholder when omitted. */\n  placeholder?: string;\n  /** Visible label of the input. Defaults to `\"Promo code\"`. */\n  title?: string;\n};\n\n/**\n * Controlled promo code form of {@link OrderSummary}.\n *\n * @public\n */\nexport type OrderSummaryPromo = {\n  /**\n   * Text under the input. With `status=\"error\"` the hint carries the error\n   * message and screen readers announce it as an alert. Without a hint, the\n   * error shows `labels.error` instead; a given hint always wins.\n   */\n  hint?: string;\n  /** Strings for the promo area. */\n  labels?: OrderSummaryPromoLabels;\n  /** Called on form submit. Skipped while `status=\"applying\"`. */\n  onApply: () => void;\n  /** Called with the new input value on every change. */\n  onValueChange: (value: string) => void;\n  /** Defaults to `\"idle\"`. */\n  status?: OrderSummaryPromoStatus;\n  /** Current input value. */\n  value: string;\n};\n\n/**\n * Props for {@link OrderSummary}.\n *\n * @public\n */\nexport type OrderSummaryProps = Omit<\n  React.ComponentPropsWithoutRef<\"section\">,\n  \"children\" | \"title\"\n> & {\n  /** Checkout button label. Defaults to `\"Checkout\"`. */\n  checkoutLabel?: string;\n  /** Sets `aria-busy`, shows a spinner and disables the checkout button. */\n  checkoutLoading?: boolean;\n  /** Density of the summary. Defaults to `\"desktop\"`. */\n  layout?: OrderSummaryLayout;\n  /** Rows above the total (subtotal, shipping, tax, …), in display order. */\n  lines: OrderSummaryLine[];\n  /** Called on a click of the checkout button. */\n  onCheckout?: () => void;\n  /** Promo code form. Not rendered when omitted. */\n  promo?: OrderSummaryPromo;\n  /** Heading that also names the section. No heading when omitted. */\n  title?: string;\n  /** Last row of the summary. */\n  total: OrderSummaryLine;\n};\n\ntype SummaryRowProps = {\n  emphasis?: boolean;\n  line: OrderSummaryLine;\n};\n\nfunction SummaryRow({\n  emphasis = false,\n  line,\n}: SummaryRowProps): React.JSX.Element {\n  return (\n    <div\n      className={cn(\n        \"flex items-baseline justify-between gap-4\",\n        emphasis && \"text-base font-semibold\",\n      )}\n    >\n      <dt className={cn(!emphasis && \"text-muted-foreground\")}>{line.label}</dt>\n      <dd className=\"text-right tabular-nums\">{line.value}</dd>\n    </div>\n  );\n}\n\ntype SummaryRowsProps = {\n  lines: OrderSummaryLine[];\n  total: OrderSummaryLine;\n};\n\nfunction SummaryRows({ lines, total }: SummaryRowsProps): React.JSX.Element {\n  return (\n    <dl className=\"flex flex-col gap-2 text-sm\">\n      {lines.map((line, index) => (\n        <SummaryRow key={`${String(index)}-${line.label}`} line={line} />\n      ))}\n      <SummaryRow emphasis line={total} />\n    </dl>\n  );\n}\n\ntype PromoFormProps = {\n  promo: OrderSummaryPromo;\n};\n\nfunction PromoForm({ promo }: PromoFormProps): React.JSX.Element {\n  const baseId = React.useId();\n  const inputId = `${baseId}-promo`;\n  const hintId = `${baseId}-promo-hint`;\n  const status = promo.status ?? \"idle\";\n  const isApplying = status === \"applying\";\n  const isError = status === \"error\";\n  const errorWords = promo.labels?.error ?? DEFAULT_PROMO_ERROR;\n  const hint = promo.hint || (isError ? errorWords : \"\");\n\n  const handleSubmit = (event: React.SyntheticEvent<HTMLFormElement>): void => {\n    event.preventDefault();\n    if (!isApplying) {\n      promo.onApply();\n    }\n  };\n\n  return (\n    <form className=\"flex flex-col gap-2\" onSubmit={handleSubmit}>\n      <Label htmlFor={inputId}>\n        {promo.labels?.title ?? DEFAULT_PROMO_TITLE}\n      </Label>\n      <div className=\"flex gap-2\">\n        <Input\n          aria-describedby={hint ? hintId : undefined}\n          aria-invalid={isError ? true : undefined}\n          className={cn(isError && \"border-destructive\")}\n          id={inputId}\n          onChange={(event) => {\n            promo.onValueChange(event.currentTarget.value);\n          }}\n          placeholder={promo.labels?.placeholder}\n          value={promo.value}\n        />\n        <Button disabled={isApplying} type=\"submit\" variant=\"outline\">\n          {isApplying ? <Spinner size=\"sm\" /> : null}\n          {promo.labels?.apply ?? DEFAULT_APPLY_LABEL}\n        </Button>\n      </div>\n      {hint ? (\n        <p\n          className={cn(\n            \"text-xs\",\n            isError ? \"text-destructive\" : \"text-muted-foreground\",\n          )}\n          id={hintId}\n          role={isError ? \"alert\" : undefined}\n        >\n          {hint}\n        </p>\n      ) : null}\n    </form>\n  );\n}\n\ntype CheckoutButtonProps = {\n  label: string;\n  loading: boolean;\n  onCheckout?: () => void;\n};\n\nfunction CheckoutButton({\n  label,\n  loading,\n  onCheckout,\n}: CheckoutButtonProps): React.JSX.Element {\n  return (\n    <Button\n      aria-busy={loading ? true : undefined}\n      className=\"w-full\"\n      disabled={loading}\n      onClick={() => {\n        onCheckout?.();\n      }}\n      size=\"lg\"\n      type=\"button\"\n    >\n      {loading ? <Spinner size=\"sm\" /> : null}\n      {label}\n    </Button>\n  );\n}\n\n/**\n * Order summary: priced rows, a total, an optional promo code form and a\n * checkout button. Pure presentation: the host formats every value, owns the\n * cart and the promo state, and reacts to `onApply` and `onCheckout`.\n *\n * @example\n * ```tsx\n * <OrderSummary\n *   title=\"Order summary\"\n *   lines={[\n *     { label: \"Subtotal\", value: \"$120.00\" },\n *     { label: \"Shipping\", value: \"$8.00\" },\n *   ]}\n *   total={{ label: \"Total\", value: \"$128.00\" }}\n *   onCheckout={startCheckout}\n * />\n * ```\n *\n * @public\n */\nexport const OrderSummary = ({\n  checkoutLabel = DEFAULT_CHECKOUT_LABEL,\n  checkoutLoading = false,\n  className,\n  layout = \"desktop\",\n  lines,\n  onCheckout,\n  promo,\n  ref: reference,\n  title,\n  total,\n  ...props\n}: OrderSummaryProps & { ref?: React.Ref<HTMLElement> }): React.JSX.Element => {\n  const titleId = React.useId();\n\n  return (\n    <section\n      aria-labelledby={title ? titleId : undefined}\n      className={className}\n      data-layout={layout}\n      ref={reference}\n      {...props}\n    >\n      <Card className={surfaceVariants({ layout })}>\n        {title ? (\n          <h2\n            className=\"text-lg font-semibold leading-none tracking-tight\"\n            id={titleId}\n          >\n            {title}\n          </h2>\n        ) : null}\n        <SummaryRows lines={lines} total={total} />\n        <Separator />\n        {promo ? <PromoForm promo={promo} /> : null}\n        <CheckoutButton\n          label={checkoutLabel}\n          loading={checkoutLoading}\n          onCheckout={onCheckout}\n        />\n      </Card>\n    </section>\n  );\n};\n\nOrderSummary.displayName = \"OrderSummary\";\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component",
  "version": "0.3.0",
  "stability": "stable",
  "platforms": [
    "web"
  ]
}
