-
Notifications
You must be signed in to change notification settings - Fork 252
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: dispatch
FocusEvent
in hidden documents (#1252)
- Loading branch information
1 parent
63f7468
commit 1ed8b15
Showing
18 changed files
with
327 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import {dispatchDOMEvent} from '../event' | ||
import {getActiveElement} from '../utils' | ||
|
||
const patched = Symbol('patched focus/blur methods') | ||
|
||
declare global { | ||
interface HTMLElement { | ||
readonly [patched]?: Pick<HTMLElement, 'focus' | 'blur'> | ||
} | ||
} | ||
|
||
export function patchFocus(HTMLElement: typeof globalThis['HTMLElement']) { | ||
if (HTMLElement.prototype[patched]) { | ||
return | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/unbound-method | ||
const {focus, blur} = HTMLElement.prototype | ||
|
||
Object.defineProperties(HTMLElement.prototype, { | ||
focus: { | ||
configurable: true, | ||
get: () => patchedFocus, | ||
}, | ||
blur: { | ||
configurable: true, | ||
get: () => patchedBlur, | ||
}, | ||
[patched]: { | ||
configurable: true, | ||
get: () => ({focus, blur}), | ||
}, | ||
}) | ||
|
||
let activeCall: symbol | ||
|
||
function patchedFocus(this: HTMLElement, options: FocusOptions) { | ||
if (this.ownerDocument.visibilityState !== 'hidden') { | ||
return focus.call(this, options) | ||
} | ||
|
||
const blured = getActiveTarget(this.ownerDocument) | ||
if (blured === this) { | ||
return | ||
} | ||
|
||
const thisCall = Symbol('focus call') | ||
activeCall = thisCall | ||
|
||
if (blured) { | ||
blur.call(blured) | ||
dispatchDOMEvent(blured, 'blur', {relatedTarget: this}) | ||
dispatchDOMEvent(blured, 'focusout', { | ||
relatedTarget: activeCall === thisCall ? this : null, | ||
}) | ||
} | ||
if (activeCall === thisCall) { | ||
focus.call(this, options) | ||
dispatchDOMEvent(this, 'focus', {relatedTarget: blured}) | ||
} | ||
if (activeCall === thisCall) { | ||
dispatchDOMEvent(this, 'focusin', {relatedTarget: blured}) | ||
} | ||
} | ||
|
||
function patchedBlur(this: HTMLElement) { | ||
if (this.ownerDocument.visibilityState !== 'hidden') { | ||
return blur.call(this) | ||
} | ||
|
||
const blured = getActiveTarget(this.ownerDocument) | ||
if (blured !== this) { | ||
return | ||
} | ||
|
||
const thisCall = Symbol('blur call') | ||
activeCall = thisCall | ||
|
||
blur.call(this) | ||
dispatchDOMEvent(blured, 'blur', {relatedTarget: null}) | ||
dispatchDOMEvent(blured, 'focusout', {relatedTarget: null}) | ||
} | ||
} | ||
|
||
function getActiveTarget(document: Document) { | ||
const active = getActiveElement(document) | ||
return active?.tagName === 'BODY' ? null : active | ||
} | ||
|
||
export function restoreFocus(HTMLElement: typeof globalThis['HTMLElement']) { | ||
if (HTMLElement.prototype[patched]) { | ||
const {focus, blur} = HTMLElement.prototype[patched] | ||
Object.defineProperties(HTMLElement.prototype, { | ||
focus: { | ||
configurable: true, | ||
get: () => focus, | ||
}, | ||
blur: { | ||
configurable: true, | ||
get: () => blur, | ||
}, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import {patchFocus, restoreFocus} from '#src/document/patchFocus' | ||
import {isJsdomEnv, render} from '#testHelpers' | ||
|
||
beforeAll(() => { | ||
patchFocus(globalThis.window.HTMLElement) | ||
return () => restoreFocus(globalThis.window.HTMLElement) | ||
}) | ||
|
||
test('dispatch focus events', () => { | ||
const { | ||
elements: [a, b], | ||
getEventSnapshot, | ||
} = render(`<input id="a"/><input id="b"/>`, {focus: false}) | ||
|
||
a.focus() | ||
b.focus() | ||
a.blur() | ||
b.blur() | ||
|
||
expect(getEventSnapshot()).toMatchInlineSnapshot(` | ||
Events fired on: input#a[value=""],input#b[value=""] | ||
input#a[value=""] - focus: β null | ||
input#a[value=""] - focusin: β null | ||
input#a[value=""] - blur: β input#b[value=""] | ||
input#a[value=""] - focusout: β input#b[value=""] | ||
input#b[value=""] - focus: β input#a[value=""] | ||
input#b[value=""] - focusin: β input#a[value=""] | ||
input#b[value=""] - blur: β null | ||
input#b[value=""] - focusout: β null | ||
`) | ||
}) | ||
|
||
test('`focus` handler can prevent subsequent `focusin`', () => { | ||
const {element, getEventSnapshot} = render(`<input/>`, {focus: false}) | ||
|
||
element.addEventListener('focus', () => { | ||
element.blur() | ||
}) | ||
|
||
element.focus() | ||
|
||
if (isJsdomEnv()) { | ||
// The unpatched focus in Jsdom behaves differently than the browser | ||
expect(getEventSnapshot()).toMatchInlineSnapshot(` | ||
Events fired on: input[value=""] | ||
input[value=""] - focus: β null | ||
input[value=""] - blur: β null | ||
input[value=""] - focusout: β null | ||
input[value=""] - focusin: β null | ||
`) | ||
} else { | ||
expect(getEventSnapshot()).toMatchInlineSnapshot(` | ||
Events fired on: input[value=""] | ||
input[value=""] - focus: β null | ||
input[value=""] - blur: β null | ||
input[value=""] - focusout: β null | ||
`) | ||
} | ||
}) |
Oops, something went wrong.