chore: Optimize loading display

pull/48/MERGE
vben 2024-05-25 10:19:41 +08:00
parent 0732a8d853
commit 666371ed67
3 changed files with 76 additions and 65 deletions

View File

@ -1,97 +1,108 @@
<script lang="ts" setup> <script lang="ts" setup>
import { useNamespace } from '@vben-core/toolkit'; import type { TimeoutHandle } from '@vben/types';
import { ref, watch } from 'vue';
interface Props { interface Props {
/**
* @zh_CN 最小加载时间
* @en_US Minimum loading time
*/
minLoadingTime?: number;
/** /**
* @zh_CN loading状态开启 * @zh_CN loading状态开启
*/ */
spinning: boolean; spinning?: boolean;
} }
defineOptions({ defineOptions({
name: 'Spinner', name: 'Spinner',
}); });
defineProps<Props>(); const props = withDefaults(defineProps<Props>(), {
minLoadingTime: 200,
});
const startTime = ref(0);
const endTime = ref(0);
const showSpinner = ref(false);
const timer = ref<TimeoutHandle>();
const { b, e } = useNamespace('spinner'); watch(
() => props.spinning,
(show) => {
if (!show) {
showSpinner.value = false;
clearTimeout(timer.value);
return;
}
startTime.value = performance.now();
timer.value = setTimeout(() => {
endTime.value = performance.now();
const loadingTime = endTime.value - startTime.value;
showSpinner.value = loadingTime > props.minLoadingTime;
}, props.minLoadingTime);
},
{
immediate: true,
},
);
</script> </script>
<template> <template>
<div <div
:class="[b(), !spinning ? 'hidden' : '']" v-if="showSpinner"
class="flex-center bg-overlay absolute left-0 top-0 size-full backdrop-blur-sm" class="flex-center bg-overlay absolute left-0 top-0 size-full backdrop-blur-sm"
> >
<div :class="e('loader')"></div> <div class="loader relative h-12 w-12"></div>
</div> </div>
</template> </template>
<style lang="scss" scoped> <style lang="scss" scoped>
@import '@vben-core/design/global'; @import '@vben-core/design/global';
@include b('spinner') { @keyframes jump-ani {
@keyframes jump-ani { 15% {
15% { border-bottom-right-radius: 3px;
border-bottom-right-radius: 3px;
}
25% {
transform: translateY(9px) rotate(22.5deg);
}
50% {
border-bottom-right-radius: 40px;
transform: translateY(18px) scale(1, 0.9) rotate(45deg);
}
75% {
transform: translateY(9px) rotate(67.5deg);
}
100% {
transform: translateY(0) rotate(90deg);
}
} }
@keyframes shadow-ani { 25% {
0%, transform: translateY(9px) rotate(22.5deg);
100% {
transform: scale(1, 1);
}
50% {
transform: scale(1.2, 1);
}
} }
@include e('loader') { 50% {
position: relative; border-bottom-right-radius: 40px;
width: 48px; transform: translateY(18px) scale(1, 0.9) rotate(45deg);
height: 48px; }
&::before { 75% {
position: absolute; transform: translateY(9px) rotate(67.5deg);
top: 60px; }
left: 0;
width: 48px;
height: 5px;
content: '';
background: hsl(var(--color-primary) / 50%);
border-radius: 50%;
animation: shadow-ani 0.5s linear infinite;
}
&::after { 100% {
position: absolute; transform: translateY(0) rotate(90deg);
top: 0; }
left: 0; }
width: 100%;
height: 100%; @keyframes shadow-ani {
content: ''; 0%,
background: hsl(var(--color-primary)); 100% {
border-radius: 4px; transform: scale(1, 1);
animation: jump-ani 0.5s linear infinite; }
}
50% {
transform: scale(1.2, 1);
}
}
.loader {
&::before {
@apply bg-primary/50 absolute left-0 top-[60px] h-[5px] w-12 animate-[shadow-ani_0.5s_linear_infinite] rounded-[50%] content-[''];
}
&::after {
@apply bg-primary absolute left-0 top-0 h-full w-full animate-[jump-ani_0.5s_linear_infinite] rounded content-[''];
} }
} }
</style> </style>

View File

@ -20,7 +20,7 @@ fallback:
widgets: widgets:
document: Document document: Document
qa: Q&A qa: FAQ & Help
setting: Setting setting: Setting
logout-tip: Do you want to log out? logout-tip: Do you want to log out?
view-all: View all messages view-all: View all messages

View File

@ -19,7 +19,7 @@ fallback:
widgets: widgets:
document: 文档 document: 文档
qa: 问题&建议 qa: 问题 & 帮助
setting: 设置 setting: 设置
logout-tip: 是否退出登录? logout-tip: 是否退出登录?
view-all: 查看所有消息 view-all: 查看所有消息