forked from nikita/muzika-gromche
21 lines
384 B
TypeScript
21 lines
384 B
TypeScript
import mitt, { type Handler } 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);
|
|
});
|
|
}
|