Skip to content

Commit f08f85b

Browse files
TooltipV2: Set the tooltip position when the popover open not when the toggle event is triggered (#4327)
* do not set position if it was set before * console * only position set on tooltip show * remove event listener with the same func signature * just test * calculate anchor position after popover-open class is added * clean up * comment tidy up * fix tests * changeset * move setting popover auto back in the render * update snapshots
1 parent 69f4489 commit f08f85b

File tree

4 files changed

+29
-27
lines changed

4 files changed

+29
-27
lines changed

.changeset/beige-meals-doubt.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@primer/react': patch
3+
---
4+
5+
Tooltip v2: Set the tooltip position when popover-open attribute is added to the tooltip element not the toggle event is triggered

packages/react/src/TooltipV2/Tooltip.tsx

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const StyledTooltip = styled.span`
2323
/* Overriding the default popover styles */
2424
display: none;
2525
&[popover] {
26+
position: absolute;
2627
padding: 0.5em 0.75em;
2728
width: max-content;
2829
margin: auto;
@@ -196,7 +197,22 @@ export const Tooltip = React.forwardRef(
196197

197198
const openTooltip = () => {
198199
if (tooltipElRef.current && triggerRef.current && !tooltipElRef.current.matches(':popover-open')) {
199-
tooltipElRef.current.showPopover()
200+
const tooltip = tooltipElRef.current
201+
const trigger = triggerRef.current
202+
tooltip.showPopover()
203+
/*
204+
* TOOLTIP POSITIONING
205+
*/
206+
const settings = {
207+
side: directionToPosition[direction].side,
208+
align: directionToPosition[direction].align,
209+
}
210+
const {top, left, anchorAlign, anchorSide} = getAnchoredPosition(tooltip, trigger, settings)
211+
// This is required to make sure the popover is positioned correctly i.e. when there is not enough space on the specified direction, we set a new direction to position the ::after
212+
const calculatedDirection = positionToDirection[`${anchorSide}-${anchorAlign}` as string]
213+
setCalculatedDirection(calculatedDirection)
214+
tooltip.style.top = `${top}px`
215+
tooltip.style.left = `${left}px`
200216
}
201217
}
202218
const closeTooltip = () => {
@@ -239,32 +255,8 @@ export const Tooltip = React.forwardRef(
239255
}
240256
}
241257

242-
/*
243-
* TOOLTIP POSITIONING
244-
*/
245258
const tooltip = tooltipElRef.current
246-
const trigger = triggerRef.current
247259
tooltip.setAttribute('popover', 'auto')
248-
const settings = {
249-
side: directionToPosition[direction].side,
250-
align: directionToPosition[direction].align,
251-
}
252-
253-
const positionSet = () => {
254-
const {top, left, anchorAlign, anchorSide} = getAnchoredPosition(tooltip, trigger, settings)
255-
256-
tooltip.style.top = `${top}px`
257-
tooltip.style.left = `${left}px`
258-
// This is required to make sure the popover is positioned correctly i.e. when there is not enough space on the specified direction, we set a new direction to position the ::after
259-
const calculatedDirection = positionToDirection[`${anchorSide}-${anchorAlign}` as string]
260-
setCalculatedDirection(calculatedDirection)
261-
}
262-
263-
tooltip.addEventListener('toggle', positionSet)
264-
265-
return () => {
266-
tooltip.removeEventListener('toggle', positionSet)
267-
}
268260
}, [tooltipElRef, triggerRef, direction, type])
269261

270262
return (

packages/react/src/__tests__/ActionMenu.test.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {render as HTMLRender, waitFor} from '@testing-library/react'
1+
import {render as HTMLRender, waitFor, act} from '@testing-library/react'
22
import userEvent from '@testing-library/user-event'
33
import {axe} from 'jest-axe'
44
import React from 'react'
@@ -360,7 +360,10 @@ describe('ActionMenu', () => {
360360
),
361361
)
362362
const button = component.getByRole('button')
363-
button.focus()
363+
act(() => {
364+
button.focus()
365+
})
366+
364367
expect(component.getByRole('tooltip')).toBeInTheDocument()
365368
})
366369

packages/react/src/__tests__/__snapshots__/TextInput.test.tsx.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1926,6 +1926,7 @@ exports[`TextInput renders trailingAction icon button 1`] = `
19261926
}
19271927
19281928
.c5[popover] {
1929+
position: absolute;
19291930
padding: 0.5em 0.75em;
19301931
width: -webkit-max-content;
19311932
width: -moz-max-content;
@@ -2920,6 +2921,7 @@ exports[`TextInput renders trailingAction text button with a tooltip 1`] = `
29202921
}
29212922
29222923
.c6[popover] {
2924+
position: absolute;
29232925
padding: 0.5em 0.75em;
29242926
width: -webkit-max-content;
29252927
width: -moz-max-content;

0 commit comments

Comments
 (0)