|
1 | 1 | 'use client'; |
2 | 2 |
|
3 | | -import {RefObject, useEffect, useRef, useState} from 'react'; |
| 3 | +import {RefObject, useContext, useEffect, useLayoutEffect, useRef, useState} from 'react'; |
4 | 4 | import {Clipboard} from 'react-feather'; |
5 | 5 |
|
6 | 6 | import {usePlausibleEvent} from 'sentry-docs/hooks/usePlausibleEvent'; |
7 | 7 |
|
8 | 8 | import styles from './code-blocks.module.scss'; |
9 | 9 |
|
| 10 | +import {CodeContext} from '../codeContext'; |
10 | 11 | import {makeHighlightBlocks} from '../codeHighlights'; |
11 | 12 | import {makeKeywordsClickable} from '../codeKeywords'; |
| 13 | +import {updateElementsVisibilityForOptions} from '../onboarding'; |
12 | 14 |
|
13 | 15 | export interface CodeBlockProps { |
14 | 16 | children: React.ReactNode; |
@@ -53,6 +55,7 @@ function getCopiableText(element: HTMLDivElement) { |
53 | 55 | export function CodeBlock({filename, language, children}: CodeBlockProps) { |
54 | 56 | const [showCopied, setShowCopied] = useState(false); |
55 | 57 | const codeRef = useRef<HTMLDivElement>(null); |
| 58 | + const codeContext = useContext(CodeContext); |
56 | 59 |
|
57 | 60 | // Show the copy button after js has loaded |
58 | 61 | // otherwise the copy button will not work |
@@ -83,6 +86,15 @@ export function CodeBlock({filename, language, children}: CodeBlockProps) { |
83 | 86 | }; |
84 | 87 | }, []); |
85 | 88 |
|
| 89 | + // Re-sync onboarding visibility after keyword interpolation recreates DOM nodes. |
| 90 | + // makeKeywordsClickable clones elements, losing .hidden classes. useLayoutEffect |
| 91 | + // corrects this synchronously before paint to prevent visible flicker. |
| 92 | + useLayoutEffect(() => { |
| 93 | + if (isMounted && codeContext?.onboardingOptions) { |
| 94 | + updateElementsVisibilityForOptions(codeContext.onboardingOptions, false); |
| 95 | + } |
| 96 | + }, [isMounted, codeContext?.onboardingOptions]); |
| 97 | + |
86 | 98 | useCleanSnippetInClipboard(codeRef, {language}); |
87 | 99 |
|
88 | 100 | // Mermaid blocks should not be processed by CodeBlock - they need special client-side rendering |
|
0 commit comments