amis-rpc-design/node_modules/rc-resize-observer/lib/Collection.js

41 lines
1.5 KiB
JavaScript
Raw Normal View History

2023-10-07 19:42:30 +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 = /*#__PURE__*/React.createContext(null);
/**
* Collect all the resize event from children ResizeObserver
*/
exports.CollectionContext = CollectionContext;
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 ? void 0 : onBatchResize(resizeInfosRef.current);
resizeInfosRef.current = [];
}
});
// Continue bubbling if parent exist
onCollectionResize === null || onCollectionResize === void 0 ? void 0 : onCollectionResize(size, element, data);
}, [onBatchResize, onCollectionResize]);
return /*#__PURE__*/React.createElement(CollectionContext.Provider, {
value: onResize
}, children);
}