Skip to content

Commit

Permalink
feat: code block fold animation (#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
StreakingMan committed Aug 6, 2024
1 parent d5ffc36 commit eafe89d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
8 changes: 5 additions & 3 deletions packages/theme-doc/src/Layout/Demo/index.module.less
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@
}

.code {
border-top: 1px dashed @colorSplit;
overflow: auto;
position: relative;
transition: height 0.3s ease-in-out;
overflow: hidden;
}

.code .codeInner {
padding: 0px 8px;
border-top: 1px dashed @colorSplit;
padding: 1em 8px;
}
36 changes: 24 additions & 12 deletions packages/theme-doc/src/Layout/Demo/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react'
import React, { useEffect, useRef, useState } from 'react'

import { Code } from './icons'
import CodeBlock from '../MDX/CodeBlock'
Expand Down Expand Up @@ -34,6 +34,15 @@ If you use it in markdown, you should use it exactly like "<Demo src="./demos/de
}
const { code, title, desc } = demoMeta
const [showCode, setShowCode] = useState(false)

const codeInnerRef = useRef<HTMLDivElement>(null)
const [CodeBlockHeight, setCodeBlockHeight] = useState(0)
useEffect(() => {
if (codeInnerRef.current) {
setCodeBlockHeight(codeInnerRef.current.offsetHeight)
}
}, [code])

return (
<div
className={[s.demoBox, className].filter(Boolean).join(' ')}
Expand All @@ -59,18 +68,21 @@ If you use it in markdown, you should use it exactly like "<Demo src="./demos/de
</div>
</div>

{showCode && (
<div className={s.code}>
<div className={s.codeInner}>
<CodeBlock
className="language-tsx"
style={{ background: 'transparent', overflow: 'visible' }}
>
{code}
</CodeBlock>
</div>
<div
className={s.code}
style={{
height: showCode ? CodeBlockHeight : 0,
}}
>
<div className={s.codeInner} ref={codeInnerRef}>
<CodeBlock
className="language-tsx"
style={{ background: 'transparent', overflow: 'visible' }}
>
{code}
</CodeBlock>
</div>
)}
</div>
</div>
)
}
1 change: 1 addition & 0 deletions packages/theme-doc/src/Layout/MDX/CodeBlock.module.less
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.pre {
position: relative;
margin: 0;

.copy {
opacity: 0;
Expand Down

0 comments on commit eafe89d

Please sign in to comment.