Skip to content

Commit f74ac4a

Browse files
committed
simplify async logic slightly
1 parent 1423fbc commit f74ac4a

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
@@ -187,7 +187,7 @@ export class FocusMonitor implements OnDestroy {
187187
// focused element.
188188
let windowFocusListener = () => {
189189
this._windowFocused = true;
190-
this._windowFocusTimeoutId = setTimeout(() => this._windowFocused = false, 0);
190+
this._windowFocusTimeoutId = setTimeout(() => this._windowFocused = false);
191191
};
192192

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

@@ -302,23 +302,20 @@ export class FocusMonitor implements OnDestroy {
302302
// 2) It was caused by a touch event, in which case we mark the origin as 'touch'.
303303
// 3) The element was programmatically focused, in which case we should mark the origin as
304304
// 'program'.
305-
if (!this._origin) {
305+
let origin = this._origin;
306+
if (!origin) {
306307
if (this._windowFocused && this._lastFocusOrigin) {
307-
this._origin = this._lastFocusOrigin;
308+
origin = this._lastFocusOrigin;
308309
} else if (this._wasCausedByTouch(event)) {
309-
this._origin = 'touch';
310+
origin = 'touch';
310311
} else {
311-
this._origin = 'program';
312+
origin = 'program';
312313
}
313314
}
314315

315-
this._setClasses(element, this._origin);
316-
elementInfo.subject.next(this._origin);
317-
this._lastFocusOrigin = this._origin;
318-
319-
// Null-out the origin after a setTimeout. This allows the full capture/bubble cycle to complete
320-
// for the current event before nulling it.
321-
setTimeout(() => this._origin = null);
316+
this._setClasses(element, origin);
317+
elementInfo.subject.next(origin);
318+
this._lastFocusOrigin = origin;
322319
}
323320

324321
/**
@@ -354,7 +351,6 @@ export class FocusMonitor implements OnDestroy {
354351
this._unregisterGlobalListeners = () => {};
355352
}
356353
}
357-
358354
}
359355

360356

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)