Skip to content

Commit 3d05afe

Browse files
committed
fix(many): fix Tooltip focus issues and make Tooltip closeable inside of a Modal
INSTUI-4459
1 parent bfe16ea commit 3d05afe

File tree

5 files changed

+25
-6
lines changed

5 files changed

+25
-6
lines changed

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ class FocusRegion {
5050
constructor(element: Element | Node | null, options: FocusRegionOptions) {
5151
this._options = options || {
5252
shouldCloseOnDocumentClick: true,
53-
shouldCloseOnEscape: true
53+
shouldCloseOnEscape: true,
54+
isTooltip: false
5455
}
5556
this._contextElement = element
5657
this._screenReaderFocusRegion = new ScreenReaderFocusRegion(
@@ -122,6 +123,10 @@ class FocusRegion {
122123
if (fileInputFocused) {
123124
;(<HTMLInputElement>activeElement).blur()
124125
} else {
126+
//This should prevent a Tooltip from closing when inside of a Modal
127+
if (this._options.isTooltip) {
128+
event.stopPropagation()
129+
}
125130
this.handleDismiss(event)
126131
}
127132
}
@@ -169,9 +174,14 @@ class FocusRegion {
169174
}
170175
})
171176
}
172-
177+
//This will ensure that the Tooltip's Escape event listener is executed first
178+
//because listeners in the capturing phase are called before other event listeners (like that of the Modal's Escape listener)
179+
//so a Modal with a Tooltip will not get closed when closing the Tooltip with Escape
180+
const useCapture = this._options.isTooltip
173181
if (this._options.shouldCloseOnEscape) {
174-
this._listeners.push(addEventListener(doc, 'keyup', this.handleKeyUp))
182+
this._listeners.push(
183+
addEventListener(doc, 'keyup', this.handleKeyUp, useCapture)
184+
)
175185
}
176186

177187
this._active = true

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,8 @@ export type FocusRegionOptions = {
8282
* provides a reference to the underlying html root element
8383
*/
8484
elementRef?: (element: Element | null) => void
85+
/**
86+
* Whether or not the element is a Tooltip
87+
*/
88+
isTooltip?: boolean
8589
}

packages/ui-dialog/src/Dialog/props.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,11 @@ const propTypes: PropValidators<PropKeys> = {
132132
/**
133133
* provides a reference to the underlying html root element
134134
*/
135-
elementRef: PropTypes.func
135+
elementRef: PropTypes.func,
136+
/**
137+
* Whether or not the element is a Tooltip
138+
*/
139+
isTooltip: PropTypes.bool
136140
}
137141

138142
const allowedProps: AllowedPropKeys = [

packages/ui-popover/src/Popover/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,8 @@ class Popover extends Component<PopoverProps, PopoverState> {
183183
this._focusRegion = new FocusRegion(this._contentElement, {
184184
shouldCloseOnEscape: this.props.shouldCloseOnEscape,
185185
shouldCloseOnDocumentClick: false,
186-
onDismiss: this.hide
186+
onDismiss: this.hide,
187+
isTooltip: true
187188
})
188189

189190
if (this.shown) {

packages/ui-tooltip/src/Tooltip/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ class Tooltip extends Component<TooltipProps, TooltipState> {
159159
defaultIsShowingContent={defaultIsShowingContent}
160160
on={on}
161161
shouldRenderOffscreen
162-
shouldReturnFocus={true}
162+
shouldReturnFocus={false}
163163
placement={placement}
164164
color={color === 'primary' ? 'primary-inverse' : 'primary'}
165165
mountNode={mountNode}

0 commit comments

Comments
 (0)