box-ui-elements
Version:
Box UI Elements
103 lines (102 loc) • 4.66 kB
JavaScript
const _excluded = ["className", "direction", "intl", "isOpen", "onClick"];
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
import * as React from 'react';
import classNames from 'classnames';
import { injectIntl } from 'react-intl';
import { IconButton, Tooltip as BPTooltip, useBreakpoint, Breakpoint } from '@box/blueprint-web';
import { ChevronDown, ChevronUp, RightSidebarChevronClose, RightSidebarChevronOpen } from '@box/blueprint-web-assets/icons/Medium';
import IconHide from '../../icons/general/IconHide';
import IconShow from '../../icons/general/IconShow';
import PlainButton from '../plain-button';
import Tooltip from '../tooltip';
import { useFeatureConfig } from '../../elements/common/feature-checking';
import messages from '../../elements/common/messages';
import './SidebarToggleButton.scss';
const DIRECTION_LEFT = 'left';
const DIRECTION_RIGHT = 'right';
const SidebarToggleButton = _ref => {
let {
className = '',
direction = DIRECTION_RIGHT,
intl,
isOpen,
onClick
} = _ref,
rest = _objectWithoutProperties(_ref, _excluded);
const breakpoint = useBreakpoint();
const {
enabled: isPreviewModernizationEnabled
} = useFeatureConfig('previewModernization');
const isCollapsed = !isOpen ? 'collapsed' : '';
const intlMessage = isOpen ? messages.sidebarHide : messages.sidebarShow;
const intlText = intl.formatMessage(intlMessage);
const classes = classNames(className, {
'bdl-is-collapsed': isCollapsed,
'bdl-SidebarToggleButton': !isPreviewModernizationEnabled,
'bdl-SidebarToggleButton--modernized': isPreviewModernizationEnabled
});
const isDirectionLeft = direction === DIRECTION_LEFT;
const tooltipPosition = isDirectionLeft ? 'middle-right' : 'middle-left';
// Adding this to stop the mousedown event from being propagated up to box-annotations as
// that will cause the active annotation to no longer be active which means that it will not be displayed.
// This causes video annotations not to work properly.
const mouseDownHandler = event => {
event.stopPropagation();
};
if (isPreviewModernizationEnabled) {
const isBelowMediumView = breakpoint <= Breakpoint.Medium;
const tooltipPositionModernized = isDirectionLeft ? DIRECTION_RIGHT : DIRECTION_LEFT;
const renderModernizedIcon = () => {
if (isBelowMediumView) {
return isOpen ? ChevronDown : ChevronUp;
}
if (isDirectionLeft) {
return isOpen ? RightSidebarChevronOpen : RightSidebarChevronClose;
}
return isOpen ? RightSidebarChevronClose : RightSidebarChevronOpen;
};
return /*#__PURE__*/React.createElement(BPTooltip, {
content: intlText,
side: tooltipPositionModernized
}, /*#__PURE__*/React.createElement(IconButton, _extends({
"aria-label": intlText,
className: classes,
icon: renderModernizedIcon(),
onClick: onClick,
onMouseDown: mouseDownHandler,
size: isBelowMediumView ? 'small' : 'large'
}, rest)));
}
const renderIcon = () => {
if (isDirectionLeft) {
return isOpen ? /*#__PURE__*/React.createElement(IconShow, {
height: 16,
width: 16
}) : /*#__PURE__*/React.createElement(IconHide, {
height: 16,
width: 16
});
}
return isOpen ? /*#__PURE__*/React.createElement(IconHide, {
height: 16,
width: 16
}) : /*#__PURE__*/React.createElement(IconShow, {
height: 16,
width: 16
});
};
return /*#__PURE__*/React.createElement(Tooltip, {
position: tooltipPosition,
text: intlText
}, /*#__PURE__*/React.createElement(PlainButton, _extends({
"aria-label": intlText,
className: classes,
onClick: onClick,
onMouseDown: mouseDownHandler,
type: "button"
}, rest), renderIcon()));
};
export default injectIntl(SidebarToggleButton);
//# sourceMappingURL=SidebarToggleButton.js.map