Skip to content

Commit 78042f2

Browse files
committed
Add codeblock button
1 parent 609f211 commit 78042f2

File tree

5 files changed

+41
-5
lines changed

5 files changed

+41
-5
lines changed

packages/mdx/pages/themes.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export default function Page({ codes, tests }) {
5858
})
5959
return (
6060
<Layout
61-
current={"test"}
61+
current={"themes"}
6262
contentFileNames={tests}
6363
style={{ width: "100%", maxWidth: undefined }}
6464
>

packages/mdx/src/mdx-client/code.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ export function InnerCode({
3232
<div className="ch-codeblock not-prose">
3333
<CodeSpring
3434
className="ch-code"
35-
config={props.codeConfig}
35+
config={{
36+
...props.codeConfig,
37+
showCopyButton: true,
38+
}}
3639
step={props.files[0]}
3740
/>
3841
</div>

packages/mdx/src/mini-editor/editor-tween.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,22 @@ function EditorTween({
108108
southPanel={southPanel}
109109
terminalPanel={terminalPanel}
110110
theme={codeConfig.theme}
111-
northButton={<CopyButton content={northContent} />}
112-
southButton={<CopyButton content={southContent} />}
111+
northButton={
112+
<CopyButton
113+
content={northContent}
114+
style={{
115+
margin: "0 0.8em",
116+
}}
117+
/>
118+
}
119+
southButton={
120+
<CopyButton
121+
content={southContent}
122+
style={{
123+
margin: "0 0.8em",
124+
}}
125+
/>
126+
}
113127
/>
114128
)
115129
}

packages/mdx/src/smooth-code/code-tween.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
CodeShift,
1818
} from "./partial-step-parser"
1919
import { SmoothLines } from "./smooth-lines"
20+
import { CopyButton } from "./copy-button"
2021

2122
type HTMLProps = React.DetailedHTMLProps<
2223
React.HTMLAttributes<HTMLDivElement>,
@@ -43,6 +44,7 @@ export type CodeConfig = {
4344
horizontalCenter?: boolean
4445
theme: IRawTheme
4546
lineNumbers?: boolean
47+
showCopyButton?: boolean
4648
}
4749

4850
function useCodeShift({
@@ -132,6 +134,7 @@ function AfterDimensions({
132134
stepInfo,
133135
progress,
134136
htmlProps,
137+
config,
135138
}: {
136139
dimensions: NonNullable<Dimensions>
137140
stepInfo: CodeShift
@@ -140,6 +143,7 @@ function AfterDimensions({
140143
htmlProps: HTMLProps
141144
}) {
142145
const { bg, fg } = getCodeColors(theme)
146+
143147
return (
144148
<Wrapper
145149
htmlProps={htmlProps}
@@ -164,6 +168,18 @@ function AfterDimensions({
164168
center={horizontalCenter}
165169
theme={theme}
166170
/>
171+
{config.showCopyButton ? (
172+
<CopyButton
173+
style={{
174+
position: "absolute",
175+
top: 10,
176+
right: 10,
177+
width: "1.1em",
178+
height: "1.1em",
179+
}}
180+
content={stepInfo?.code?.prev}
181+
/>
182+
) : undefined}
167183
</Wrapper>
168184
)
169185
}
@@ -183,6 +199,7 @@ function Wrapper({
183199
style={{
184200
margin: 0,
185201
padding: 0,
202+
position: "relative",
186203
// using this instead of <pre> because https://github.com/code-hike/codehike/issues/120
187204
whiteSpace: "pre",
188205
...style,

packages/mdx/src/smooth-code/copy-button.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import React from "react"
22

33
export function CopyButton({
44
content,
5+
style,
56
}: {
67
content: string
8+
style?: React.CSSProperties
79
}) {
810
const [copied, setCopied] = React.useState(false)
911

@@ -12,8 +14,8 @@ export function CopyButton({
1214
style={{
1315
width: "1.5em",
1416
height: "1.5em",
15-
margin: "0 0.8em",
1617
cursor: "pointer",
18+
...style,
1719
}}
1820
onClick={() => {
1921
copyToClipboard(content)

0 commit comments

Comments
 (0)