UNPKG

@carbon/react

Version:

React components for the Carbon Design System

66 lines (56 loc) 1.61 kB
/** * Copyright IBM Corp. 2016, 2023 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var window = require('window-or-global'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } var window__default = /*#__PURE__*/_interopDefaultLegacy(window); // mdn resize function /** * A callback function to be executed on `resize`. */ const OptimizedResize = (() => { const callbacks = []; let running = false; const runCallbacks = () => { callbacks.forEach(callback => { callback(); }); running = false; }; const handleResize = () => { if (!running) { running = true; window__default["default"].requestAnimationFrame(runCallbacks); } }; const addCallback = callback => { const index = callbacks.indexOf(callback); if (index < 0) { callbacks.push(callback); } }; return { /** Adds a callback function to be executed on window `resize`. */ add: callback => { if (!callbacks.length) { window__default["default"].addEventListener('resize', handleResize); } addCallback(callback); return { /** Removes the callback. */ remove: () => { const index = callbacks.indexOf(callback); if (index >= 0) { callbacks.splice(index, 1); } } }; } }; })(); exports.OptimizedResize = OptimizedResize;