Skip to content
This repository was archived by the owner on Feb 25, 2020. It is now read-only.

Commit e84d62f

Browse files
committed
fix: whitelist supported styles instead of blacklist
1 parent 70e5a40 commit e84d62f

File tree

3 files changed

+109
-55
lines changed

3 files changed

+109
-55
lines changed

src/navigators/__tests__/__snapshots__/NestedNavigator.test.tsx.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,8 @@ Array [
387387
Array [
388388
Object {
389389
"height": 64,
390+
"maxHeight": undefined,
391+
"minHeight": undefined,
390392
},
391393
]
392394
}
@@ -522,6 +524,8 @@ Array [
522524
Array [
523525
Object {
524526
"height": 64,
527+
"maxHeight": undefined,
528+
"minHeight": undefined,
525529
},
526530
]
527531
}

src/navigators/__tests__/__snapshots__/StackNavigator.test.tsx.snap

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,6 @@ Array [
222222
},
223223
Object {
224224
"backgroundColor": "red",
225-
"opacity": 0.5,
226225
},
227226
]
228227
}
@@ -234,6 +233,8 @@ Array [
234233
Array [
235234
Object {
236235
"height": 64,
236+
"maxHeight": undefined,
237+
"minHeight": undefined,
237238
},
238239
]
239240
}
@@ -540,7 +541,6 @@ Array [
540541
},
541542
Object {
542543
"backgroundColor": "red",
543-
"opacity": 0.5,
544544
},
545545
]
546546
}
@@ -552,6 +552,8 @@ Array [
552552
Array [
553553
Object {
554554
"height": 64,
555+
"maxHeight": undefined,
556+
"minHeight": undefined,
555557
},
556558
]
557559
}

src/views/Header/HeaderSegment.tsx

Lines changed: 101 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,20 @@ type State = {
4040
leftLabelLayout?: Layout;
4141
};
4242

43-
const warnIfHeaderStyleDefined = (value: any, styleProp: string) => {
44-
if (styleProp === 'position' && value === 'absolute') {
45-
console.warn(
46-
"position: 'absolute' is not supported on headerStyle. If you would like to render content under the header, use the headerTransparent navigationOption."
47-
);
48-
} else if (value !== undefined) {
49-
console.warn(
50-
`${styleProp} was given a value of ${value}, this has no effect on headerStyle.`
51-
);
52-
}
43+
const warnIfHeaderStylesDefined = (styles: { [key: string]: any }) => {
44+
Object.keys(styles).forEach(styleProp => {
45+
const value = styles[styleProp];
46+
47+
if (styleProp === 'position' && value === 'absolute') {
48+
console.warn(
49+
"position: 'absolute' is not supported on headerStyle. If you would like to render content under the header, use the 'headerTransparent' navigationOption."
50+
);
51+
} else if (value !== undefined) {
52+
console.warn(
53+
`${styleProp} was given a value of ${value}, this has no effect on headerStyle.`
54+
);
55+
}
56+
});
5357
};
5458

5559
export const getDefaultHeaderHeight = (layout: Layout) => {
@@ -179,50 +183,91 @@ export default class HeaderSegment extends React.Component<Props, State> {
179183

180184
const {
181185
height = getDefaultHeaderHeight(layout),
182-
alignItems,
183-
justifyContent,
184-
flex,
185-
flexDirection,
186-
flexGrow,
187-
flexShrink,
188-
flexBasis,
189-
flexWrap,
190-
position,
191-
padding,
192-
paddingHorizontal,
193-
paddingRight,
194-
paddingLeft,
195-
paddingVertical,
196-
paddingTop,
197-
paddingBottom,
198-
top,
199-
right: _right,
200-
bottom: _bottom,
201-
left: _left,
202-
...safeHeaderStyle
186+
minHeight,
187+
maxHeight,
188+
backgroundColor,
189+
borderBottomColor,
190+
borderBottomEndRadius,
191+
borderBottomLeftRadius,
192+
borderBottomRightRadius,
193+
borderBottomStartRadius,
194+
borderBottomWidth,
195+
borderColor,
196+
borderEndColor,
197+
borderEndWidth,
198+
borderLeftColor,
199+
borderLeftWidth,
200+
borderRadius,
201+
borderRightColor,
202+
borderRightWidth,
203+
borderStartColor,
204+
borderStartWidth,
205+
borderStyle,
206+
borderTopColor,
207+
borderTopEndRadius,
208+
borderTopLeftRadius,
209+
borderTopRightRadius,
210+
borderTopStartRadius,
211+
borderTopWidth,
212+
borderWidth,
213+
// @ts-ignore: web support for shadow
214+
boxShadow,
215+
elevation,
216+
shadowColor,
217+
shadowOffset,
218+
shadowOpacity,
219+
shadowRadius,
220+
...unsafeStyles
203221
} = StyleSheet.flatten(customHeaderStyle || {}) as ViewStyle;
204222

205223
if (process.env.NODE_ENV !== 'production') {
206-
warnIfHeaderStyleDefined(alignItems, 'alignItems');
207-
warnIfHeaderStyleDefined(justifyContent, 'justifyContent');
208-
warnIfHeaderStyleDefined(flex, 'flex');
209-
warnIfHeaderStyleDefined(flexDirection, 'flexDirection');
210-
warnIfHeaderStyleDefined(flexGrow, 'flexGrow');
211-
warnIfHeaderStyleDefined(flexShrink, 'flexShrink');
212-
warnIfHeaderStyleDefined(flexBasis, 'flexBasis');
213-
warnIfHeaderStyleDefined(flexWrap, 'flexWrap');
214-
warnIfHeaderStyleDefined(padding, 'padding');
215-
warnIfHeaderStyleDefined(position, 'position');
216-
warnIfHeaderStyleDefined(paddingHorizontal, 'paddingHorizontal');
217-
warnIfHeaderStyleDefined(paddingRight, 'paddingRight');
218-
warnIfHeaderStyleDefined(paddingLeft, 'paddingLeft');
219-
warnIfHeaderStyleDefined(paddingVertical, 'paddingVertical');
220-
warnIfHeaderStyleDefined(paddingTop, 'paddingTop');
221-
warnIfHeaderStyleDefined(paddingBottom, 'paddingBottom');
222-
warnIfHeaderStyleDefined(top, 'top');
223-
warnIfHeaderStyleDefined(_right, 'right');
224-
warnIfHeaderStyleDefined(_bottom, 'bottom');
225-
warnIfHeaderStyleDefined(_left, 'left');
224+
warnIfHeaderStylesDefined(unsafeStyles);
225+
}
226+
227+
const safeStyles = {
228+
backgroundColor,
229+
borderBottomColor,
230+
borderBottomEndRadius,
231+
borderBottomLeftRadius,
232+
borderBottomRightRadius,
233+
borderBottomStartRadius,
234+
borderBottomWidth,
235+
borderColor,
236+
borderEndColor,
237+
borderEndWidth,
238+
borderLeftColor,
239+
borderLeftWidth,
240+
borderRadius,
241+
borderRightColor,
242+
borderRightWidth,
243+
borderStartColor,
244+
borderStartWidth,
245+
borderStyle,
246+
borderTopColor,
247+
borderTopEndRadius,
248+
borderTopLeftRadius,
249+
borderTopRightRadius,
250+
borderTopStartRadius,
251+
borderTopWidth,
252+
borderWidth,
253+
// @ts-ignore
254+
boxShadow,
255+
elevation,
256+
shadowColor,
257+
shadowOffset,
258+
shadowOpacity,
259+
shadowRadius,
260+
};
261+
262+
// Setting a property to undefined triggers default style
263+
// So we need to filter them out
264+
// Users can use `null` instead
265+
for (const styleProp in safeStyles) {
266+
// @ts-ignore
267+
if (safeStyles[styleProp] === undefined) {
268+
// @ts-ignore
269+
delete safeStyles[styleProp];
270+
}
226271
}
227272

228273
return (
@@ -234,10 +279,13 @@ export default class HeaderSegment extends React.Component<Props, State> {
234279
{headerBackground ? (
235280
headerBackground()
236281
) : headerTransparent ? null : (
237-
<HeaderBackground style={safeHeaderStyle} />
282+
<HeaderBackground style={safeStyles} />
238283
)}
239284
</Animated.View>
240-
<Animated.View pointerEvents="box-none" style={[{ height }]}>
285+
<Animated.View
286+
pointerEvents="box-none"
287+
style={[{ height, minHeight, maxHeight }]}
288+
>
241289
<View
242290
pointerEvents="none"
243291
style={{ height: headerStatusBarHeight }}

0 commit comments

Comments
 (0)