forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReactFiberApplyGesture.js
1253 lines (1203 loc) · 44.6 KB
/
ReactFiberApplyGesture.js
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import type {ViewTransitionProps} from 'shared/ReactTypes';
import type {Fiber, FiberRoot} from './ReactInternalTypes';
import type {Instance, TextInstance, Props} from './ReactFiberConfig';
import type {OffscreenState} from './ReactFiberOffscreenComponent';
import type {ViewTransitionState} from './ReactFiberViewTransitionComponent';
import {
cloneMutableInstance,
cloneMutableTextInstance,
cloneRootViewTransitionContainer,
removeRootViewTransitionClone,
cancelRootViewTransitionName,
restoreRootViewTransitionName,
cancelViewTransitionName,
applyViewTransitionName,
appendChild,
commitUpdate,
commitTextUpdate,
resetTextContent,
supportsResources,
supportsSingletons,
unhideInstance,
unhideTextInstance,
} from './ReactFiberConfig';
import {
popMutationContext,
pushMutationContext,
viewTransitionMutationContext,
trackHostMutation,
} from './ReactFiberMutationTracking';
import {
MutationMask,
Update,
ContentReset,
NoFlags,
Visibility,
ViewTransitionNamedStatic,
ViewTransitionStatic,
AffectedParentLayout,
} from './ReactFiberFlags';
import {
HostComponent,
HostHoistable,
HostSingleton,
HostText,
HostPortal,
OffscreenComponent,
ViewTransitionComponent,
} from './ReactWorkTags';
import {
restoreEnterOrExitViewTransitions,
restoreNestedViewTransitions,
restoreUpdateViewTransitionForGesture,
appearingViewTransitions,
commitEnterViewTransitions,
measureNestedViewTransitions,
measureUpdateViewTransition,
viewTransitionCancelableChildren,
pushViewTransitionCancelableScope,
popViewTransitionCancelableScope,
} from './ReactFiberCommitViewTransitions';
import {
getViewTransitionName,
getViewTransitionClassName,
} from './ReactFiberViewTransitionComponent';
let didWarnForRootClone = false;
// Used during the apply phase to track whether a parent ViewTransition component
// might have been affected by any mutations / relayouts below.
let viewTransitionContextChanged: boolean = false;
function detectMutationOrInsertClones(finishedWork: Fiber): boolean {
return true;
}
const CLONE_UPDATE = 0; // Mutations in this subtree or potentially affected by layout.
const CLONE_EXIT = 1; // Inside a reappearing offscreen before the next ViewTransition or HostComponent.
const CLONE_UNHIDE = 2; // Inside a reappearing offscreen before the next HostComponent.
const CLONE_APPEARING_PAIR = 3; // Like UNHIDE but we're already inside the first Host Component only finding pairs.
const CLONE_UNCHANGED = 4; // Nothing in this tree was changed but we're still walking to clone it.
const INSERT_EXIT = 5; // Inside a newly mounted tree before the next ViewTransition or HostComponent.
const INSERT_APPEND = 6; // Inside a newly mounted tree before the next HostComponent.
const INSERT_APPEARING_PAIR = 7; // Inside a newly mounted tree only finding pairs.
type VisitPhase = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7;
function applyViewTransitionToClones(
name: string,
className: ?string,
clones: Array<Instance>,
): void {
// This gets called when we have found a pair, but after the clone in created. The clone is
// created by the insertion side. If the insertion side if found before the deletion side
// then this is called by the deletion. If the deletion is visited first then this is called
// later by the insertion when the clone has been created.
for (let i = 0; i < clones.length; i++) {
applyViewTransitionName(
clones[i],
i === 0
? name
: // If we have multiple Host Instances below, we add a suffix to the name to give
// each one a unique name.
name + '_' + i,
className,
);
}
}
function trackDeletedPairViewTransitions(deletion: Fiber): void {
if (
appearingViewTransitions === null ||
appearingViewTransitions.size === 0
) {
// We've found all.
return;
}
const pairs = appearingViewTransitions;
if ((deletion.subtreeFlags & ViewTransitionNamedStatic) === NoFlags) {
// This has no named view transitions in its subtree.
return;
}
let child = deletion.child;
while (child !== null) {
if (child.tag === OffscreenComponent && child.memoizedState === null) {
// This tree was already hidden so we skip it.
} else {
if (
child.tag === ViewTransitionComponent &&
(child.flags & ViewTransitionNamedStatic) !== NoFlags
) {
const props: ViewTransitionProps = child.memoizedProps;
const name = props.name;
if (name != null && name !== 'auto') {
const pair = pairs.get(name);
if (pair !== undefined) {
// Delete the entry so that we know when we've found all of them
// and can stop searching (size reaches zero).
pairs.delete(name);
const className: ?string = getViewTransitionClassName(
props.default,
props.share,
);
if (className !== 'none') {
// TODO: Since the deleted instance already has layout we could
// check if it's in the viewport and if not skip the pairing.
// It would currently cause layout thrash though so if we did that
// we need to avoid inserting the root of the cloned trees until
// the end.
// The "old" instance is actually the one we're inserting.
const oldInstance: ViewTransitionState = pair;
// The "new" instance is the already mounted one we're deleting.
const newInstance: ViewTransitionState = child.stateNode;
oldInstance.paired = newInstance;
newInstance.paired = oldInstance;
const clones = oldInstance.clones;
if (clones !== null) {
// If we have clones that means that we've already visited this
// ViewTransition boundary before and we can now apply the name
// to those clones. Otherwise, we have to wait until we clone it.
applyViewTransitionToClones(name, className, clones);
}
}
if (pairs.size === 0) {
break;
}
}
}
}
trackDeletedPairViewTransitions(child);
}
child = child.sibling;
}
}
function trackEnterViewTransitions(deletion: Fiber): void {
if (deletion.tag === ViewTransitionComponent) {
const props: ViewTransitionProps = deletion.memoizedProps;
const name = getViewTransitionName(props, deletion.stateNode);
const pair =
appearingViewTransitions !== null
? appearingViewTransitions.get(name)
: undefined;
const className: ?string = getViewTransitionClassName(
props.default,
pair !== undefined ? props.share : props.enter,
);
if (className !== 'none') {
if (pair !== undefined) {
// TODO: Since the deleted instance already has layout we could
// check if it's in the viewport and if not skip the pairing.
// It would currently cause layout thrash though so if we did that
// we need to avoid inserting the root of the cloned trees until
// the end.
// Delete the entry so that we know when we've found all of them
// and can stop searching (size reaches zero).
// $FlowFixMe[incompatible-use]: Refined by the pair.
appearingViewTransitions.delete(name);
// The "old" instance is actually the one we're inserting.
const oldInstance: ViewTransitionState = pair;
// The "new" instance is the already mounted one we're deleting.
const newInstance: ViewTransitionState = deletion.stateNode;
oldInstance.paired = newInstance;
newInstance.paired = oldInstance;
const clones = oldInstance.clones;
if (clones !== null) {
// If we have clones that means that we've already visited this
// ViewTransition boundary before and we can now apply the name
// to those clones. Otherwise, we have to wait until we clone it.
applyViewTransitionToClones(name, className, clones);
}
}
}
// Look for more pairs deeper in the tree.
trackDeletedPairViewTransitions(deletion);
} else if ((deletion.subtreeFlags & ViewTransitionStatic) !== NoFlags) {
let child = deletion.child;
while (child !== null) {
trackEnterViewTransitions(child);
child = child.sibling;
}
} else {
trackDeletedPairViewTransitions(deletion);
}
}
function applyAppearingPairViewTransition(child: Fiber): void {
// Normally these helpers do recursive calls but since insertion/offscreen is forked
// we call this helper from those loops instead. This must be called only on
// ViewTransitionComponent that has already had their clones filled.
if ((child.flags & ViewTransitionNamedStatic) !== NoFlags) {
const state: ViewTransitionState = child.stateNode;
// If this is not yet paired, it doesn't mean that we won't pair it later when
// we find the deletion side. If that's the case then we'll add the names to
// the clones then.
if (state.paired) {
const props: ViewTransitionProps = child.memoizedProps;
if (props.name == null || props.name === 'auto') {
throw new Error(
'Found a pair with an auto name. This is a bug in React.',
);
}
const name = props.name;
// Note that this class name that doesn't actually really matter because the
// "new" side will be the one that wins in practice.
const className: ?string = getViewTransitionClassName(
props.default,
props.share,
);
if (className !== 'none') {
const clones = state.clones;
// If there are no clones at this point, that should mean that there are no
// HostComponent children in this ViewTransition.
if (clones !== null) {
applyViewTransitionToClones(name, className, clones);
}
}
}
}
}
function applyExitViewTransition(placement: Fiber): void {
// Normally these helpers do recursive calls but since insertion/offscreen is forked
// we call this helper from those loops instead. This must be called only on
// ViewTransitionComponent that has already had their clones filled.
const state: ViewTransitionState = placement.stateNode;
const props: ViewTransitionProps = placement.memoizedProps;
const name = getViewTransitionName(props, state);
const className: ?string = getViewTransitionClassName(
props.default,
// Note that just because we don't have a pair yet doesn't mean we won't find one
// later. However, that doesn't matter because if we do the class name that wins
// is the one applied by the "new" side anyway.
state.paired ? props.share : props.exit,
);
if (className !== 'none') {
// TODO: Ideally we could determine if this exit is in the viewport and
// exclude it otherwise but that would require waiting until we insert
// and layout the clones first. Currently wait until the view transition
// starts before reading the layout.
const clones = state.clones;
// If there are no clones at this point, that should mean that there are no
// HostComponent children in this ViewTransition.
if (clones !== null) {
applyViewTransitionToClones(name, className, clones);
}
}
}
function applyNestedViewTransition(child: Fiber): void {
const state: ViewTransitionState = child.stateNode;
const props: ViewTransitionProps = child.memoizedProps;
const name = getViewTransitionName(props, state);
const className: ?string = getViewTransitionClassName(
props.default,
props.update,
);
if (className !== 'none') {
const clones = state.clones;
// If there are no clones at this point, that should mean that there are no
// HostComponent children in this ViewTransition.
if (clones !== null) {
applyViewTransitionToClones(name, className, clones);
}
}
}
function applyUpdateViewTransition(current: Fiber, finishedWork: Fiber): void {
const state: ViewTransitionState = finishedWork.stateNode;
// Updates can have conflicting names and classNames.
// Since we're doing a reverse animation the "new" state is actually the current
// and the "old" state is the finishedWork.
const newProps: ViewTransitionProps = current.memoizedProps;
const oldProps: ViewTransitionProps = finishedWork.memoizedProps;
const oldName = getViewTransitionName(oldProps, state);
// This className applies only if there are fewer child DOM nodes than
// before or if this update should've been cancelled but we ended up with
// a parent animating so we need to animate the child too. Otherwise
// the "new" state wins. Since "new" normally wins, that's usually what
// we would use. However, since this animation is going in reverse we actually
// want the props from "current" since that's the class that would've won if
// it was the normal direction. To preserve the same effect in either direction.
const className: ?string = getViewTransitionClassName(
newProps.default,
newProps.update,
);
if (className === 'none') {
// If update is "none" then we don't have to apply a name. Since we won't animate this boundary.
return;
}
const clones = state.clones;
// If there are no clones at this point, that should mean that there are no
// HostComponent children in this ViewTransition.
if (clones !== null) {
applyViewTransitionToClones(oldName, className, clones);
}
}
function recursivelyInsertNew(
parentFiber: Fiber,
hostParentClone: Instance,
parentViewTransition: null | ViewTransitionState,
visitPhase: VisitPhase,
): void {
if (
visitPhase === INSERT_APPEARING_PAIR &&
parentViewTransition === null &&
(parentFiber.subtreeFlags & ViewTransitionNamedStatic) === NoFlags
) {
// We're just searching for pairs but we have reached the end.
return;
}
let child = parentFiber.child;
while (child !== null) {
recursivelyInsertNewFiber(
child,
hostParentClone,
parentViewTransition,
visitPhase,
);
child = child.sibling;
}
}
function recursivelyInsertNewFiber(
finishedWork: Fiber,
hostParentClone: Instance,
parentViewTransition: null | ViewTransitionState,
visitPhase: VisitPhase,
): void {
switch (finishedWork.tag) {
case HostHoistable: {
if (supportsResources) {
// TODO: Hoistables should get optimistically inserted and then removed.
recursivelyInsertNew(
finishedWork,
hostParentClone,
parentViewTransition,
visitPhase,
);
break;
}
// Fall through
}
case HostSingleton: {
if (supportsSingletons) {
recursivelyInsertNew(
finishedWork,
hostParentClone,
parentViewTransition,
visitPhase,
);
if (__DEV__) {
// We cannot apply mutations to Host Singletons since by definition
// they cannot be cloned. Therefore we warn in DEV if this commit
// had any effect.
if (finishedWork.flags & Update) {
console.error(
'startGestureTransition() caused something to render a new <%s>. ' +
'This is not possible in the current implementation. ' +
"Make sure that the swipe doesn't mount any new <%s> elements.",
finishedWork.type,
finishedWork.type,
);
}
}
break;
}
// Fall through
}
case HostComponent: {
const instance: Instance = finishedWork.stateNode;
// For insertions we don't need to clone. It's already new state node.
if (visitPhase !== INSERT_APPEARING_PAIR) {
appendChild(hostParentClone, instance);
trackHostMutation();
recursivelyInsertNew(
finishedWork,
instance,
null,
INSERT_APPEARING_PAIR,
);
} else {
recursivelyInsertNew(finishedWork, instance, null, visitPhase);
}
if (parentViewTransition !== null) {
if (parentViewTransition.clones === null) {
parentViewTransition.clones = [instance];
} else {
parentViewTransition.clones.push(instance);
}
}
break;
}
case HostText: {
const textInstance: TextInstance = finishedWork.stateNode;
if (textInstance === null) {
throw new Error(
'This should have a text node initialized. This error is likely ' +
'caused by a bug in React. Please file an issue.',
);
}
// For insertions we don't need to clone. It's already new state node.
if (visitPhase !== INSERT_APPEARING_PAIR) {
appendChild(hostParentClone, textInstance);
trackHostMutation();
}
break;
}
case HostPortal: {
// TODO: Consider what should happen to Portals. For now we exclude them.
break;
}
case OffscreenComponent: {
const newState: OffscreenState | null = finishedWork.memoizedState;
const isHidden = newState !== null;
if (!isHidden) {
// Only insert nodes if this tree is going to be visible. No need to
// insert invisible content.
// Since there was no mutation to this node, it couldn't have changed
// visibility so we don't need to update visitPhase here.
recursivelyInsertNew(
finishedWork,
hostParentClone,
parentViewTransition,
visitPhase,
);
}
break;
}
case ViewTransitionComponent:
const prevMutationContext = pushMutationContext();
const viewTransitionState: ViewTransitionState = finishedWork.stateNode;
// TODO: If this was already cloned by a previous pass we can reuse those clones.
viewTransitionState.clones = null;
let nextPhase;
if (visitPhase === INSERT_EXIT) {
// This was an Enter of a ViewTransition. We now move onto inserting the inner
// HostComponents and finding inner pairs.
nextPhase = INSERT_APPEND;
} else {
nextPhase = visitPhase;
}
recursivelyInsertNew(
finishedWork,
hostParentClone,
viewTransitionState,
nextPhase,
);
// After we've inserted the new nodes into the "clones" set we can apply share
// or exit transitions to them.
if (visitPhase === INSERT_EXIT) {
applyExitViewTransition(finishedWork);
} else if (
visitPhase === INSERT_APPEARING_PAIR ||
visitPhase === INSERT_APPEND
) {
applyAppearingPairViewTransition(finishedWork);
}
popMutationContext(prevMutationContext);
break;
default: {
recursivelyInsertNew(
finishedWork,
hostParentClone,
parentViewTransition,
visitPhase,
);
break;
}
}
}
function recursivelyInsertClonesFromExistingTree(
parentFiber: Fiber,
hostParentClone: Instance,
parentViewTransition: null | ViewTransitionState,
visitPhase: VisitPhase,
): void {
let child = parentFiber.child;
while (child !== null) {
switch (child.tag) {
case HostComponent: {
const instance: Instance = child.stateNode;
let nextPhase: VisitPhase;
switch (visitPhase) {
case CLONE_EXIT:
case CLONE_UNHIDE:
case CLONE_APPEARING_PAIR:
// If this was an unhide, we need to keep going if there are any named
// pairs in this subtree, since they might need to be marked.
nextPhase =
(child.subtreeFlags & ViewTransitionNamedStatic) !== NoFlags
? CLONE_APPEARING_PAIR
: CLONE_UNCHANGED;
break;
default:
// We've found any "layout" View Transitions at this point so we can bail.
nextPhase = CLONE_UNCHANGED;
}
let clone: Instance;
if (nextPhase !== CLONE_UNCHANGED) {
// We might need a handle on these clones, so we need to do a shallow clone
// and keep going.
clone = cloneMutableInstance(instance, false);
recursivelyInsertClonesFromExistingTree(
child,
clone,
null,
nextPhase,
);
} else {
// If we have no mutations in this subtree, and we don't need a handle on the
// clones, then we can do a deep clone instead and bailout.
clone = cloneMutableInstance(instance, true);
// TODO: We may need to transfer some DOM state such as scroll position
// for the deep clones.
// TODO: If there's a manual view-transition-name inside the clone we
// should ideally remove it from the original and then restore it in mutation
// phase. Otherwise it leads to duplicate names.
}
appendChild(hostParentClone, clone);
if (parentViewTransition !== null) {
if (parentViewTransition.clones === null) {
parentViewTransition.clones = [clone];
} else {
parentViewTransition.clones.push(clone);
}
}
if (visitPhase === CLONE_EXIT || visitPhase === CLONE_UNHIDE) {
unhideInstance(clone, child.memoizedProps);
trackHostMutation();
}
break;
}
case HostText: {
const textInstance: TextInstance = child.stateNode;
if (textInstance === null) {
throw new Error(
'This should have a text node initialized. This error is likely ' +
'caused by a bug in React. Please file an issue.',
);
}
const clone = cloneMutableTextInstance(textInstance);
appendChild(hostParentClone, clone);
if (visitPhase === CLONE_EXIT || visitPhase === CLONE_UNHIDE) {
unhideTextInstance(clone, child.memoizedProps);
trackHostMutation();
}
break;
}
case HostPortal: {
// TODO: Consider what should happen to Portals. For now we exclude them.
break;
}
case OffscreenComponent: {
const newState: OffscreenState | null = child.memoizedState;
const isHidden = newState !== null;
if (!isHidden) {
// Only insert clones if this tree is going to be visible. No need to
// clone invisible content.
// TODO: If this is visible but detached it should still be cloned.
// Since there was no mutation to this node, it couldn't have changed
// visibility so we don't need to update visitPhase here.
recursivelyInsertClonesFromExistingTree(
child,
hostParentClone,
parentViewTransition,
visitPhase,
);
}
break;
}
case ViewTransitionComponent:
const prevMutationContext = pushMutationContext();
const viewTransitionState: ViewTransitionState = child.stateNode;
// TODO: If this was already cloned by a previous pass we can reuse those clones.
viewTransitionState.clones = null;
// "Existing" view transitions are in subtrees that didn't update so
// this is a "current". We normally clear this upon rerendering
// but we use this flag to track changes from layout in the commit.
// So we need it to be cleared before we do that.
// TODO: Use some other temporary state to track this.
child.flags &= ~Update;
let nextPhase;
if (visitPhase === CLONE_EXIT) {
// This was an Enter of a ViewTransition. We now move onto unhiding the inner
// HostComponents and finding inner pairs.
nextPhase = CLONE_UNHIDE;
// TODO: Mark the name and find a pair.
} else if (visitPhase === CLONE_UPDATE) {
// If the tree had no mutations and we've found the top most ViewTransition
// then this is the one we might apply the "layout" state too if it has changed
// position. After we've found its HostComponents we can bail out.
nextPhase = CLONE_UNCHANGED;
} else {
nextPhase = visitPhase;
}
recursivelyInsertClonesFromExistingTree(
child,
hostParentClone,
viewTransitionState,
nextPhase,
);
// After we've collected the cloned instances, we can apply exit or share transitions
// to them.
if (visitPhase === CLONE_EXIT) {
applyExitViewTransition(child);
} else if (
visitPhase === CLONE_APPEARING_PAIR ||
visitPhase === CLONE_UNHIDE
) {
applyAppearingPairViewTransition(child);
} else if (visitPhase === CLONE_UPDATE) {
applyNestedViewTransition(child);
}
popMutationContext(prevMutationContext);
break;
default: {
recursivelyInsertClonesFromExistingTree(
child,
hostParentClone,
parentViewTransition,
visitPhase,
);
break;
}
}
child = child.sibling;
}
}
function recursivelyInsertClones(
parentFiber: Fiber,
hostParentClone: Instance,
parentViewTransition: null | ViewTransitionState,
visitPhase: VisitPhase,
) {
const deletions = parentFiber.deletions;
if (deletions !== null) {
for (let i = 0; i < deletions.length; i++) {
const childToDelete = deletions[i];
trackEnterViewTransitions(childToDelete);
// Normally we would only mark something as triggering a mutation if there was
// actually a HostInstance below here. If this tree didn't contain a HostInstances
// we shouldn't trigger a mutation even though a virtual component was deleted.
trackHostMutation();
}
}
if (
parentFiber.alternate === null ||
(parentFiber.subtreeFlags & MutationMask) !== NoFlags
) {
// If we have mutations or if this is a newly inserted tree, clone as we go.
let child = parentFiber.child;
while (child !== null) {
insertDestinationClonesOfFiber(
child,
hostParentClone,
parentViewTransition,
visitPhase,
);
child = child.sibling;
}
} else {
// Once we reach a subtree with no more mutations we can bail out.
// However, we must still insert deep clones of the HostComponents.
recursivelyInsertClonesFromExistingTree(
parentFiber,
hostParentClone,
parentViewTransition,
visitPhase,
);
}
}
function insertDestinationClonesOfFiber(
finishedWork: Fiber,
hostParentClone: Instance,
parentViewTransition: null | ViewTransitionState,
visitPhase: VisitPhase,
) {
const current = finishedWork.alternate;
if (current === null) {
// This is a newly mounted subtree. Insert any HostComponents and trigger
// Enter transitions.
recursivelyInsertNewFiber(
finishedWork,
hostParentClone,
parentViewTransition,
INSERT_EXIT,
);
return;
}
const flags = finishedWork.flags;
// The effect flag should be checked *after* we refine the type of fiber,
// because the fiber tag is more specific. An exception is any flag related
// to reconciliation, because those can be set on all fiber types.
switch (finishedWork.tag) {
case HostHoistable: {
if (supportsResources) {
// TODO: Hoistables should get optimistically inserted and then removed.
recursivelyInsertClones(
finishedWork,
hostParentClone,
parentViewTransition,
visitPhase,
);
break;
}
// Fall through
}
case HostSingleton: {
if (supportsSingletons) {
recursivelyInsertClones(
finishedWork,
hostParentClone,
parentViewTransition,
visitPhase,
);
if (__DEV__) {
// We cannot apply mutations to Host Singletons since by definition
// they cannot be cloned. Therefore we warn in DEV if this commit
// had any effect.
if (flags & Update) {
const newProps = finishedWork.memoizedProps;
const oldProps = current.memoizedProps;
const instance = finishedWork.stateNode;
const type = finishedWork.type;
const prev = pushMutationContext();
try {
// Since we currently don't have a separate diffing algorithm for
// individual properties, the Update flag can be a false positive.
// We have to apply the new props first o detect any mutations and
// then revert them.
commitUpdate(instance, type, oldProps, newProps, finishedWork);
if (viewTransitionMutationContext) {
console.error(
'startGestureTransition() caused something to mutate <%s>. ' +
'This is not possible in the current implementation. ' +
"Make sure that the swipe doesn't update any state which " +
'causes <%s> to change.',
finishedWork.type,
finishedWork.type,
);
}
// Revert
commitUpdate(instance, type, newProps, oldProps, finishedWork);
} finally {
popMutationContext(prev);
}
}
}
break;
}
// Fall through
}
case HostComponent: {
const instance: Instance = finishedWork.stateNode;
let clone: Instance;
if (finishedWork.child === null) {
// This node is terminal. We still do a deep clone in case this has user
// inserted content, text content or dangerouslySetInnerHTML.
clone = cloneMutableInstance(instance, true);
if (finishedWork.flags & ContentReset) {
resetTextContent(clone);
trackHostMutation();
}
} else {
// If we have children we'll clone them as we walk the tree so we just
// do a shallow clone here.
clone = cloneMutableInstance(instance, false);
}
if (flags & Update) {
const newProps = finishedWork.memoizedProps;
const oldProps = current.memoizedProps;
const type = finishedWork.type;
// Apply the delta to the clone.
commitUpdate(clone, type, oldProps, newProps, finishedWork);
}
if (visitPhase === CLONE_EXIT || visitPhase === CLONE_UNHIDE) {
appendChild(hostParentClone, clone);
unhideInstance(clone, finishedWork.memoizedProps);
recursivelyInsertClones(
finishedWork,
clone,
null,
CLONE_APPEARING_PAIR,
);
trackHostMutation();
} else {
appendChild(hostParentClone, clone);
recursivelyInsertClones(finishedWork, clone, null, visitPhase);
}
if (parentViewTransition !== null) {
if (parentViewTransition.clones === null) {
parentViewTransition.clones = [clone];
} else {
parentViewTransition.clones.push(clone);
}
}
break;
}
case HostText: {
const textInstance: TextInstance = finishedWork.stateNode;
if (textInstance === null) {
throw new Error(
'This should have a text node initialized. This error is likely ' +
'caused by a bug in React. Please file an issue.',
);
}
const clone = cloneMutableTextInstance(textInstance);
if (flags & Update) {
const newText: string = finishedWork.memoizedProps;
const oldText: string = current.memoizedProps;
commitTextUpdate(clone, newText, oldText);
trackHostMutation();
}
appendChild(hostParentClone, clone);
if (visitPhase === CLONE_EXIT || visitPhase === CLONE_UNHIDE) {
unhideTextInstance(clone, finishedWork.memoizedProps);
trackHostMutation();
}
break;
}
case HostPortal: {
// TODO: Consider what should happen to Portals. For now we exclude them.
break;
}
case OffscreenComponent: {
const newState: OffscreenState | null = finishedWork.memoizedState;
const isHidden = newState !== null;
if (!isHidden) {
// Only insert clones if this tree is going to be visible. No need to
// clone invisible content.
// TODO: If this is visible but detached it should still be cloned.
let nextPhase;
if (visitPhase === CLONE_UPDATE && (flags & Visibility) !== NoFlags) {
// This is the root of an appear. We need to trigger Enter transitions.
nextPhase = CLONE_EXIT;
} else {
nextPhase = visitPhase;
}
recursivelyInsertClones(
finishedWork,
hostParentClone,
parentViewTransition,
nextPhase,
);
} else if (current !== null && current.memoizedState === null) {
// Was previously mounted as visible but is now hidden.
trackEnterViewTransitions(current);
// Normally we would only mark something as triggering a mutation if there was
// actually a HostInstance below here. If this tree didn't contain a HostInstances
// we shouldn't trigger a mutation even though a virtual component was hidden.
trackHostMutation();
}
break;
}
case ViewTransitionComponent:
const prevMutationContext = pushMutationContext();
const viewTransitionState: ViewTransitionState = finishedWork.stateNode;
// TODO: If this was already cloned by a previous pass we can reuse those clones.
viewTransitionState.clones = null;
let nextPhase;
if (visitPhase === CLONE_EXIT) {
// This was an Enter of a ViewTransition. We now move onto unhiding the inner
// HostComponents and finding inner pairs.
nextPhase = CLONE_UNHIDE;
// TODO: Mark the name and find a pair.
} else {
nextPhase = visitPhase;
}
recursivelyInsertClones(
finishedWork,
hostParentClone,
viewTransitionState,
nextPhase,
);
if (viewTransitionMutationContext) {
// Track that this boundary had a mutation and therefore needs to animate
// whether it resized or not.
finishedWork.flags |= Update;
}
// After we've collected the cloned instances, we can apply exit or share transitions
// to them.
if (visitPhase === CLONE_EXIT) {
applyExitViewTransition(finishedWork);
} else if (
visitPhase === CLONE_APPEARING_PAIR ||
visitPhase === CLONE_UNHIDE
) {
applyAppearingPairViewTransition(finishedWork);
} else if (visitPhase === CLONE_UPDATE) {
applyUpdateViewTransition(current, finishedWork);
}
popMutationContext(prevMutationContext);
break;
default: {
recursivelyInsertClones(
finishedWork,
hostParentClone,
parentViewTransition,
visitPhase,
);
break;
}
}
}
// Clone View Transition boundaries that have any mutations or might have had their
// layout affected by child insertions.
export function insertDestinationClones(
root: FiberRoot,
finishedWork: Fiber,
): void {
// We'll either not transition the root, or we'll transition the clone. Regardless
// we cancel the root view transition name.
const needsClone = detectMutationOrInsertClones(finishedWork);
if (needsClone) {
if (__DEV__) {
if (!didWarnForRootClone) {
didWarnForRootClone = true;
console.warn(
'startGestureTransition() caused something to mutate or relayout the root. ' +
'This currently requires a clone of the whole document. Make sure to ' +
'add a <ViewTransition> directly around an absolutely positioned DOM node ' +
'to minimize the impact of any changes caused by the Gesture Transition.',
);
}
}
// Clone the whole root
const rootClone = cloneRootViewTransitionContainer(root.containerInfo);
root.gestureClone = rootClone;
recursivelyInsertClones(finishedWork, rootClone, null, CLONE_UPDATE);
} else {
root.gestureClone = null;
cancelRootViewTransitionName(root.containerInfo);
}
}