UNPKG

@atlaskit/editor-common

Version:

A package that contains common classes and components for editor and renderer

117 lines (113 loc) 5.27 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _typeof = require("@babel/runtime/helpers/typeof"); Object.defineProperty(exports, "__esModule", { value: true }); exports.WidthProvider = exports.WidthContext = exports.WidthConsumer = void 0; exports.createWidthContext = createWidthContext; exports.getBodyWidth = void 0; exports.getBreakpoint = getBreakpoint; var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray")); var _react = _interopRequireWildcard(require("react")); var _react2 = require("@emotion/react"); var _memoizeOne = _interopRequireDefault(require("memoize-one")); var _rafSchd = _interopRequireDefault(require("raf-schd")); var _widthDetector = require("@atlaskit/width-detector"); var _isSsr = require("../../core-utils/is-ssr"); function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); } /** * @jsxRuntime classic * @jsx jsx */ // eslint-disable-next-line @atlaskit/ui-styling-standard/use-compiled, @typescript-eslint/consistent-type-imports -- Ignored via go/DSP-18766; jsx required at runtime for @jsxRuntime classic var styles = (0, _react2.css)({ position: 'relative', width: '100%' }); var SCROLLBAR_WIDTH = 30; function getBreakpoint() { var width = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0; var MAX_S = 1266; var MAX_M = 2146; if (width >= MAX_S && width < MAX_M) { return 'M'; } else if (width >= MAX_M) { return 'L'; } return 'S'; } function createWidthContext() { var width = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0; return { width: width, breakpoint: getBreakpoint(width) }; } var WidthContext = exports.WidthContext = /*#__PURE__*/_react.default.createContext(createWidthContext()); var Provider = WidthContext.Provider; var Consumer = exports.WidthConsumer = WidthContext.Consumer; /** * 🧱 Internal function: Editor FE Platform * * Returns the width of the document body. * * This function is memoized to avoid forcing a layout reflow multiple times. * It uses `document.body.offsetWidth` as the source of the width, which can lead to * a layout reflow if accessed repeatedly. To mitigate performance issues, the result * is cached using `memoizeOne`. * * @returns {number} The width of the document body or 0 if the document is undefined. */ var getBodyWidth = exports.getBodyWidth = (0, _memoizeOne.default)(function () { var _document$body$offset, _document$body; return (0, _isSsr.isSSR)() ? 0 : (_document$body$offset = (_document$body = document.body) === null || _document$body === void 0 ? void 0 : _document$body.offsetWidth) !== null && _document$body$offset !== void 0 ? _document$body$offset : 0; }); var WidthProvider = exports.WidthProvider = function WidthProvider(_ref) { var className = _ref.className, shouldCheckExistingValue = _ref.shouldCheckExistingValue, children = _ref.children; var existingContextValue = (0, _react.useContext)(WidthContext); var _useState = (0, _react.useState)(getBodyWidth), _useState2 = (0, _slicedToArray2.default)(_useState, 2), width = _useState2[0], setWidth = _useState2[1]; var widthRef = (0, _react.useRef)(width); var isMountedRef = (0, _react.useRef)(true); var providerValue = (0, _react.useMemo)(function () { return createWidthContext(width); }, [width]); var updateWidth = (0, _react.useMemo)(function () { return (0, _rafSchd.default)(function (nextWidth) { var currentWidth = widthRef.current || 0; // Ignore changes that are less than SCROLLBAR_WIDTH, otherwise it can cause infinite re-scaling if (Math.abs(currentWidth - nextWidth) < SCROLLBAR_WIDTH) { return; } // Avoid React memory leak by checking if the component is still mounted if (!isMountedRef.current) { return; } widthRef.current = nextWidth; setWidth(nextWidth); }); }, []); var skipWidthDetection = shouldCheckExistingValue && existingContextValue.width > 0; _react.default.useLayoutEffect(function () { isMountedRef.current = true; return function () { isMountedRef.current = false; }; }, []); return (0, _react2.jsx)("div", { css: styles // eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop -- Ignored via go/DSP-18766 , className: className }, !skipWidthDetection && (0, _react2.jsx)(_react.Fragment, null, (0, _react2.jsx)(_widthDetector.WidthObserver, { setWidth: updateWidth, offscreen: true }), (0, _react2.jsx)(Provider, { value: providerValue }, children)), skipWidthDetection && children); };