react-resize-aware
Version:
A React hook that makes it possible to listen to element resize events.
1 lines • 5.04 kB
Source Map (JSON)
{"version":3,"file":"index.cjs","sources":["../src/ResizeListener.js","../src/useOnResize.js","../src/useResizeAware.js"],"sourcesContent":["// @flow\nimport * as React from \"react\";\nimport useOnResize from \"./useOnResize\";\n\nexport type ReactRef<T> = { current: ?T };\n\nconst style = {\n display: \"block\",\n opacity: 0,\n position: \"absolute\",\n top: 0,\n left: 0,\n height: \"100%\",\n width: \"100%\",\n overflow: \"hidden\",\n pointerEvents: \"none\",\n zIndex: -1,\n maxHeight: \"inherit\",\n maxWidth: \"inherit\",\n};\n\n// This automatically attaches to itself the resize event listener\n// and adds onResize as callback\nexport default ({\n onResize,\n}: {\n onResize: (ReactRef<HTMLIFrameElement>) => void,\n}): React.Node => {\n const ref = React.useRef();\n useOnResize(ref, () => onResize(ref));\n\n return (\n <iframe\n style={style}\n src=\"about:blank\"\n ref={ref}\n aria-hidden={true}\n tabIndex={-1}\n frameBorder={0}\n />\n );\n};\n","// @flow\nimport * as React from \"react\";\n\nexport type ReactRef<T> = { current: ?T };\n\n// This is just an utility to cleanly attach a `resize` event listener\n// to a target HTMLObjectElement or HTMLIFrameElement\n// The real Hook is `useResizeAware.js`\nexport default (\n ref: ReactRef<HTMLIFrameElement>,\n onResize: () => void\n): void => {\n const getTarget = () =>\n ref.current &&\n ref.current.contentDocument &&\n ref.current.contentDocument.defaultView;\n function run() {\n // trigger onResize event on mount to provide initial sizes\n onResize();\n var target = getTarget();\n target && target.addEventListener(\"resize\", onResize);\n }\n React.useEffect(() => {\n if (getTarget()) {\n run();\n } else if (ref.current && \"addEventListener\" in ref.current) {\n ref.current.addEventListener(\"load\", run);\n }\n\n // clean event listener on unmount\n return () => {\n // Ensure the target exists and is in fact an event listener\n // this fixes an issue where contentDocument.defaultView is not a real window object\n // as can be the case when used with React portals\n const target = getTarget();\n if (target == null) return;\n\n const isListener =\n target && typeof target.removeEventListener === \"function\";\n\n isListener && target.removeEventListener(\"resize\", onResize);\n };\n }, []);\n};\n","// @flow\nimport * as React from \"react\";\nimport ResizeListener from \"./ResizeListener\";\n\ntype Reporter<T> = (target: ?HTMLIFrameElement) => T;\n\nconst defaultReporter = (target: ?HTMLIFrameElement) =>\n target != null\n ? {\n width: target.offsetWidth,\n height: target.offsetHeight,\n }\n : null;\n\ndeclare export default function useResizeAware(): [\n React.Node,\n ?{ width: number, height: number }\n];\ndeclare export default function useResizeAware<T>(\n reporter: Reporter<T>\n): [React.Node, T];\n\nexport default function useResizeAware(\n reporter?: Reporter<mixed> = defaultReporter\n) {\n const [sizes, setSizes] = React.useState(reporter(null));\n const onResize = React.useCallback(\n (ref) => setSizes(reporter(ref.current)),\n [reporter]\n );\n const resizeListenerNode = React.useMemo(\n () => <ResizeListener onResize={onResize} />,\n [onResize]\n );\n\n return [resizeListenerNode, sizes];\n}\n"],"names":["style","display","opacity","position","top","left","height","width","overflow","pointerEvents","zIndex","maxHeight","maxWidth","ResizeListener","_ref","onResize","ref","React","useRef","getTarget","current","contentDocument","defaultView","run","target","addEventListener","useEffect","removeEventListener","useOnResize","_jsx","jsx","src","tabIndex","frameBorder","defaultReporter","offsetWidth","offsetHeight","reporter","_React$useState","useState","sizes","setSizes","useCallback","useMemo"],"mappings":"4VAMMA,EAAQ,CACZC,QAAS,QACTC,QAAS,EACTC,SAAU,WACVC,IAAK,EACLC,KAAM,EACNC,OAAQ,OACRC,MAAO,OACPC,SAAU,SACVC,cAAe,OACfC,QAAS,EACTC,UAAW,UACXC,SAAU,WAKZC,EAAA,SAAeC,GAIG,IAHhBC,EAAQD,EAARC,SAIMC,EAAMC,EAAMC,SAGlB,OCvBa,SACbF,EACAD,GAEA,IAAMI,EAAY,WAChB,OAAAH,EAAII,SACJJ,EAAII,QAAQC,iBACZL,EAAII,QAAQC,gBAAgBC,WAAW,EACzC,SAASC,IAEPR,IACA,IAAIS,EAASL,IACbK,GAAUA,EAAOC,iBAAiB,SAAUV,EAC9C,CACAE,EAAMS,UA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