{"version":3,"file":"chunk-slots.mjs","names":[],"sources":["../../src/util/slots.ts"],"sourcesContent":["//\n// Copyright 2022 DXOS.org\n//\n\nimport {\n  Children,\n  type CSSProperties,\n  type ForwardedRef,\n  type ForwardRefExoticComponent,\n  type HTMLAttributes,\n  type ReactNode,\n  type RefAttributes,\n  createElement,\n  forwardRef,\n  isValidElement,\n} from 'react';\n\nimport { log } from '@dxos/log';\nimport { mx } from '@dxos/ui-theme';\nimport { type ComposableProps, type SlottableProps, type ThemedClassName } from '@dxos/ui-types';\n\n/**\n * Reconciles className properties from a parent slot.\n * - `className` is injected at runtime by the `asChild` merge; it is absent from\n *   `ComposableProps` so consumers cannot pass it, hence the widened parameter type here.\n * - `classNames` is the consumer-facing prop for theming overrides.\n * Use `composableProps` to reconcile both into a single `className`.\n */\nexport const composableProps = <P extends HTMLElement = HTMLElement>(\n  { className, classNames, role, style, ...props }: ComposableProps & { className?: string },\n  { classNames: defaultClassNames, ...defaults }: ThemedClassName<Partial<HTMLAttributes<P>>> | undefined = {},\n) => ({\n  // Default props.\n  ...(defaults as object),\n\n  // Spread supplied props.\n  ...props,\n\n  // No `role` default. On a div it is a no-op, so the only elements it ever reached were the\n  // semantic ones — `Card.Header` renders a <header>, `Main.Content` a <main> — where it stripped\n  // the landmark. A part that genuinely wants its element ignored passes the role itself\n  // (`role=''` included, so the undefined check rather than truthiness).\n  ...((role ?? defaults.role) !== undefined ? { role: role ?? defaults.role } : null),\n\n  // Merge styles.\n  style: { ...defaults.style, ...style } as CSSProperties,\n\n  // Compose classnames.\n  className: mx(defaultClassNames, className, classNames),\n});\n\n/** Symbol used to mark components created by `composable()` or `slottable()`. */\nconst COMPOSABLE = Symbol.for('dxos.composable');\n\n/**\n * Factory for slottable components.\n * The implementation receives full `HTMLAttributes<E>` so it can destructure `role`, `style`, etc.\n * Consumers see only `SlottableProps<P>` — a narrow type exposing `classNames`, `className`,\n * `children`, `asChild`, and the custom props `P`.\n *\n * @example\n * ```tsx\n * const MyPanel = slottable<HTMLDivElement, { border?: boolean }>(\n *   ({ children, asChild, border, ...props }, forwardedRef) => (\n *     <ark.div asChild={asChild} {...composableProps(props, { classNames: border && 'border' })} ref={forwardedRef}>\n *       {children}\n *     </ark.div>\n *   ),\n * );\n * ```\n */\nexport function slottable<E extends HTMLElement, P extends object = {}>(\n  render: (props: SlottableProps<P> & HTMLAttributes<E>, forwardedRef: ForwardedRef<E>) => ReactNode,\n): ForwardRefExoticComponent<SlottableProps<P> & RefAttributes<E>> {\n  const wrapped = (props: SlottableProps<P> & HTMLAttributes<E>, forwardedRef: ForwardedRef<E>) => {\n    let warn = false;\n    // Dev-only: the check walks children on every render of every `asChild` part, and the marker it\n    // paints is a developer diagnostic, not a product affordance.\n    if (process.env.NODE_ENV !== 'production' && props.asChild) {\n      try {\n        const child = Children.only(props.children);\n        if (isValidElement(child) && typeof child.type !== 'string' && !(child.type as any)[COMPOSABLE]) {\n          warn = true;\n          log.warn('slot child is not composable; create it with composable() or slottable()', {\n            child: (child.type as any).displayName ?? (child.type as any).name,\n          });\n        }\n      } catch {\n        // Children.only throws if not exactly one child — `asChild` renders nothing in that case.\n      }\n    }\n\n    const result = render(props, forwardedRef);\n    if (warn) {\n      // The marker cannot go on the rendered element: under `asChild` the class is merged into the\n      // very child that is dropping props — the bug being flagged\n      // swallows its own diagnostic. So it goes on a wrapper, which `dx-slot-warning` renders as\n      // `display: contents` so it adds no layout box; the outline is drawn on its children instead.\n      return createElement('div', { role: 'none', className: 'dx-slot-warning' }, result);\n    }\n\n    return result;\n  };\n\n  const component = forwardRef(wrapped as any) as any;\n  (component as any)[COMPOSABLE] = true;\n  return component;\n}\n\n/**\n * Factory for composable (leaf) components.\n * The implementation receives full `HTMLAttributes<E>` so it can destructure `role`, `style`, etc.\n * Consumers see only `ComposableProps<P>` — a narrow type exposing `classNames`, `className`,\n * `children`, and the custom props `P`.\n *\n * For generic components, use `any` for the type parameter inside `composable` and\n * cast the result to restore the generic signature for consumers.\n *\n * @example\n * ```tsx\n * const Leaf = composable<HTMLButtonElement>(({ children, ...props }, forwardedRef) => {\n *   return (\n *     <button {...composableProps(props, { classNames: 'btn' })} ref={forwardedRef}>\n *       {children}\n *     </button>\n *   );\n * });\n * ```\n */\nexport function composable<E extends HTMLElement, P extends object = {}>(\n  render: (props: ComposableProps<P> & HTMLAttributes<E>, forwardedRef: ForwardedRef<E>) => ReactNode,\n): ForwardRefExoticComponent<ComposableProps<P> & RefAttributes<E>> {\n  const component = forwardRef(render as any) as any;\n  (component as any)[COMPOSABLE] = true;\n  return component;\n}\n"],"mappings":";;;;;;;;;;;;AA4BA,IAAa,mBACX,EAAE,WAAW,YAAY,MAAM,OAAO,GAAG,SACzC,EAAE,YAAY,mBAAmB,GAAG,aAAsE,CAAC,OACvG;CAEJ,GAAI;CAGJ,GAAG;CAMH,IAAK,QAAQ,SAAS,UAAU,KAAA,IAAY,EAAE,MAAM,QAAQ,SAAS,KAAK,IAAI;CAG9E,OAAO;EAAE,GAAG,SAAS;EAAO,GAAG;CAAM;CAGrC,WAAW,GAAG,mBAAmB,WAAW,UAAU;AACxD;;AAGA,IAAM,aAAa,OAAO,IAAI,iBAAiB;;;;;;;;;;;;;;;;;;AAmB/C,SAAgB,UACd,QACiE;CACjE,MAAM,WAAW,OAA8C,iBAAkC;EAC/F,IAAI,OAAO;EAGX,IAAA,QAAA,IAAA,aAA6B,gBAAgB,MAAM,SACjD,IAAI;GACF,MAAM,QAAQ,SAAS,KAAK,MAAM,QAAQ;GAC1C,IAAI,eAAe,KAAK,KAAK,OAAO,MAAM,SAAS,YAAY,CAAE,MAAM,KAAa,aAAa;IAC/F,OAAO;IACP,IAAI,KAAK,4EAA4E,EACnF,OAAQ,MAAM,KAAa,eAAgB,MAAM,KAAa,KAChE,GAAA;KAAA,YAAA;KAAA,GAAA;KAAA,GAAA;KAAA,GAAA;IAAA,CAAC;GACH;EACF,QAAQ,CAER;EAGF,MAAM,SAAS,OAAO,OAAO,YAAY;EACzC,IAAI,MAKF,OAAO,cAAc,OAAO;GAAE,MAAM;GAAQ,WAAW;EAAkB,GAAG,MAAM;EAGpF,OAAO;CACT;CAEA,MAAM,YAAY,WAAW,OAAc;CAC3C,UAAmB,cAAc;CACjC,OAAO;AACT;;;;;;;;;;;;;;;;;;;;;AAsBA,SAAgB,WACd,QACkE;CAClE,MAAM,YAAY,WAAW,MAAa;CAC1C,UAAmB,cAAc;CACjC,OAAO;AACT"}