-
Notifications
You must be signed in to change notification settings - Fork 175
/
Copy pathloading.tsx
181 lines (162 loc) · 5.12 KB
/
loading.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { defineComponent, computed, ref, watch, h, setBlockTracking, Teleport, onMounted } from 'vue';
import TGradientIcon from './icon/gradient';
import SpinnerIcon from './icon/spinner';
import config from '../config';
import props from './props';
import { useContent, useTNodeJSX } from '../hooks/tnode';
import { usePrefixClass } from '../hooks/useClass';
import { addClass, getAttach, removeClass } from '../shared/dom';
const { prefix } = config;
export default defineComponent({
name: `${prefix}-loading`,
props,
setup(props) {
const renderTNodeJSX = useTNodeJSX();
const renderTNodeContent = useContent();
const loadingClass = usePrefixClass('loading');
const delayShowLoading = ref(false);
const teleportElement = ref();
const countDelay = () => {
delayShowLoading.value = false;
const timer = setTimeout(() => {
delayShowLoading.value = true;
clearTimeout(timer);
}, props.delay);
};
const realLoading = computed(() => (!props.delay || delayShowLoading.value) && props.loading);
watch(
() => props.loading,
(value) => {
if (value) {
props.delay && countDelay();
}
},
{
immediate: true,
},
);
const rootClass = computed(() => [
loadingClass.value,
{ [`${loadingClass.value}--vertical`]: props.layout === 'vertical' },
{ [`${loadingClass.value}--fullscreen`]: props.fullscreen },
{ [`${loadingClass.value}--full`]: !props.fullscreen && !!props.attach },
]);
const textClass = computed(() => [
`${loadingClass.value}__text`,
{
[`${loadingClass.value}__text--only`]: !props.indicator,
},
]);
const rootStyle = computed(() => {
const style = [];
if (props.inheritColor) {
style.push('color: inherit');
}
if (props.size) {
style.push(`font-size: ${props.size};`);
}
return style.join(';');
});
const defaultIndicator = {
circular: TGradientIcon,
spinner: SpinnerIcon,
};
onMounted(() => {
if (props.attach) {
const attach = getAttach(props.attach);
if (!attach) {
console.error('attach is not exist');
} else {
teleportElement.value = attach;
}
}
if (props.fullscreen) {
teleportElement.value = getAttach('body');
}
});
const dotsLoading = computed(() => {
setBlockTracking(-1);
const node = (
<div
class={`${loadingClass.value}__dots`}
style={{
animationPlayState: props.pause ? 'paused' : '',
animationDirection: props.reverse ? 'reverse' : '',
animationDuration: `${props.duration}ms`,
width: props.size,
height: props.size,
}}
>
{Array.from({ length: 3 }).map((val, i) => {
return (
<div
class={`${loadingClass.value}__dot`}
style={
props.duration
? `animation-duration: ${props.duration / 1000}s; animation-delay: ${(props.duration * i) / 3000}s`
: ''
}
></div>
);
})}
</div>
);
setBlockTracking(1);
return node;
});
const defaultLoading = computed(() => {
setBlockTracking(-1);
const TIndicator = defaultIndicator[props.theme || 'circular'];
const node = (
<TIndicator
style={{
animationPlayState: props.pause ? 'paused' : '',
animationDirection: props.reverse ? 'reverse' : '',
animationDuration: `${props.duration}ms`,
width: props.size,
height: props.size,
}}
/>
);
setBlockTracking(1);
return node;
});
watch(
() => props.loading,
(isLoading) => {
if (isLoading && props.fullscreen) {
countDelay();
addClass(document.body, `${loadingClass.value}--lock`);
} else {
removeClass(document.body, `${loadingClass.value}--lock`);
}
},
);
return () => {
const indicator = renderTNodeJSX('indicator', {
defaultNode: props.theme === 'dots' ? dotsLoading.value : defaultLoading.value,
});
const text = renderTNodeJSX('text');
const TNodeContent = renderTNodeContent('default', 'content');
if (props.fullscreen || props.attach) {
if (!props.loading) return null;
return (
<Teleport disabled={!props.attach || !teleportElement.value} to={teleportElement.value}>
<div class={rootClass.value} style={rootStyle.value}>
{realLoading.value && indicator}
{text && realLoading.value && <span class={textClass.value}>{text}</span>}
{TNodeContent}
</div>
</Teleport>
);
}
return (
<div class={rootClass.value} style={rootStyle.value}>
{realLoading.value && indicator}
{text && realLoading.value && <span class={textClass.value}>{text}</span>}
{TNodeContent}
</div>
);
};
},
});