Skip to content

Commit 7a64409

Browse files
committed
Support tooltip on disabled buttons, support click action
1 parent 25d6152 commit 7a64409

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"node": ">=14"
55
},
66
"dependencies": {
7-
"@floating-ui/react-dom-interactions": "^0.6.3",
7+
"@floating-ui/react-dom-interactions": "^0.6.6",
88
"@fortawesome/fontawesome-svg-core": "^1.2.32",
99
"@fortawesome/free-solid-svg-icons": "^5.15.1",
1010
"@fortawesome/react-fontawesome": "^0.1.11",

resources/scripts/components/elements/tooltip/Tooltip.tsx

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
Placement,
88
shift,
99
Side,
10-
Strategy,
10+
useClick,
1111
useDismiss,
1212
useFloating,
1313
useFocus,
@@ -18,15 +18,16 @@ import {
1818
import { AnimatePresence, motion } from 'framer-motion';
1919
import classNames from 'classnames';
2020

21+
type Interaction = 'hover' | 'click' | 'focus';
22+
2123
interface Props {
2224
rest?: number;
2325
delay?: number | Partial<{ open: number; close: number }>;
24-
alwaysOpen?: boolean;
2526
content: string | React.ReactChild;
2627
disabled?: boolean;
2728
arrow?: boolean;
29+
interactions?: Interaction[];
2830
placement?: Placement;
29-
strategy?: Strategy;
3031
className?: string;
3132
children: React.ReactElement;
3233
}
@@ -38,35 +39,41 @@ const arrowSides: Record<Side, string> = {
3839
left: 'top-0 right-[-6px]',
3940
};
4041

41-
export default ({ content, children, disabled = false, alwaysOpen = false, delay = 0, rest = 30, ...props }: Props) => {
42+
export default ({ children, ...props }: Props) => {
4243
const arrowEl = useRef<HTMLDivElement>(null);
43-
const [open, setOpen] = useState(alwaysOpen || false);
44+
const [open, setOpen] = useState(false);
4445

4546
const { x, y, reference, floating, middlewareData, strategy, context } = useFloating({
4647
open,
48+
strategy: 'fixed',
4749
placement: props.placement || 'top',
48-
strategy: props.strategy || 'absolute',
4950
middleware: [
5051
offset(props.arrow ? 10 : 6),
5152
flip(),
5253
shift({ padding: 6 }),
5354
arrow({ element: arrowEl, padding: 6 }),
5455
],
55-
onOpenChange: (o) => setOpen(o || alwaysOpen || false),
56+
onOpenChange: setOpen,
5657
whileElementsMounted: autoUpdate,
5758
});
5859

60+
const interactions = props.interactions || ['hover', 'focus'];
5961
const { getReferenceProps, getFloatingProps } = useInteractions([
60-
useFocus(context),
61-
useHover(context, { restMs: rest, delay }),
62+
useHover(context, {
63+
restMs: props.rest ?? 30,
64+
delay: props.delay ?? 0,
65+
enabled: interactions.includes('hover'),
66+
}),
67+
useFocus(context, { enabled: interactions.includes('focus') }),
68+
useClick(context, { enabled: interactions.includes('click') }),
6269
useRole(context, { role: 'tooltip' }),
6370
useDismiss(context),
6471
]);
6572

6673
const side = arrowSides[(props.placement || 'top').split('-')[0] as Side];
6774
const { x: ax, y: ay } = middlewareData.arrow || {};
6875

69-
if (disabled) {
76+
if (props.disabled) {
7077
return children;
7178
}
7279

@@ -83,15 +90,15 @@ export default ({ content, children, disabled = false, alwaysOpen = false, delay
8390
{...getFloatingProps({
8491
ref: floating,
8592
className:
86-
'absolute top-0 left-0 bg-gray-900 text-sm text-gray-200 px-3 py-2 rounded pointer-events-none max-w-[20rem] z-[9999]',
93+
'bg-gray-900 text-sm text-gray-200 px-3 py-2 rounded pointer-events-none max-w-[24rem]',
8794
style: {
8895
position: strategy,
8996
top: `${y || 0}px`,
9097
left: `${x || 0}px`,
9198
},
9299
})}
93100
>
94-
{content}
101+
{props.content}
95102
{props.arrow && (
96103
<div
97104
ref={arrowEl}

0 commit comments

Comments
 (0)