Files
swingmusic-extended/src/stores/loader.ts
T
2022-09-10 10:46:45 -04:00

38 lines
859 B
TypeScript

import { defineStore } from "pinia";
export default defineStore("Loader", {
state: () => ({
loading: false,
duration: 0,
page: <HTMLHtmlElement | null>null,
}),
actions: {
startLoading() {
this.loading = true;
this.duration = new Date().getTime();
if (!this.page) {
this.page = document.getElementsByTagName("html")[0] as HTMLHtmlElement;
}
this.page.classList.add("loading");
},
stopLoading() {
const diff = new Date().getTime() - this.duration;
const resetCursor = () => {
this.page ? this.page.classList.remove("loading") : null;
};
if (diff <= 250) {
setTimeout(() => {
resetCursor();
this.loading = false;
}, 250 - diff);
} else {
resetCursor();
this.loading = false;
}
},
},
});