MCPcopy Create free account
hub / github.com/angular/components / _forceFocus

Method _forceFocus

src/cdk/dialog/dialog-container.ts:219–235  ·  view source on GitHub ↗

* Focuses the provided element. If the element is not focusable, it will add a tabIndex * attribute to forcefully focus it. The attribute is removed after focus is moved. * @param element The element to focus.

(element: HTMLElement, options?: FocusOptions)

Source from the content-addressed store, hash-verified

217 * @param element The element to focus.
218 */
219 private _forceFocus(element: HTMLElement, options?: FocusOptions) {
220 if (!this._interactivityChecker.isFocusable(element)) {
221 element.tabIndex = -1;
222 // The tabindex attribute should be removed to avoid navigating to that element again
223 this._ngZone.runOutsideAngular(() => {
224 const callback = () => {
225 deregisterBlur();
226 deregisterMousedown();
227 element.removeAttribute('tabindex');
228 };
229
230 const deregisterBlur = this._renderer.listen(element, 'blur', callback);
231 const deregisterMousedown = this._renderer.listen(element, 'mousedown', callback);
232 });
233 }
234 element.focus(options);
235 }
236
237 /**
238 * Focuses the first element that matches the given selector within the focus trap.

Callers 1

_focusByCssSelectorMethod · 0.95

Calls 3

listenMethod · 0.80
focusMethod · 0.65
isFocusableMethod · 0.45

Tested by

no test coverage detected