Skip to content

Commit e63bb7a

Browse files
Fix focus bugs that occur on initial load. (#982)
* Prevent side panel stealing focus on load. * Sidebar tabs stay accessible on load See #973 and #977.
1 parent dee5c3c commit e63bb7a

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

src/simulator/Simulator.tsx

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@
33
*
44
* SPDX-License-Identifier: MIT
55
*/
6-
import { AspectRatio, Box, Flex, useToken, VStack } from "@chakra-ui/react";
6+
import {
7+
AspectRatio,
8+
Box,
9+
Flex,
10+
usePrevious,
11+
useToken,
12+
VStack,
13+
} from "@chakra-ui/react";
714
import { useEffect, useRef, useState } from "react";
815
import { IntlShape, useIntl } from "react-intl";
916
import HideSplitViewButton from "../common/SplitView/HideSplitViewButton";
@@ -67,16 +74,20 @@ const Simulator = ({
6774
const simHeight = contentRect?.height ?? 0;
6875
const [brand500] = useToken("colors", ["brand.500"]);
6976
const [running, setRunning] = useState<RunningStatus>(RunningStatus.STOPPED);
77+
const previouslyShown = usePrevious(shown);
7078

7179
useEffect(() => {
7280
if (shown) {
73-
ref.current!.focus();
81+
// Prevents the simulator stealing focus on initial load.
82+
if (previouslyShown !== undefined && previouslyShown !== shown) {
83+
ref.current!.focus();
84+
}
7485
} else {
7586
if (simFocus) {
7687
showSimulatorButtonRef.current!.focus();
7788
}
7889
}
79-
}, [showSimulatorButtonRef, shown, simFocus]);
90+
}, [previouslyShown, showSimulatorButtonRef, shown, simFocus]);
8091

8192
return (
8293
<DeviceContextProvider value={simulator.current}>

src/workbench/SideBar.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
TabPanel,
1212
TabPanels,
1313
Tabs,
14+
usePrevious,
1415
VStack,
1516
} from "@chakra-ui/react";
1617
import { ReactNode, useCallback, useEffect, useMemo, useRef } from "react";
@@ -130,11 +131,18 @@ const SideBar = ({
130131
}
131132
}, [onSidebarExpand, panes, onTabIndexChange, tab, slug, focus]);
132133

134+
const previouslyShown = usePrevious(shown);
133135
useEffect(() => {
134-
if (shown && (!slug || focus)) {
136+
// Prevents the sidebar stealing focus on initial load.
137+
if (
138+
shown &&
139+
(!slug || focus) &&
140+
previouslyShown !== undefined &&
141+
previouslyShown !== shown
142+
) {
135143
setPanelFocus();
136144
}
137-
}, [shown, slug, focus]);
145+
}, [previouslyShown, shown, slug, focus]);
138146

139147
const handleTabChange = useCallback(
140148
(index: number) => {

src/workbench/SideBarTab.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ const SideBarTab = ({
2929
const ref = useRef<HTMLButtonElement>(null);
3030
useEffect(() => {
3131
// Override tabindex.
32+
// Has no dependencies as it needs to run for every re-render.
3233
ref.current!.setAttribute("tabindex", "0");
33-
}, [tabIndex]);
34+
});
3435
return (
3536
<Tab
3637
ref={ref}

0 commit comments

Comments
 (0)