{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "consent",
  "type": "registry:ui",
  "title": "Consent",
  "description": "Asks permission before something is switched on or shared: what is asked for, item by item, each optional one with its own switch that starts off, and two equal buttons to allow or decline. As a sheet (ConsentSheet) or in the page (ConsentPanel).",
  "categories": [
    "ui"
  ],
  "dependencies": [
    "lucide-react",
    "radix-ui"
  ],
  "registryDependencies": [
    "@rivier-ui/theme",
    "@rivier-ui/button",
    "@rivier-ui/cn",
    "@rivier-ui/sheet",
    "@rivier-ui/switch",
    "@rivier-ui/use-controllable-state"
  ],
  "files": [
    {
      "path": "registry/ui/consent.tsx",
      "type": "registry:ui",
      "target": "components/ui/consent.tsx",
      "content": "\"use client\";\n/**\n * @name consent\n * @category ui\n * @description Asks permission before something is switched on or shared: what is asked for, item by item, each optional one with its own switch that starts off, and two equal buttons to allow or decline. As a sheet (ConsentSheet) or in the page (ConsentPanel).\n * @when Before sharing a person's data with another party, connecting an account, turning on an assistant or a feature that reads their content, or using a device permission. One consent per purpose, in plain words, at the moment it is needed. Nothing is pre-ticked and declining is as easy as allowing. For a yes or no about an action the person just started (delete, leave) use dialog; for a cookie notice use the same parts in a ConsentPanel.\n * @example\n * <ConsentSheet side=\"bottom\" defaultValue={[]} onAccept={(granted) => connect(granted)} onDecline={skip} onDismiss={skip}>\n *   <ConsentTrigger asChild><Button>Connect calendar</Button></ConsentTrigger>\n *   <ConsentContent>\n *     <ConsentHeader>\n *       <ConsentTitle>Let the planner see your calendar?</ConsentTitle>\n *       <ConsentDescription>It is used to avoid times when you are busy. You can stop this in Settings at any time.</ConsentDescription>\n *     </ConsentHeader>\n *     <ConsentScopes>\n *       <ConsentScope value=\"busy\" title=\"Busy times\" description=\"When you are busy, without what the events are.\" required requiredLabel=\"Always needed\" />\n *       <ConsentScope value=\"titles\" title=\"Event names\" description=\"So that suggestions can mention what you have planned.\" />\n *     </ConsentScopes>\n *     <ConsentDetails summary=\"How this is stored\">Kept for 30 days, never sold, deleted when you disconnect.</ConsentDetails>\n *     <ConsentFooter>\n *       <ConsentDecline>Do not allow</ConsentDecline>\n *       <ConsentAccept>Allow</ConsentAccept>\n *     </ConsentFooter>\n *   </ConsentContent>\n * </ConsentSheet>\n */\nimport { Collapsible } from \"radix-ui\";\nimport { ChevronDown } from \"lucide-react\";\nimport { createContext, use, useId, type ComponentProps, type ReactNode } from \"react\";\nimport { Button, type ButtonProps } from \"@/components/ui/button\";\nimport { Sheet, SheetContent, SheetDescription, SheetTitle, SheetTrigger, type SheetContentProps, type SheetSide } from \"@/components/ui/sheet\";\nimport { Switch } from \"@/components/ui/switch\";\nimport { cn } from \"@/lib/cn\";\nimport { useControllableState } from \"@/lib/use-controllable-state\";\n\ntype ConsentContextValue = {\n  /** The optional scopes that are switched on. */\n  granted: string[];\n  setGranted: (next: string[]) => void;\n  /** True inside ConsentSheet: the title and description are the dialog's, and deciding closes it. */\n  sheet: boolean;\n  side: SheetSide;\n  accept: () => void;\n  decline: () => void;\n  dismiss: () => void;\n};\n\nconst ConsentContext = createContext<ConsentContextValue | null>(null);\n\nfunction useConsent(part: string): ConsentContextValue {\n  const context = use(ConsentContext);\n  if (!context) throw new Error(`<${part}> must be used inside <ConsentSheet> or <ConsentPanel>.`);\n  return context;\n}\n\ntype ConsentState = {\n  /** The optional scopes that are switched on (controlled). */\n  value?: string[];\n  /** The optional scopes switched on to begin with. Leave it empty: consent that is ticked in advance is not consent. */\n  defaultValue?: string[];\n  onValueChange?: (granted: string[]) => void;\n  /** The person allowed it. `granted` lists the optional scopes they switched on; required scopes are implied. */\n  onAccept?: (granted: string[]) => void;\n  /** The person declined. Nothing is granted, required scopes included. */\n  onDecline?: () => void;\n  /** The person left without deciding (Escape, the close button, \"not now\"). Nothing is granted and nothing is refused: it may be asked again. */\n  onDismiss?: () => void;\n};\n\nexport type ConsentSheetProps = ConsentState & {\n  open?: boolean;\n  defaultOpen?: boolean;\n  onOpenChange?: (open: boolean) => void;\n  /** The edge it comes from. \"bottom\" (default) suits phones; \"end\" suits wide screens. The project chooses, for example by a media query. */\n  side?: SheetSide;\n  children?: ReactNode;\n};\n\n/**\n * The consent as a modal sheet: focus is held inside it and returns to what opened it. Allowing or declining\n * closes it; closing it any other way is neither (`onDismiss`).\n */\nexport function ConsentSheet({ open, defaultOpen = false, onOpenChange, side = \"bottom\", value, defaultValue = [], onValueChange, onAccept, onDecline, onDismiss, children }: ConsentSheetProps) {\n  const [isOpen, setOpen] = useControllableState({ value: open, defaultValue: defaultOpen, onChange: onOpenChange });\n  const [granted, setGranted] = useControllableState({ value, defaultValue, onChange: onValueChange });\n  return (\n    <ConsentContext\n      value={{\n        granted,\n        setGranted,\n        sheet: true,\n        side,\n        accept: () => {\n          onAccept?.(granted);\n          setOpen(false);\n        },\n        decline: () => {\n          onDecline?.();\n          setOpen(false);\n        },\n        dismiss: () => {\n          onDismiss?.();\n          setOpen(false);\n        },\n      }}\n    >\n      <Sheet\n        open={isOpen}\n        onOpenChange={(next) => {\n          // Radix closes it only for Escape, the veil and the close button: none of them is a decision.\n          if (!next) onDismiss?.();\n          setOpen(next);\n        }}\n      >\n        {children}\n      </Sheet>\n    </ConsentContext>\n  );\n}\n\n/** The control that opens a ConsentSheet. Focus returns to it. */\nexport function ConsentTrigger(props: ComponentProps<typeof SheetTrigger>) {\n  return <SheetTrigger data-slot=\"consent-trigger\" {...props} />;\n}\n\n/** The sheet's panel. It takes the side set on ConsentSheet. */\nexport function ConsentContent({ className, side, ...props }: SheetContentProps) {\n  const context = useConsent(\"ConsentContent\");\n  const from = side ?? context.side;\n  return (\n    <SheetContent\n      data-slot=\"consent-content\"\n      side={from}\n      className={cn((from === \"bottom\" || from === \"top\") && \"mx-auto w-full max-w-xl sm:rounded-t-surface sm:border-x\", className)}\n      {...props}\n    />\n  );\n}\n\nexport type ConsentPanelProps = Omit<ComponentProps<\"section\">, \"defaultValue\"> & ConsentState;\n\n/** The consent in the page, not over it: in a settings screen, a set-up step, a conversation. Name it with `aria-labelledby` pointing at its ConsentTitle, or `aria-label`. */\nexport function ConsentPanel({ className, value, defaultValue = [], onValueChange, onAccept, onDecline, onDismiss, children, ...props }: ConsentPanelProps) {\n  const [granted, setGranted] = useControllableState({ value, defaultValue, onChange: onValueChange });\n  return (\n    <ConsentContext\n      value={{\n        granted,\n        setGranted,\n        sheet: false,\n        side: \"bottom\",\n        accept: () => onAccept?.(granted),\n        decline: () => onDecline?.(),\n        dismiss: () => onDismiss?.(),\n      }}\n    >\n      <section data-slot=\"consent-panel\" className={cn(\"flex w-full min-w-0 flex-col rounded-surface border bg-card text-card-foreground\", className)} {...props}>\n        {children}\n      </section>\n    </ConsentContext>\n  );\n}\n\n/** Leaves room at the end for the sheet's close button. */\nexport function ConsentHeader({ className, ...props }: ComponentProps<\"div\">) {\n  const { sheet } = useConsent(\"ConsentHeader\");\n  return <div data-slot=\"consent-header\" className={cn(\"flex flex-col gap-1.5 p-5 text-start\", sheet && \"pe-14\", className)} {...props} />;\n}\n\n/** The question, as a question: \"Let the planner see your calendar?\". In a sheet it names the dialog. */\nexport function ConsentTitle({ className, ...props }: ComponentProps<\"h2\">) {\n  const { sheet } = useConsent(\"ConsentTitle\");\n  const look = cn(\"text-base leading-tight font-medium\", className);\n  return sheet ? <SheetTitle data-slot=\"consent-title\" className={look} {...props} /> : <h2 data-slot=\"consent-title\" className={look} {...props} />;\n}\n\n/** Who gets what, what for, and how to stop it, in a sentence or two. In a sheet it describes the dialog. */\nexport function ConsentDescription({ className, ...props }: ComponentProps<\"p\">) {\n  const { sheet } = useConsent(\"ConsentDescription\");\n  const look = cn(\"text-sm text-muted-foreground\", className);\n  return sheet ? <SheetDescription data-slot=\"consent-description\" className={look} {...props} /> : <p data-slot=\"consent-description\" className={look} {...props} />;\n}\n\n/** The list of what is asked for. */\nexport function ConsentScopes({ className, ...props }: ComponentProps<\"ul\">) {\n  return <ul data-slot=\"consent-scopes\" className={cn(\"mx-5 flex flex-col divide-y divide-border border-y\", className)} {...props} />;\n}\n\nexport type ConsentScopeProps = Omit<ComponentProps<\"li\">, \"title\"> & {\n  /** The scope's identifier, as it appears in `value` and in `onAccept`. */\n  value: string;\n  /** What is asked for, in two or three words. */\n  title: ReactNode;\n  /** What is shared and why, plainly. */\n  description?: ReactNode;\n  /** The consent cannot be given without this one: it has no switch, and shows `requiredLabel` instead. */\n  required?: boolean;\n  /** The words for a required scope (\"Always needed\"). Translate them with the interface. */\n  requiredLabel?: ReactNode;\n  disabled?: boolean;\n};\n\n/**\n * One thing being asked for. An optional scope has a switch, off unless the person turns it on; its title\n * is the switch's label, so pressing the words works too. A required scope says so in words.\n */\nexport function ConsentScope({ value, title, description, required = false, requiredLabel = \"Required\", disabled = false, className, children, ...props }: ConsentScopeProps) {\n  const { granted, setGranted } = useConsent(\"ConsentScope\");\n  const id = useId();\n  const on = granted.includes(value);\n  return (\n    <li data-slot=\"consent-scope\" data-required={required ? \"\" : undefined} data-state={required || on ? \"on\" : \"off\"} className={cn(\"flex min-w-0 items-start gap-4 py-3.5\", className)} {...props}>\n      <div className=\"flex min-w-0 flex-1 flex-col gap-0.5\">\n        {required ? (\n          <span id={`${id}-title`} className=\"text-sm font-medium\">\n            {title}\n          </span>\n        ) : (\n          <label id={`${id}-title`} htmlFor={`${id}-switch`} className=\"text-sm font-medium\">\n            {title}\n          </label>\n        )}\n        {description ? (\n          <span id={`${id}-description`} className=\"text-sm text-muted-foreground\">\n            {description}\n          </span>\n        ) : null}\n        {children}\n      </div>\n      {required ? (\n        <span data-slot=\"consent-scope-required\" className=\"mt-0.5 shrink-0 text-xs font-medium text-muted-foreground\">\n          {requiredLabel}\n        </span>\n      ) : (\n        <Switch\n          id={`${id}-switch`}\n          className=\"mt-0.5\"\n          checked={on}\n          disabled={disabled}\n          aria-describedby={description ? `${id}-description` : undefined}\n          onCheckedChange={(checked) => setGranted(checked ? [...granted.filter((scope) => scope !== value), value] : granted.filter((scope) => scope !== value))}\n        />\n      )}\n    </li>\n  );\n}\n\nexport type ConsentDetailsProps = Omit<ComponentProps<typeof Collapsible.Root>, \"title\"> & {\n  /** The words of the button that shows the longer text (\"How this is stored\"). */\n  summary: ReactNode;\n};\n\n/** The longer text (retention, the other party's terms, legal wording), closed until asked for. */\nexport function ConsentDetails({ summary, className, children, ...props }: ConsentDetailsProps) {\n  return (\n    <Collapsible.Root data-slot=\"consent-details\" className={cn(\"group/details mx-5 min-w-0 text-sm\", className)} {...props}>\n      <Collapsible.Trigger className=\"flex min-h-10 w-full items-center justify-between gap-3 rounded-control py-2 text-start font-medium text-foreground underline-offset-4 hover:underline\">\n        {summary}\n        <ChevronDown\n          aria-hidden=\"true\"\n          strokeWidth={1.5}\n          className=\"size-4 shrink-0 text-muted-foreground transition-transform duration-base ease-out group-data-[state=open]/details:rotate-180 motion-reduce:transition-none\"\n        />\n      </Collapsible.Trigger>\n      <Collapsible.Content className=\"pb-3 text-muted-foreground\">{children}</Collapsible.Content>\n    </Collapsible.Root>\n  );\n}\n\n/**\n * Holds ConsentDecline and ConsentAccept side by side at equal width, and an optional ConsentNotNow under\n * them. Decline comes first in reading order; neither is styled as the easy way out.\n */\nexport function ConsentFooter({ className, ...props }: ComponentProps<\"div\">) {\n  return <div data-slot=\"consent-footer\" className={cn(\"mt-auto grid grid-cols-2 gap-2 p-5\", className)} {...props} />;\n}\n\ntype ConsentButtonProps = Omit<ButtonProps, \"asChild\">;\n\n/** Allows what is asked for, with the optional scopes as they are switched. Same look and size as ConsentDecline by default. */\nexport function ConsentAccept({ onClick, variant = \"outline\", className, ...props }: ConsentButtonProps) {\n  const { accept } = useConsent(\"ConsentAccept\");\n  return (\n    <Button\n      data-slot=\"consent-accept\"\n      variant={variant}\n      className={cn(\"h-auto min-h-(--control-h) w-full min-w-0 py-2 whitespace-normal\", className)}\n      onClick={(event) => {\n        onClick?.(event);\n        if (!event.defaultPrevented) accept();\n      }}\n      {...props}\n    />\n  );\n}\n\n/** Declines all of it. Same look and size as ConsentAccept by default: do not make it fainter. */\nexport function ConsentDecline({ onClick, variant = \"outline\", className, ...props }: ConsentButtonProps) {\n  const { decline } = useConsent(\"ConsentDecline\");\n  return (\n    <Button\n      data-slot=\"consent-decline\"\n      variant={variant}\n      className={cn(\"h-auto min-h-(--control-h) w-full min-w-0 py-2 whitespace-normal\", className)}\n      onClick={(event) => {\n        onClick?.(event);\n        if (!event.defaultPrevented) decline();\n      }}\n      {...props}\n    />\n  );\n}\n\n/** Puts the question off without answering it (`onDismiss`). Spans the footer under the two decisions. */\nexport function ConsentNotNow({ onClick, variant = \"ghost\", className, ...props }: ConsentButtonProps) {\n  const { dismiss } = useConsent(\"ConsentNotNow\");\n  return (\n    <Button\n      data-slot=\"consent-not-now\"\n      variant={variant}\n      className={cn(\"col-span-2 w-full\", className)}\n      onClick={(event) => {\n        onClick?.(event);\n        if (!event.defaultPrevented) dismiss();\n      }}\n      {...props}\n    />\n  );\n}\n"
    }
  ],
  "meta": {
    "category": "ui",
    "when": "Before sharing a person's data with another party, connecting an account, turning on an assistant or a feature that reads their content, or using a device permission. One consent per purpose, in plain words, at the moment it is needed. Nothing is pre-ticked and declining is as easy as allowing. For a yes or no about an action the person just started (delete, leave) use dialog; for a cookie notice use the same parts in a ConsentPanel.",
    "example": "<ConsentSheet side=\"bottom\" defaultValue={[]} onAccept={(granted) => connect(granted)} onDecline={skip} onDismiss={skip}>\n  <ConsentTrigger asChild><Button>Connect calendar</Button></ConsentTrigger>\n  <ConsentContent>\n    <ConsentHeader>\n      <ConsentTitle>Let the planner see your calendar?</ConsentTitle>\n      <ConsentDescription>It is used to avoid times when you are busy. You can stop this in Settings at any time.</ConsentDescription>\n    </ConsentHeader>\n    <ConsentScopes>\n      <ConsentScope value=\"busy\" title=\"Busy times\" description=\"When you are busy, without what the events are.\" required requiredLabel=\"Always needed\" />\n      <ConsentScope value=\"titles\" title=\"Event names\" description=\"So that suggestions can mention what you have planned.\" />\n    </ConsentScopes>\n    <ConsentDetails summary=\"How this is stored\">Kept for 30 days, never sold, deleted when you disconnect.</ConsentDetails>\n    <ConsentFooter>\n      <ConsentDecline>Do not allow</ConsentDecline>\n      <ConsentAccept>Allow</ConsentAccept>\n    </ConsentFooter>\n  </ConsentContent>\n</ConsentSheet>",
    "client": true
  }
}
