Skip to content

Commit 9d51420

Browse files
committed
Remove unnecessary setViewStyle calls
1 parent fe7414f commit 9d51420

File tree

6 files changed

+42
-46
lines changed

6 files changed

+42
-46
lines changed

packages/react-native-reanimated/src/ReanimatedModule/NativeReanimated.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ See https://docs.swmansion.com/react-native-reanimated/docs/guides/troubleshooti
187187
this.#reanimatedModuleProxy.unsubscribeFromKeyboardEvents(listenerId);
188188
}
189189

190-
setViewStyle(viewTag: number, style: StyleProps) {
190+
setViewStyle(viewTag: number, style: StyleProps | null) {
191191
this.#reanimatedModuleProxy.setViewStyle(viewTag, style);
192192
}
193193

packages/react-native-reanimated/src/ReanimatedModule/js-reanimated/JSReanimated.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ class JSReanimated implements IReanimatedModule {
271271
// noop
272272
}
273273

274-
setViewStyle(_viewTag: number, _style: StyleProps): void {
274+
setViewStyle(_viewTag: number, _style: StyleProps | null): void {
275275
throw new ReanimatedError('setViewStyle is not available in JSReanimated.');
276276
}
277277

packages/react-native-reanimated/src/ReanimatedModule/reanimatedModuleProxy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export interface ReanimatedModuleProxy {
6060

6161
setShouldAnimateExitingForTag(viewTag: number, shouldAnimate: boolean): void;
6262

63-
setViewStyle(viewTag: number, style: StyleProps): void;
63+
setViewStyle(viewTag: number, style: StyleProps | null): void;
6464

6565
markNodeAsRemovable(shadowNodeWrapper: ShadowNodeWrapper): void;
6666
unmarkNodeAsRemovable(viewTag: number): void;

packages/react-native-reanimated/src/css/native/managers/CSSManager.ts

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default class CSSManager implements ICSSManager {
1818
private readonly viewTag: number;
1919
private readonly viewName: string;
2020
private readonly styleBuilder: StyleBuilder<AnyRecord> | null = null;
21-
private isFirstUpdate: boolean = true;
21+
private isStyleSet = false;
2222

2323
constructor({ shadowNodeWrapper, viewTag, viewName = 'RCTView' }: ViewInfo) {
2424
const tag = (this.viewTag = viewTag as number);
@@ -46,25 +46,20 @@ export default class CSSManager implements ICSSManager {
4646
);
4747
}
4848

49-
const normalizedStyle = this.styleBuilder?.buildFrom(filteredStyle);
49+
const normalizedStyle = this.styleBuilder?.buildFrom(filteredStyle) ?? null;
5050

51-
// If the update is called during the first css style update, we won't
52-
// trigger CSS transitions and set styles before attaching CSS transitions
53-
if (this.isFirstUpdate && normalizedStyle) {
54-
setViewStyle(this.viewTag, normalizedStyle);
55-
}
56-
57-
this.cssTransitionsManager.update(transitionProperties, normalizedStyle ?? null);
51+
this.cssTransitionsManager.update(transitionProperties, normalizedStyle);
5852
this.cssAnimationsManager.update(animationProperties);
5953

60-
// If the current update is not the fist one, we want to update CSS
61-
// animations and transitions first and update the style then to make
62-
// sure that the new transition is fired with new settings (like duration)
63-
if (!this.isFirstUpdate && normalizedStyle) {
64-
setViewStyle(this.viewTag, normalizedStyle);
54+
if (normalizedStyle) {
55+
if (animationProperties) {
56+
setViewStyle(this.viewTag, normalizedStyle);
57+
this.isStyleSet = true;
58+
}
59+
} else if (this.isStyleSet) {
60+
setViewStyle(this.viewTag, null);
61+
this.isStyleSet = false;
6562
}
66-
67-
this.isFirstUpdate = false;
6863
}
6964

7065
unmountCleanup(): void {

packages/react-native-reanimated/src/css/native/managers/CSSTransitionsManager.ts

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -46,36 +46,38 @@ export default class CSSTransitionsManager implements ICSSTransitionsManager {
4646
return;
4747
}
4848

49-
const propertyDiff =
50-
transitionConfig.properties.length > 0
51-
? getChangedProps(previousStyle, style, transitionConfig.properties)
52-
: null;
49+
const propertyDiff = getChangedProps(
50+
previousStyle,
51+
style,
52+
transitionConfig.properties
53+
);
5354

5455
this.previousStyle = style;
5556

56-
if (propertyDiff) {
57-
const settingsDiff = this.getSettingsUpdates(
58-
transitionConfig,
59-
previousConfig
60-
);
57+
if (!propertyDiff) {
58+
this.transitionConfig = transitionConfig;
59+
return;
60+
}
6161

62-
if (!previousConfig) {
63-
registerCSSTransition(this.shadowNodeWrapper, {
64-
properties: propertyDiff,
65-
settings: transitionConfig.settings,
66-
settingsUpdates: settingsDiff ?? undefined,
67-
});
68-
} else {
69-
const updates: CSSTransitionUpdates = {
70-
properties: propertyDiff,
71-
};
72-
73-
if (settingsDiff) {
74-
updates.settings = settingsDiff;
75-
}
76-
77-
updateCSSTransition(this.viewTag, updates);
62+
const settingsDiff = previousConfig
63+
? this.getSettingsUpdates(transitionConfig, previousConfig)
64+
: null;
65+
66+
if (!previousConfig) {
67+
registerCSSTransition(this.shadowNodeWrapper, {
68+
properties: propertyDiff,
69+
settings: transitionConfig.settings,
70+
});
71+
} else {
72+
const updates: CSSTransitionUpdates = {
73+
properties: propertyDiff,
74+
};
75+
76+
if (settingsDiff) {
77+
updates.settings = settingsDiff;
7878
}
79+
80+
updateCSSTransition(this.viewTag, updates);
7981
}
8082

8183
this.transitionConfig = transitionConfig;

packages/react-native-reanimated/src/css/native/proxy.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ import type {
55
CSSAnimationUpdates,
66
CSSTransitionUpdates,
77
NormalizedCSSAnimationKeyframesConfig,
8-
NormalizedCSSTransitionConfig,
98
NormalizedNewCSSTransitionConfig,
109
} from './types';
1110

1211
// COMMON
1312

14-
export function setViewStyle(viewTag: number, style: StyleProps) {
13+
export function setViewStyle(viewTag: number, style: StyleProps | null) {
1514
ReanimatedModule.setViewStyle(viewTag, style);
1615
}
1716

0 commit comments

Comments
 (0)