advent22/ui/src/components/bulma/Secret.vue

53 lines
1.3 KiB
Vue
Raw Normal View History

<!-- eslint-disable vue/multi-word-component-names -->
<template>
<slot v-if="state === 'visible'" name="default" />
<span v-else>***</span>
2023-09-14 13:54:23 +00:00
<BulmaButton
:class="`is-small is-${record.color} ml-2`"
:icon="['fas', record.icon]"
:busy="state === 'pending'"
2023-09-14 13:54:23 +00:00
@click="on_click"
/>
</template>
<script setup lang="ts">
import { computed, ref } from "vue";
2024-08-27 00:25:42 +00:00
2023-09-14 13:54:23 +00:00
import BulmaButton from "./Button.vue";
const emit = defineEmits<{
(event: "show"): void;
(event: "hide"): void;
}>();
type State = "hidden" | "pending" | "visible";
const state = ref<State>("hidden");
const state_map: Record<State, { color: string; icon: string; next: State }> = {
hidden: { color: "primary", icon: "eye-slash", next: "pending" },
pending: { color: "warning", icon: "eye-slash", next: "visible" },
visible: { color: "danger", icon: "eye", next: "hidden" },
} as const;
const record = computed(() => state_map[state.value] ?? state_map.hidden);
let pending_timeout: number | undefined;
function on_click(): void {
state.value = record.value.next;
if (state.value === "hidden") {
emit("hide");
}
if (state.value === "pending") {
pending_timeout = window.setTimeout(() => (state.value = "hidden"), 2500);
} else {
window.clearTimeout(pending_timeout);
}
if (state.value === "visible") {
emit("show");
}
}
</script>