main
js 73 lines 2.13 KB
Raw
1 /**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * @flow
8 */
9
10 import type {ViewTransitionProps, ViewTransitionClass} from 'shared/ReactTypes';
11 import type {TreeContext} from './ReactFizzTreeContext';
12 import type {ResumableState} from './ReactFizzConfig';
13
14 import {getTreeId} from './ReactFizzTreeContext';
15 import {makeId} from './ReactFizzConfig';
16
17 export function getViewTransitionName(
18 props: ViewTransitionProps,
19 treeContext: TreeContext,
20 resumableState: ResumableState,
21 ): string {
22 if (props.name != null && props.name !== 'auto') {
23 return props.name;
24 }
25 const treeId = getTreeId(treeContext);
26 return makeId(resumableState, treeId, 0);
27 }
28
29 function getClassNameByType(classByType: ?ViewTransitionClass): ?string {
30 if (classByType == null || typeof classByType === 'string') {
31 return classByType;
32 }
33 let className: ?string = null;
34 const activeTypes = null; // TODO: Support passing active types.
35 if (activeTypes !== null) {
36 for (let i = 0; i < activeTypes.length; i++) {
37 const match = classByType[activeTypes[i]];
38 if (match != null) {
39 // $FlowFixMe[invalid-compare]
40 if (match === 'none') {
41 // If anything matches "none" that takes precedence over any other
42 // type that also matches.
43 return 'none';
44 }
45 if (className == null) {
46 className = match;
47 } else {
48 className += ' ' + match;
49 }
50 }
51 }
52 }
53 if (className == null) {
54 // We had no other matches. Match the default for this configuration.
55 return classByType.default;
56 }
57 return className;
58 }
59
60 export function getViewTransitionClassName(
61 defaultClass: ?ViewTransitionClass,
62 eventClass: ?ViewTransitionClass,
63 ): ?string {
64 const className: ?string = getClassNameByType(defaultClass);
65 const eventClassName: ?string = getClassNameByType(eventClass);
66 if (eventClassName == null) {
67 return className === 'auto' ? null : className;
68 }
69 if (eventClassName === 'auto') {
70 return null;
71 }
72 return eventClassName;
73 }