{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "tw-tearsheet",
  "type": "registry:ui",
  "title": "react-tw/tearsheet.tsx",
  "description": "Carbon for IBM Products `Tearsheet` as React + Tailwind, the look and rules of `<ui-tearsheet>`: a large modal rising Copied from @purpurabog/ui 2.3.0; the package is the maintained source, this copy is yours to change. Icons from the Carbon Design System (Apache-2.0, © IBM Corp.; see THIRD-PARTY-NOTICES.md in the package).",
  "dependencies": [
    "@purpurabog/ui@^2.3.0"
  ],
  "registryDependencies": [
    "@kanok-ui/core-action-set",
    "@kanok-ui/tw-action-set",
    "@kanok-ui/tw-i18n",
    "@kanok-ui/tw-layer",
    "@kanok-ui/tw-theme",
    "@kanok-ui/tw-utils"
  ],
  "files": [
    {
      "path": "src/react-tw/tearsheet.tsx",
      "type": "registry:ui",
      "target": "components/ui/react-tw/tearsheet.tsx",
      "content": "import { useEffect, useLayoutEffect, useRef, type ReactNode, type ComponentProps } from 'react';\nimport { willStack } from '../core/action-set.ts';\nimport { Tearsheet as TearsheetPrimitive } from '@purpurabog/ui/headless';\nimport { ActionButtons, actionRowClasses, stackedClasses, type ActionSetAction } from './action-set.tsx';\nimport { Layer } from './layer.tsx';\nimport { useTranslation } from './lib/i18n.ts';\nimport { cn } from './lib/utils.ts';\n\nexport type TearsheetCloseReason = TearsheetPrimitive.TearsheetCloseReason;\n\nconst MAX_ACTIONS = 4;\n\nexport interface TearsheetProps extends Omit<ComponentProps<'dialog'>, 'title' | 'children' | 'open' | 'onClose'> {\n    open: boolean;\n    /** Esc, × or a click outside asked to close; set `open` to false to close (Carbon `onClose`). */\n    onRequestClose?: (reason: TearsheetCloseReason) => void;\n    /** After it closed: the sheet has sunk back (Lit `ui-close`; not Carbon's `onClose`, which is `onRequestClose`). */\n    onClose?: () => void;\n    /** narrow: Carbon's small modal; wide: all but 64px each side (Carbon React's `Tearsheet`; `TearsheetNarrow` is narrow). */\n    width?: 'narrow' | 'wide';\n    /** normal 90px from the top, lower 130px (default lower for a narrow one). */\n    verticalPosition?: 'normal' | 'lower';\n    /** The heading (Carbon `title`). */\n    heading?: string;\n    label?: ReactNode;\n    description?: ReactNode;\n    /** Small buttons in the header (wide). */\n    headerActions?: ReactNode;\n    /** Tabs under the header text (wide; Carbon `navigation`). */\n    navigation?: ReactNode;\n    /** A side pane beside the content (wide). */\n    influencer?: ReactNode;\n    influencerPosition?: 'left' | 'right';\n    influencerWidth?: 'narrow' | 'wide';\n    /** Up to four action buttons, as Carbon's ActionSet (64px; 80px wide). */\n    actions?: readonly ActionSetAction[];\n    /** Show the × even with actions (it always shows without them). */\n    hasCloseIcon?: boolean;\n    closeIconDescription?: string;\n    preventCloseOnClickOutside?: boolean;\n    /** What takes focus on open (Carbon `selectorPrimaryFocus`), else × or the first control. */\n    selectorInitialFocus?: string;\n    lang?: string;\n    children?: ReactNode;\n}\n\n// literal classes, so Tailwind sees them\nconst BACKDROP: Record<string, string> = {\n    '1-of-2': 'backdrop:opacity-[0.67]',\n    '1-of-3': 'backdrop:opacity-[0.22]',\n    '2-of-2': 'backdrop:opacity-50',\n    '2-of-3': 'backdrop:opacity-50',\n    '3-of-3': 'backdrop:opacity-50',\n};\nconst SCALE: Record<string, string> = { '1-of-2': 'scale-95 [--_stack-room:16px]', '2-of-3': 'scale-95 [--_stack-room:16px]', '1-of-3': 'scale-90 [--_stack-room:32px]' };\n\ninterface HeaderProps {\n    wide: boolean;\n    heading?: string;\n    label?: ReactNode;\n    description?: ReactNode;\n    headerActions?: ReactNode;\n    navigation?: ReactNode;\n    closeLabel?: string;\n}\n\n/** The label, heading, description, header actions and tabs (wide), and the ×. */\nfunction TearsheetHeader({ wide, heading, label, description, headerActions, navigation, closeLabel }: Readonly<HeaderProps>) {\n    return (\n        <div className={cn('relative flex-none border-0 border-b border-solid border-border-subtle-2 bg-(--_surface)', wide ? 'px-8 py-6' : 'p-4', wide && closeLabel && 'pe-20', wide && navigation && 'pb-0')}>\n            {label ? <p className=\"m-0 mb-1 text-ink-muted type-caption\">{label}</p> : null}\n            {heading ? <TearsheetPrimitive.Title className={cn('m-0 pe-[calc(20%-3rem)]', wide ? 'type-heading-04' : 'type-heading-03')}>{heading}</TearsheetPrimitive.Title> : null}\n            {description ? <div className={cn('inline-flex type-body-sm', wide ? 'mt-4 max-w-full min-[42rem]:max-w-[60%]' : 'mt-2 max-w-[80%]')}>{description}</div> : null}\n            {wide && headerActions ? <div className=\"absolute end-40 top-4 z-[1] flex gap-2\">{headerActions}</div> : null}\n            {wide && navigation ? <div className=\"mt-3\">{navigation}</div> : null}\n            {closeLabel ? (\n                <TearsheetPrimitive.Close\n                    data-part=\"close\"\n                    aria-label={closeLabel}\n                    className=\"absolute end-4 top-2 z-[1] grid size-[var(--ui-close-size,48px)] m-[var(--ui-close-inset,0px)] rounded-[var(--ui-icon-control-radius,0px)] cursor-pointer place-items-center border-0 bg-transparent p-0 text-ink hover:bg-ghost-hover focus-visible:focus-ring\"\n                >\n                    <svg viewBox=\"0 0 32 32\" aria-hidden=\"true\" className=\"size-5 fill-current\">\n                        <path d=\"M24 9.4 22.6 8 16 14.6 9.4 8 8 9.4l6.6 6.6L8 22.6 9.4 24l6.6-6.6 6.6 6.6 1.4-1.4-6.6-6.6z\" />\n                    </svg>\n                </TearsheetPrimitive.Close>\n            ) : null}\n        </div>\n    );\n}\n\nfunction Influencer({ shown, placement, wide, children }: Readonly<{ shown: boolean; placement: 'left' | 'right'; wide: boolean; children: ReactNode }>) {\n    if (!shown) return null;\n    return <div className={cn('box-border flex-[0_0_257px] overflow-y-auto border-0 border-solid border-border-subtle-2 max-[41.98rem]:basis-auto', wide && 'basis-[321px]', placement === 'left' ? 'border-e' : 'border-s bg-(--_surface)')}>{children}</div>;\n}\n\n/** The sheet: its width (wide / Carbon's small modal), how far from the top, and its place in a stack. */\nconst containerClasses = (wide: boolean, lower: boolean, place: string | undefined) =>\n    cn(\n        // the sheet's surface; a design can raise it and outline it (--ui-overlay-background / -border; Carbon: layer 1, subtle border)\n        'relative box-border flex h-full w-full flex-col [border:var(--ui-overlay-border,1px_solid_var(--ui-border-subtle-2))] [--_surface:var(--ui-overlay-background,var(--ui-layer-1))] bg-(--_surface) outline-none max-[41.98rem]:max-h-full',\n        // the top corners can round (--ui-tearsheet-radius; Carbon: square)\n        'rounded-ss-[var(--ui-tearsheet-radius,0px)] rounded-se-[var(--ui-tearsheet-radius,0px)]',\n        lower ? 'max-h-[calc(100%_-_130px_+_var(--_stack-room,0px))]' : 'max-h-[calc(100%_-_90px_+_var(--_stack-room,0px))]',\n        wide ? 'min-[42rem]:w-[calc(100%-128px)]' : 'min-[42rem]:w-[60%] min-[66rem]:w-[42%] min-[82rem]:w-[36%]',\n        'motion-safe:transition-[transform,max-height] motion-safe:duration-(--ui-duration-moderate-02) motion-safe:ease-[cubic-bezier(0,0,0.3,1)]',\n        place && SCALE[place],\n        'forced-colors:border-[CanvasText]',\n    );\n\ninterface BodyProps {\n    wide: boolean;\n    influencer?: ReactNode;\n    influencerPosition: 'left' | 'right';\n    influencerWidth: 'narrow' | 'wide';\n    actions: readonly ActionSetAction[];\n    children?: ReactNode;\n}\n\n/** The influencer pane, the content (on the background when wide, layer 1 when narrow) and the actions. */\nfunction TearsheetBody({ wide, influencer, influencerPosition, influencerWidth, actions, children }: Readonly<BodyProps>) {\n    const actionSize = wide ? '2xl' : 'xl';\n    const stacking = willStack(actionSize, actions.length);\n    const pane = (placement: 'left' | 'right') =>\n        influencerPosition === placement ? (\n            <Influencer shown={wide && !!influencer} placement={placement} wide={influencerWidth === 'wide'}>\n                {influencer}\n            </Influencer>\n        ) : null;\n    return (\n        <div className=\"flex min-h-0 flex-1 overflow-hidden\">\n            {pane('left')}\n            <div className=\"grid min-w-0 grow grid-cols-[100%] grid-rows-[1fr_auto]\">\n                <div className={cn('flex min-h-0 min-w-0', wide ? 'bg-canvas' : 'bg-(--_surface)')}>\n                    <Layer level={wide ? 0 : 1} className=\"min-w-0 grow overflow-auto\">\n                        {children}\n                    </Layer>\n                    {pane('right')}\n                </div>\n                {actions.length > 0 ? (\n                    <div className=\"min-h-fit overflow-x-auto\">\n                        <div className={cn('flex min-w-full justify-end border-0 border-t border-solid border-border-subtle-2', actionRowClasses, wide ? 'bg-canvas [--ui-field-height-lg:80px]' : 'bg-(--_surface) [--ui-field-height-lg:64px]', stacking && stackedClasses)}>\n                            <ActionButtons actions={actions} size={actionSize} stacking={stacking} />\n                        </div>\n                    </div>\n                ) : null}\n            </div>\n        </div>\n    );\n}\n\n/** Inside the Root, mounted while the sheet shows: `onClose` once it has sunk back (not when the whole sheet goes away open). */\nfunction ClosedNotice({ onClose }: Readonly<{ onClose?: () => void }>) {\n    const { closing } = TearsheetPrimitive.useTearsheetContext();\n    const latest = useRef({ closing, onClose });\n    useLayoutEffect(() => {\n        latest.current = { closing, onClose };\n    });\n    useEffect(\n        () => () => {\n            if (latest.current.closing) latest.current.onClose?.();\n        },\n        [],\n    );\n    return null;\n}\n\n/**\n * Carbon for IBM Products `Tearsheet` as React + Tailwind, the look and rules of `<ui-tearsheet>`: a large modal rising\n * from the bottom over the dimmed page — `wide` (Carbon React's default; all but 64px each side, 90px from the top)\n * or `narrow` (Carbon's small modal, 130px from the top; `TearsheetNarrow`). Header: label, heading, description,\n * and when wide header actions, tabs (`navigation`) and an `influencer` pane. Content on the background (wide) or\n * the layer (narrow). `actions` as Carbon's ActionSet. Esc, × and a click outside ask to close; Tab stays inside;\n * a newer tearsheet over it scales it back and dims the page less, up to three deep. `onClose` follows once it has sunk back.\n */\nexport function Tearsheet({ open, onRequestClose, onClose, preventCloseOnClickOutside = false, selectorInitialFocus, ...rest }: Readonly<TearsheetProps>) {\n    return (\n        <TearsheetPrimitive.Root open={open} onRequestClose={onRequestClose} closeOnOutsideClick={!preventCloseOnClickOutside} selectorInitialFocus={selectorInitialFocus}>\n            <ClosedNotice onClose={onClose} />\n            <TearsheetDialog {...rest} />\n        </TearsheetPrimitive.Root>\n    );\n}\n\ntype DialogProps = Omit<TearsheetProps, 'open' | 'onRequestClose' | 'onClose' | 'preventCloseOnClickOutside' | 'selectorInitialFocus'>;\n\n/** The dialog and its sheet, scaled back and dimmed less under newer tearsheets. */\nfunction TearsheetDialog({\n    width = 'wide',\n    verticalPosition,\n    heading,\n    label,\n    description,\n    headerActions,\n    navigation,\n    influencer,\n    influencerPosition = 'right',\n    influencerWidth = 'narrow',\n    actions = [],\n    hasCloseIcon = false,\n    closeIconDescription,\n    lang,\n    className,\n    children,\n    ...rest\n}: Readonly<DialogProps>) {\n    const t = useTranslation(lang).common;\n    const place = TearsheetPrimitive.useTearsheetContext().stackPlace;\n    const wide = width === 'wide';\n    const lower = (verticalPosition ?? (wide ? 'normal' : 'lower')) === 'lower';\n    const shownActions = actions.slice(0, MAX_ACTIONS);\n    const closeLabel = hasCloseIcon || shownActions.length === 0 ? (closeIconDescription ?? t.close) : undefined;\n\n    return (\n        <TearsheetPrimitive.Dialog\n            {...rest}\n            className={cn(\n                'm-0 box-border h-full max-h-none w-full max-w-none flex-col items-center justify-end border-0 bg-transparent p-0 font-sans text-ink open:flex backdrop:bg-overlay',\n                place && BACKDROP[place],\n                className,\n            )}\n        >\n            <TearsheetPrimitive.Sheet className={containerClasses(wide, lower, place)}>\n                <TearsheetHeader wide={wide} heading={heading} label={label} description={description} headerActions={headerActions} navigation={navigation} closeLabel={closeLabel} />\n                <TearsheetBody wide={wide} influencer={influencer} influencerPosition={influencerPosition} influencerWidth={influencerWidth} actions={shownActions}>\n                    {children}\n                </TearsheetBody>\n            </TearsheetPrimitive.Sheet>\n        </TearsheetPrimitive.Dialog>\n    );\n}\n\n/** Carbon React's `TearsheetNarrow`: a narrow Tearsheet. */\nexport function TearsheetNarrow(props: Readonly<Omit<TearsheetProps, 'width' | 'headerActions' | 'navigation' | 'influencer'>>) {\n    return <Tearsheet {...props} width=\"narrow\" />;\n}\n"
    }
  ]
}
