ano-ui
Version:
<p align="center"> <img src="https://github.com/ano-ui/ano-ui/raw/main/public/logo.svg" style="width:100px;" /> <h1 align="center">Ano-UI (WIP)</h1> <p align="center">An UniApp UI components with UnoCSS.</p> </p> <p align="center"> <a href="https://www.np
60 lines (48 loc) • 1.29 kB
text/typescript
import type { SetupContext } from 'vue'
import { computed, ref } from 'vue'
import { CLOSE_EVENT } from '../constants'
import type { ToastOptions } from './types'
import type { ToastEmits, ToastProps } from './toast'
// @unocss-include
export function useToast(props: ToastProps, emit: SetupContext<ToastEmits>['emit']) {
let timer: NodeJS.Timeout
const visible = ref(false)
const state = ref<ToastOptions>()
function close() {
clearTimeout(timer)
visible.value = false
emit(CLOSE_EVENT)
}
function show(options: ToastOptions = {}) {
const {
position = 'default',
message = '',
duration = 2000,
type,
} = options
visible.value = true
state.value = {
type,
position,
message,
duration,
}
clearTimeout(timer)
if (state.value.duration !== false)
timer = setTimeout(close, state.value.duration)
}
const classes = computed(() => {
return [
state.value?.position === 'default' && 'left-50% top-50% translate--50%',
state.value?.position === 'top' && 'left-50% top-20% translate--50%',
state.value?.position === 'bottom' && 'left-50% top-80% translate--50%',
] as const
})
return {
state,
show,
close,
visible,
classes,
}
}