Skip to content

Commit cbb0b34

Browse files
committed
feat: add toast for copied
1 parent 4d5e25e commit cbb0b34

File tree

3 files changed

+65
-3
lines changed

3 files changed

+65
-3
lines changed

src/components/base-layout.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<div class='min-w-screen min-h-screen flex flex-col p-5'>
2+
<div class="min-w-screen min-h-screen flex flex-col p-5">
33
<slot />
44
</div>
55
</template>

src/components/toast.vue

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<template>
2+
<div
3+
v-if="state.show"
4+
v-bind:class="{
5+
'bg-black text-white': !state.error && !state.success,
6+
'bg-green-500 text-white': state.success,
7+
'bg-red-500 text-white': state.error,
8+
}"
9+
class="fixed top-2 right-2 shadow-xl border w-60 px-5 py-4 rounded-md z-20"
10+
>
11+
{{ state.message }}
12+
</div>
13+
</template>
14+
15+
<script setup>
16+
import { reactive } from "vue";
17+
18+
const state = reactive({
19+
message: "",
20+
show: false,
21+
success: false,
22+
error: false,
23+
});
24+
25+
function createToast(message, options = { delay: 2500 }) {
26+
state.message = message;
27+
state.show = true;
28+
setTimeout(() => {
29+
state.show = false;
30+
}, options.delay);
31+
}
32+
33+
function createSuccessToast(message, options) {
34+
state.success = true;
35+
state.error = false;
36+
createToast(message, options);
37+
}
38+
39+
function createErrorToast(message, options) {
40+
state.success = false;
41+
state.error = true;
42+
createToast(message, options);
43+
}
44+
45+
defineExpose({
46+
createToast,
47+
createSuccessToast,
48+
createErrorToast,
49+
});
50+
</script>
51+
52+
53+
<style scoped>
54+
@tailwind base;
55+
@tailwind components;
56+
@tailwind utilities;
57+
</style>

src/pages/Home.vue

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<template>
22
<BaseLayout>
3+
<Toast ref="toastRef" />
34
<h1
45
class="
56
mb-12
@@ -60,11 +61,14 @@ import MenuItem from "../components/menu-item.vue";
6061
import Editor from "../components/editor.vue";
6162
import Button from "../components/button.vue";
6263
import Preview from "../components/preview.vue";
64+
import Toast from "../components/toast.vue";
6365
import { copy } from "../lib/copy";
6466
import { defaultMarkdownText } from "../resources/default-md";
65-
import { reactive, onMounted, onUnmounted } from "vue";
67+
import { reactive, onMounted, ref, onUnmounted } from "vue";
6668
import marked from "marked";
6769
70+
const toastRef = ref(null);
71+
6872
const state = reactive({ code: defaultMarkdownText, showPreview: false });
6973
7074
onMounted(() => {
@@ -97,11 +101,12 @@ function handleChange(code) {
97101
}
98102
99103
async function handleCopyAsHTML() {
100-
if (!value) {
104+
if (!state.code) {
101105
return;
102106
}
103107
const htmlValue = marked(state.code);
104108
await copy(htmlValue);
109+
toastRef.value.createSuccessToast("Copied!");
105110
}
106111
107112
function handlePreviewToggle() {

0 commit comments

Comments
 (0)