Skip to content

Commit 87fb8ea

Browse files
committed
Revert "Diff properties in the commit phase instead of generating an update payload (#26583)"
This reverts commit ca41adb.
1 parent 29f84bd commit 87fb8ea

19 files changed

+130
-675
lines changed

packages/react-dom-bindings/src/client/CSSPropertyOperations.js

Lines changed: 36 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import hyphenateStyleName from '../shared/hyphenateStyleName';
1111
import warnValidStyle from '../shared/warnValidStyle';
1212
import isUnitlessNumber from '../shared/isUnitlessNumber';
1313
import {checkCSSPropertyStringCoercion} from 'shared/CheckStringCoercion';
14-
import {diffInCommitPhase} from 'shared/ReactFeatureFlags';
1514

1615
/**
1716
* Operations for dealing with CSS properties.
@@ -65,50 +64,14 @@ export function createDangerousStringForStyles(styles) {
6564
}
6665
}
6766

68-
function setValueForStyle(style, styleName, value) {
69-
const isCustomProperty = styleName.indexOf('--') === 0;
70-
if (__DEV__) {
71-
if (!isCustomProperty) {
72-
warnValidStyle(styleName, value);
73-
}
74-
}
75-
76-
if (value == null || typeof value === 'boolean' || value === '') {
77-
if (isCustomProperty) {
78-
style.setProperty(styleName, '');
79-
} else if (styleName === 'float') {
80-
style.cssFloat = '';
81-
} else {
82-
style[styleName] = '';
83-
}
84-
} else if (isCustomProperty) {
85-
style.setProperty(styleName, value);
86-
} else if (
87-
typeof value === 'number' &&
88-
value !== 0 &&
89-
!isUnitlessNumber(styleName)
90-
) {
91-
style[styleName] = value + 'px'; // Presumes implicit 'px' suffix for unitless numbers
92-
} else {
93-
if (styleName === 'float') {
94-
style.cssFloat = value;
95-
} else {
96-
if (__DEV__) {
97-
checkCSSPropertyStringCoercion(value, styleName);
98-
}
99-
style[styleName] = ('' + value).trim();
100-
}
101-
}
102-
}
103-
10467
/**
10568
* Sets the value for multiple styles on a node. If a value is specified as
10669
* '' (empty string), the corresponding style property will be unset.
10770
*
10871
* @param {DOMElement} node
10972
* @param {object} styles
11073
*/
111-
export function setValueForStyles(node, styles, prevStyles) {
74+
export function setValueForStyles(node, styles) {
11275
if (styles != null && typeof styles !== 'object') {
11376
throw new Error(
11477
'The `style` prop expects a mapping from style properties to values, ' +
@@ -125,39 +88,42 @@ export function setValueForStyles(node, styles, prevStyles) {
12588
}
12689

12790
const style = node.style;
128-
129-
if (diffInCommitPhase && prevStyles != null) {
130-
if (__DEV__) {
131-
validateShorthandPropertyCollisionInDev(prevStyles, styles);
91+
for (const styleName in styles) {
92+
if (!styles.hasOwnProperty(styleName)) {
93+
continue;
13294
}
133-
134-
for (const styleName in prevStyles) {
135-
if (
136-
prevStyles.hasOwnProperty(styleName) &&
137-
(styles == null || !styles.hasOwnProperty(styleName))
138-
) {
139-
// Clear style
140-
const isCustomProperty = styleName.indexOf('--') === 0;
141-
if (isCustomProperty) {
142-
style.setProperty(styleName, '');
143-
} else if (styleName === 'float') {
144-
style.cssFloat = '';
145-
} else {
146-
style[styleName] = '';
147-
}
95+
const value = styles[styleName];
96+
const isCustomProperty = styleName.indexOf('--') === 0;
97+
if (__DEV__) {
98+
if (!isCustomProperty) {
99+
warnValidStyle(styleName, value);
148100
}
149101
}
150-
for (const styleName in styles) {
151-
const value = styles[styleName];
152-
if (styles.hasOwnProperty(styleName) && prevStyles[styleName] !== value) {
153-
setValueForStyle(style, styleName, value);
102+
103+
if (value == null || typeof value === 'boolean' || value === '') {
104+
if (isCustomProperty) {
105+
style.setProperty(styleName, '');
106+
} else if (styleName === 'float') {
107+
style.cssFloat = '';
108+
} else {
109+
style[styleName] = '';
154110
}
155-
}
156-
} else {
157-
for (const styleName in styles) {
158-
if (styles.hasOwnProperty(styleName)) {
159-
const value = styles[styleName];
160-
setValueForStyle(style, styleName, value);
111+
} else if (isCustomProperty) {
112+
style.setProperty(styleName, value);
113+
} else if (
114+
typeof value === 'number' &&
115+
value !== 0 &&
116+
!isUnitlessNumber(styleName)
117+
) {
118+
style[styleName] = value + 'px'; // Presumes implicit 'px' suffix for unitless numbers
119+
} else {
120+
if (styleName === 'float') {
121+
style.cssFloat = value;
122+
} else {
123+
if (__DEV__) {
124+
checkCSSPropertyStringCoercion(value, styleName);
125+
}
126+
style[styleName] = ('' + value).trim();
161127
}
162128
}
163129
}
@@ -201,38 +167,15 @@ function expandShorthandMap(styles) {
201167
* becomes .style.fontVariant = ''
202168
*/
203169
export function validateShorthandPropertyCollisionInDev(
204-
prevStyles,
170+
styleUpdates,
205171
nextStyles,
206172
) {
207173
if (__DEV__) {
208174
if (!nextStyles) {
209175
return;
210176
}
211177

212-
// Compute the diff as it would happen elsewhere.
213-
const expandedUpdates = {};
214-
if (prevStyles) {
215-
for (const key in prevStyles) {
216-
if (prevStyles.hasOwnProperty(key) && !nextStyles.hasOwnProperty(key)) {
217-
const longhands = shorthandToLonghand[key] || [key];
218-
for (let i = 0; i < longhands.length; i++) {
219-
expandedUpdates[longhands[i]] = key;
220-
}
221-
}
222-
}
223-
}
224-
for (const key in nextStyles) {
225-
if (
226-
nextStyles.hasOwnProperty(key) &&
227-
(!prevStyles || prevStyles[key] !== nextStyles[key])
228-
) {
229-
const longhands = shorthandToLonghand[key] || [key];
230-
for (let i = 0; i < longhands.length; i++) {
231-
expandedUpdates[longhands[i]] = key;
232-
}
233-
}
234-
}
235-
178+
const expandedUpdates = expandShorthandMap(styleUpdates);
236179
const expandedStyles = expandShorthandMap(nextStyles);
237180
const warnedAbout = {};
238181
for (const key in expandedUpdates) {
@@ -250,7 +193,7 @@ export function validateShorthandPropertyCollisionInDev(
250193
"avoid this, don't mix shorthand and non-shorthand properties " +
251194
'for the same value; instead, replace the shorthand with ' +
252195
'separate values.',
253-
isValueEmpty(nextStyles[originalKey]) ? 'Removing' : 'Updating',
196+
isValueEmpty(styleUpdates[originalKey]) ? 'Removing' : 'Updating',
254197
originalKey,
255198
correctOriginalKey,
256199
);

0 commit comments

Comments
 (0)