2022-06-05 22:27:54 +00:00
|
|
|
import React, { cloneElement, useRef, useState } from 'react';
|
|
|
|
import {
|
|
|
|
arrow,
|
|
|
|
autoUpdate,
|
|
|
|
flip,
|
|
|
|
offset,
|
|
|
|
Placement,
|
|
|
|
shift,
|
|
|
|
Side,
|
|
|
|
Strategy,
|
|
|
|
useDismiss,
|
|
|
|
useFloating,
|
|
|
|
useFocus,
|
|
|
|
useHover,
|
|
|
|
useInteractions,
|
|
|
|
useRole,
|
|
|
|
} from '@floating-ui/react-dom-interactions';
|
|
|
|
import { AnimatePresence, motion } from 'framer-motion';
|
2022-06-12 19:07:52 +00:00
|
|
|
import classNames from 'classnames';
|
2022-06-05 22:27:54 +00:00
|
|
|
|
|
|
|
interface Props {
|
2022-06-12 13:35:02 +00:00
|
|
|
rest?: number;
|
|
|
|
delay?: number | Partial<{ open: number; close: number }>;
|
|
|
|
alwaysOpen?: boolean;
|
2022-06-05 22:27:54 +00:00
|
|
|
content: string | React.ReactChild;
|
2022-06-05 22:35:53 +00:00
|
|
|
disabled?: boolean;
|
2022-06-05 22:27:54 +00:00
|
|
|
arrow?: boolean;
|
|
|
|
placement?: Placement;
|
|
|
|
strategy?: Strategy;
|
|
|
|
className?: string;
|
|
|
|
children: React.ReactElement;
|
|
|
|
}
|
|
|
|
|
2022-06-12 19:07:52 +00:00
|
|
|
const arrowSides: Record<Side, string> = {
|
|
|
|
top: 'bottom-[-6px] left-0',
|
|
|
|
bottom: 'top-[-6px] left-0',
|
|
|
|
right: 'top-0 left-[-6px]',
|
|
|
|
left: 'top-0 right-[-6px]',
|
2022-06-05 22:27:54 +00:00
|
|
|
};
|
|
|
|
|
2022-06-12 13:35:02 +00:00
|
|
|
export default ({
|
|
|
|
content,
|
|
|
|
children,
|
|
|
|
disabled = false,
|
|
|
|
alwaysOpen = false,
|
2022-06-12 13:39:20 +00:00
|
|
|
delay = 0,
|
2022-06-12 13:35:02 +00:00
|
|
|
rest = 30,
|
|
|
|
...props
|
|
|
|
}: Props) => {
|
2022-06-05 22:35:53 +00:00
|
|
|
const arrowEl = useRef<HTMLDivElement>(null);
|
2022-06-12 13:35:02 +00:00
|
|
|
const [ open, setOpen ] = useState(alwaysOpen || false);
|
2022-06-05 22:27:54 +00:00
|
|
|
|
|
|
|
const { x, y, reference, floating, middlewareData, strategy, context } = useFloating({
|
|
|
|
open,
|
|
|
|
placement: props.placement || 'top',
|
|
|
|
strategy: props.strategy || 'absolute',
|
2022-06-12 13:35:02 +00:00
|
|
|
middleware: [
|
|
|
|
offset(props.arrow ? 10 : 6),
|
|
|
|
flip(),
|
|
|
|
shift({ padding: 6 }),
|
|
|
|
arrow({ element: arrowEl, padding: 6 }),
|
|
|
|
],
|
|
|
|
onOpenChange: (o) => setOpen(o || alwaysOpen || false),
|
2022-06-05 22:27:54 +00:00
|
|
|
whileElementsMounted: autoUpdate,
|
|
|
|
});
|
|
|
|
|
|
|
|
const { getReferenceProps, getFloatingProps } = useInteractions([
|
|
|
|
useFocus(context),
|
2022-06-12 13:35:02 +00:00
|
|
|
useHover(context, { restMs: rest, delay }),
|
2022-06-05 22:27:54 +00:00
|
|
|
useRole(context, { role: 'tooltip' }),
|
|
|
|
useDismiss(context),
|
|
|
|
]);
|
|
|
|
|
|
|
|
const side = arrowSides[(props.placement || 'top').split('-')[0] as Side];
|
|
|
|
const { x: ax, y: ay } = middlewareData.arrow || {};
|
|
|
|
|
2022-06-05 22:35:53 +00:00
|
|
|
if (disabled) {
|
|
|
|
return children;
|
|
|
|
}
|
|
|
|
|
2022-06-05 22:27:54 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{cloneElement(children, getReferenceProps({ ref: reference, ...children.props }))}
|
|
|
|
<AnimatePresence>
|
|
|
|
{open &&
|
2022-06-05 22:35:53 +00:00
|
|
|
<motion.div
|
2022-06-05 22:27:54 +00:00
|
|
|
initial={{ opacity: 0, scale: 0.85 }}
|
|
|
|
animate={{ opacity: 1, scale: 1 }}
|
|
|
|
exit={{ opacity: 0 }}
|
2022-06-12 13:35:02 +00:00
|
|
|
transition={{ type: 'spring', damping: 20, stiffness: 300, duration: 0.075 }}
|
2022-06-05 22:27:54 +00:00
|
|
|
{...getFloatingProps({
|
|
|
|
ref: floating,
|
2022-06-12 13:09:01 +00:00
|
|
|
className: 'absolute top-0 left-0 bg-gray-900 text-sm text-gray-200 px-3 py-2 rounded pointer-events-none max-w-[90vw]',
|
2022-06-05 22:27:54 +00:00
|
|
|
style: {
|
|
|
|
position: strategy,
|
|
|
|
top: `${y || 0}px`,
|
|
|
|
left: `${x || 0}px`,
|
|
|
|
},
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
{content}
|
|
|
|
{props.arrow &&
|
2022-06-05 22:35:53 +00:00
|
|
|
<div
|
2022-06-05 22:27:54 +00:00
|
|
|
ref={arrowEl}
|
|
|
|
style={{
|
2022-06-12 13:35:02 +00:00
|
|
|
transform: `translate(${Math.round(ax || 0)}px, ${Math.round(ay || 0)}px) rotate(45deg)`,
|
2022-06-05 22:27:54 +00:00
|
|
|
}}
|
2022-06-12 19:07:52 +00:00
|
|
|
className={classNames('absolute bg-gray-900 w-3 h-3', side)}
|
2022-06-05 22:27:54 +00:00
|
|
|
/>
|
|
|
|
}
|
2022-06-05 22:35:53 +00:00
|
|
|
</motion.div>
|
2022-06-05 22:27:54 +00:00
|
|
|
}
|
|
|
|
</AnimatePresence>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|