pleroma/app/soapbox/components/ui/tooltip/tooltip.tsx

68 lines
1.9 KiB
TypeScript
Raw Normal View History

2022-07-06 11:19:51 -07:00
import { TooltipPopup, useTooltip } from '@reach/tooltip';
2022-03-21 11:09:01 -07:00
import React from 'react';
import Portal from '../portal/portal';
2022-03-21 11:09:01 -07:00
import './tooltip.css';
interface ITooltip {
/** Text to display in the tooltip. */
text: string
2023-01-10 15:03:15 -08:00
/** Element to display the tooltip around. */
children: React.ReactNode
2022-03-21 11:09:01 -07:00
}
2022-07-06 11:19:51 -07:00
const centered = (triggerRect: any, tooltipRect: any) => {
const triggerCenter = triggerRect.left + triggerRect.width / 2;
const left = triggerCenter - tooltipRect.width / 2;
const maxLeft = window.innerWidth - tooltipRect.width - 2;
return {
left: Math.min(Math.max(2, left), maxLeft) + window.scrollX,
top: triggerRect.bottom + 8 + window.scrollY,
};
};
/** Hoverable tooltip element. */
2022-03-21 11:09:01 -07:00
const Tooltip: React.FC<ITooltip> = ({
children,
text,
}) => {
2022-07-06 11:19:51 -07:00
// get the props from useTooltip
const [trigger, tooltip] = useTooltip();
// destructure off what we need to position the triangle
const { isVisible, triggerRect } = tooltip;
2022-03-21 11:09:01 -07:00
return (
2022-07-06 11:19:51 -07:00
<React.Fragment>
{React.cloneElement(children as any, trigger)}
{isVisible && (
// The Triangle. We position it relative to the trigger, not the popup
// so that collisions don't have a triangle pointing off to nowhere.
// Using a Portal may seem a little extreme, but we can keep the
// positioning logic simpler here instead of needing to consider
// the popup's position relative to the trigger and collisions
<Portal>
<div
data-reach-tooltip-arrow='true'
style={{
left:
triggerRect && triggerRect.left - 10 + triggerRect.width / 2 as any,
top: triggerRect && triggerRect.bottom + window.scrollY as any,
}}
/>
</Portal>
)}
<TooltipPopup
{...tooltip}
label={text}
aria-label={text}
position={centered}
/>
</React.Fragment>
2022-03-21 11:09:01 -07:00
);
};
export default Tooltip;