Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function TriangleTooltip(props: { children: React.ReactNode; label: React.ReactNode; ariaLabel?: React.ReactNode }) {
const globalVars = globalVariables();
const { children, label, ariaLabel } = props;
// get the props from useTooltip
const [trigger, tooltip] = useTooltip();
// destructure off what we need to position the triangle
const { isVisible, triggerRect } = tooltip;
const [hasOverflow, setHasOverflow] = useState(false);
const classes = toolTipClasses();
const toolTipVars = tooltipVariables();
const borderOffset = globalVars.border.width * 2;
const toolBoxPosition = (triggerRect, tooltipRect) => {
const triangleHeight = toolTipVars.nub.width / 2;
const triggerCenter = triggerRect.left + triggerRect.width / 2;
const left = triggerCenter - tooltipRect.width / 2;
const maxLeft = window.innerWidth - tooltipRect.width - 2;
const hasOverflow = triggerRect.bottom + tooltipRect.height + triangleHeight > window.innerHeight;
function ExampleTriangle({ children, ...rest }) {
// get the props from useTooltip
const [trigger, tooltip] = useTooltip();
// destructure off what we need to position the triangle
const { isVisible, triggerRect } = tooltip;
return (
{cloneElement(children, 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
export function Example() {
const handleRef = useRef();
const [trigger, tooltip] = useTooltip();
const centered = (triggerRect, tooltipRect) => {
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.pageXOffset,
top: triggerRect.bottom + 8 + window.pageYOffset
};
};
// We want to show the tooltip whenever the handle is focused, regardless
// of what happens with mouse events.
const preventDefaultWhenFocused = useCallback(event => {
if (document.activeElement === event.currentTarget) {
event.preventDefault();
}
function TriangleTooltip(props: TriangleTooltipProps) {
const { label, children, ariaLabel } = props;
// get the props from useTooltip
const [trigger, tooltip] = useTooltip();
// Tooltip direction
const [direction, setDirection] = React.useState('bottom');
// destructure off what we need to position the triangle
const { isVisible, triggerRect } = tooltip;
let arrowTop;
// Top
if (direction === 'top') {
arrowTop = triggerRect && triggerRect.bottom - (triggerRect.height + ARROW_SIZE) + window.scrollY;
}
// Bottom
if (direction === 'bottom') {
function ExampleAnimatedTooltip({ children, ...rest }) {
const [trigger, tooltip, isVisible] = useTooltip();
const transitions = useTransition(isVisible ? tooltip : false, null, {
from: { opacity: 0 },
enter: { opacity: 1 },
leave: { opacity: 0 },
config: { mass: 1, tension: 500, friction: 40 }
});
return (
{cloneElement(children, trigger)}
{transitions.map(
({ item: tooltip, props: styles, key }) =>
tooltip && (