index.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334
  1. import _extends from "@babel/runtime/helpers/esm/extends";
  2. import * as React from 'react';
  3. import toArray from "rc-util/es/Children/toArray";
  4. import { warning } from "rc-util/es/warning";
  5. import SingleObserver from "./SingleObserver";
  6. import { Collection } from "./Collection";
  7. var INTERNAL_PREFIX_KEY = 'rc-observer-key';
  8. import { _rs } from "./utils/observerUtil";
  9. export { /** @private Test only for mock trigger resize event */
  10. _rs };
  11. function ResizeObserver(props, ref) {
  12. var children = props.children;
  13. var childNodes = typeof children === 'function' ? [children] : toArray(children);
  14. if (process.env.NODE_ENV !== 'production') {
  15. if (childNodes.length > 1) {
  16. warning(false, 'Find more than one child node with `children` in ResizeObserver. Please use ResizeObserver.Collection instead.');
  17. } else if (childNodes.length === 0) {
  18. warning(false, '`children` of ResizeObserver is empty. Nothing is in observe.');
  19. }
  20. }
  21. return childNodes.map(function (child, index) {
  22. var key = (child === null || child === void 0 ? void 0 : child.key) || "".concat(INTERNAL_PREFIX_KEY, "-").concat(index);
  23. return /*#__PURE__*/React.createElement(SingleObserver, _extends({}, props, {
  24. key: key,
  25. ref: index === 0 ? ref : undefined
  26. }), child);
  27. });
  28. }
  29. var RefResizeObserver = /*#__PURE__*/React.forwardRef(ResizeObserver);
  30. if (process.env.NODE_ENV !== 'production') {
  31. RefResizeObserver.displayName = 'ResizeObserver';
  32. }
  33. RefResizeObserver.Collection = Collection;
  34. export default RefResizeObserver;