{"version":3,"file":"index.jsx","sources":["../src/index.tsx"],"sourcesContent":["// Copyright 2026 The Lynx Authors. All rights reserved.\n// Licensed under the Apache License Version 2.0 that can be found in the\n// LICENSE file in the root directory of this source tree.\n\nimport { forwardRef, memo, useRef, useState } from '@lynx-js/react'\nimport type { ForwardedRef, ReactNode } from '@lynx-js/react'\n\nimport { useGlobalEventListener } from '@lynx-js/lynx-ui-common'\nimport './styles.css'\nimport type { LayoutChangeEvent } from '@lynx-js/types'\n\nimport type { LazyComponentProps, LazyComponentRef } from './types'\n\nexport type { LazyComponentRef, LazyComponentProps }\n\ntype LazyComponentType = (props: LazyComponentProps) => ReactNode\n\nexport const LazyComponent = memo(\n  forwardRef(LazyComponentImpl),\n) as LazyComponentType\n\n/**\n * @example\n * ```javascript\n      <LazyComponent\n        scene={'scene'}\n        pid={`pid_1`}\n        estimatedStyle={{width:\"1px\", height:\"1px\"}}>\n            <text>Hello, ReactLynx 3!</text>\n      </LazyComponent>\n * ```\n */\nfunction LazyComponentImpl(\n  props: LazyComponentProps,\n  _ref: ForwardedRef<LazyComponentRef>,\n) {\n  const {\n    pid,\n    scene,\n    estimatedStyle,\n    top = '10px',\n    bottom = '10px',\n    left = '10px',\n    right = '10px',\n    unmountOnExit,\n    unloadable = false,\n    children,\n  } = props\n  const shouldUnmountOnExit = unmountOnExit ?? unloadable\n  const [show, setShow] = useState<boolean>(false)\n  const cacheSizeRef = useRef<{ width: string, height: string }>(undefined)\n\n  useGlobalEventListener(\n    'exposure',\n    (events: unknown[]) => {\n      'background-only'\n\n      events.forEach(event => {\n        if (\n          !show\n          // @ts-expect-error exposure-scene and exposure-id is currently not declared in eventProps\n          && event['exposure-scene'] === scene\n          // @ts-expect-error exposure-scene and exposure-id is currently not declared in eventProps\n          && event['exposure-id'] === pid\n        ) {\n          setShow(true)\n        }\n      })\n    },\n    [show],\n  )\n\n  const onLayoutChange = ({ detail }: LayoutChangeEvent) => {\n    if (show) {\n      cacheSizeRef.current = {\n        width: `${detail.width}px`,\n        height: `${detail.height}px`,\n      }\n    }\n  }\n\n  useGlobalEventListener(\n    'disexposure',\n    (events: unknown[]) => {\n      'background-only'\n\n      if (shouldUnmountOnExit) {\n        events.forEach(event => {\n          if (\n            // @ts-expect-error exposure-scene and exposure-id is currently not declared in eventProps\n            event['exposure-scene'] === scene\n            // @ts-expect-error exposure-scene and exposure-id is currently not declared in eventProps\n            && event['exposure-id'] === pid\n          ) {\n            setShow(false)\n          }\n        })\n      }\n    },\n    [shouldUnmountOnExit],\n  )\n\n  if (shouldUnmountOnExit) {\n    const { width, height, ...restsStyles } = estimatedStyle\n    return (\n      <view\n        id='component'\n        className='lynx-ui-lazy-component__min'\n        flatten={false}\n        exposure-screen-margin-top={top}\n        exposure-screen-margin-bottom={bottom}\n        exposure-screen-margin-left={left}\n        exposure-screen-margin-right={right}\n        exposure-id={pid}\n        exposure-scene={scene}\n        bindlayoutchange={onLayoutChange}\n        style={show\n          ? {}\n          : {\n            width: cacheSizeRef.current?.width ?? width,\n            height: cacheSizeRef.current?.height ?? height,\n            ...restsStyles,\n          }}\n      >\n        {show && children}\n      </view>\n    )\n  } else {\n    return show ? children : (\n      <view\n        id='component'\n        className='lynx-ui-lazy-component__min'\n        flatten={false}\n        exposure-screen-margin-top={top}\n        exposure-screen-margin-bottom={bottom}\n        exposure-screen-margin-left={left}\n        exposure-screen-margin-right={right}\n        exposure-id={pid}\n        exposure-scene={scene}\n        style={estimatedStyle}\n      />\n    )\n  }\n}\n"],"names":["LazyComponent","memo","forwardRef","LazyComponentImpl","props","_ref","pid","scene","estimatedStyle","top","bottom","left","right","unmountOnExit","unloadable","children","shouldUnmountOnExit","show","setShow","useState","cacheSizeRef","useRef","undefined","useGlobalEventListener","events","event","onLayoutChange","detail","view","width","height","restsStyles"],"mappings":";;;AAiBO,MAAMA,gBAAgBC,KAC3BC,WAAWC;AAcb,SAASA,kBACPC,KAAyB,EACzBC,IAAoC;IAEpC,MAAM,EACJC,GAAG,EACHC,KAAK,EAC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