UNPKG

@atlaskit/editor-plugin-toolbar

Version:

Toolbar plugin for @atlaskit/editor-core

139 lines (138 loc) • 6.35 kB
/* ScrollButton.tsx generated by @compiled/babel-plugin v3.0.4 */ import _slicedToArray from "@babel/runtime/helpers/slicedToArray"; import "./ScrollButton.compiled.css"; import { ax, ix } from "@compiled/react/runtime"; // ponytail: copy of editor-plugin-floating-toolbar/src/ui/ScrollButton.tsx. Keep the two in sync until // the floating toolbar plugin is deleted, then delete that copy. import React, { useEffect, useState } from 'react'; import { bind } from 'bind-event-listener'; import rafSchedule from 'raf-schd'; import IconButton from '@atlaskit/button/icon/button'; import messages from '@atlaskit/editor-common/floating-toolbar/messages'; import ChevronLeftLargeIcon from '@atlaskit/icon/core/chevron-left'; import ChevronRightLargeIcon from '@atlaskit/icon/core/chevron-right'; import { Box } from '@atlaskit/primitives/compiled/box'; var styles = { rightSide: "_t6vde4h9 _1p3lnqa1 _5wra1l7x _1xi2ze3t _qrwq12b0 _1o0z12b0 _bfhk1bhr _1pbykb7n _kqswstnw", leftSide: "_hr4je4h9 _1apgnqa1 _mowc1l7x _1ltvze3t _13li12b0 _mrkb12b0 _bfhk1bhr _1pbykb7n _kqswstnw" }; export var ScrollButton = function ScrollButton(_ref) { var intl = _ref.intl, scrollContainerRef = _ref.scrollContainerRef, node = _ref.node, disabled = _ref.disabled, side = _ref.side; var _useState = useState(false), _useState2 = _slicedToArray(_useState, 2), needScroll = _useState2[0], setNeedScroll = _useState2[1]; var _useState3 = useState(true), _useState4 = _slicedToArray(_useState3, 2), canScrollToSide = _useState4[0], setCanScrollToSide = _useState4[1]; var setCanScrollDebounced = rafSchedule(function () { // Refs are null before mounting and after unmount if (!scrollContainerRef.current) { return; } var _scrollContainerRef$c = scrollContainerRef.current, scrollLeft = _scrollContainerRef$c.scrollLeft, scrollWidth = _scrollContainerRef$c.scrollWidth, offsetWidth = _scrollContainerRef$c.offsetWidth; setCanScrollToSide( // -1 to account for pixel rounding error side === 'left' ? scrollLeft > 0 : scrollLeft < scrollWidth - offsetWidth - 1); }); var onScroll = function onScroll() { setCanScrollDebounced(); }; var onClick = function onClick() { var _scrollContainerRef$c2, _scrollContainerRef$c3, _scrollContainerRef$c4; var _ref2 = ((_scrollContainerRef$c2 = scrollContainerRef.current) === null || _scrollContainerRef$c2 === void 0 ? void 0 : _scrollContainerRef$c2.getBoundingClientRect()) || {}, _ref2$width = _ref2.width, scrollContainerWidth = _ref2$width === void 0 ? 0 : _ref2$width; var scrollLeft = ((_scrollContainerRef$c3 = scrollContainerRef.current) === null || _scrollContainerRef$c3 === void 0 ? void 0 : _scrollContainerRef$c3.scrollLeft) || 0; var scrollTo = side === 'left' ? scrollLeft - scrollContainerWidth : scrollLeft + scrollContainerWidth; (_scrollContainerRef$c4 = scrollContainerRef.current) === null || _scrollContainerRef$c4 === void 0 || _scrollContainerRef$c4.scrollTo({ top: 0, left: scrollTo, behavior: 'smooth' }); }; var resizeObserver = new ResizeObserver(function (_t) { var _scrollContainerRef$c5, _scrollContainerRef$c6; var widthNeededToShowAllItems = ((_scrollContainerRef$c5 = scrollContainerRef.current) === null || _scrollContainerRef$c5 === void 0 ? void 0 : _scrollContainerRef$c5.scrollWidth) || 0; var parentNode = (_scrollContainerRef$c6 = scrollContainerRef.current) === null || _scrollContainerRef$c6 === void 0 ? void 0 : _scrollContainerRef$c6.parentNode; var availableSpace = -1; if (parentNode instanceof HTMLElement) { availableSpace = parentNode.offsetWidth; } if (availableSpace === -1) { return; } if (availableSpace >= widthNeededToShowAllItems) { setNeedScroll(false); } else { setNeedScroll(true); onScroll(); } }); useEffect(function () { onScroll(); var scrollContainerRefCurrent = scrollContainerRef.current; var unbind; if (scrollContainerRefCurrent) { // Adding/removing scroll button depending on scroll position unbind = bind(scrollContainerRefCurrent, { type: 'scroll', listener: onScroll }); // watch for toolbar resize and show/hide scroll buttons if needed resizeObserver.observe(scrollContainerRefCurrent); } return function () { if (scrollContainerRefCurrent) { var _unbind; (_unbind = unbind) === null || _unbind === void 0 || _unbind(); resizeObserver.unobserve(scrollContainerRefCurrent); } setCanScrollDebounced.cancel(); }; // eslint-disable-next-line react-hooks/exhaustive-deps }, []); useEffect(function () { var scrollContainerRefCurrent = scrollContainerRef.current; if (scrollContainerRefCurrent) { var _scrollContainerRefCu; // reset scroll position when switching from one node with toolbar to another // scroll to made optional as it may not be rendered in testing env (_scrollContainerRefCu = scrollContainerRefCurrent.scrollTo) === null || _scrollContainerRefCu === void 0 || _scrollContainerRefCu.call(scrollContainerRefCurrent, { left: 0 }); } }, [node.type, scrollContainerRef]); var Icon = side === 'left' ? ChevronLeftLargeIcon : ChevronRightLargeIcon; return needScroll && (side === 'left' && canScrollToSide || side === 'right' && canScrollToSide) && /*#__PURE__*/React.createElement(Box, { padding: "space.050", xcss: side === 'left' ? styles.leftSide : styles.rightSide }, /*#__PURE__*/React.createElement(IconButton, { appearance: "subtle", label: intl.formatMessage(side === 'left' ? messages.floatingToolbarScrollLeft : messages.floatingToolbarScrollRight), onClick: onClick, isDisabled: disabled // eslint-disable-next-line @atlassian/perf-linting/no-unstable-inline-props -- Ignored via go/ees017 (to be fixed) , icon: function icon(iconProps) { return /*#__PURE__*/React.createElement(Icon, { label: iconProps.label, size: "small" }); }, isTooltipDisabled: false // eslint-disable-next-line @atlassian/perf-linting/no-unstable-inline-props -- Ignored via go/ees017 (to be fixed) , tooltip: { position: 'top' } })); };