Skip to content

Commit df11a0f

Browse files
committed
fix: fixed animation/resize/align fields & typing information
1 parent db0e6e7 commit df11a0f

File tree

3 files changed

+37
-62
lines changed

3 files changed

+37
-62
lines changed

src/SharedElementRendererData.tsx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import SharedElementSceneData from './SharedElementSceneData';
22
import {
33
SharedElementEventSubscription,
4-
SharedElementsConfig,
4+
SharedElementsStrictConfig,
55
SharedElementAnimatedValue,
66
SharedElementTransitionProps,
77
} from './types';
@@ -20,10 +20,9 @@ function getSharedElements(
2020
sceneData: SharedElementSceneData,
2121
otherSceneData: SharedElementSceneData,
2222
showing: boolean
23-
): SharedElementsConfig | null {
23+
): SharedElementsStrictConfig | null {
2424
const { sharedElements } = sceneData.Component;
2525
if (!sharedElements) return null;
26-
// TODO push/pop distinction?
2726
return normalizeSharedElementsConfig(
2827
sharedElements(sceneData.navigation, otherSceneData.navigation, showing)
2928
);
@@ -35,7 +34,7 @@ export default class SharedElementRendererData
3534
private prevSceneData: SharedElementSceneData | null = null;
3635
private updateSubscribers = new Set<SharedElementRendererUpdateHandler>();
3736
private sceneSubscription: SharedElementEventSubscription | null = null;
38-
private sharedElements: SharedElementsConfig = [];
37+
private sharedElements: SharedElementsStrictConfig = [];
3938
private isShowing: boolean = true;
4039
private animValue: SharedElementAnimatedValue;
4140

@@ -106,7 +105,7 @@ export default class SharedElementRendererData
106105
getTransitions(): SharedElementTransitionProps[] {
107106
const { sharedElements, prevSceneData, sceneData, isShowing } = this;
108107
// console.log('getTransitions: ', sharedElements);
109-
return sharedElements.map(({ id, otherId, animation, debug }) => {
108+
return sharedElements.map(({ id, otherId, ...other }) => {
110109
const startId = isShowing ? otherId || id : id;
111110
const endId = isShowing ? id : otherId || id;
112111
return {
@@ -122,8 +121,7 @@ export default class SharedElementRendererData
122121
ancestor: (sceneData ? sceneData.getAncestor() : undefined) || null,
123122
node: (sceneData ? sceneData.getNode(endId) : undefined) || null,
124123
},
125-
...animation,
126-
debug,
124+
...other,
127125
};
128126
});
129127
}

src/types.tsx

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,28 @@ export type SharedElementEventSubscription = {
5656
remove(): void;
5757
};
5858

59-
export type SharedElementAnimationConfig = {
60-
animation: SharedElementAnimation;
61-
resize?: SharedElementResize;
62-
align?: SharedElementAlign;
63-
};
64-
65-
export type SharedElementConfig = {
59+
export type SharedElementStrictConfig = {
6660
readonly id: string;
6761
readonly otherId: string;
68-
readonly animation: SharedElementAnimationConfig;
62+
readonly animation: SharedElementAnimation;
63+
readonly resize?: SharedElementResize;
64+
readonly align?: SharedElementAlign;
6965
readonly debug?: boolean;
7066
};
7167

68+
export type SharedElementsStrictConfig = SharedElementStrictConfig[];
69+
70+
export type SharedElementConfig =
71+
| {
72+
readonly id: string;
73+
readonly otherId?: string;
74+
readonly animation?: SharedElementAnimation;
75+
readonly resize?: SharedElementResize;
76+
readonly align?: SharedElementAlign;
77+
readonly debug?: boolean;
78+
}
79+
| string;
80+
7281
export type SharedElementsConfig = SharedElementConfig[];
7382

7483
export type SharedElementAnimatedValue = any;
@@ -77,7 +86,7 @@ export type SharedElementsComponentConfig = (
7786
navigation: NavigationProp,
7887
otherNavigation: NavigationProp,
7988
showing: boolean
80-
) => SharedElementsConfig | null;
89+
) => SharedElementsConfig | undefined;
8190

8291
export type SharedElementSceneComponent = React.ComponentType<any> & {
8392
sharedElements: SharedElementsComponentConfig;

src/utils.tsx

Lines changed: 14 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,33 @@
11
import {
2-
SharedElementAnimationConfig,
32
SharedElementConfig,
43
SharedElementsConfig,
4+
SharedElementStrictConfig,
5+
SharedElementsStrictConfig,
56
} from './types';
67

7-
export function normalizeSharedElementAnimationConfig(
8-
animationConfig: any
9-
): SharedElementAnimationConfig {
10-
if (animationConfig === true) {
11-
return {
12-
animation: 'move',
13-
};
14-
} else if (typeof animationConfig === 'string') {
15-
return {
16-
// @ts-ignore
17-
animation: animationConfig,
18-
};
19-
} else {
20-
return animationConfig;
21-
}
22-
}
23-
248
export function normalizeSharedElementConfig(
25-
sharedElementConfig: any
26-
): SharedElementConfig {
9+
sharedElementConfig: SharedElementConfig
10+
): SharedElementStrictConfig {
2711
if (typeof sharedElementConfig === 'string') {
2812
return {
2913
id: sharedElementConfig,
3014
otherId: sharedElementConfig,
31-
animation: normalizeSharedElementAnimationConfig('move'),
15+
animation: 'move',
3216
};
3317
} else {
18+
const { id, otherId, animation, ...other } = sharedElementConfig;
3419
return {
35-
id: sharedElementConfig.id,
36-
otherId: sharedElementConfig.otherId || sharedElementConfig.id,
37-
debug: sharedElementConfig.debug || false,
38-
animation: normalizeSharedElementAnimationConfig(
39-
sharedElementConfig.animation || 'move'
40-
),
20+
id,
21+
otherId: otherId || id,
22+
animation: animation || 'move',
23+
...other,
4124
};
4225
}
4326
}
4427

4528
export function normalizeSharedElementsConfig(
46-
sharedElementsConfig: any
47-
): SharedElementsConfig | null {
48-
if (!sharedElementsConfig) return null;
49-
if (Array.isArray(sharedElementsConfig)) {
50-
if (!sharedElementsConfig.length) return null;
51-
return sharedElementsConfig.map(normalizeSharedElementConfig);
52-
} else {
53-
const keys = Object.keys(sharedElementsConfig);
54-
if (!keys.length) return null;
55-
return keys.map(id => {
56-
return {
57-
id,
58-
otherId: id,
59-
animation: normalizeSharedElementAnimationConfig(
60-
sharedElementsConfig[id]
61-
),
62-
};
63-
});
64-
}
29+
sharedElementsConfig: SharedElementsConfig | undefined
30+
): SharedElementsStrictConfig | null {
31+
if (!sharedElementsConfig || !sharedElementsConfig.length) return null;
32+
return sharedElementsConfig.map(normalizeSharedElementConfig);
6533
}

0 commit comments

Comments
 (0)