UNPKG

@atlaskit/editor-common

Version:

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

178 lines (176 loc) 7.72 kB
import _extends from "@babel/runtime/helpers/extends"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; import _inherits from "@babel/runtime/helpers/inherits"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } import React from 'react'; import { ShadowObserver, shadowObserverClassNames } from './shadowObserver'; export var shadowClassNames = { RIGHT_SHADOW: 'right-shadow', LEFT_SHADOW: 'left-shadow' }; export default function overflowShadow(Component, options) { return /*#__PURE__*/function (_React$Component) { function OverflowShadow() { var _this; _classCallCheck(this, OverflowShadow); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _callSuper(this, OverflowShadow, [].concat(args)); _defineProperty(_this, "overflowContainerWidth", 0); _defineProperty(_this, "state", { showLeftShadow: false, showRightShadow: false }); _defineProperty(_this, "handleScroll", function (event) { if (!_this.overflowContainer || event.target !== _this.overflowContainer || options.useShadowObserver) { return; } _this.updateShadows(); }); _defineProperty(_this, "updateShadows", function () { if (options.useShadowObserver) { return; } _this.setState(function (prevState) { if (!_this.overflowContainer) { return; } var diff = _this.calcOverflowDiff(); var showRightShadow = diff > 0 && diff > _this.overflowContainer.scrollLeft; var showLeftShadow = _this.showLeftShadow(_this.overflowContainer); if (showLeftShadow !== prevState.showLeftShadow || showRightShadow !== _this.state.showRightShadow) { return { showLeftShadow: showLeftShadow, showRightShadow: showRightShadow }; } return null; }); }); _defineProperty(_this, "showLeftShadow", function (overflowContainer) { return !!overflowContainer && overflowContainer.scrollLeft > 0; }); _defineProperty(_this, "calcOverflowDiff", function () { if (!_this.overflowContainer) { return 0; } _this.diff = _this.calcScrollableWidth(); return _this.diff - _this.overflowContainer.offsetWidth; }); _defineProperty(_this, "calcScrollableWidth", function () { if (!_this.scrollable && _this.overflowContainer) { return _this.overflowContainer.scrollWidth; } if (!_this.scrollable) { return 0; } var width = 0; for (var i = 0; i < _this.scrollable.length; i++) { var scrollableElement = _this.scrollable[i]; if (isElementNode(scrollableElement)) { width += scrollableElement.scrollWidth; } } return width; }); _defineProperty(_this, "handleContainer", function (container) { if (!container || _this.container) { return; } _this.container = container; _this.overflowContainer = container.querySelector(options.overflowSelector); if (!_this.overflowContainer) { _this.overflowContainer = container; } if (options.useShadowObserver) { _this.initShadowObserver(); return; } _this.updateShadows(); // Ignored via go/ees005 // eslint-disable-next-line @repo/internal/dom-events/no-unsafe-event-listeners _this.overflowContainer.addEventListener('scroll', _this.handleScroll); }); return _this; } _inherits(OverflowShadow, _React$Component); return _createClass(OverflowShadow, [{ key: "componentWillUnmount", value: function componentWillUnmount() { if (options.useShadowObserver) { return this.shadowObserver && this.shadowObserver.dispose(); } if (this.overflowContainer) { // Ignored via go/ees005 // eslint-disable-next-line @repo/internal/dom-events/no-unsafe-event-listeners this.overflowContainer.removeEventListener('scroll', this.handleScroll); } this.updateShadows(); } }, { key: "componentDidUpdate", value: function componentDidUpdate() { if (!options.useShadowObserver) { this.updateShadows(); } } }, { key: "initShadowObserver", value: function initShadowObserver() { var _this2 = this; if (this.shadowObserver || !this.overflowContainer) { return; } this.shadowObserver = new ShadowObserver({ scrollContainer: this.overflowContainer, onUpdateShadows: function onUpdateShadows(shadowStates) { _this2.setState(function (prevState) { var showLeftShadow = shadowStates.showLeftShadow, showRightShadow = shadowStates.showRightShadow; if (showLeftShadow !== prevState.showLeftShadow || showRightShadow !== prevState.showRightShadow) { return { showLeftShadow: showLeftShadow, showRightShadow: showRightShadow }; } return null; }); } }); } }, { key: "render", value: function render() { var _this$props, _this$props2; var _this$state = this.state, showLeftShadow = _this$state.showLeftShadow, showRightShadow = _this$state.showRightShadow; var classNames = [!((_this$props = this.props) !== null && _this$props !== void 0 && _this$props.disableTableOverflowShadow) && showRightShadow && shadowClassNames.RIGHT_SHADOW, !((_this$props2 = this.props) !== null && _this$props2 !== void 0 && _this$props2.disableTableOverflowShadow) && showLeftShadow && shadowClassNames.LEFT_SHADOW, options.useShadowObserver && shadowObserverClassNames.SHADOW_CONTAINER].filter(Boolean).join(' '); /** * The widths have already been calculated to determine * showRightShadow and showLeftShadow. If either is true, * then the content is scrollable and we need to set the * tabIndex to 0 to allow the user to scroll the content * for a11y purposes. */ var hasOverflowScroll = showRightShadow || showLeftShadow; return /*#__PURE__*/React.createElement(Component, _extends({ handleRef: this.handleContainer, tabIndex: hasOverflowScroll ? 0 : undefined, shadowClassNames: classNames // eslint-disable-next-line react/jsx-props-no-spreading -- Spreading props to pass all component props through to wrapped generic component }, this.props)); } }]); }(React.Component); } // Helper function to check if the passed node is of Element class function isElementNode(node) { return node.nodeType === 1; }