Skip to content

Commit 862ca81

Browse files
committed
fix: in react ui, toggling sidebar results in tab resetting to the first
Signed-off-by: Nick Mitchell <[email protected]>
1 parent eb98053 commit 862ca81

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

pdl-live-react/src/page/Masthead.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useCallback } from "react"
2-
import { useSearchParams } from "react-router"
2+
import { useLocation, useSearchParams } from "react-router"
33

44
import {
55
Flex,
@@ -28,17 +28,24 @@ const alignRight = { default: "alignEnd" as const }
2828
const alignCenter = { default: "alignItemsCenter" as const }
2929

3030
function Toggle() {
31+
const { hash } = useLocation()
3132
const [searchParams, setSearchParams] = useSearchParams()
33+
3234
const onSidebarToggle = useCallback(() => {
3335
const newSearchParams = new URLSearchParams(searchParams)
34-
const isOpen = !!searchParams.get("sidebar")
36+
const isOpen = searchParams.has("sidebar")
3537
if (isOpen) {
3638
newSearchParams.delete("sidebar")
3739
} else {
3840
newSearchParams.set("sidebar", "true")
3941
}
4042
setSearchParams(newSearchParams)
41-
}, [searchParams, setSearchParams])
43+
44+
// sigh, see https://github.com/remix-run/react-router/issues/8393
45+
if (hash) {
46+
setTimeout(() => (window.location.hash = hash))
47+
}
48+
}, [searchParams, setSearchParams, hash])
4249

4350
return (
4451
<MastheadToggle>

0 commit comments

Comments
 (0)