Skip to content

Commit cdadbcc

Browse files
committed
refactor(ui): improve naming and typing of new snackbar implementation
1 parent b18eed3 commit cdadbcc

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

ui/src/components/Snackbar/SnackbarNew.vue

+7-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
v-model="show"
44
location="top"
55
:timeout="4000"
6-
:color="type"
6+
:color="color"
77
transition="slide-x-transition"
88
>
99
{{ message }}
@@ -12,13 +12,14 @@
1212

1313
<script setup lang="ts">
1414
import { computed } from "vue";
15-
import { rawPlugin } from "@/plugins/snackbar";
15+
import { plugin } from "@/plugins/snackbar";
1616
1717
const show = computed({
18-
get: () => rawPlugin.state.show,
19-
set: (value) => rawPlugin.setShow(value),
18+
get: () => plugin.state.show,
19+
set: (value) => plugin.setShow(value),
2020
});
2121
22-
const message = computed(() => rawPlugin.state.message);
23-
const type = computed(() => rawPlugin.state.type);
22+
const message = computed(() => plugin.state.message);
23+
const type = computed(() => plugin.state.type);
24+
const color = computed(() => type.value ?? "info");
2425
</script>

ui/src/plugins/snackbar.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import { App, Plugin, reactive, readonly } from "vue";
22

33
const InjectionKey = Symbol("snackbar");
44

5+
type SnackbarType = "success" | "error" | "info" | "warning";
6+
57
interface SnackbarState {
68
message: string;
7-
type: "success" | "error" | "info" | "warning" | "";
9+
type: SnackbarType | null;
810
show: boolean;
911
}
1012

@@ -13,17 +15,17 @@ interface ISnackbarPlugin {
1315
showError: (msg: string) => void;
1416
showInfo: (msg: string) => void;
1517
showWarning: (msg: string) => void;
16-
hideSnackbar: () => void;
18+
setShow: (value: boolean) => void;
1719
state: Readonly<SnackbarState>;
1820
}
1921

2022
const state = reactive<SnackbarState>({
2123
message: "",
22-
type: "",
24+
type: null,
2325
show: false,
2426
});
2527

26-
const showSnackbar = (type: SnackbarState["type"], message: string) => {
28+
const showSnackbar = (type: SnackbarType, message: string) => {
2729
state.message = message;
2830
state.type = type;
2931
state.show = true;
@@ -33,7 +35,7 @@ const showSnackbar = (type: SnackbarState["type"], message: string) => {
3335
}, 4000);
3436
};
3537

36-
const plugin = {
38+
const plugin: ISnackbarPlugin = {
3739
showSuccess: (msg: string) => showSnackbar("success", msg),
3840
showError: (msg: string) => showSnackbar("error", msg),
3941
showInfo: (msg: string) => showSnackbar("info", msg),
@@ -48,5 +50,5 @@ const SnackbarPlugin: Plugin = {
4850
},
4951
};
5052

51-
export { SnackbarPlugin, InjectionKey, plugin as rawPlugin };
53+
export { SnackbarPlugin, InjectionKey, plugin };
5254
export type { SnackbarState, ISnackbarPlugin };

0 commit comments

Comments
 (0)