From 06bcc929dca0a009994109261dd313c705e708ea Mon Sep 17 00:00:00 2001 From: Linh Chau <94718676+linh-transcend@users.noreply.github.com> Date: Fri, 6 Dec 2024 21:00:34 +0000 Subject: [PATCH] update autofocus type to on/off --- src/components/Main.tsx | 2 +- src/hooks/useViewState.ts | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/components/Main.tsx b/src/components/Main.tsx index 7c838e6..36075af 100644 --- a/src/components/Main.tsx +++ b/src/components/Main.tsx @@ -77,7 +77,7 @@ export function Main({ const dialogRef = useRef(null); useEffect(() => { if (!isViewStateClosed(viewState) && dialogRef.current) { - const shouldAutofocus = config.autofocus ?? true; + const shouldAutofocus = !config?.autofocus || config?.autofocus === 'on'; // This setTimeout was necessary for the api triggered states, (DoNotSell|OptOut)Disclosure setTimeout(() => { if (dialogRef.current && shouldAutofocus) { diff --git a/src/hooks/useViewState.ts b/src/hooks/useViewState.ts index 2605099..ab7eba3 100644 --- a/src/hooks/useViewState.ts +++ b/src/hooks/useViewState.ts @@ -63,8 +63,8 @@ export function useViewState({ eventTarget: EventTarget; /** Element previously focused before our ui modal was opened */ savedActiveElement: HTMLElement | null; - /** Whether to on last focused element on reopen */ - autofocus?: boolean; + /** Whether to on last focused element on reopen: on or off */ + autofocus?: string; }): { /** The current view state */ viewState: ViewState; @@ -137,7 +137,7 @@ export function useViewState({ * very difficult to interact with. We create an element with maximum focus priority and * focus it so that when we delete it the user will be at the start of the focus order * just like if they had freshly loaded the page. */ - const shouldFocus = autofocus ?? true; + const shouldFocus = !autofocus || autofocus === 'on'; if (savedActiveElement !== null && shouldFocus) { savedActiveElement.focus(); } else {