1
- import React , { useState } from 'react'
1
+ import React , { useEffect , useRef , useState } from 'react'
2
2
3
3
import { Code } from './icons'
4
4
import CodeBlock from '../MDX/CodeBlock'
@@ -34,6 +34,15 @@ If you use it in markdown, you should use it exactly like "<Demo src="./demos/de
34
34
}
35
35
const { code, title, desc } = demoMeta
36
36
const [ showCode , setShowCode ] = useState ( false )
37
+
38
+ const codeInnerRef = useRef < HTMLDivElement > ( null )
39
+ const [ CodeBlockHeight , setCodeBlockHeight ] = useState ( 0 )
40
+ useEffect ( ( ) => {
41
+ if ( codeInnerRef . current ) {
42
+ setCodeBlockHeight ( codeInnerRef . current . offsetHeight )
43
+ }
44
+ } , [ code ] )
45
+
37
46
return (
38
47
< div
39
48
className = { [ s . demoBox , className ] . filter ( Boolean ) . join ( ' ' ) }
@@ -59,18 +68,21 @@ If you use it in markdown, you should use it exactly like "<Demo src="./demos/de
59
68
</ div >
60
69
</ div >
61
70
62
- { showCode && (
63
- < div className = { s . code } >
64
- < div className = { s . codeInner } >
65
- < CodeBlock
66
- className = "language-tsx"
67
- style = { { background : 'transparent' , overflow : 'visible' } }
68
- >
69
- { code }
70
- </ CodeBlock >
71
- </ div >
71
+ < div
72
+ className = { s . code }
73
+ style = { {
74
+ height : showCode ? CodeBlockHeight : 0 ,
75
+ } }
76
+ >
77
+ < div className = { s . codeInner } ref = { codeInnerRef } >
78
+ < CodeBlock
79
+ className = "language-tsx"
80
+ style = { { background : 'transparent' , overflow : 'visible' } }
81
+ >
82
+ { code }
83
+ </ CodeBlock >
72
84
</ div >
73
- ) }
85
+ </ div >
74
86
</ div >
75
87
)
76
88
}
0 commit comments