index.mjs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import {useState as $9gyGR$useState} from "react";
  2. import {useLayoutEffect as $9gyGR$useLayoutEffect} from "@radix-ui/react-use-layout-effect";
  3. function $db6c3485150b8e66$export$1ab7ae714698c4b8(element) {
  4. const [size, setSize] = $9gyGR$useState(undefined);
  5. $9gyGR$useLayoutEffect(()=>{
  6. if (element) {
  7. // provide size as early as possible
  8. setSize({
  9. width: element.offsetWidth,
  10. height: element.offsetHeight
  11. });
  12. const resizeObserver = new ResizeObserver((entries)=>{
  13. if (!Array.isArray(entries)) return;
  14. // Since we only observe the one element, we don't need to loop over the
  15. // array
  16. if (!entries.length) return;
  17. const entry = entries[0];
  18. let width;
  19. let height;
  20. if ('borderBoxSize' in entry) {
  21. const borderSizeEntry = entry['borderBoxSize']; // iron out differences between browsers
  22. const borderSize = Array.isArray(borderSizeEntry) ? borderSizeEntry[0] : borderSizeEntry;
  23. width = borderSize['inlineSize'];
  24. height = borderSize['blockSize'];
  25. } else {
  26. // for browsers that don't support `borderBoxSize`
  27. // we calculate it ourselves to get the correct border box.
  28. width = element.offsetWidth;
  29. height = element.offsetHeight;
  30. }
  31. setSize({
  32. width: width,
  33. height: height
  34. });
  35. });
  36. resizeObserver.observe(element, {
  37. box: 'border-box'
  38. });
  39. return ()=>resizeObserver.unobserve(element)
  40. ;
  41. } else // We only want to reset to `undefined` when the element becomes `null`,
  42. // not if it changes to another element.
  43. setSize(undefined);
  44. }, [
  45. element
  46. ]);
  47. return size;
  48. }
  49. export {$db6c3485150b8e66$export$1ab7ae714698c4b8 as useSize};
  50. //# sourceMappingURL=index.mjs.map