Skip to content

Commit 96f2faa

Browse files
committed
simplify async logic slightly
1 parent 0a02676 commit 96f2faa

File tree

2 files changed

+12
-15
lines changed

2 files changed

+12
-15
lines changed

src/cdk/a11y/focus-monitor/focus-monitor.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ export class FocusMonitor implements OnDestroy {
183183
// focused element.
184184
let windowFocusListener = () => {
185185
this._windowFocused = true;
186-
this._windowFocusTimeoutId = setTimeout(() => this._windowFocused = false, 0);
186+
this._windowFocusTimeoutId = setTimeout(() => this._windowFocused = false);
187187
};
188188

189189
// Note: we listen to events in the capture phase so we can detect them even if the user stops
@@ -242,7 +242,7 @@ export class FocusMonitor implements OnDestroy {
242242
private _setOriginForCurrentEventQueue(origin: FocusOrigin): void {
243243
this._ngZone.runOutsideAngular(() => {
244244
this._origin = origin;
245-
this._originTimeoutId = setTimeout(() => this._origin = null, 0);
245+
this._originTimeoutId = setTimeout(() => this._origin = null);
246246
});
247247
}
248248

@@ -298,23 +298,20 @@ export class FocusMonitor implements OnDestroy {
298298
// 2) It was caused by a touch event, in which case we mark the origin as 'touch'.
299299
// 3) The element was programmatically focused, in which case we should mark the origin as
300300
// 'program'.
301-
if (!this._origin) {
301+
let origin = this._origin;
302+
if (!origin) {
302303
if (this._windowFocused && this._lastFocusOrigin) {
303-
this._origin = this._lastFocusOrigin;
304+
origin = this._lastFocusOrigin;
304305
} else if (this._wasCausedByTouch(event)) {
305-
this._origin = 'touch';
306+
origin = 'touch';
306307
} else {
307-
this._origin = 'program';
308+
origin = 'program';
308309
}
309310
}
310311

311-
this._setClasses(element, this._origin);
312-
elementInfo.subject.next(this._origin);
313-
this._lastFocusOrigin = this._origin;
314-
315-
// Null-out the origin after a setTimeout. This allows the full capture/bubble cycle to complete
316-
// for the current event before nulling it.
317-
setTimeout(() => this._origin = null);
312+
this._setClasses(element, origin);
313+
elementInfo.subject.next(origin);
314+
this._lastFocusOrigin = origin;
318315
}
319316

320317
/**
@@ -350,7 +347,6 @@ export class FocusMonitor implements OnDestroy {
350347
this._unregisterGlobalListeners = () => {};
351348
}
352349
}
353-
354350
}
355351

356352

src/demo-app/focus-origin/focus-origin-demo.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@
2121
</div>
2222

2323
<div class="demo-focusable" cdkMonitorSubtreeFocus>
24-
<mat-checkbox>Focus ring should show on keyboard focus</mat-checkbox>
24+
<p>Parent div should get same focus origin as button when button is focused:</p>
25+
<button class="demo-focusable" cdkMonitorElementFocus>focus me</button>
2526
</div>

0 commit comments

Comments
 (0)