Skip to content

Fix focus bugs that occur on initial load. #982

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/simulator/Simulator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@
*
* SPDX-License-Identifier: MIT
*/
import { AspectRatio, Box, Flex, useToken, VStack } from "@chakra-ui/react";
import {
AspectRatio,
Box,
Flex,
usePrevious,
useToken,
VStack,
} from "@chakra-ui/react";
import { useEffect, useRef, useState } from "react";
import { IntlShape, useIntl } from "react-intl";
import HideSplitViewButton from "../common/SplitView/HideSplitViewButton";
Expand Down Expand Up @@ -67,16 +74,20 @@ const Simulator = ({
const simHeight = contentRect?.height ?? 0;
const [brand500] = useToken("colors", ["brand.500"]);
const [running, setRunning] = useState<RunningStatus>(RunningStatus.STOPPED);
const previouslyShown = usePrevious(shown);

useEffect(() => {
if (shown) {
ref.current!.focus();
// Prevents the simulator stealing focus on initial load.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd really love to find a way to move away from effects for this but I think this is a reasonable fix at this point.

if (previouslyShown !== undefined && previouslyShown !== shown) {
ref.current!.focus();
}
} else {
if (simFocus) {
showSimulatorButtonRef.current!.focus();
}
}
}, [showSimulatorButtonRef, shown, simFocus]);
}, [previouslyShown, showSimulatorButtonRef, shown, simFocus]);

return (
<DeviceContextProvider value={simulator.current}>
Expand Down
12 changes: 10 additions & 2 deletions src/workbench/SideBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
TabPanel,
TabPanels,
Tabs,
usePrevious,
VStack,
} from "@chakra-ui/react";
import { ReactNode, useCallback, useEffect, useMemo, useRef } from "react";
Expand Down Expand Up @@ -130,11 +131,18 @@ const SideBar = ({
}
}, [onSidebarExpand, panes, onTabIndexChange, tab, slug, focus]);

const previouslyShown = usePrevious(shown);
useEffect(() => {
if (shown && (!slug || focus)) {
// Prevents the sidebar stealing focus on initial load.
if (
shown &&
(!slug || focus) &&
previouslyShown !== undefined &&
previouslyShown !== shown
) {
setPanelFocus();
}
}, [shown, slug, focus]);
}, [previouslyShown, shown, slug, focus]);

const handleTabChange = useCallback(
(index: number) => {
Expand Down
3 changes: 2 additions & 1 deletion src/workbench/SideBarTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ const SideBarTab = ({
const ref = useRef<HTMLButtonElement>(null);
useEffect(() => {
// Override tabindex.
// Has no dependencies as it needs to run for every re-render.
ref.current!.setAttribute("tabindex", "0");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable, though I'd love a world where we didn't have to hack this.

}, [tabIndex]);
});
return (
<Tab
ref={ref}
Expand Down