Skip to content

Commit eafe89d

Browse files
authored
feat: code block fold animation (#180)
1 parent d5ffc36 commit eafe89d

File tree

3 files changed

+30
-15
lines changed

3 files changed

+30
-15
lines changed

packages/theme-doc/src/Layout/Demo/index.module.less

+5-3
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,12 @@
5555
}
5656

5757
.code {
58-
border-top: 1px dashed @colorSplit;
59-
overflow: auto;
58+
position: relative;
59+
transition: height 0.3s ease-in-out;
60+
overflow: hidden;
6061
}
6162

6263
.code .codeInner {
63-
padding: 0px 8px;
64+
border-top: 1px dashed @colorSplit;
65+
padding: 1em 8px;
6466
}

packages/theme-doc/src/Layout/Demo/index.tsx

+24-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState } from 'react'
1+
import React, { useEffect, useRef, useState } from 'react'
22

33
import { Code } from './icons'
44
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
3434
}
3535
const { code, title, desc } = demoMeta
3636
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+
3746
return (
3847
<div
3948
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
5968
</div>
6069
</div>
6170

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>
7284
</div>
73-
)}
85+
</div>
7486
</div>
7587
)
7688
}

packages/theme-doc/src/Layout/MDX/CodeBlock.module.less

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
.pre {
22
position: relative;
3+
margin: 0;
34

45
.copy {
56
opacity: 0;

0 commit comments

Comments
 (0)