@yamada-ui/use-resize-observer
Version:
Yamada UI useResizeObserver custom hook
72 lines (71 loc) • 2.33 kB
JavaScript
"use client"
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var index_exports = {};
__export(index_exports, {
useElementSize: () => useElementSize,
useResizeObserver: () => useResizeObserver
});
module.exports = __toCommonJS(index_exports);
var import_utils = require("@yamada-ui/utils");
var import_react = require("react");
var defaultRect = {
bottom: 0,
height: 0,
left: 0,
right: 0,
top: 0,
width: 0,
x: 0,
y: 0
};
var useResizeObserver = () => {
const id = (0, import_react.useRef)(0);
const ref = (0, import_react.useRef)(null);
const [rect, setRect] = (0, import_react.useState)(defaultRect);
const observer = (0, import_react.useMemo)(() => {
if (!(0, import_utils.createdDom)()) return null;
return new ResizeObserver(([entry]) => {
if (!entry) return;
cancelAnimationFrame(id.current);
id.current = requestAnimationFrame(() => {
if (ref.current) setRect(entry.contentRect);
});
});
}, []);
(0, import_react.useEffect)(() => {
if (ref.current) observer == null ? void 0 : observer.observe(ref.current);
return () => {
observer == null ? void 0 : observer.disconnect();
if (id.current) cancelAnimationFrame(id.current);
};
}, [observer]);
return [ref, rect];
};
var useElementSize = () => {
const [ref, { height, width }] = useResizeObserver();
return { ref, height, width };
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
useElementSize,
useResizeObserver
});
//# sourceMappingURL=index.js.map