Files
ui/src/runtime/components/overlays/Notification.vue
2023-11-14 17:19:59 +01:00

231 lines
6.0 KiB
Vue

<template>
<Transition appear v-bind="ui.transition">
<div
:class="wrapperClass"
role="status"
v-bind="attrs"
@mouseover="onMouseover"
@mouseleave="onMouseleave"
>
<div :class="[ui.container, ui.rounded, ui.ring]">
<div class="flex" :class="[ui.padding, ui.gap, { 'items-start': description || $slots.description, 'items-center': !description && !$slots.description }]">
<UIcon v-if="icon" :name="icon" :class="iconClass" />
<UAvatar v-if="avatar" v-bind="{ size: ui.avatar.size, ...avatar }" :class="ui.avatar.base" />
<div class="w-0 flex-1">
<p :class="ui.title">
<slot name="title" :title="title">
{{ title }}
</slot>
</p>
<p v-if="(description || $slots.description)" :class="ui.description">
<slot name="description" :description="description">
{{ description }}
</slot>
</p>
<div v-if="(description || $slots.description) && actions.length" :class="ui.actions">
<UButton v-for="(action, index) of actions" :key="index" v-bind="{ ...ui.default.actionButton, ...action }" @click.stop="onAction(action)" />
</div>
</div>
<div v-if="closeButton || (!description && !$slots.description && actions.length)" :class="twMerge(ui.actions, 'mt-0')">
<template v-if="!description && !$slots.description && actions.length">
<UButton v-for="(action, index) of actions" :key="index" v-bind="{ ...ui.default.actionButton, ...action }" @click.stop="onAction(action)" />
</template>
<UButton v-if="closeButton" aria-label="Close" v-bind="{ ...ui.default.closeButton, ...closeButton }" @click.stop="onClose" />
</div>
</div>
<div v-if="timeout" :class="progressClass" :style="progressStyle" />
</div>
</div>
</Transition>
</template>
<script lang="ts">
import { ref, computed, toRef, onMounted, onUnmounted, watchEffect, defineComponent } from 'vue'
import type { PropType } from 'vue'
import { twMerge, twJoin } from 'tailwind-merge'
import UIcon from '../elements/Icon.vue'
import UAvatar from '../elements/Avatar.vue'
import UButton from '../elements/Button.vue'
import { useUI } from '../../composables/useUI'
import { useTimer } from '../../composables/useTimer'
import { mergeConfig } from '../../utils'
import type { Avatar, Button, NotificationColor, NotificationAction, Strategy } from '../../types'
// @ts-expect-error
import appConfig from '#build/app.config'
import { notification } from '#ui/ui.config'
const config = mergeConfig<typeof notification>(appConfig.ui.strategy, appConfig.ui.notification, notification)
export default defineComponent({
components: {
UIcon,
UAvatar,
UButton
},
inheritAttrs: false,
props: {
id: {
type: [String, Number],
required: true
},
title: {
type: String,
required: true
},
description: {
type: String,
default: null
},
icon: {
type: String,
default: () => config.default.icon
},
avatar: {
type: Object as PropType<Avatar>,
default: null
},
closeButton: {
type: Object as PropType<Button>,
default: () => config.default.closeButton as Button
},
timeout: {
type: Number,
default: 5000
},
actions: {
type: Array as PropType<NotificationAction[]>,
default: () => []
},
callback: {
type: Function,
default: null
},
color: {
type: String as PropType<NotificationColor>,
default: () => config.default.color,
validator (value: string) {
return ['gray', ...appConfig.ui.colors].includes(value)
}
},
class: {
type: [String, Object, Array] as PropType<any>,
default: undefined
},
ui: {
type: Object as PropType<Partial<typeof config & { strategy?: Strategy }>>,
default: undefined
}
},
emits: ['close'],
setup (props, { emit }) {
const { ui, attrs } = useUI('notification', toRef(props, 'ui'), config)
let timer: any = null
const remaining = ref(props.timeout)
const wrapperClass = computed(() => {
return twMerge(twJoin(
ui.value.wrapper,
ui.value.background,
ui.value.rounded,
ui.value.shadow
), props.class)
})
const progressClass = computed(() => {
return twJoin(
ui.value.progress.base,
ui.value.progress.background?.replaceAll('{color}', props.color)
)
})
const progressStyle = computed(() => {
const remainingPercent = remaining.value / props.timeout * 100
return { width: `${remainingPercent || 0}%` }
})
const iconClass = computed(() => {
return twJoin(
ui.value.icon.base,
ui.value.icon.color?.replaceAll('{color}', props.color)
)
})
function onMouseover () {
if (timer) {
timer.pause()
}
}
function onMouseleave () {
if (timer) {
timer.resume()
}
}
function onClose () {
if (timer) {
timer.stop()
}
if (props.callback) {
props.callback()
}
emit('close')
}
function onAction (action: NotificationAction) {
if (timer) {
timer.stop()
}
if (action.click) {
action.click()
}
emit('close')
}
onMounted(() => {
if (!props.timeout) {
return
}
timer = useTimer(() => {
onClose()
}, props.timeout)
watchEffect(() => {
remaining.value = timer.remaining.value
})
})
onUnmounted(() => {
if (timer) {
timer.stop()
}
})
return {
// eslint-disable-next-line vue/no-dupe-keys
ui,
attrs,
wrapperClass,
progressClass,
progressStyle,
iconClass,
onMouseover,
onMouseleave,
onClose,
onAction,
twMerge
}
}
})
</script>