minishouyin/node_modules/rc-resize-observer/lib/Collection.js

41 lines
1.5 KiB
JavaScript
Raw Permalink Normal View History

2025-11-12 11:35:57 +08:00
"use strict";
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Collection = Collection;
exports.CollectionContext = void 0;
var React = _interopRequireWildcard(require("react"));
var CollectionContext = exports.CollectionContext = /*#__PURE__*/React.createContext(null);
/**
* Collect all the resize event from children ResizeObserver
*/
function Collection(_ref) {
var children = _ref.children,
onBatchResize = _ref.onBatchResize;
var resizeIdRef = React.useRef(0);
var resizeInfosRef = React.useRef([]);
var onCollectionResize = React.useContext(CollectionContext);
var onResize = React.useCallback(function (size, element, data) {
resizeIdRef.current += 1;
var currentId = resizeIdRef.current;
resizeInfosRef.current.push({
size: size,
element: element,
data: data
});
Promise.resolve().then(function () {
if (currentId === resizeIdRef.current) {
onBatchResize === null || onBatchResize === void 0 || onBatchResize(resizeInfosRef.current);
resizeInfosRef.current = [];
}
});
// Continue bubbling if parent exist
onCollectionResize === null || onCollectionResize === void 0 || onCollectionResize(size, element, data);
}, [onBatchResize, onCollectionResize]);
return /*#__PURE__*/React.createElement(CollectionContext.Provider, {
value: onResize
}, children);
}