import React, { PureComponent } from 'react'; import styled from 'styled-components'; import DropList from '@atlaskit/droplist'; import Item, { ItemGroup } from '@atlaskit/item'; import Tooltip from '@atlaskit/tooltip'; import { Popup } from '@atlaskit/editor-common'; import { akEditorFloatingPanelZIndex } from '@atlaskit/editor-shared-styles'; import withOuterListeners from '../with-outer-listeners'; import { Props, State } from './types'; const Wrapper = styled.div` /* tooltip in ToolbarButton is display:block */ & > div > div { display: flex; } `; const DropListWithOutsideListeners: any = withOuterListeners(DropList); /** * Hack for item to imitate old dropdown-menu selected styles */ const ItemWrapper: any = styled.div` ${(props: any) => props.isSelected ? '&& > span, && > span:hover { background: #6c798f; color: #fff; }' : ''}; `; const ItemContentWrapper: any = styled.span` ${(props: any) => (props.hasElemBefore ? 'margin-left: 8px;' : '')}; `; /** * Wrapper around @atlaskit/droplist which uses Popup and Portal to render * dropdown-menu outside of "overflow: hidden" containers when needed. * * Also it controls popper's placement. */ export default class DropdownMenuWrapper extends PureComponent { state: State = { popupPlacement: ['bottom', 'left'], }; private handleRef = (target: HTMLElement | null) => { this.setState({ target: target || undefined }); }; private updatePopupPlacement = (placement: [string, string]) => { const { popupPlacement: previousPlacement } = this.state; if ( placement[0] !== previousPlacement[0] || placement[1] !== previousPlacement[1] ) { this.setState({ popupPlacement: placement }); } }; private handleClose = () => { if (this.props.onOpenChange) { this.props.onOpenChange({ isOpen: false }); } }; private renderItem(item: typeof Item) { const { onItemActivated, onMouseEnter, onMouseLeave } = this.props; // onClick and value.name are the action indicators in the handlers // If neither are present, don't wrap in an Item. if (!item.onClick && !item.value && !item.value.name) { return {item.content}; } const dropListItem = ( onItemActivated && onItemActivated({ item })} onMouseEnter={() => onMouseEnter && onMouseEnter({ item })} onMouseLeave={() => onMouseLeave && onMouseLeave({ item })} className={item.className} aria-label={String(item.content)} > {item.content} ); if (item.tooltipDescription) { return ( {dropListItem} ); } return dropListItem; } private renderDropdownMenu() { const { target, popupPlacement } = this.state; const { items, mountTo, boundariesElement, scrollableElement, offset, fitHeight, fitWidth, isOpen, zIndex, } = this.props; return (
{items.map((group, index) => ( {group.items.map((item) => this.renderItem(item))} ))} ); } render() { const { children, isOpen } = this.props; return (
{children}
{isOpen ? this.renderDropdownMenu() : null}
); } }