misc_pterodactyl-panel/resources/scripts/components/elements/tooltip/Tooltip.tsx

120 lines
3.9 KiB
TypeScript
Raw Normal View History

2022-11-25 20:25:03 +00:00
import { cloneElement, useRef, useState } from 'react';
import * as React from 'react';
2022-06-05 22:27:54 +00:00
import {
arrow,
autoUpdate,
flip,
offset,
Placement,
shift,
Side,
useClick,
2022-06-05 22:27:54 +00:00
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
type Interaction = 'hover' | 'click' | 'focus';
2022-06-05 22:27:54 +00:00
interface Props {
rest?: number;
delay?: number | Partial<{ open: number; close: number }>;
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;
interactions?: Interaction[];
2022-06-05 22:27:54 +00:00
placement?: Placement;
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
};
export default ({ children, ...props }: Props) => {
2022-06-05 22:35:53 +00:00
const arrowEl = useRef<HTMLDivElement>(null);
const [open, setOpen] = useState(false);
2022-06-05 22:27:54 +00:00
const { x, y, reference, floating, middlewareData, strategy, context } = useFloating({
open,
strategy: 'fixed',
2022-06-05 22:27:54 +00:00
placement: props.placement || 'top',
middleware: [
offset(props.arrow ? 10 : 6),
flip(),
shift({ padding: 6 }),
arrow({ element: arrowEl, padding: 6 }),
],
onOpenChange: setOpen,
2022-06-05 22:27:54 +00:00
whileElementsMounted: autoUpdate,
});
const interactions = props.interactions || ['hover', 'focus'];
2022-06-05 22:27:54 +00:00
const { getReferenceProps, getFloatingProps } = useInteractions([
useHover(context, {
restMs: props.rest ?? 30,
delay: props.delay ?? 0,
enabled: interactions.includes('hover'),
}),
useFocus(context, { enabled: interactions.includes('focus') }),
useClick(context, { enabled: interactions.includes('click') }),
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 || {};
if (props.disabled) {
2022-06-05 22:35:53 +00:00
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 }}
transition={{ type: 'spring', damping: 20, stiffness: 300, duration: 0.075 }}
2022-06-05 22:27:54 +00:00
{...getFloatingProps({
ref: floating,
className:
'bg-slate-900 text-sm text-slate-200 px-3 py-2 rounded pointer-events-none max-w-[24rem]',
2022-06-05 22:27:54 +00:00
style: {
position: strategy,
top: `${y || 0}px`,
left: `${x || 0}px`,
},
})}
>
{props.content}
{props.arrow && (
2022-06-05 22:35:53 +00:00
<div
2022-06-05 22:27:54 +00:00
ref={arrowEl}
style={{
transform: `translate(${Math.round(ax || 0)}px, ${Math.round(
2022-11-25 20:25:03 +00:00
ay || 0,
)}px) rotate(45deg)`,
2022-06-05 22:27:54 +00:00
}}
className={classNames('absolute bg-slate-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>
</>
);
};