@yamada-ui/react
Version:
React UI components of the Yamada, by the Yamada, for the Yamada built with React and Emotion
1 lines • 1.59 kB
Source Map (JSON)
{"version":3,"file":"index.cjs","names":[],"sources":["../../../../src/hooks/use-lazy-mount/index.ts"],"sourcesContent":["\"use client\"\n\nimport type { ReactNode } from \"react\"\nimport { useRef } from \"react\"\n\nexport type LazyBehavior = \"keepMounted\" | \"unmount\"\n\nexport interface UseLazyMountProps {\n children?: ReactNode\n /**\n * If `true`, rendering of the content will be deferred until it is selected.\n *\n * @default false\n */\n lazy?: boolean\n /**\n * The lazy behavior of content when not active. Only works when `lazy={true}`.\n *\n * - `unmount`: The content is always unmounted.\n * - `keepMounted`: The content is initially unmounted, but stays mounted when selected.\n *\n * @default 'unmount'\n */\n lazyBehavior?: LazyBehavior\n /**\n * If `true`, the content is mounted.\n *\n * @default false\n */\n mounted?: boolean\n}\n\nexport const useLazyMount = ({\n children,\n lazy,\n lazyBehavior = \"unmount\",\n mounted,\n}: UseLazyMountProps): ReactNode => {\n const beforeMounted = useRef<boolean>(false)\n\n if (!lazy) {\n beforeMounted.current = false\n\n return children\n }\n\n if (mounted) {\n beforeMounted.current = true\n\n return children\n }\n\n if (lazyBehavior === \"keepMounted\" && beforeMounted.current) return children\n\n return null\n}\n"],"mappings":";;;;;;;;AAgCA,MAAa,gBAAgB,EAC3B,UACA,MACA,eAAe,WACf,cACkC;CAClC,MAAM,kCAAgC,MAAM;AAE5C,KAAI,CAAC,MAAM;AACT,gBAAc,UAAU;AAExB,SAAO;;AAGT,KAAI,SAAS;AACX,gBAAc,UAAU;AAExB,SAAO;;AAGT,KAAI,iBAAiB,iBAAiB,cAAc,QAAS,QAAO;AAEpE,QAAO"}