Skip to content

Commit e8f5f53

Browse files
committed
Fix native zIndex error condition
Flex items are inherently positioned for zIndex. Unlike other elements, flex items (direct children of a flex container) can utilize zIndex to control their stacking order without explicitly setting a position value other than static.
1 parent 1b46594 commit e8f5f53

2 files changed

Lines changed: 61 additions & 34 deletions

File tree

packages/react-strict-dom/src/native/modules/createStrictDOMComponent.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,13 @@ export function createStrictDOMComponent<T, P: StrictProps>(
114114
);
115115
}
116116
});
117-
}
118-
119-
if (nativeStyle.zIndex != null && nativeStyle.position === 'static') {
120-
errorMsg(
121-
'"position:static" prevents "zIndex" from having an effect. Try setting "position" to something other than "static".'
122-
);
117+
// Error message if the element is not a flex child but tries to use
118+
// zIndex without non-static position
119+
if (nativeStyle.zIndex != null && nativeStyle.position === 'static') {
120+
errorMsg(
121+
'"position:static" prevents "zIndex" from having an effect. Try setting "position" to something other than "static".'
122+
);
123+
}
123124
}
124125
}
125126

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

Lines changed: 54 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -138,38 +138,64 @@ describe('<html.*>', () => {
138138
});
139139
});
140140

141-
test('zIndex with position:static', () => {
142-
const styles = css.create({
143-
static: {
144-
position: 'static',
145-
zIndex: 1
146-
}
147-
});
148-
act(() => {
149-
create(<html.div style={styles.static} />);
141+
describe('zIndex', () => {
142+
test('with position:static', () => {
143+
const styles = css.create({
144+
static: {
145+
position: 'static',
146+
zIndex: 1
147+
}
148+
});
149+
act(() => {
150+
create(<html.div style={styles.static} />);
151+
});
152+
expect(console.error).toHaveBeenCalledWith(
153+
expect.stringContaining(
154+
'"position:static" prevents "zIndex" from having an effect.'
155+
)
156+
);
150157
});
151-
expect(console.error).toHaveBeenCalledWith(
152-
expect.stringContaining(
153-
'"position:static" prevents "zIndex" from having an effect.'
154-
)
155-
);
156-
});
157158

158-
test('zIndex with position:relative', () => {
159-
const styles = css.create({
160-
relative: {
161-
position: 'relative',
162-
zIndex: 1
163-
}
159+
test('with position:static flex child', () => {
160+
const styles = css.create({
161+
flex: {
162+
display: 'flex'
163+
},
164+
static: {
165+
position: 'static',
166+
zIndex: 1
167+
}
168+
});
169+
act(() => {
170+
create(
171+
<html.div style={styles.flex}>
172+
<html.div style={styles.static} />
173+
</html.div>
174+
);
175+
});
176+
expect(console.error).not.toHaveBeenCalledWith(
177+
expect.stringContaining(
178+
'"position:static" prevents "zIndex" from having an effect.'
179+
)
180+
);
164181
});
165-
act(() => {
166-
create(<html.div style={styles.relative} />);
182+
183+
test('with position:relative', () => {
184+
const styles = css.create({
185+
relative: {
186+
position: 'relative',
187+
zIndex: 1
188+
}
189+
});
190+
act(() => {
191+
create(<html.div style={styles.relative} />);
192+
});
193+
expect(console.error).not.toHaveBeenCalledWith(
194+
expect.stringContaining(
195+
'"position:static" prevents "zIndex" from having an effect.'
196+
)
197+
);
167198
});
168-
expect(console.error).not.toHaveBeenCalledWith(
169-
expect.stringContaining(
170-
'"position:static" prevents "zIndex" from having an effect.'
171-
)
172-
);
173199
});
174200

175201
test('auto-wraps raw strings', () => {

0 commit comments

Comments
 (0)