Skip to content

Commit 00c9dc4

Browse files
committed
[fix] setNativeProps can apply flex styles
Updates the 'setValueForStyle' implementation to support style values that contain "!important". This allows the 'flex{Basis,Grow,Shrink}' values created by the style resolver to be applied. They currently use the important priority as a work-around for browser-inconsistencies in the 'flex' shorthand. Upstream fix: facebook/react#12181 Ref #798 Close #813
1 parent b66aba1 commit 00c9dc4

File tree

1 file changed

+19
-13
lines changed
  • packages/react-native-web/src/vendor/setValueForStyles

1 file changed

+19
-13
lines changed

packages/react-native-web/src/vendor/setValueForStyles/index.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313

1414
import dangerousStyleValue from '../dangerousStyleValue';
15+
import hyphenateStyleName from 'hyphenate-style-name';
1516
import warnValidStyle from '../warnValidStyle';
1617

1718
/**
@@ -20,32 +21,37 @@ import warnValidStyle from '../warnValidStyle';
2021
*
2122
* @param {DOMElement} node
2223
* @param {object} styles
23-
* @param {ReactDOMComponent} component
2424
*/
25-
const setValueForStyles = function(node, styles, component) {
26-
var style = node.style;
27-
for (var styleName in styles) {
25+
function setValueForStyles(node, styles, getStack) {
26+
const style = node.style;
27+
for (let styleName in styles) {
2828
if (!styles.hasOwnProperty(styleName)) {
2929
continue;
3030
}
31-
var isCustomProperty = styleName.indexOf('--') === 0;
31+
const isCustomProperty = styleName.indexOf('--') === 0;
32+
const isImportant =
33+
typeof styles[styleName] === 'string' && styles[styleName].indexOf('!important') > -1;
3234
if (process.env.NODE_ENV !== 'production') {
3335
if (!isCustomProperty) {
34-
warnValidStyle(styleName, styles[styleName], component);
36+
warnValidStyle(styleName, styles[styleName], getStack);
3537
}
3638
}
37-
var styleValue = dangerousStyleValue(styleName, styles[styleName], isCustomProperty);
39+
const styleValue = dangerousStyleValue(styleName, styles[styleName], isCustomProperty);
3840
if (styleName === 'float') {
3941
styleName = 'cssFloat';
4042
}
41-
if (isCustomProperty) {
42-
style.setProperty(styleName, styleValue);
43-
} else if (styleValue) {
44-
style[styleName] = styleValue;
43+
if (isCustomProperty || isImportant) {
44+
const name = isCustomProperty ? styleName : hyphenateStyleName(styleName);
45+
if (isImportant) {
46+
const [value, priority] = styleValue.split('!');
47+
style.setProperty(name, value, priority);
48+
} else {
49+
style.setProperty(name, styleValue);
50+
}
4551
} else {
46-
style[styleName] = '';
52+
style[styleName] = styleValue;
4753
}
4854
}
49-
};
55+
}
5056

5157
export default setValueForStyles;

0 commit comments

Comments
 (0)