Skip to content

Commit 567df7e

Browse files
committed
add: click to copy
1 parent 4fd0e42 commit 567df7e

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

src/App.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<h1 class="">Vuejs Code Block UI Component</h1>
33

44
<CodeBlock
5-
theme="ultramin"
5+
theme="dracula"
66
:code="code"
77
language="python"
88
:numbered="true"

src/components/code-block/CodeBlock.vue

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
alt="" />
2121
<span>{{ props.fileName }}</span>
2222
</div>
23-
<div class="actions">
23+
<div
24+
@click="copyCodeToClipboard(props.code)"
25+
class="actions">
2426
<button
2527
type="button"
2628
class="copy-button">
@@ -79,14 +81,12 @@
7981
codeBlockInstance
8082
} from './types';
8183
import { useCodeBlock } from './use-code-block';
82-
import { parseCodeIntoLines } from '../utils';
83-
84+
import { copyCodeToClipboard, parseCodeIntoLines } from '../utils';
8485
import { addThemeToCodeBlock, themes } from '../themes';
8586
8687
const props = defineProps(codeBlockProps());
8788
8889
const codeBlock: Ref<HTMLDivElement | null> = ref(null);
89-
9090
const code = computed(() => parseCodeIntoLines(props.code, props.language));
9191
const currentTheme = computed(() => themes[props.theme]);
9292
@@ -133,9 +133,6 @@
133133
gap: 0.5rem;
134134
padding: 0.5rem 1.5rem;
135135
padding-left: 0.25rem;
136-
137-
/* align-items: center;
138-
border-bottom: 1px solid #7dd3fc; */
139136
}
140137
141138
.vuejs-code-block pre .header .file-name p {
@@ -154,6 +151,8 @@
154151
padding: 0;
155152
cursor: pointer;
156153
color: #64748b;
154+
background: rgba(127.5, 127.5, 127.5, 0.15);
155+
border-radius: 0.5rem;
157156
transition: color 150ms ease-in-out;
158157
}
159158

src/components/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,7 @@ export function parseCodeIntoLines(
3939
.map((line) => line.replace(/\s+$/, ''))
4040
.filter((line) => line.length > 0 || line === '');
4141
}
42+
43+
export function copyCodeToClipboard(code: string) {
44+
window.navigator.clipboard.writeText(code);
45+
}

0 commit comments

Comments
 (0)