@@ -252,10 +252,26 @@ export const focusVisibleElement = (el: HTMLElement) => {
252252 * however, there are times when we need to manually control
253253 * this behavior so we call the `setFocus` method on ion-app
254254 * which will let us explicitly set the elements to focus.
255+ *
256+ * Note: The element passed to this function might be an inner
257+ * focusable element (e.g., a native <button> inside ion-button's
258+ * shadow root). If so, we need to find the host element that has
259+ * the ion-focusable class to pass to setFocus.
255260 */
256- if ( el . classList . contains ( 'ion-focusable' ) ) {
261+ let elToFocus = el ;
262+
263+ // If the element doesn't have ion-focusable, check if it's inside
264+ // a shadow root and use the host element instead
265+ if ( ! el . classList . contains ( 'ion-focusable' ) ) {
266+ const rootNode = el . getRootNode ( ) ;
267+ if ( rootNode instanceof ShadowRoot && rootNode . host instanceof HTMLElement ) {
268+ elToFocus = rootNode . host ;
269+ }
270+ }
271+
272+ if ( elToFocus . classList . contains ( 'ion-focusable' ) ) {
257273 const appRootSelector : string = config . get ( 'appRootSelector' , 'ion-app' ) ;
258- const app = el . closest ( appRootSelector ) as HTMLIonAppElement | null ;
274+ const app = elToFocus . closest ( appRootSelector ) as HTMLIonAppElement | null ;
259275 if ( app ) {
260276 if ( appRootSelector === 'ion-app' ) {
261277 /**
@@ -264,7 +280,7 @@ export const focusVisibleElement = (el: HTMLElement) => {
264280 * focus-visible utility is attached to the app root
265281 * and will handle setting focus on the correct element.
266282 */
267- app . setFocus ( [ el ] ) ;
283+ app . setFocus ( [ elToFocus ] ) ;
268284 } else {
269285 /**
270286 * When using a custom app root selector, the focus-visible
@@ -280,7 +296,7 @@ export const focusVisibleElement = (el: HTMLElement) => {
280296 * The focus-visible utility is used to set focus on an
281297 * element that uses `ion-focusable`.
282298 */
283- focusElements ( [ el ] ) ;
299+ focusElements ( [ elToFocus ] ) ;
284300 } ) ;
285301 }
286302 }
0 commit comments