Skip to content

Commit

Permalink
fix(ui-a11y-utils): fix focus region missing mouse down target
Browse files Browse the repository at this point in the history
  • Loading branch information
czeslaaw authored and matyasf committed Feb 11, 2025
1 parent b092b45 commit e37aee9
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions packages/ui-a11y-utils/src/FocusRegion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ class FocusRegion {
private readonly _id: string
private _listeners: ReturnType<typeof addEventListener>[] = []
private _active = false
private _documentClickTarget: Node | null = null
private _contextContainsTarget = false
private _documentMouseDownTarget: Node | null = null

constructor(element: Element | Node | null, options: FocusRegionOptions) {
this._options = options || {
Expand Down Expand Up @@ -83,19 +82,17 @@ class FocusRegion {
}

captureDocumentMousedown = (event: React.MouseEvent) => {
this._documentClickTarget = event.target as Node
this._contextContainsTarget = contains(
this._contextElement,
this._documentClickTarget
)
// FocusRegion can be activated after mousedown but before click so this is not guaranteed to fire.
this._documentMouseDownTarget = event.target as Node
}

handleDocumentClick = (event: React.PointerEvent) => {
if (
this._options.shouldCloseOnDocumentClick &&
event.button === 0 &&
event.detail > 0 && // if event.detail is 0 then this is a keyboard and not a mouse press
!this._contextContainsTarget
this._documentMouseDownTarget !== null &&
!contains(this._contextElement, this._documentMouseDownTarget)
) {
this.handleDismiss(event, true)
}
Expand Down

0 comments on commit e37aee9

Please sign in to comment.