Skip to content

Commit

Permalink
Tooltip2: Prevent closing other overlays when tooltip has the focus a…
Browse files Browse the repository at this point in the history
…nd ESC is hit (#4471)

* prevent closing other overlays when tooltip has the focus

* Create stale-ties-cover.md

* wrap user interaction into act fn
  • Loading branch information
broccolinisoup committed Apr 11, 2024
1 parent ae14db7 commit aa8b6d8
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/stale-ties-cover.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/react": patch
---

Tooltipv2: Prevent closing other overlays when tooltip has the focus when ESC is hit
21 changes: 12 additions & 9 deletions packages/react/src/Overlay/Overlay.features.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
ActionMenu,
} from '..'
import type {AnchorSide} from '@primer/behaviors'
import {Tooltip} from '../TooltipV2'

export default {
title: 'Private/Components/Overlay',
Expand Down Expand Up @@ -302,15 +303,17 @@ export const NestedOverlays = () => {
</CheckboxGroup>
</Box>
<ActionList.Divider />
<Button
variant="invisible"
ref={secondaryButtonRef}
sx={{px: 2, mx: 2, display: 'flex'}}
leadingVisual={PlusIcon}
onClick={() => setCreateListOverlayOpen(!createListOverlayOpen)}
>
Create list
</Button>
<Tooltip text="Allows you to add more lists">
<Button
variant="invisible"
ref={secondaryButtonRef}
sx={{px: 2, mx: 2, display: 'flex'}}
leadingVisual={PlusIcon}
onClick={() => setCreateListOverlayOpen(!createListOverlayOpen)}
>
Create list
</Button>
</Tooltip>
</Box>
{createListOverlayOpen && (
<Overlay
Expand Down
17 changes: 16 additions & 1 deletion packages/react/src/TooltipV2/Tooltip.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, {Children, useEffect, useRef, useState} from 'react'
import type {SxProp} from '../sx'
import sx from '../sx'
import {useId, useProvidedRefOrCreate} from '../hooks'
import {useId, useProvidedRefOrCreate, useOnEscapePress} from '../hooks'
import {invariant} from '../utils/invariant'
import {warning} from '../utils/warning'
import styled from 'styled-components'
Expand Down Expand Up @@ -195,6 +195,8 @@ export const Tooltip = React.forwardRef(

const [calculatedDirection, setCalculatedDirection] = useState<TooltipDirection>(direction)

const [isPopoverOpen, setIsPopoverOpen] = useState(false)

const openTooltip = () => {
if (
tooltipElRef.current &&
Expand All @@ -205,6 +207,7 @@ export const Tooltip = React.forwardRef(
const tooltip = tooltipElRef.current
const trigger = triggerRef.current
tooltip.showPopover()
setIsPopoverOpen(true)
/*
* TOOLTIP POSITIONING
*/
Expand All @@ -228,6 +231,7 @@ export const Tooltip = React.forwardRef(
tooltipElRef.current.matches(':popover-open')
) {
tooltipElRef.current.hidePopover()
setIsPopoverOpen(false)
}
}

Expand Down Expand Up @@ -269,6 +273,17 @@ export const Tooltip = React.forwardRef(
tooltip.setAttribute('popover', 'auto')
}, [tooltipElRef, triggerRef, direction, type])

useOnEscapePress(
(event: KeyboardEvent) => {
if (isPopoverOpen) {
event.stopImmediatePropagation()
event.preventDefault()
closeTooltip()
}
},
[isPopoverOpen],
)

return (
<TooltipContext.Provider value={{tooltipId}}>
<>
Expand Down
5 changes: 4 additions & 1 deletion packages/react/src/__tests__/ActionMenu.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,10 @@ describe('ActionMenu', () => {
),
)
const button = component.getByRole('button')
button.focus()
act(() => {
button.focus()
})

expect(component.getByRole('tooltip')).toBeInTheDocument()
})

Expand Down

0 comments on commit aa8b6d8

Please sign in to comment.