index.ts
5.14 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// @ts-nocheck
import { raf } from '@/uni_modules/lime-shared/raf';
// #ifndef UNI-APP-X
import { watchEffect, ref, nextTick } from '@/uni_modules/lime-shared/vue'
// #endif
export type TransitionEmitStatus = "before-enter" | "enter" | "after-enter" | "before-leave" | "leave" | "after-leave"
export type TransitionStatus = '' | 'enter' | 'leave';
export type UseTransitionOptions = {
element ?: Ref<UniElement | null>,
enterClass ?: string,
enterActiveClass ?: string,
enterToClass ?: string,
leaveClass ?: string,
leaveActiveClass ?: string,
leaveToClass ?: string,
appear ?: boolean,
defaultName ?: string,
name ?: () => string,
visible ?: () => boolean,
emits ?: (name : TransitionEmitStatus) => void,
onNextTick ?: (name : TransitionEmitStatus) => Promise<void>,
duration ?: number
}
type ClassNameMap = Map<string, string>;
export type UseTransitionReturn = {
state : Ref<boolean>,
display : Ref<boolean>,
inited : Ref<boolean>,
classes : Ref<string>,
name : Ref<string>,
finished : () => void,
toggle : (v : boolean) => void,
}
export function useTransition(options : UseTransitionOptions) : UseTransitionReturn {
const state = ref(false);
const display = ref(false);
const inited = ref(false);
const classes = ref('');
const name = ref(options.defaultName ?? 'fade');
const enterClass = options.enterClass ?? '';
const enterActiveClass = options.enterActiveClass ?? '';
const enterToClass = options.enterToClass ?? '';
const leaveActiveClass = options.leaveActiveClass ?? '';
const leaveToClass = options.leaveToClass ?? '';
const leaveClass = options.leaveClass ?? '';
const appear = options.appear ?? false
const duration = options.duration ?? 300;
let status : TransitionStatus = '';
let isTransitionEnd = false;
let isTransitioning = false;
let timeoutId = -1
const emitEvent = (event : TransitionEmitStatus) => {
options.emits?.(event);
};
// 结束
const finished = () => {
if (isTransitionEnd) return;
isTransitionEnd = true;
emitEvent(`after-${status}`)
if (display.value && !state.value) {
display.value = false
}
}
const sleep = () : Promise<void> => {
return new Promise((resolve) => {
nextTick(() => {
raf(() => {
// #ifdef APP-ANDROID || APP-IOS || APP-HARMONY
if (options.element?.value != null) {
options.element?.value?.getBoundingClientRectAsync()?.then(res => {
resolve()
})
} else {
resolve()
}
// #endif
// #ifndef APP-ANDROID || APP-IOS || APP-HARMONY
resolve()
// #endif
})
})
})
}
const getClassNames = (name : string) : ClassNameMap => {
return new Map<string, string>([
['enter', `l-${name}-enter l-${name}-enter-active ${enterClass} ${enterActiveClass}`],
['enter-to', `l-${name}-enter-to l-${name}-enter-active ${enterToClass} ${enterActiveClass}`],
['leave', `l-${name}-leave l-${name}-leave-active ${leaveClass} ${leaveActiveClass}`],
['leave-to', `l-${name}-leave-to l-${name}-leave-active ${leaveToClass} ${leaveActiveClass}`]
])
};
const transitionQueue = ref<TransitionStatus[]>([]);
const performTransition = async (newStatus : TransitionStatus, eventName : TransitionStatus) => {
if (status == newStatus) return
transitionQueue.value.push(newStatus);
if (isTransitioning) return;
isTransitioning = true;
// 防止因结束 又切换为开始导致闪烁
isTransitionEnd = true;
while (transitionQueue.value.length > 0) {
const currentStatus = transitionQueue.value.shift()!;
status = currentStatus;
emitEvent(`before-${eventName}`);
await sleep();
await sleep();
if (status != currentStatus) continue;
const classNames = getClassNames(name.value);
inited.value = true;
display.value = true;
classes.value = classNames.get(eventName)!;
emitEvent(eventName);
const executeAfterTick = options.onNextTick?.(eventName);
if (executeAfterTick != null) {
await executeAfterTick;
}
await sleep();
// #ifdef MP
await sleep();
await sleep();
// #endif
if (status != currentStatus) continue;
classes.value = classNames.get(`${eventName}-to`)!;
// isTransitionEnd = false;
// 防止最后无法关闭
if (status == 'leave') {
setTimeout(() => {
finished()
}, duration)
}
}
// 不延迟 会导致快速切换时 因display none 闪烁
clearTimeout(timeoutId)
timeoutId = setTimeout(() => {
if (transitionQueue.value.length == 0 && status == newStatus) {
isTransitionEnd = false;
}
}, duration * 0.8)
isTransitioning = false;
}
// 定义进入过渡的函数
const enter = () => {
performTransition('enter', 'enter');
}
const leave = () => {
performTransition('leave', 'leave');
}
let init = false;
watchEffect(() => {
if (options.visible == null) return
state.value = options.visible!();
if (!appear && !init) {
init = true
return
}
if (state.value) {
enter()
} else {
leave()
}
})
watchEffect(() => {
if (options.name == null) return
name.value = options.name!()
})
const toggle = (v : boolean) => {
state.value = v
if (v) {
enter()
} else {
leave()
}
}
return {
state,
inited,
display,
classes,
name,
finished,
toggle
} as UseTransitionReturn
}