Skip to content

Commit c18cbe8

Browse files
committed
fix(ui-a11y-utils): prevent clicking on a Tooltip from closing the parent dialog
INSTUI-4475
1 parent 9c257f4 commit c18cbe8

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed

cypress/component/Tooltip.cy.tsx

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@
2323
*/
2424
import React from 'react'
2525
import 'cypress-real-events'
26+
import { useState } from 'react'
2627

27-
import { Tooltip } from '../../packages/ui'
28+
import { Tooltip, Modal, Button, CloseButton } from '@instructure/ui'
2829
import '../support/component'
2930

3031
describe('<Tooltip/>', () => {
@@ -131,4 +132,64 @@ describe('<Tooltip/>', () => {
131132
cy.contains('Hello').should('not.be.visible')
132133
cy.wrap(onHideContent).should('have.been.calledOnce')
133134
})
135+
136+
it('should not close parent Modal with shouldCloseOnDocumentClick when Tooltip is clicked on', async () => {
137+
const TestModal = () => {
138+
const [open, setOpen] = useState(false)
139+
140+
return (
141+
<div>
142+
<Button
143+
onClick={() => {
144+
setOpen(true)
145+
}}
146+
>
147+
Open the Modal
148+
</Button>
149+
<Modal
150+
label="modal"
151+
open={open}
152+
onDismiss={() => {
153+
setOpen(false)
154+
}}
155+
shouldCloseOnDocumentClick
156+
>
157+
<CloseButton
158+
screenReaderLabel="Close"
159+
onClick={() => {
160+
setOpen(false)
161+
}}
162+
/>
163+
<Tooltip renderTip="Tooltip!">
164+
<Button data-testid="trigger">Hello</Button>
165+
</Tooltip>
166+
</Modal>
167+
</div>
168+
)
169+
}
170+
cy.mount(<TestModal />)
171+
172+
cy.contains('Open the Modal').click()
173+
174+
cy.get('[data-testid="trigger"]').then(($trigger) => {
175+
const tooltipId = $trigger.attr('data-position-target')
176+
const tooltip = `span[data-position-content="${tooltipId}"]`
177+
178+
cy.get(tooltip).should('not.be.visible')
179+
180+
cy.get('[data-testid="trigger"]')
181+
.realHover()
182+
.then(() => {
183+
cy.get(tooltip).should('be.visible')
184+
})
185+
186+
cy.get(tooltip)
187+
.realClick()
188+
.wait(500)
189+
.then(() => {
190+
cy.get(tooltip).should('be.visible')
191+
cy.get('[role="dialog"]').should('be.visible')
192+
})
193+
})
194+
})
134195
})

packages/ui-a11y-utils/src/FocusRegion.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,12 @@ class FocusRegion {
9393
event.button === 0 &&
9494
event.detail > 0 && // if event.detail is 0 then this is a keyboard and not a mouse press
9595
this._documentMouseDownTarget !== null &&
96-
!contains(this._contextElement, this._documentMouseDownTarget)
96+
!contains(this._contextElement, this._documentMouseDownTarget) &&
97+
//this prevents clicking on Tooltip from closing the parent dialog
98+
!(
99+
this._documentMouseDownTarget instanceof HTMLElement &&
100+
this._documentMouseDownTarget.getAttribute('role') === 'tooltip'
101+
)
97102
) {
98103
this.handleDismiss(event, true)
99104
}

0 commit comments

Comments
 (0)