// @flow strict import * as React from 'react'; import { // $FlowFixMe[untyped-import] autoUpdate, // $FlowFixMe[untyped-import] flip, // $FlowFixMe[untyped-import] FloatingFocusManager, // $FlowFixMe[untyped-import] FloatingPortal, // $FlowFixMe[untyped-import] offset, // $FlowFixMe[untyped-import] shift, // $FlowFixMe[untyped-import] useFloating, } from '@floating-ui/react'; import {useReferenceElementWidth} from '../../hooks'; import {spaceNone, spaceXXSmall} from '../../styles/variables/_space'; import {classify} from '../../utils/classify'; import {type ClickAwayRefType, ClickAway} from '../../utils/click-away'; import {mergeRefs} from '../../utils/merge-refs'; import type {UnstyledButtonProps} from '../Button'; import {UnstyledButton} from '../Button'; import type {AnchorType} from '../ButtonDropdown'; import {Icon} from '../Icon'; import type {MenuOption, MenuProps} from '../Menu'; import {Menu} from '../Menu'; import {type ElevationType, getElevationValue} from '../Tooltip'; import {Truncate} from '../Truncate'; import css from './InlineDropdown.module.css'; type ClassNames = $ReadOnly<{ buttonWrapper?: string, dropdownContainer?: string, }>; export type InlineDropdownProps = { ...UnstyledButtonProps, classNames?: ClassNames, menu?: MenuProps, anchorPosition?: AnchorType, onOptionSelect?: (option: MenuOption, ?SyntheticEvent) => mixed, onMenuOpen?: () => mixed, onMenuClose?: () => mixed, size?: 'medium' | 'small' | 'extraSmall', elevation?: ElevationType, clickAwayRef?: ClickAwayRefType, ... }; export const InlineDropdown: React$AbstractComponent< InlineDropdownProps, HTMLDivElement, > = React.forwardRef( ( { anchorPosition = 'bottom-start', size = 'medium', onOptionSelect, menu, classNames, disabled, onMenuOpen, onMenuClose, children, clickAwayRef, elevation = 'modal', ...restButtonProps }: InlineDropdownProps, ref, ): React.Node => { const {x, y, refs, strategy, context} = useFloating({ open: true, strategy: 'absolute', placement: anchorPosition, whileElementsMounted: autoUpdate, middleware: [shift(), flip(), offset(parseInt(spaceXXSmall))], }); const dropdownWidth = useReferenceElementWidth(refs.reference?.current); const onMenuToggle = (isOpen: boolean) => { isOpen ? onMenuOpen?.() : onMenuClose?.(); }; return ( {({isOpen, onOpen, clickAway, boundaryRef, triggerRef}) => (
{ e.stopPropagation(); onOpen(); }} className={classify( css.inlineButton, { [css.disabled]: disabled, }, css[size], classNames?.buttonWrapper, )} > {children} {isOpen && menu && (
element. This means the menu would otherwise default to the body's width. To support fluid width, we must manually set the dropdown width here; otherwise, it uses a fixed width. Also, Only treat menu as non-fluid if isFluid is strictly false, since default is true in menu and undefined means fluid. */ ...(menu.isFluid !== false && { '--dropdown-width': dropdownWidth, }), '--menu-elevation': getElevationValue(elevation), }} > { onOptionSelect && onOptionSelect(option, e); if ( // option.keepMenuOpenOnOptionSelect - to allow the menu persist its open stat upon option selection in normal variant !option.keepMenuOpenOnOptionSelect && (!menu.optionsVariant || menu.optionsVariant === 'normal') ) { clickAway(); } }} size={menu.size || 'medium'} onTabOut={clickAway} />
)}
)}
); }, );