{"version":3,"file":"useSlots.cjs","names":["useRef","collectSlots","isDifferentTree","renderSlotContent","SLOT_COMPONENT"],"sources":["../../src/Slots/useSlots.tsx"],"sourcesContent":["import { cloneElement, isValidElement, useCallback, useState, type ReactNode, useEffect } from 'react';\nimport { useRef } from '../useRef';\nimport { Slots } from './Slots';\nimport { markComponent, SLOT_COMPONENT } from './constants';\nimport { collectSlots, isDifferentTree, renderSlotContent } from './utils';\nimport type { SlotProps } from './types';\n\n/**\n * Collects default children and named `Template` children into renderable slots.\n *\n * The returned `Slot` component renders default content when no `name` is\n * supplied, renders fallback children when a named slot is missing, supports\n * `props` injection for element/function slot content, and supports `map` for\n * custom wrapping or transformation.\n *\n * @param defaultNodes - Children to scan for default content and `Template` markers.\n * @returns The slot boundary component and the collected slot renderer.\n *\n * @example\n * ```tsx\n * function Panel({ children }: { children: React.ReactNode }) {\n *   const { Slot } = useSlots(children);\n *\n *   return (\n *     <>\n *       <Slot name=\"title\" />\n *       <Slot />\n *     </>\n *   );\n * }\n * ```\n */\nexport function useSlots(defaultNodes: ReactNode) {\n  const prevDefaultNodes = useRef<ReactNode>(defaultNodes);\n  const [slots, setSlots] = useState(() => collectSlots(defaultNodes));\n\n  useEffect(() => {\n    if (isDifferentTree(prevDefaultNodes.current, defaultNodes)) {\n      setSlots(collectSlots(defaultNodes));\n      prevDefaultNodes.current = defaultNodes;\n    }\n  }, [defaultNodes]);\n\n  const Slot = useCallback(function Slot(props: SlotProps) {\n    const { name = 'default', children, props: slotProps, map } = props;\n    const slotContent = slots[name] ? Array.isArray(slots[name]) ? slots[name] : [slots[name]] : null;\n\n    if (slotContent) {\n      return slotContent.map((child, i) => {\n        const rendered = renderSlotContent(child, i, slotProps, map);\n\n        if (isValidElement(rendered) && rendered.key == null) {\n          return cloneElement(rendered, { key: i });\n        }\n\n        return rendered;\n      });\n    }\n\n    return children ? <>{children}</> : null;\n  }, [slots]);\n\n  markComponent(Slot, SLOT_COMPONENT);\n\n  return { Slots, Slot };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgCA,SAAgB,SAAS,cAAyB;CAChD,MAAM,mBAAmBA,sBAAAA,OAAkB,YAAY;CACvD,MAAM,CAAC,OAAO,aAAA,GAAA,MAAA,SAAA,OAA2BC,iCAAAA,aAAa,YAAY,CAAC;CAEnE,CAAA,GAAA,MAAA,UAAA,OAAgB;EACd,IAAIC,oCAAAA,gBAAgB,iBAAiB,SAAS,YAAY,GAAG;GAC3D,SAASD,iCAAAA,aAAa,YAAY,CAAC;GACnC,iBAAiB,UAAU;EAC7B;CACF,GAAG,CAAC,YAAY,CAAC;CAEjB,MAAM,QAAA,GAAA,MAAA,YAAA,CAAmB,SAAS,KAAK,OAAkB;EACvD,MAAM,EAAE,OAAO,WAAW,UAAU,OAAO,WAAW,QAAQ;EAC9D,MAAM,cAAc,MAAM,QAAQ,MAAM,QAAQ,MAAM,KAAK,IAAI,MAAM,QAAQ,CAAC,MAAM,KAAK,IAAI;EAE7F,IAAI,aACF,OAAO,YAAY,KAAK,OAAO,MAAM;GACnC,MAAM,WAAWE,sCAAAA,kBAAkB,OAAO,GAAG,WAAW,GAAG;GAE3D,KAAA,GAAA,MAAA,eAAA,CAAmB,QAAQ,KAAK,SAAS,OAAO,MAC9C,QAAA,GAAA,MAAA,aAAA,CAAoB,UAAU,EAAE,KAAK,EAAE,CAAC;GAG1C,OAAO;EACT,CAAC;EAGH,OAAO,WAAW,iBAAA,GAAA,kBAAA,IAAA,CAAA,kBAAA,UAAA,EAAG,SAAW,CAAA,IAAI;CACtC,GAAG,CAAC,KAAK,CAAC;CAEV,wBAAA,cAAc,MAAMC,wBAAAA,cAAc;CAElC,OAAO;EAAE,OAAA,cAAA;EAAO;CAAK;AACvB"}