MCPcopy Index your code
hub / github.com/ionic-team/ionic-framework / focusElementInContext

Function focusElementInContext

core/src/utils/focus-trap.ts:68–92  ·  view source on GitHub ↗
(
  hostToFocus: HTMLElement | null | undefined,
  fallbackElement: T
)

Source from the content-addressed store, hash-verified

66 * the host element itself.
67 */
68const focusElementInContext = <T extends HTMLElement>(
69 hostToFocus: HTMLElement | null | undefined,
70 fallbackElement: T
71) => {
72 let elementToFocus = hostToFocus;
73
74 const shadowRoot = hostToFocus?.shadowRoot;
75 if (shadowRoot) {
76 // If there are no inner focusable elements, just focus the host element.
77 elementToFocus = shadowRoot.querySelector<HTMLElement>(focusableQueryString) || hostToFocus;
78 }
79
80 if (elementToFocus) {
81 const radioGroup = elementToFocus.closest('ion-radio-group');
82
83 if (radioGroup) {
84 radioGroup.setFocus();
85 } else {
86 focusVisibleElement(elementToFocus);
87 }
88 } else {
89 // Focus fallback element instead of letting focus escape
90 fallbackElement.focus();
91 }
92};

Callers 2

focusFirstDescendantFunction · 0.85
focusLastDescendantFunction · 0.85

Calls 2

focusVisibleElementFunction · 0.90
setFocusMethod · 0.45

Tested by

no test coverage detected