feat(modal): fix confirm and add notification

This commit is contained in:
DecDuck
2024-12-24 08:54:45 +11:00
parent 98fd24ceb9
commit 3530ee90ba
3 changed files with 121 additions and 6 deletions

View File

@ -1,6 +1,10 @@
<template>
<TransitionRoot as="template" :show="true">
<Dialog class="relative z-50">
<TransitionRoot
as="template"
:show="true"
:style="{ 'z-index': props.zHeight }"
>
<Dialog class="relative z-50" @close="emit('event', 'cancel')">
<TransitionChild
as="template"
enter="ease-out duration-300"
@ -56,12 +60,12 @@
type="submit"
class="w-full sm:w-fit"
>
Confirm
{{ props.data.buttonText ?? "Confirm" }}
</LoadingButton>
<button
type="button"
class="mt-3 inline-flex w-full justify-center rounded-md bg-zinc-800 px-3 py-2 text-sm font-semibold text-zinc-100 shadow-sm ring-1 ring-inset ring-zinc-700 hover:bg-zinc-900 sm:mt-0 sm:w-auto"
@click="emit('event', 'close')"
@click="emit('event', 'cancel')"
ref="cancelButtonRef"
>
Cancel
@ -82,12 +86,18 @@ import {
TransitionChild,
TransitionRoot,
} from "@headlessui/vue";
import type { ModalDatas, ModalType } from "../composables/modal-stack";
import type {
ModalDatas,
ModalEvents,
ModalType,
} from "../composables/modal-stack";
const props = defineProps<{
zHeight: number;
loading: boolean;
data: ModalDatas[ModalType.Confirmation];
}>();
const emit = defineEmits<{ (e: "event", v: string): void }>();
const emit = defineEmits<{
(e: "event", v: ModalEvents[ModalType.Confirmation]): void;
}>();
</script>

View File

@ -0,0 +1,95 @@
<template>
<TransitionRoot
as="template"
:show="true"
:style="{ 'z-index': props.zHeight }"
>
<Dialog class="relative z-50" @close="emit('event', 'close')">
<TransitionChild
as="template"
enter="ease-out duration-300"
enter-from="opacity-0"
enter-to="opacity-100"
leave="ease-in duration-200"
leave-from="opacity-100"
leave-to="opacity-0"
>
<div
class="fixed inset-0 bg-zinc-950 bg-opacity-75 transition-opacity"
/>
</TransitionChild>
<div class="fixed inset-0 z-10 w-screen overflow-y-auto">
<div
class="flex min-h-full items-start justify-center p-4 text-center sm:items-center sm:p-0"
>
<TransitionChild
as="template"
enter="ease-out duration-300"
enter-from="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
enter-to="opacity-100 translate-y-0 sm:scale-100"
leave="ease-in duration-200"
leave-from="opacity-100 translate-y-0 sm:scale-100"
leave-to="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
>
<div
class="relative transform rounded-lg bg-zinc-900 text-left shadow-xl transition-all sm:my-8 sm:w-full sm:max-w-lg"
>
<div class="px-4 pb-4 pt-5 space-y-4 sm:p-6 sm:pb-4">
<div class="sm:flex sm:items-start">
<div class="mt-3 text-center sm:mt-0 sm:text-left">
<DialogTitle
as="h3"
class="text-base font-semibold text-zinc-100"
>{{ props.data.title }}
</DialogTitle>
<div class="mt-2">
<p class="text-sm text-zinc-400">
{{ props.data.description }}
</p>
</div>
</div>
</div>
</div>
<div
class="rounded-b-lg bg-zinc-800 px-4 py-3 sm:flex sm:gap-x-2 sm:flex-row-reverse sm:px-6"
>
<LoadingButton
:loading="props.loading"
@click="emit('event', 'close')"
type="submit"
class="w-full sm:w-fit"
>
{{ props.data.buttonText ?? "Confirm" }}
</LoadingButton>
</div>
</div>
</TransitionChild>
</div>
</div>
</Dialog>
</TransitionRoot>
</template>
<script setup lang="ts">
import {
Dialog,
DialogTitle,
TransitionChild,
TransitionRoot,
} from "@headlessui/vue";
import type {
ModalDatas,
ModalEvents,
ModalType,
} from "../composables/modal-stack";
const props = defineProps<{
zHeight: number;
loading: boolean;
data: ModalDatas[ModalType.Notification];
}>();
const emit = defineEmits<{
(e: "event", v: ModalEvents[ModalType.Notification]): void;
}>();
</script>

View File

@ -1,5 +1,6 @@
import type { Component } from "vue";
import ConfirmationModal from "../components/ConfirmationModal.vue";
import NotificationModal from "../components/NotificationModal.vue";
export type ModalCallbackType<T extends ModalType> = (
event: ModalEvents[T],
@ -16,21 +17,30 @@ export interface ModalStackElement<T extends ModalType> {
export enum ModalType {
Confirmation,
Notification,
}
export type ModalEvents = {
[ModalType.Confirmation]: "confirm" | "cancel";
[ModalType.Notification]: "close";
};
export type ModalDatas = {
[ModalType.Confirmation]: {
title: string;
description: string;
buttonText?: string;
};
[ModalType.Notification]: {
title: string;
description: string;
buttonText?: string;
};
};
const modalComponents: { [key in ModalType]: Component } = {
[ModalType.Confirmation]: ConfirmationModal,
[ModalType.Notification]: NotificationModal,
};
export function createModal<T extends ModalType>(