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

Method focusInitialElement

src/cdk/a11y/focus-trap/focus-trap.ts:205–245  ·  view source on GitHub ↗

* Focuses the element that should be focused when the focus trap is initialized. * @returns Whether focus was moved successfully.

(options?: FocusOptions)

Source from the content-addressed store, hash-verified

203 * @returns Whether focus was moved successfully.
204 */
205 focusInitialElement(options?: FocusOptions): boolean {
206 // Contains the deprecated version of selector, for temporary backwards comparability.
207 const redirectToElement = this._element.querySelector(
208 `[cdk-focus-initial], ` + `[cdkFocusInitial]`,
209 ) as HTMLElement;
210
211 if (redirectToElement) {
212 // @breaking-change 8.0.0
213 if (
214 (typeof ngDevMode === 'undefined' || ngDevMode) &&
215 redirectToElement.hasAttribute(`cdk-focus-initial`)
216 ) {
217 console.warn(
218 `Found use of deprecated attribute 'cdk-focus-initial', ` +
219 `use 'cdkFocusInitial' instead. The deprecated attribute ` +
220 `will be removed in 8.0.0`,
221 redirectToElement,
222 );
223 }
224
225 // Warn the consumer if the element they've pointed to
226 // isn't focusable, when not in production mode.
227 if (
228 (typeof ngDevMode === 'undefined' || ngDevMode) &&
229 !this._checker.isFocusable(redirectToElement)
230 ) {
231 console.warn(`Element matching '[cdkFocusInitial]' is not focusable.`, redirectToElement);
232 }
233
234 if (!this._checker.isFocusable(redirectToElement)) {
235 const focusableChild = this._getFirstTabbableElement(redirectToElement) as HTMLElement;
236 focusableChild?.focus(options);
237 return !!focusableChild;
238 }
239
240 redirectToElement.focus(options);
241 return true;
242 }
243
244 return this.focusFirstTabbableElement(options);
245 }
246
247 /**
248 * Focuses the first tabbable element within the focus trap region.

Callers 5

_showEditOverlayMethod · 0.80
_takeFocusMethod · 0.80
focus-trap.spec.tsFile · 0.80
_trapFocusMethod · 0.80

Calls 5

warnMethod · 0.80
focusMethod · 0.65
isFocusableMethod · 0.45

Tested by

no test coverage detected