// @flow import * as React from 'react'; import classNames from 'classnames'; import { injectIntl } from 'react-intl'; import type { IntlShape } 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'; type Props = { className?: string, direction?: string, intl: IntlShape, isOpen: boolean, onClick?: Function, }; const SidebarToggleButton = ({ className = '', direction = DIRECTION_RIGHT, intl, isOpen, onClick, ...rest }: Props) => { 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: SyntheticMouseEvent) => { 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 ( ); } const renderIcon = () => { if (isDirectionLeft) { return isOpen ? : ; } return isOpen ? : ; }; return ( {renderIcon()} ); }; export default injectIntl(SidebarToggleButton);