forked from mantinedev/mantine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
use-tooltip.ts
114 lines (103 loc) · 2.8 KB
/
use-tooltip.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { useState, useCallback } from 'react';
import {
useFloating,
flip,
arrow,
offset,
shift,
useInteractions,
useHover,
useFocus,
useRole,
useDismiss,
useDelayGroupContext,
useDelayGroup,
} from '@floating-ui/react-dom-interactions';
import { useId, useDidUpdate } from '@mantine/hooks';
import { useTooltipGroupContext } from './TooltipGroup/TooltipGroup.context';
import { FloatingPosition, useFloatingAutoUpdate } from '../Floating';
interface UseTooltip {
position: FloatingPosition;
closeDelay: number;
openDelay: number;
onPositionChange?(position: FloatingPosition): void;
opened?: boolean;
offset: number;
arrowRef?: React.RefObject<HTMLDivElement>;
events: { hover: boolean; focus: boolean; touch: boolean };
positionDependencies: any[];
}
export function useTooltip(settings: UseTooltip) {
const [uncontrolledOpened, setUncontrolledOpened] = useState(false);
const controlled = typeof settings.opened === 'boolean';
const opened = controlled ? settings.opened : uncontrolledOpened;
const withinGroup = useTooltipGroupContext();
const uid = useId();
const { delay: groupDelay, currentId, setCurrentId } = useDelayGroupContext();
const onChange = useCallback(
(_opened: boolean) => {
setUncontrolledOpened(_opened);
if (_opened) {
setCurrentId(uid);
}
},
[setCurrentId, uid]
);
const {
x,
y,
reference,
floating,
context,
refs,
update,
placement,
middlewareData: { arrow: { x: arrowX, y: arrowY } = {} },
} = useFloating({
placement: settings.position,
open: opened,
onOpenChange: onChange,
middleware: [
offset(settings.offset),
shift({ padding: 8 }),
flip(),
arrow({
element: settings.arrowRef,
}),
],
});
const { getReferenceProps, getFloatingProps } = useInteractions([
useHover(context, {
enabled: settings.events.hover,
delay: withinGroup ? groupDelay : { open: settings.openDelay, close: settings.closeDelay },
mouseOnly: !settings.events.touch,
}),
useFocus(context, { enabled: settings.events.focus, keyboardOnly: true }),
useRole(context, { role: 'tooltip' }),
// cannot be used with controlled tooltip, page jumps
useDismiss(context, { enabled: typeof settings.opened === undefined }),
useDelayGroup(context, { id: uid }),
]);
useFloatingAutoUpdate({
opened,
positionDependencies: settings.positionDependencies,
floating: { refs, update },
});
useDidUpdate(() => {
settings.onPositionChange?.(placement);
}, [placement]);
const isGroupPhase = opened && currentId && currentId !== uid;
return {
x,
y,
arrowX,
arrowY,
reference,
floating,
getFloatingProps,
getReferenceProps,
isGroupPhase,
opened,
placement,
};
}