@yamada-ui/react
Version:
React UI components of the Yamada, by the Yamada, for the Yamada built with React and Emotion
1 lines • 2.29 kB
Source Map (JSON)
{"version":3,"file":"index.cjs","names":["defaultRect: ObserverRect"],"sources":["../../../../src/hooks/use-resize-observer/index.ts"],"sourcesContent":["\"use client\"\n\nimport { useEffect, useMemo, useRef, useState } from \"react\"\nimport { createdDom } from \"../../utils\"\n\nexport interface ObserverRect extends Omit<DOMRectReadOnly, \"toJSON\"> {}\n\nconst defaultRect: ObserverRect = {\n bottom: 0,\n height: 0,\n left: 0,\n right: 0,\n top: 0,\n width: 0,\n x: 0,\n y: 0,\n}\n\n/**\n * `useResizeObserver` is a custom hook that tracks changes in the size and position of an element.\n *\n * @see https://yamada-ui.com/docs/hooks/use-resize-observer\n */\nexport const useResizeObserver = <Y extends HTMLElement = any>() => {\n const id = useRef(0)\n const ref = useRef<Y>(null)\n const [rect, setRect] = useState<ObserverRect>(defaultRect)\n\n const observer = useMemo(() => {\n if (!createdDom()) return null\n\n return new ResizeObserver(([entry]) => {\n if (!entry) return\n\n cancelAnimationFrame(id.current)\n\n id.current = requestAnimationFrame(() => {\n if (ref.current) setRect(entry.contentRect)\n })\n })\n }, [])\n\n useEffect(() => {\n if (ref.current) observer?.observe(ref.current)\n\n return () => {\n observer?.disconnect()\n\n if (id.current) cancelAnimationFrame(id.current)\n }\n }, [observer])\n\n return [ref, rect] as const\n}\n\nexport const useElementSize = <Y extends HTMLElement = any>() => {\n const [ref, { height, width }] = useResizeObserver<Y>()\n\n return { ref, height, width }\n}\n"],"mappings":";;;;;;;;;AAOA,MAAMA,cAA4B;CAChC,QAAQ;CACR,QAAQ;CACR,MAAM;CACN,OAAO;CACP,KAAK;CACL,OAAO;CACP,GAAG;CACH,GAAG;CACJ;;;;;;AAOD,MAAa,0BAAuD;CAClE,MAAM,uBAAY,EAAE;CACpB,MAAM,wBAAgB,KAAK;CAC3B,MAAM,CAAC,MAAM,+BAAkC,YAAY;CAE3D,MAAM,oCAAyB;AAC7B,MAAI,oDAAa,CAAE,QAAO;AAE1B,SAAO,IAAI,gBAAgB,CAAC,WAAW;AACrC,OAAI,CAAC,MAAO;AAEZ,wBAAqB,GAAG,QAAQ;AAEhC,MAAG,UAAU,4BAA4B;AACvC,QAAI,IAAI,QAAS,SAAQ,MAAM,YAAY;KAC3C;IACF;IACD,EAAE,CAAC;AAEN,4BAAgB;AACd,MAAI,IAAI,QAAS,WAAU,QAAQ,IAAI,QAAQ;AAE/C,eAAa;AACX,aAAU,YAAY;AAEtB,OAAI,GAAG,QAAS,sBAAqB,GAAG,QAAQ;;IAEjD,CAAC,SAAS,CAAC;AAEd,QAAO,CAAC,KAAK,KAAK;;AAGpB,MAAa,uBAAoD;CAC/D,MAAM,CAAC,KAAK,EAAE,QAAQ,WAAW,mBAAsB;AAEvD,QAAO;EAAE;EAAK;EAAQ;EAAO"}