Skip to content

Commit cb36912

Browse files
authored
Adds background compatibility to layout components (#742)
1 parent 696c83f commit cb36912

File tree

4 files changed

+51
-2
lines changed

4 files changed

+51
-2
lines changed

packages/core/src/components/Layout/Center.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import React from "react";
22
import { View, ViewProps, StyleSheet } from "react-native";
3+
import { convertBackwardCompatiblePropsToStyle } from "./LayoutCommon";
34

45
const Center: React.FC<ViewProps> = ({ style, ...rest }) => {
5-
return <View {...rest} style={[styles.center, style]} />;
6+
const backwardsCompatibleStyle = convertBackwardCompatiblePropsToStyle(rest);
7+
return (
8+
<View {...rest} style={[styles.center, backwardsCompatibleStyle, style]} />
9+
);
610
};
711

812
const styles = StyleSheet.create({

packages/core/src/components/Layout/Circle.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
import React from "react";
22
import { ViewProps, StyleSheet } from "react-native";
33
import Square from "./Square";
4+
import { pick } from "lodash";
5+
import { convertBackwardCompatiblePropsToStyle } from "./LayoutCommon";
46

57
interface CircleProps extends ViewProps {
68
size?: number;
79
}
810

911
const Circle: React.FC<CircleProps> = ({ size, style, ...rest }) => {
10-
return <Square {...rest} size={size} style={[style, styles.circle]} />;
12+
const backwardsCompatibleStyle = pick(
13+
convertBackwardCompatiblePropsToStyle(rest),
14+
"backgroundColor"
15+
);
16+
17+
return (
18+
<Square
19+
{...rest}
20+
size={size}
21+
style={[backwardsCompatibleStyle, style, styles.circle]}
22+
/>
23+
);
1124
};
1225

1326
const styles = StyleSheet.create({
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { ViewProps, ViewStyle } from "react-native";
2+
3+
/**
4+
* Old and deprecated layout components of the same names had props that have since been removed
5+
* This maintains correct rendering when using the older/deprecated props as to not break existing usage
6+
* (See: deprecared-components/Layout)
7+
*
8+
* These deprecated props are not exposed in the component type to discourage their usage
9+
*/
10+
11+
interface BackwardsCompatibleProps {
12+
width?: number;
13+
height?: number;
14+
bgColor?: string;
15+
}
16+
17+
export function convertBackwardCompatiblePropsToStyle(
18+
props: BackwardsCompatibleProps & ViewProps
19+
): ViewStyle {
20+
return {
21+
width: props.width,
22+
height: props.height,
23+
backgroundColor: props.bgColor,
24+
};
25+
}

packages/core/src/components/Layout/Square.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
import React from "react";
22
import { ViewProps } from "react-native";
33
import Center from "./Center";
4+
import { pick } from "lodash";
5+
import { convertBackwardCompatiblePropsToStyle } from "./LayoutCommon";
46

57
interface SquareProps extends ViewProps {
68
size?: number;
79
}
810

911
const Square: React.FC<SquareProps> = ({ size, style, onLayout, ...rest }) => {
1012
const [calculatedSize, setCalculatedSize] = React.useState(0);
13+
const backwardsCompatibleStyle = pick(
14+
convertBackwardCompatiblePropsToStyle(rest),
15+
"backgroundColor"
16+
);
1117

1218
return (
1319
<Center
@@ -18,6 +24,7 @@ const Square: React.FC<SquareProps> = ({ size, style, onLayout, ...rest }) => {
1824
}}
1925
{...rest}
2026
style={[
27+
backwardsCompatibleStyle,
2128
style,
2229
size != undefined ? { width: size, height: size } : {},
2330
calculatedSize > 0

0 commit comments

Comments
 (0)