@spaced-out/ui-design-system
Version:
Sense UI components library
182 lines (171 loc) • 6.05 kB
Flow
// @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<HTMLElement>) => mixed,
onMenuOpen?: () => mixed,
onMenuClose?: () => mixed,
size?: 'medium' | 'small' | 'extraSmall',
elevation?: ElevationType,
clickAwayRef?: ClickAwayRefType,
...
};
export const InlineDropdown: React$AbstractComponent<
InlineDropdownProps,
HTMLDivElement,
> = React.forwardRef<InlineDropdownProps, HTMLDivElement>(
(
{
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 (
<ClickAway onChange={onMenuToggle} clickAwayRef={clickAwayRef}>
{({isOpen, onOpen, clickAway, boundaryRef, triggerRef}) => (
<div
data-testid="InlineDropdown"
className={classify(
css.inlineDropdownContainer,
classNames?.dropdownContainer,
)}
ref={ref}
>
<UnstyledButton
{...restButtonProps}
disabled={disabled}
ref={mergeRefs([refs.setReference, triggerRef])}
onClick={(e) => {
e.stopPropagation();
onOpen();
}}
className={classify(
css.inlineButton,
{
[css.disabled]: disabled,
},
css[size],
classNames?.buttonWrapper,
)}
>
<Truncate>{children}</Truncate>
<Icon
name={isOpen ? 'caret-up' : 'caret-down'}
size="small"
type="solid"
className={classify({
[css.disabled]: disabled,
})}
/>
</UnstyledButton>
{isOpen && menu && (
<FloatingPortal>
<FloatingFocusManager
modal={false}
context={context}
initialFocus={triggerRef}
>
<div
className={css.menuWrapper}
ref={mergeRefs([refs.setFloating, boundaryRef])}
style={{
display: 'flex',
position: strategy,
top: y ?? spaceNone,
left: x ?? spaceNone,
/* NOTE(Sharad): The FloatingPortal renders the menu outside the normal DOM structure,
so its parent is effectively the <body> 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),
}}
>
<Menu
{...menu}
onSelect={(option, e) => {
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}
/>
</div>
</FloatingFocusManager>
</FloatingPortal>
)}
</div>
)}
</ClickAway>
);
},
);