Skip to content

Commit

Permalink
refactor(app): move focus utils to global
Browse files Browse the repository at this point in the history
  • Loading branch information
brandyscarney committed Sep 12, 2024
1 parent 3e99534 commit 5a1af41
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 19 deletions.
22 changes: 5 additions & 17 deletions core/src/components/app/app.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,17 @@
import type { ComponentInterface } from '@stencil/core';
import { Build, Component, Element, Host, Method, h } from '@stencil/core';
import type { FocusVisibleUtility } from '@utils/focus-visible';
import { Component, Element, Host, Method, h } from '@stencil/core';
import { initFocusVisibleUtility } from '@utils/focus-visible';

import { config } from '../../global/config';
import { getIonMode, rIC } from '../../global/ionic-global';
import { getIonMode } from '../../global/ionic-global';

@Component({
tag: 'ion-app',
styleUrl: 'app.scss',
})
export class App implements ComponentInterface {
private focusVisible?: FocusVisibleUtility;

@Element() el!: HTMLElement;

componentDidLoad() {
// TODO can we move focus visible to global
if (Build.isBrowser) {
rIC(async () => {
import('../../utils/focus-visible').then((module) => (this.focusVisible = module.startFocusVisible()));
});
}
}

/**
* @internal
* Used to set focus on an element that uses `ion-focusable`.
Expand All @@ -35,9 +24,8 @@ export class App implements ComponentInterface {
*/
@Method()
async setFocus(elements: HTMLElement[]) {
if (this.focusVisible) {
this.focusVisible.setFocus(elements);
}
const focusVisible = initFocusVisibleUtility();
focusVisible.setFocus(elements);
}

render() {
Expand Down
3 changes: 1 addition & 2 deletions core/src/global/ionic-global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,7 @@ export const initialize = (userConfig: IonicConfig = {}) => {
if (typeof (window as any) !== 'undefined') {
import('../utils/keyboard/keyboard').then((module) => module.startKeyboardAssist(window));
}
// TODO
// import('../../utils/focus-visible').then((module) => (this.focusVisible = module.startFocusVisible()));
import('../utils/focus-visible').then((module) => (module.initFocusVisibleUtility()));
});
}
};
Expand Down
9 changes: 9 additions & 0 deletions core/src/utils/focus-visible.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ export interface FocusVisibleUtility {
setFocus: (elements: Element[]) => void;
}

let focusVisibleUtility: FocusVisibleUtility | null = null;

export const initFocusVisibleUtility = () => {
if (!focusVisibleUtility) {
focusVisibleUtility = startFocusVisible();
}
return focusVisibleUtility;
};

export const startFocusVisible = (rootEl?: HTMLElement): FocusVisibleUtility => {
let currentFocus: Element[] = [];
let keyboardMode = true;
Expand Down

0 comments on commit 5a1af41

Please sign in to comment.