@react-awesome/autosizer
Version:
Autosizer keeps tracking the parent size and report back to its children.
42 lines (41 loc) • 1.34 kB
JavaScript
;
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxRuntime = require("react/jsx-runtime");
const react = require("react");
const index = require("../../../node_modules/react-merge-refs/dist/index.cjs");
const Autosizer = react.forwardRef(
({ children = () => void 0, initialSize = {}, ...props }, ref) => {
const elRef = react.useRef(null);
const [size, setSize] = react.useState({
width: initialSize.width || 0,
height: initialSize.height || 0
});
const watch = react.useCallback(
/* istanbul ignore next */
() => {
if (!elRef.current)
return;
const observer = new ResizeObserver(
/* istanbul ignore next */
(entries) => {
if (!entries.length)
return;
const [entry] = entries;
setSize({
width: entry.contentRect.width,
height: entry.contentRect.height
});
}
);
observer.observe(elRef.current);
return () => {
observer.disconnect();
};
},
[]
);
react.useEffect(() => watch(), [watch]);
return /* @__PURE__ */ jsxRuntime.jsx("div", { ref: index.mergeRefs([elRef, ref]), ...props, children: children(size) });
}
);
exports.Autosizer = Autosizer;