Skip to content

Commit

Permalink
add autofocus + fix-ish eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
linh-transcend committed Dec 4, 2024
1 parent 6bc2541 commit ff4854c
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ export function App({
const [currentVariables, handleChangeUiVariables] = useState({});

// Get default view states
const { initialViewStateByPrivacyRegime, dismissedViewState } = config;
const { initialViewStateByPrivacyRegime, dismissedViewState, autofocus } =
config;
const initialViewState =
initialViewStateByPrivacyRegime[
privacyRegime as keyof typeof initialViewStateByPrivacyRegime
Expand All @@ -66,6 +67,7 @@ export function App({
document.activeElement !== document.body
? document.activeElement
: null,
autofocus,
});

// Language setup
Expand Down
1 change: 1 addition & 0 deletions src/components/CompleteOptionsToggles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export function CompleteOptionsToggles({
consentSelection: initialConsentSelections,
defaultPurposeToMessageKey: DEFAULT_PURPOSE_TO_MESSAGE_KEY,
});
// eslint-disable-next-line react-hooks/exhaustive-deps
const purposeToDescription = useMemo(() => airgap.getPurposeTypes(), []);
const purposeToDescriptionKey = useGetPurposeDescriptionKeys({
consentSelection: initialConsentSelections,
Expand Down
1 change: 1 addition & 0 deletions src/components/DoNotSellDisclosure.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export function DoNotSellDisclosure({
useEffect(() => {
handleOptOut(modalOpenAuth);
setIsOptedOut(true);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

// delay UI until opt out happens
Expand Down
7 changes: 5 additions & 2 deletions src/components/Main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,15 @@ export function Main({
const dialogRef = useRef<HTMLDivElement>(null);
useEffect(() => {
if (!isViewStateClosed(viewState) && dialogRef.current) {
const shouldAutofocus = config.autofocus ?? true;
// This setTimeout was necessary for the api triggered states, (DoNotSell|OptOut)Disclosure
setTimeout(() => {
if (dialogRef.current) initialFocusElement(dialogRef.current);
if (dialogRef.current && shouldAutofocus) {
initialFocusElement(dialogRef.current);
}
}, 0);
}
}, [viewState, dialogRef]);
}, [viewState, dialogRef, config.autofocus]);

// Modal open views
if (!isViewStateClosed(viewState)) {
Expand Down
1 change: 1 addition & 0 deletions src/components/OptOutDisclosure.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export function OptOutDisclosure({
useEffect(() => {
handleOptOut(modalOpenAuth);
setIsOptedOut(true);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

// delay UI until opt out happens
Expand Down
1 change: 1 addition & 0 deletions src/hooks/useGetPurposeDescriptionKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export const useGetPurposeDescriptionKeys = ({
},
defaultPurposeToDescriptionKey as Record<string, DefinedMessage>,
),
// eslint-disable-next-line react-hooks/exhaustive-deps
[consentSelection, defaultPurposeToDescriptionKey],
);

Expand Down
10 changes: 8 additions & 2 deletions src/hooks/useViewState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export function useViewState({
dismissedViewState,
eventTarget,
savedActiveElement,
autofocus,
}: {
/** Which state this consent manager should go to when opened */
initialViewState: InitialViewState;
Expand All @@ -62,6 +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;
}): {
/** The current view state */
viewState: ViewState;
Expand Down Expand Up @@ -134,7 +137,9 @@ 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. */
if (savedActiveElement !== null) {
const shouldFocus = autofocus ?? true;
if (savedActiveElement !== null && shouldFocus) {
savedActiveElement.focus();
} else {
const tempInteractiveEl = document.createElement('span');
tempInteractiveEl.setAttribute('tabindex', '1');
Expand All @@ -158,7 +163,8 @@ export function useViewState({
break;
}
},
[state, setState, initialViewState, dismissedViewState],
// eslint-disable-next-line react-hooks/exhaustive-deps
[state, setState, initialViewState, dismissedViewState, autofocus],
);

// Now that the viewState has updated, dispatch an event on the `transcend` API / event target
Expand Down

0 comments on commit ff4854c

Please sign in to comment.