-
-
Notifications
You must be signed in to change notification settings - Fork 8.4k
/
TransitionGroup.ts
231 lines (209 loc) · 6.19 KB
/
TransitionGroup.ts
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import {
type ElementWithTransition,
type TransitionProps,
TransitionPropsValidators,
addTransitionClass,
forceReflow,
getTransitionInfo,
removeTransitionClass,
resolveTransitionProps,
vtcKey,
} from './Transition'
import {
type ComponentOptions,
DeprecationTypes,
Fragment,
type SetupContext,
type VNode,
compatUtils,
createVNode,
getCurrentInstance,
getTransitionRawChildren,
onUpdated,
resolveTransitionHooks,
setTransitionHooks,
toRaw,
useTransitionState,
warn,
} from '@vue/runtime-core'
import { extend } from '@vue/shared'
const positionMap = new WeakMap<VNode, DOMRect>()
const newPositionMap = new WeakMap<VNode, DOMRect>()
const moveCbKey = Symbol('_moveCb')
const enterCbKey = Symbol('_enterCb')
export type TransitionGroupProps = Omit<TransitionProps, 'mode'> & {
tag?: string
moveClass?: string
}
/**
* Wrap logic that modifies TransitionGroup properties in a function
* so that it can be annotated as pure
*/
const decorate = (t: typeof TransitionGroupImpl) => {
// TransitionGroup does not support "mode" so we need to remove it from the
// props declarations, but direct delete operation is considered a side effect
delete t.props.mode
if (__COMPAT__) {
t.__isBuiltIn = true
}
return t
}
const TransitionGroupImpl: ComponentOptions = /*@__PURE__*/ decorate({
name: 'TransitionGroup',
props: /*@__PURE__*/ extend({}, TransitionPropsValidators, {
tag: String,
moveClass: String,
}),
setup(props: TransitionGroupProps, { slots }: SetupContext) {
const instance = getCurrentInstance()!
const state = useTransitionState()
let prevChildren: VNode[]
let children: VNode[]
onUpdated(() => {
// children is guaranteed to exist after initial render
if (!prevChildren.length) {
return
}
const moveClass = props.moveClass || `${props.name || 'v'}-move`
if (
!hasCSSTransform(
prevChildren[0].el as ElementWithTransition,
instance.vnode.el as Node,
moveClass,
)
) {
return
}
// we divide the work into three loops to avoid mixing DOM reads and writes
// in each iteration - which helps prevent layout thrashing.
prevChildren.forEach(callPendingCbs)
prevChildren.forEach(recordPosition)
const movedChildren = prevChildren.filter(applyTranslation)
// force reflow to put everything in position
forceReflow()
movedChildren.forEach(c => {
const el = c.el as ElementWithTransition
const style = el.style
addTransitionClass(el, moveClass)
style.transform = style.webkitTransform = style.transitionDuration = ''
const cb = ((el as any)[moveCbKey] = (e: TransitionEvent) => {
if (e && e.target !== el) {
return
}
if (!e || /transform$/.test(e.propertyName)) {
el.removeEventListener('transitionend', cb)
;(el as any)[moveCbKey] = null
removeTransitionClass(el, moveClass)
}
})
el.addEventListener('transitionend', cb)
})
})
return () => {
const rawProps = toRaw(props)
const cssTransitionProps = resolveTransitionProps(rawProps)
let tag = rawProps.tag || Fragment
if (
__COMPAT__ &&
!rawProps.tag &&
compatUtils.checkCompatEnabled(
DeprecationTypes.TRANSITION_GROUP_ROOT,
instance.parent,
)
) {
tag = 'span'
}
prevChildren = []
if (children) {
for (let i = 0; i < children.length; i++) {
const child = children[i]
if (child.el && child.el instanceof Element) {
prevChildren.push(child)
setTransitionHooks(
child,
resolveTransitionHooks(
child,
cssTransitionProps,
state,
instance,
),
)
positionMap.set(
child,
(child.el as Element).getBoundingClientRect(),
)
}
}
}
children = slots.default ? getTransitionRawChildren(slots.default()) : []
for (let i = 0; i < children.length; i++) {
const child = children[i]
if (child.key != null) {
setTransitionHooks(
child,
resolveTransitionHooks(child, cssTransitionProps, state, instance),
)
} else if (__DEV__) {
warn(`<TransitionGroup> children must be keyed.`)
}
}
return createVNode(tag, null, children)
}
},
})
export const TransitionGroup = TransitionGroupImpl as unknown as {
new (): {
$props: TransitionGroupProps
}
}
function callPendingCbs(c: VNode) {
const el = c.el as any
if (el[moveCbKey]) {
el[moveCbKey]()
}
if (el[enterCbKey]) {
el[enterCbKey]()
}
}
function recordPosition(c: VNode) {
newPositionMap.set(c, (c.el as Element).getBoundingClientRect())
}
function applyTranslation(c: VNode): VNode | undefined {
const oldPos = positionMap.get(c)!
const newPos = newPositionMap.get(c)!
const dx = oldPos.left - newPos.left
const dy = oldPos.top - newPos.top
if (dx || dy) {
const s = (c.el as HTMLElement).style
s.transform = s.webkitTransform = `translate(${dx}px,${dy}px)`
s.transitionDuration = '0s'
return c
}
}
function hasCSSTransform(
el: ElementWithTransition,
root: Node,
moveClass: string,
): boolean {
// Detect whether an element with the move class applied has
// CSS transitions. Since the element may be inside an entering
// transition at this very moment, we make a clone of it and remove
// all other transition classes applied to ensure only the move class
// is applied.
const clone = el.cloneNode() as HTMLElement
const _vtc = el[vtcKey]
if (_vtc) {
_vtc.forEach(cls => {
cls.split(/\s+/).forEach(c => c && clone.classList.remove(c))
})
}
moveClass.split(/\s+/).forEach(c => c && clone.classList.add(c))
clone.style.display = 'none'
const container = (
root.nodeType === 1 ? root : root.parentNode
) as HTMLElement
container.appendChild(clone)
const { hasTransform } = getTransitionInfo(clone)
container.removeChild(clone)
return hasTransform
}