import React, { FC, ReactNode, useEffect, useState } from 'react'; import { Placement } from '@popperjs/core'; import classNames from 'classnames'; import { usePopper } from 'react-popper'; import { Icon } from '~/components/input/Icon'; import { useFocusEvent } from '~/hooks/dom/useFocusEvent'; import styles from './styles.module.scss'; interface MenuButtonProps { position?: Placement; icon?: ReactNode; className?: string; activate?: 'hover' | 'focus'; fixed?: boolean; translucent?: boolean; } const MenuButton: FC = ({ position = 'auto', children, className, icon = , translucent = true, activate = 'focus', fixed, }) => { const focus = useFocusEvent(false, 300); const hover = useFocusEvent(false, 300); const [referenceElement, setReferenceElement] = useState(null); const [popperElement, setPopperElement] = useState( null, ); const [arrowElement, setArrowElement] = useState(null); const popper = usePopper(referenceElement, popperElement, { placement: position, strategy: fixed ? 'fixed' : 'absolute', modifiers: [ { name: 'arrow', options: { element: arrowElement } }, { name: 'offset', options: { offset: [0, 10], }, }, { name: 'preventOverflow', options: { padding: 10, }, }, ], }); const visible = activate === 'focus' ? focus.focused : hover.focused; useEffect(() => { popper.update?.(); }, [visible]); return ( <>
{children}
); }; export { MenuButton };