feat: initially focus "create account" if no account (#944)
This commit is contained in:
@@ -57,6 +57,9 @@ export const GeneralAccountMenu: FunctionComponent<Props> = observer(
|
|||||||
|
|
||||||
const user = application.getUser();
|
const user = application.getUser();
|
||||||
|
|
||||||
|
const CREATE_ACCOUNT_INDEX = 1;
|
||||||
|
const SWITCHER_INDEX = 0;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<div className="flex items-center justify-between px-3 mt-1 mb-1">
|
<div className="flex items-center justify-between px-3 mt-1 mb-1">
|
||||||
@@ -113,6 +116,9 @@ export const GeneralAccountMenu: FunctionComponent<Props> = observer(
|
|||||||
isOpen={appState.accountMenu.show}
|
isOpen={appState.accountMenu.show}
|
||||||
a11yLabel="General account menu"
|
a11yLabel="General account menu"
|
||||||
closeMenu={closeMenu}
|
closeMenu={closeMenu}
|
||||||
|
initialFocus={
|
||||||
|
!application.hasAccount() ? CREATE_ACCOUNT_INDEX : SWITCHER_INDEX
|
||||||
|
}
|
||||||
>
|
>
|
||||||
<MenuItemSeparator />
|
<MenuItemSeparator />
|
||||||
<WorkspaceSwitcherOption
|
<WorkspaceSwitcherOption
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ type MenuProps = {
|
|||||||
children: ComponentChildren;
|
children: ComponentChildren;
|
||||||
closeMenu?: () => void;
|
closeMenu?: () => void;
|
||||||
isOpen: boolean;
|
isOpen: boolean;
|
||||||
|
initialFocus?: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const Menu: FunctionComponent<MenuProps> = ({
|
export const Menu: FunctionComponent<MenuProps> = ({
|
||||||
@@ -28,6 +29,7 @@ export const Menu: FunctionComponent<MenuProps> = ({
|
|||||||
a11yLabel,
|
a11yLabel,
|
||||||
closeMenu,
|
closeMenu,
|
||||||
isOpen,
|
isOpen,
|
||||||
|
initialFocus,
|
||||||
}: MenuProps) => {
|
}: MenuProps) => {
|
||||||
const menuItemRefs = useRef<(HTMLButtonElement | null)[]>([]);
|
const menuItemRefs = useRef<(HTMLButtonElement | null)[]>([]);
|
||||||
|
|
||||||
@@ -46,7 +48,7 @@ export const Menu: FunctionComponent<MenuProps> = ({
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
useListKeyboardNavigation(menuElementRef);
|
useListKeyboardNavigation(menuElementRef, initialFocus);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (isOpen && menuItemRefs.current.length > 0) {
|
if (isOpen && menuItemRefs.current.length > 0) {
|
||||||
|
|||||||
@@ -92,10 +92,12 @@ export const calculateDifferenceBetweenDatesInDays = (
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const useListKeyboardNavigation = (
|
export const useListKeyboardNavigation = (
|
||||||
container: Ref<HTMLElement | null>
|
container: Ref<HTMLElement | null>,
|
||||||
|
initialFocus = 0
|
||||||
) => {
|
) => {
|
||||||
const [listItems, setListItems] = useState<HTMLButtonElement[]>();
|
const [listItems, setListItems] = useState<HTMLButtonElement[]>();
|
||||||
const [focusedItemIndex, setFocusedItemIndex] = useState<number>(0);
|
const [focusedItemIndex, setFocusedItemIndex] =
|
||||||
|
useState<number>(initialFocus);
|
||||||
|
|
||||||
const focusItemWithIndex = useCallback(
|
const focusItemWithIndex = useCallback(
|
||||||
(index: number, items?: HTMLButtonElement[]) => {
|
(index: number, items?: HTMLButtonElement[]) => {
|
||||||
@@ -171,12 +173,13 @@ export const useListKeyboardNavigation = (
|
|||||||
const selectedItemIndex = Array.from(temporaryItems).findIndex(
|
const selectedItemIndex = Array.from(temporaryItems).findIndex(
|
||||||
(item) => item.dataset.selected
|
(item) => item.dataset.selected
|
||||||
);
|
);
|
||||||
const indexToFocus = selectedItemIndex > -1 ? selectedItemIndex : 0;
|
const indexToFocus =
|
||||||
|
selectedItemIndex > -1 ? selectedItemIndex : initialFocus;
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
focusItemWithIndex(indexToFocus, temporaryItems);
|
focusItemWithIndex(indexToFocus, temporaryItems);
|
||||||
}, FIRST_ITEM_FOCUS_TIMEOUT);
|
}, FIRST_ITEM_FOCUS_TIMEOUT);
|
||||||
}
|
}
|
||||||
}, [container, focusItemWithIndex, listItems]);
|
}, [container, focusItemWithIndex, initialFocus, listItems]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const containerElement = container.current;
|
const containerElement = container.current;
|
||||||
|
|||||||
Reference in New Issue
Block a user