mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-01-19 06:21:46 +01:00
chore: update
This commit is contained in:
19
src/plugins/ticker.client.ts
Normal file
19
src/plugins/ticker.client.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { defineNuxtPlugin } from '#app'
|
||||
|
||||
function Ticker (callback, interval) {
|
||||
let id
|
||||
|
||||
this.start = function () {
|
||||
id = window.setInterval(callback, interval)
|
||||
}
|
||||
|
||||
this.stop = function () {
|
||||
window.clearInterval(id)
|
||||
}
|
||||
|
||||
this.start()
|
||||
}
|
||||
|
||||
export default defineNuxtPlugin((nuxtApp) => {
|
||||
nuxtApp.provide('ticker', Ticker)
|
||||
})
|
||||
34
src/plugins/timer.client.ts
Normal file
34
src/plugins/timer.client.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { defineNuxtPlugin } from '#app'
|
||||
|
||||
function Timer (callback, delay) {
|
||||
let timerId
|
||||
let start
|
||||
let remaining = delay
|
||||
|
||||
this.pause = function () {
|
||||
window.clearTimeout(timerId)
|
||||
remaining -= new Date() - start
|
||||
}
|
||||
|
||||
this.resume = function () {
|
||||
start = new Date()
|
||||
window.clearTimeout(timerId)
|
||||
timerId = window.setTimeout(callback, remaining)
|
||||
}
|
||||
|
||||
this.reset = function () {
|
||||
start = new Date()
|
||||
window.clearTimeout(timerId)
|
||||
timerId = window.setTimeout(callback, delay)
|
||||
}
|
||||
|
||||
this.stop = function () {
|
||||
window.clearTimeout(timerId)
|
||||
}
|
||||
|
||||
this.resume()
|
||||
}
|
||||
|
||||
export default defineNuxtPlugin((nuxtApp) => {
|
||||
nuxtApp.provide('timer', Timer)
|
||||
})
|
||||
53
src/plugins/toast.client.ts
Normal file
53
src/plugins/toast.client.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import { nanoid } from 'nanoid'
|
||||
import { Ref } from 'vue'
|
||||
import { Notification } from '../types'
|
||||
import { defineNuxtPlugin, useState } from '#app'
|
||||
|
||||
export default defineNuxtPlugin((nuxtApp) => {
|
||||
const notifications: Ref<Notification[]> = useState('notifications', () => [])
|
||||
|
||||
function addNotification (notification: Partial<Notification>) {
|
||||
const body = {
|
||||
id: nanoid(),
|
||||
...notification
|
||||
}
|
||||
|
||||
const index = notifications.value.findIndex((n: Notification) => n.id === body.id)
|
||||
if (index === -1) {
|
||||
notifications.value.push(body as Notification)
|
||||
}
|
||||
|
||||
return body
|
||||
}
|
||||
|
||||
function removeNotification (id: string) {
|
||||
notifications.value = notifications.value.filter((n: Notification) => n.id !== id)
|
||||
}
|
||||
|
||||
nuxtApp.provide('toast', {
|
||||
addNotification,
|
||||
removeNotification,
|
||||
success ({ title, description }: { title?: string, description?: string } = {}) {
|
||||
addNotification({
|
||||
type: 'success',
|
||||
title,
|
||||
description,
|
||||
timeout: 4000
|
||||
})
|
||||
},
|
||||
error ({ title = 'An error occurred!', description }: { title?: string, description?: string } = {}) {
|
||||
addNotification({
|
||||
type: 'error',
|
||||
title,
|
||||
description,
|
||||
timeout: 4000
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
declare module '#app' {
|
||||
interface NuxtApp {
|
||||
$toast: object
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user