Skip to content

Commit 0b67363

Browse files
committed
fix: auto-expand deep-linked DeepDive/Example after hydration
Reading window.location.hash during render returned false under the App Router (the server render sees no hash), so the queued-expand ref stuck at false and deep-linked DeepDive/Example blocks no longer opened on load. Move the hash check into the post-mount effect so the initial render stays deterministic and the deep link expands again after hydration.
1 parent 423ff30 commit 0b67363

1 file changed

Lines changed: 6 additions & 7 deletions

File tree

src/components/MDX/ExpandableExample.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {IconDeepDive} from '../Icon/IconDeepDive';
1616
import {IconCodeBlock} from '../Icon/IconCodeBlock';
1717
import {Button} from '../Button';
1818
import {H4} from './Heading';
19-
import {useEffect, useRef, useState} from 'react';
19+
import {useEffect, useState} from 'react';
2020

2121
interface ExpandableExampleProps {
2222
children: React.ReactNode;
@@ -34,17 +34,16 @@ function ExpandableExample({children, excerpt, type}: ExpandableExampleProps) {
3434
const isExample = type === 'Example';
3535
const id = children[0].props.id;
3636

37-
const shouldAutoExpand =
38-
typeof window !== 'undefined' && id === window.location.hash.slice(1);
39-
const queuedExpandRef = useRef<boolean>(shouldAutoExpand);
4037
const [isExpanded, setIsExpanded] = useState(false);
4138

39+
// Auto-expand when deep-linked to this section. Read the hash in an effect
40+
// (not during render) so the initial render stays deterministic across the
41+
// server and client hydration.
4242
useEffect(() => {
43-
if (queuedExpandRef.current) {
44-
queuedExpandRef.current = false;
43+
if (id === window.location.hash.slice(1)) {
4544
setIsExpanded(true);
4645
}
47-
}, []);
46+
}, [id]);
4847

4948
return (
5049
<details

0 commit comments

Comments
 (0)