Skip to content

Commit 372c149

Browse files
committed
Preserve focus changes made by event handlers
1 parent ba36e6c commit 372c149

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

‎packages/ui/src/dom.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1199,12 +1199,18 @@ function transitionActiveElement(document: Document, to: Element | null): void {
11991199
isTrusted: true,
12001200
});
12011201

1202+
const detected = getActiveElement(document);
1203+
12021204
dispatchEvent(current, {
12031205
event: new FocusEvent('focusout', {
1204-
relatedTarget: to,
1206+
relatedTarget: detected ? null : to,
12051207
}),
12061208
isTrusted: true,
12071209
});
1210+
1211+
if (detected || getActiveElement(document)) {
1212+
return;
1213+
}
12081214
}
12091215

12101216
setActiveElement(document, to);
@@ -1216,6 +1222,11 @@ function transitionActiveElement(document: Document, to: Element | null): void {
12161222
}),
12171223
isTrusted: true,
12181224
});
1225+
1226+
if (getActiveElement(document) !== to) {
1227+
return;
1228+
}
1229+
12191230
dispatchEvent(to, {
12201231
event: new FocusEvent('focusin', {
12211232
relatedTarget: current,

‎packages/ui/wpt.config.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,12 @@ const config: WPTConfig<Document> = {
292292
'html/editing/focus/focus-management/focusing-disconnected-noop.html': true,
293293
// focusing-focused-noop.html requires the innerHTML setter for setup and cleanup.
294294
'html/interaction/focus/focus-management/focus-events.html': true,
295+
'focus/focus-sync-when-blur.html': [
296+
'Element.focus() in blur listener when focus has moved away',
297+
],
298+
'focus/focus-double-sync-calls.html': [
299+
'Element.focus() in focus listener when focus has moved away',
300+
],
295301
// inert/inert-node-is-unfocusable.html requires testdriver click setup:
296302
// real layout, hit testing, and an input backend are not available yet.
297303
'inert/nested-inert-unfocusable.html': true,

0 commit comments

Comments
 (0)