From 4e4b82e065be81eeb86e6a74a9aef47ad9ebe3cc Mon Sep 17 00:00:00 2001 From: gcornut Date: Mon, 11 Dec 2023 18:27:16 +0100 Subject: [PATCH 1/2] fix(tooltip): fixed closing when mouse is hovering the tooltip text DSW-47 --- CHANGELOG.md | 4 + packages/lumx-core/src/js/constants/index.ts | 2 +- .../src/components/tooltip/Tooltip.test.tsx | 32 +++++++ .../src/components/tooltip/Tooltip.tsx | 7 +- .../tooltip/useInjectTooltipRef.tsx | 2 +- .../src/components/tooltip/useTooltipOpen.tsx | 85 +++++++++++-------- 6 files changed, 92 insertions(+), 40 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 536c3912b..13b819c8b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- Tooltip: fixed tooltip closing when mouse is hovering the tooltip text. + ## [3.6.0][] - 2023-12-05 ### Changed diff --git a/packages/lumx-core/src/js/constants/index.ts b/packages/lumx-core/src/js/constants/index.ts index a8d2658da..7b46d4d6f 100644 --- a/packages/lumx-core/src/js/constants/index.ts +++ b/packages/lumx-core/src/js/constants/index.ts @@ -22,7 +22,7 @@ export const SLIDESHOW_TRANSITION_DURATION = 5000; */ export const TOOLTIP_HOVER_DELAY = { open: 500, - close: 0, + close: 500, }; /** diff --git a/packages/lumx-react/src/components/tooltip/Tooltip.test.tsx b/packages/lumx-react/src/components/tooltip/Tooltip.test.tsx index 89f42a130..1d93fd3bb 100644 --- a/packages/lumx-react/src/components/tooltip/Tooltip.test.tsx +++ b/packages/lumx-react/src/components/tooltip/Tooltip.test.tsx @@ -125,6 +125,38 @@ describe(`<${Tooltip.displayName}>`, () => { }); }); + it('should activate on hover anchor and then tooltip', async () => { + let { tooltip } = await setup({ + label: 'Tooltip label', + children: , + forceOpen: false, + }); + + expect(tooltip).not.toBeInTheDocument(); + + // Hover anchor button + const button = getByClassName(document.body, Button.className as string); + await userEvent.hover(button); + + // Tooltip opened + tooltip = await findByClassName(document.body, CLASSNAME); + expect(tooltip).toBeInTheDocument(); + expect(button).toHaveAttribute('aria-describedby', tooltip?.id); + + // Hover tooltip + await userEvent.hover(tooltip); + expect(tooltip).toBeInTheDocument(); + expect(button).toHaveAttribute('aria-describedby', tooltip?.id); + + // Un-hover tooltip + userEvent.unhover(tooltip); + await waitFor(() => { + expect(button).not.toHaveFocus(); + // Tooltip closed + expect(tooltip).not.toBeInTheDocument(); + }); + }); + it('should activate on anchor focus', async () => { let { tooltip } = await setup({ label: 'Tooltip label', diff --git a/packages/lumx-react/src/components/tooltip/Tooltip.tsx b/packages/lumx-react/src/components/tooltip/Tooltip.tsx index 34df77df5..1aadcf087 100644 --- a/packages/lumx-react/src/components/tooltip/Tooltip.tsx +++ b/packages/lumx-react/src/components/tooltip/Tooltip.tsx @@ -85,8 +85,9 @@ export const Tooltip: Comp = forwardRef((props, re }); const position = attributes?.popper?.['data-popper-placement'] ?? placement; - const isOpen = useTooltipOpen(delay, anchorElement) || forceOpen; - const wrappedChildren = useInjectTooltipRef(children, setAnchorElement, isOpen as boolean, id); + const { isOpen: isActivated, onPopperMount } = useTooltipOpen(delay, anchorElement); + const isOpen = isActivated || forceOpen; + const wrappedChildren = useInjectTooltipRef(children, setAnchorElement, isOpen, id); return ( <> @@ -94,7 +95,7 @@ export const Tooltip: Comp = forwardRef((props, re {isOpen && createPortal(