Skip to content

Commit ac60bdb

Browse files
committed
feat!(utils): Updated the HoverMode API
BREAKING CHANGE: The `useHoverMode` hook no longer returns `stickyHandlers` and instead returns `hoverHandlers` that only include `onMouseEnter` and `onMouseLeave`. The `handlers` that are returned now include `onClick`, `onMouseEnter`, and `onMouseLeave`. This was kind of what the `stickyHandlers` was before. In addition, clicking an element no longer disabled the hover mode behavior. BREAKING CHANGE: The `useHoverMode` hook no longer accepts an `HTMLElement` generic and instead the event handlers will automatically infer the `HTMLElement` based on usage. BREAKING CHANGE: `DEFAULT_HOVER_MODE_STICKY_EXIT_TIME` has been renamed to `DEFAULT_HOVER_MODE_EXIT_TIME`. BREAKING CHANGE: The following typescript types have been removed: `HoverModeOnlyOptions`, `HoverModeOnlyReturnValue` BREAKING CHANGE: The `exitVisibilityDelay` always defaults to `DEFAULT_HOVER_MODE_EXIT_TIME`.
1 parent 2752a98 commit ac60bdb

6 files changed

Lines changed: 244 additions & 252 deletions

File tree

‎packages/documentation/src/components/Demos/Utils/StickyHoverMode.tsx‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,17 @@ import { DialogContent, FixedDialog } from "@react-md/dialog";
44
import { BELOW_CENTER_ANCHOR, useHoverMode } from "@react-md/utils";
55

66
export default function StickyHoverMode(): ReactElement {
7-
const { stuck, active, handlers, stickyHandlers, visible, setVisible } =
8-
useHoverMode({
9-
sticky: true,
10-
});
7+
const { stuck, active, handlers, hoverHandlers, visible, setVisible } =
8+
useHoverMode();
119
const buttonRef = useRef<HTMLButtonElement>(null);
1210

1311
return (
1412
<>
15-
<Button {...stickyHandlers} ref={buttonRef}>
13+
<Button {...handlers} ref={buttonRef}>
1614
Button
1715
</Button>
1816
<FixedDialog
19-
{...handlers}
17+
{...hoverHandlers}
2018
aria-label="Additional Information"
2119
id="some-dialog-id"
2220
visible={visible}

‎packages/documentation/src/components/WikipediaPreviewLink/WikipediaPreviewLink.tsx‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,22 @@ export default function WikipediaPreviewLink({
3030
children,
3131
}: WikipediaPreviewLinkProps): ReactElement {
3232
const id = useId("wiki");
33-
const { active, handlers, visible, setVisible } = useHoverMode();
33+
const { active, hoverHandlers, visible, setVisible } = useHoverMode({
34+
exitVisibilityDelay: 0,
35+
});
3436

3537
const linkRef = useRef<HTMLAnchorElement>(null);
3638
return (
3739
<>
38-
<Link ref={linkRef} href={href} {...handlers}>
40+
<Link ref={linkRef} href={href} {...hoverHandlers}>
3941
{children}
4042
</Link>
4143
<FixedDialog
4244
aria-label="Wikipedia Preview"
4345
id={id}
4446
visible={visible}
4547
onRequestClose={() => setVisible(false)}
46-
{...handlers}
48+
{...hoverHandlers}
4749
fixedTo={linkRef}
4850
onClick={() => {
4951
window.location.href = href;

‎packages/tooltip/src/useTooltip.ts‎

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
CENTER_LEFT_ANCHOR,
2424
CENTER_RIGHT_ANCHOR,
2525
HoverModeEventHandlers,
26-
HoverModeOnlyReturnValue,
26+
HoverModeHookReturnValue,
2727
PositionAnchor,
2828
SimplePosition,
2929
unitToNumber,
@@ -89,9 +89,9 @@ export type TooltipKeyboardEventHandlers<E extends HTMLElement> = Pick<
8989
* @remarks \@since 2.8.0
9090
*/
9191
export type TooltippedElementEventHandlers<E extends HTMLElement> =
92-
HoverModeEventHandlers<E> &
93-
TooltipTouchEventHandlers<E> &
94-
TooltipKeyboardEventHandlers<E>;
92+
TooltipTouchEventHandlers<E> &
93+
TooltipKeyboardEventHandlers<E> &
94+
Pick<HTMLAttributes<E>, keyof HoverModeEventHandlers>;
9595

9696
/** @remarks \@since 2.8.0 */
9797
export interface TooltipHookProvidedElementProps<E extends HTMLElement>
@@ -245,7 +245,7 @@ export type TooltipHookProvidedTooltipProps = Pick<TooltipProps, "style"> &
245245
* @remarks \@since 2.8.0
246246
*/
247247
export interface TooltipHookReturnValue<E extends HTMLElement>
248-
extends Omit<HoverModeOnlyReturnValue<E>, "handlers"> {
248+
extends Omit<HoverModeHookReturnValue, "handlers" | "hoverHandlers"> {
249249
/** {@inheritDoc TooltippedElementEventHandlers} */
250250
handlers: Required<TooltippedElementEventHandlers<E>>;
251251

@@ -334,41 +334,14 @@ export function useTooltip<E extends HTMLElement>({
334334
const {
335335
visible,
336336
setVisible,
337-
handlers: mouseHandlers,
337+
handlers,
338+
hoverHandlers: _hoverHandlers,
338339
disableHoverMode,
340+
clearHoverTimeout,
339341
...others
340-
} = useHoverMode<E>({
342+
} = useHoverMode({
341343
disabled,
342-
onClick: (event) => {
343-
onClick?.(event);
344-
if (event.isPropagationStopped()) {
345-
return;
346-
}
347-
348-
setVisible(false);
349-
setInitiatedBy(null);
350-
window.clearTimeout(timeout.current);
351-
},
352-
onMouseEnter: (event) => {
353-
onMouseEnter?.(event);
354-
if (initiatedBy !== null) {
355-
event.stopPropagation();
356-
return;
357-
}
358-
359-
containerRef.current = event.currentTarget;
360-
updatePosition(event.currentTarget);
361-
setInitiatedBy("mouse");
362-
},
363-
onMouseLeave: (event) => {
364-
onMouseLeave?.(event);
365-
if (initiatedBy !== "mouse") {
366-
event.stopPropagation();
367-
return;
368-
}
369-
370-
setInitiatedBy(null);
371-
},
344+
exitVisibilityDelay: 0,
372345
});
373346
const hide = useCallback(() => {
374347
window.clearTimeout(timeout.current);
@@ -530,12 +503,44 @@ export function useTooltip<E extends HTMLElement>({
530503
});
531504

532505
const tooltipHandlers: Required<TooltippedElementEventHandlers<E>> = {
533-
...mouseHandlers,
534506
onFocus,
535507
onBlur,
536508
onKeyDown,
537509
onTouchStart,
538510
onContextMenu,
511+
onClick(event) {
512+
onClick?.(event);
513+
if (event.isPropagationStopped()) {
514+
return;
515+
}
516+
517+
setVisible(false);
518+
setInitiatedBy(null);
519+
window.clearTimeout(timeout.current);
520+
clearHoverTimeout();
521+
},
522+
onMouseEnter(event) {
523+
onMouseEnter?.(event);
524+
if (initiatedBy !== null) {
525+
event.stopPropagation();
526+
return;
527+
}
528+
529+
containerRef.current = event.currentTarget;
530+
updatePosition(event.currentTarget);
531+
setInitiatedBy("mouse");
532+
handlers.onMouseEnter(event);
533+
},
534+
onMouseLeave(event) {
535+
onMouseLeave?.(event);
536+
if (initiatedBy !== "mouse") {
537+
event.stopPropagation();
538+
return;
539+
}
540+
541+
setInitiatedBy(null);
542+
handlers.onMouseLeave(event);
543+
},
539544
};
540545

541546
const tooltipId = `${baseId}-tooltip`;
@@ -562,5 +567,6 @@ export function useTooltip<E extends HTMLElement>({
562567
elementProps,
563568
tooltipProps,
564569
disableHoverMode,
570+
clearHoverTimeout,
565571
};
566572
}

0 commit comments

Comments
 (0)