Files
xLog/src/components/ui/Tooltip.tsx
T
2022-06-01 00:58:02 +08:00

64 lines
1.3 KiB
TypeScript

import { cloneElement, useState } from "react"
import {
Placement,
offset,
flip,
shift,
autoUpdate,
useFloating,
useInteractions,
useHover,
useFocus,
useRole,
useDismiss,
} from "@floating-ui/react-dom-interactions"
interface Props {
label: string
placement?: Placement
children: JSX.Element
}
export const Tooltip = ({ children, label, placement = "top" }: Props) => {
const [open, setOpen] = useState(false)
const { x, y, reference, floating, strategy, context } = useFloating({
placement,
open,
onOpenChange: setOpen,
middleware: [offset(5), flip(), shift({ padding: 8 })],
whileElementsMounted: autoUpdate,
})
const { getReferenceProps, getFloatingProps } = useInteractions([
useHover(context),
useFocus(context),
useRole(context, { role: "tooltip" }),
useDismiss(context),
])
return (
<>
{cloneElement(
children,
getReferenceProps({ ref: reference, ...children.props }),
)}
{open && (
<div
{...getFloatingProps({
ref: floating,
className: "bg-black/75 text-white rounded-lg shadow-lg px-3 py-2",
style: {
position: strategy,
top: y ?? "",
left: x ?? "",
},
})}
>
{label}
</div>
)}
</>
)
}