Skip to content

Commit 4ad6b0a

Browse files
authored
Merge pull request #234 from code-hike/add-copy-button-to-code-browser
Add copy button to code browser
2 parents bcbbd1b + b37c5a9 commit 4ad6b0a

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

packages/mdx/src/mini-editor/code-browser.scss

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
font-size: 1rem;
2323
line-height: 1.2rem;
2424
letter-spacing: 0px;
25+
position: relative;
2526
}
2627

2728
.ch-code-browser-content ::selection {
@@ -42,3 +43,14 @@
4243
background-color: var(--ch-hover-background);
4344
color: var(--ch-hover-foreground);
4445
}
46+
47+
.ch-code-browser-button {
48+
width: 1.5em;
49+
height: 1.5em;
50+
cursor: pointer;
51+
min-width: 1.5em;
52+
min-height: 1.5em;
53+
position: absolute;
54+
right: 0.8em;
55+
top: 0.8em;
56+
}

packages/mdx/src/mini-editor/code-browser.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import { CodeFile } from "./editor-shift"
22
import { IRawTheme } from "vscode-textmate"
3-
import { ColorName, getColor, getColorScheme } from "utils"
3+
import {
4+
codeToText,
5+
ColorName,
6+
getColor,
7+
getColorScheme,
8+
} from "utils"
49
import React from "react"
10+
import { CopyButton } from "smooth-code/copy-button"
511

612
export function CodeBrowser({
713
files,
@@ -192,6 +198,10 @@ function Content({
192198
colorScheme: getColorScheme(theme),
193199
}}
194200
>
201+
<CopyButton
202+
className="ch-code-browser-button"
203+
content={codeToText(file.code)}
204+
/>
195205
{file.code.lines.map((line, i) => (
196206
<div key={i}>
197207
{line.tokens.length === 0 ? (

0 commit comments

Comments
 (0)