UNPKG

@yamada-ui/react

Version:

React UI components of the Yamada, by the Yamada, for the Yamada built with React and Emotion

1 lines 2.09 kB
{"version":3,"file":"portal.cjs","names":["Portal: FC<PortalProps>","useSsr","useEnvironment","Children"],"sources":["../../../../src/components/portal/portal.tsx"],"sourcesContent":["\"use client\"\n\nimport type { FC, PropsWithChildren, RefObject } from \"react\"\nimport type { RootNode } from \"../../core\"\nimport { Children, useEffect, useState } from \"react\"\nimport { createPortal } from \"react-dom\"\nimport { useEnvironment } from \"../../core\"\nimport { getDocument, isShadowRoot, useSsr } from \"../../utils\"\n\nconst getPortalNode = (node?: RootNode) => {\n const rootNode = node?.getRootNode()\n\n if (isShadowRoot(rootNode)) return rootNode\n\n return getDocument(node).body\n}\n\nexport interface PortalProps extends PropsWithChildren {\n /**\n * The `ref` to the component where the portal will be attached to.\n */\n containerRef?: RefObject<HTMLElement | null>\n /**\n * If `true`, the forwarding will be disabled.\n */\n disabled?: boolean\n}\n\n/**\n * `Portal` is a component that renders elements outside of the current `DOM` hierarchy.\n *\n * @see https://yamada-ui.com/docs/components/portal\n */\nexport const Portal: FC<PortalProps> = ({\n children,\n containerRef: ref,\n disabled,\n}) => {\n const [target, setTarget] = useState(ref?.current)\n const ssr = useSsr()\n const { getRootNode } = useEnvironment()\n\n useEffect(() => {\n setTarget(() => ref?.current)\n }, [ref])\n\n if (ssr || disabled) return children\n\n const container = target ?? getPortalNode(getRootNode())\n\n return (\n <>{Children.map(children, (child) => createPortal(child, container))}</>\n )\n}\n"],"mappings":";;;;;;;;;;;;;;;AASA,MAAM,iBAAiB,SAAoB;CACzC,MAAM,WAAW,MAAM,aAAa;AAEpC,yDAAiB,SAAS,CAAE,QAAO;AAEnC,2DAAmB,KAAK,CAAC;;;;;;;AAmB3B,MAAaA,UAA2B,EACtC,UACA,cAAc,KACd,eACI;CACJ,MAAM,CAAC,QAAQ,iCAAsB,KAAK,QAAQ;CAClD,MAAM,MAAMC,oBAAQ;CACpB,MAAM,EAAE,gBAAgBC,6CAAgB;AAExC,4BAAgB;AACd,kBAAgB,KAAK,QAAQ;IAC5B,CAAC,IAAI,CAAC;AAET,KAAI,OAAO,SAAU,QAAO;CAE5B,MAAM,YAAY,UAAU,cAAc,aAAa,CAAC;AAExD,QACE,mFAAGC,eAAS,IAAI,WAAW,sCAAuB,OAAO,UAAU,CAAC,GAAI"}