resize-event.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. 'use strict';
  2. exports.__esModule = true;
  3. exports.removeResizeListener = exports.addResizeListener = undefined;
  4. var _resizeObserverPolyfill = require('resize-observer-polyfill');
  5. var _resizeObserverPolyfill2 = _interopRequireDefault(_resizeObserverPolyfill);
  6. var _throttleDebounce = require('throttle-debounce');
  7. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8. var isServer = typeof window === 'undefined';
  9. /* istanbul ignore next */
  10. var resizeHandler = function resizeHandler(entries) {
  11. for (var _iterator = entries, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
  12. var _ref;
  13. if (_isArray) {
  14. if (_i >= _iterator.length) break;
  15. _ref = _iterator[_i++];
  16. } else {
  17. _i = _iterator.next();
  18. if (_i.done) break;
  19. _ref = _i.value;
  20. }
  21. var entry = _ref;
  22. var listeners = entry.target.__resizeListeners__ || [];
  23. if (listeners.length) {
  24. listeners.forEach(function (fn) {
  25. fn();
  26. });
  27. }
  28. }
  29. };
  30. /* istanbul ignore next */
  31. var addResizeListener = exports.addResizeListener = function addResizeListener(element, fn) {
  32. if (isServer) return;
  33. if (!element.__resizeListeners__) {
  34. element.__resizeListeners__ = [];
  35. element.__ro__ = new _resizeObserverPolyfill2.default((0, _throttleDebounce.debounce)(16, resizeHandler));
  36. element.__ro__.observe(element);
  37. }
  38. element.__resizeListeners__.push(fn);
  39. };
  40. /* istanbul ignore next */
  41. var removeResizeListener = exports.removeResizeListener = function removeResizeListener(element, fn) {
  42. if (!element || !element.__resizeListeners__) return;
  43. element.__resizeListeners__.splice(element.__resizeListeners__.indexOf(fn), 1);
  44. if (!element.__resizeListeners__.length) {
  45. element.__ro__.disconnect();
  46. }
  47. };