Skip to content

Commit 43bceff

Browse files
committed
Add flexbox error logging for native
Print an error message if flex properties are used on native without the required "display:flex" style. This should help prevent layout divergence between native and web for native-first development.
1 parent ecacaec commit 43bceff

2 files changed

Lines changed: 56 additions & 0 deletions

File tree

packages/react-strict-dom/src/native/stylex/index.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,37 @@ export function props(
637637
if (boxSizingValue === 'content-box' && !version.experimental) {
638638
nextStyle = fixContentBox(nextStyle);
639639
}
640+
641+
// Print an error message if flex properties are used without
642+
// "display:flex" being set. React Native is always using "flex"
643+
// layout but web uses "flow" layout by default, which can lead
644+
// to layout divergence if building for native first.
645+
if (__DEV__) {
646+
if (
647+
nextStyle.display == null ||
648+
(nextStyle.display !== 'flex' && nextStyle.display !== 'none')
649+
) {
650+
if (
651+
nextStyle.alignContent != null ||
652+
nextStyle.alignItems != null ||
653+
nextStyle.alignSelf != null ||
654+
nextStyle.columnGap != null ||
655+
nextStyle.flex != null ||
656+
nextStyle.flexBasis != null ||
657+
nextStyle.flexDirection != null ||
658+
nextStyle.flexGrow != null ||
659+
nextStyle.flexShrink != null ||
660+
nextStyle.flexWrap != null ||
661+
nextStyle.gap != null ||
662+
nextStyle.justifyContent != null ||
663+
nextStyle.placeContent != null ||
664+
nextStyle.rowGap != null
665+
) {
666+
errorMsg('"display:flex" is required to use flexbox properties');
667+
}
668+
}
669+
}
670+
640671
nativeProps.style = nextStyle;
641672

642673
return nativeProps;

packages/react-strict-dom/tests/css-test.native.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,31 @@ describe('properties: general', () => {
259259
expect(css.props.call(mockOptions, styles.rtl)).toMatchSnapshot('rtl');
260260
});
261261

262+
test('display', () => {
263+
const styles = css.create({
264+
good: {
265+
display: 'flex',
266+
flexDirection: 'row'
267+
},
268+
bad: {
269+
flexDirection: 'row'
270+
}
271+
});
272+
css.props.call(mockOptions, styles.good);
273+
274+
expect(console.error).not.toHaveBeenCalledWith(
275+
expect.stringContaining(
276+
'"display:flex" is required to use flexbox properties'
277+
)
278+
);
279+
css.props.call(mockOptions, styles.bad);
280+
expect(console.error).toHaveBeenCalledWith(
281+
expect.stringContaining(
282+
'"display:flex" is required to use flexbox properties'
283+
)
284+
);
285+
});
286+
262287
test('filter', () => {
263288
const { underTest } = css.create({
264289
underTest: {

0 commit comments

Comments
 (0)