forked from nikita/muzika-gromche
22 lines
393 B
TypeScript
22 lines
393 B
TypeScript
import type { Handler } from 'mitt'
|
|
import mitt from 'mitt'
|
|
import { onBeforeUnmount } from 'vue'
|
|
|
|
export type Events = {
|
|
scrollToTop: void
|
|
}
|
|
|
|
export const emitter = mitt<Events>()
|
|
|
|
export function useEvent<Key extends keyof Events>(
|
|
type: Key,
|
|
handler: Handler<Events[Key]>,
|
|
) {
|
|
const { on, off } = emitter
|
|
|
|
on(type, handler)
|
|
onBeforeUnmount(() => {
|
|
off(type, handler)
|
|
})
|
|
}
|