Skip to content

Commit 9eca920

Browse files
committed
feat(color): use box and icon color from theme
1 parent 1d044d2 commit 9eca920

6 files changed

Lines changed: 49 additions & 28 deletions

File tree

src/components/Alert/Alert.tsx

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { TouchableOpacity, View } from 'react-native';
33
import { DeepPartial } from 'ts-essentials';
44

55
import { Intent } from '../../constants/Intent';
6-
import { Theme, useTheme } from '../../theme';
6+
import { useTheme } from '../../theme';
77
import { mergeStyles, ReplaceReturnType } from '../../utils/mergeStyles';
88
import { Box } from '../Box';
99
import { Icon } from '../Icon';
@@ -24,26 +24,16 @@ export interface AlertProps {
2424
getStyles?: ReplaceReturnType<GetAlertStyles, DeepPartial<AlertStyles>>;
2525
}
2626

27-
const resolveIcon = (intent: Intent, theme: Theme) => {
27+
const resolveIcon = (intent: Intent) => {
2828
switch (intent) {
2929
case 'success':
30-
return (
31-
<Icon name="check-circle" size={24} color={theme.colors.text.success} />
32-
);
30+
return <Icon name="check-circle" size={24} color="success" />;
3331
case 'warning':
34-
return (
35-
<Icon
36-
name="alert-triangle"
37-
size={24}
38-
color={theme.colors.text.warning}
39-
/>
40-
);
32+
return <Icon name="alert-triangle" size={24} color="warning" />;
4133
case 'danger':
42-
return (
43-
<Icon name="alert-circle" size={24} color={theme.colors.text.danger} />
44-
);
34+
return <Icon name="alert-circle" size={24} color="danger" />;
4535
default:
46-
return <Icon name="info" size={24} color={theme.colors.text.info} />;
36+
return <Icon name="info" size={24} color="info" />;
4737
}
4838
};
4939

@@ -71,7 +61,7 @@ export const Alert = (props: AlertProps) => {
7161
<View style={containerStyle} testID={testID}>
7262
{icon || (
7363
<Box paddingRight={16} justifyContent="center">
74-
{resolveIcon(intent, theme)}
64+
{resolveIcon(intent)}
7565
</Box>
7666
)}
7767
{component || (

src/components/Box/Box.mdx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ Box is a primitive component for convenient layout of components. All style prop
1515

1616
<Playground>
1717
<Box
18-
paddingVertical={40}
19-
paddingHorizontal={40}
18+
// ...ViewStyle
2019
height={200}
2120
width={200}
22-
backgroundColor="#67c6bb"
21+
shape="rounded" // pill, circle or square (default)
22+
elevation={5}
23+
backgroundColor="primaryDefault" // background colors or any custom color "#67c6bb"
2324
/>
2425
</Playground>

src/components/Box/Box.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as React from 'react';
22
import { View, ViewStyle } from 'react-native';
33

4-
import { Theme, useTheme } from '../../theme';
4+
import { BackgroundColor, Theme, useTheme } from '../../theme';
55

66
export const BASE_BORDER_RADII = 4;
77

@@ -58,6 +58,16 @@ export const shapeMapping: {
5858
};
5959

6060
const propToFn = {
61+
backgroundColor: (color: BackgroundColor, theme: Theme) => {
62+
// @ts-ignore
63+
if (theme.colors.background[color]) {
64+
// @ts-ignore
65+
return { backgroundColor: theme.colors.background[color] };
66+
}
67+
return {
68+
backgroundColor: color,
69+
};
70+
},
6171
elevation: (elevation: 0 | 1 | 2 | 3 | 4, theme: Theme) => {
6272
return theme.elevations[elevation];
6373
},

src/components/Icon/Icon.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import * as React from 'react';
22
import Feather from 'react-native-vector-icons/Feather';
33

4+
import { TextColor, useTheme } from '../../theme';
5+
import { getTextColor } from '../Typography/Text.styles';
6+
47
export interface IconProps {
5-
color?: string;
8+
color?: TextColor;
69
name: FeatherIconName;
710
size?: number;
811
}
@@ -277,5 +280,13 @@ export type FeatherIconName =
277280
| 'zoom-out';
278281

279282
export const Icon = ({ name, color, size }: IconProps) => {
280-
return <Feather name={name} color={color} size={size} />;
283+
const theme = useTheme();
284+
285+
return (
286+
<Feather
287+
name={name}
288+
color={color ? getTextColor(theme.colors.text)(color) : color}
289+
size={size}
290+
/>
291+
);
281292
};

src/components/Icon/Icon.web.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import * as React from 'react';
22
import * as Feather from 'react-icons/fi';
33

4+
import { useTheme } from '../../theme';
5+
import { getTextColor } from '../Typography/Text.styles';
46
import { IconProps } from './Icon';
57

68
function convertToPascal(name: string) {
@@ -13,8 +15,14 @@ function convertToPascal(name: string) {
1315
}
1416

1517
export const Icon = ({ name, color, size }: IconProps) => {
18+
const theme = useTheme();
1619
// @ts-ignore
1720
const IconTag = Feather[`Fi${convertToPascal(name)}`];
1821

19-
return <IconTag color={color} size={size} />;
22+
return (
23+
<IconTag
24+
color={color ? getTextColor(theme.colors.text)(color) : color}
25+
size={size}
26+
/>
27+
);
2028
};

tests/__snapshots__/snapshot.test.js.snap

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2758,11 +2758,12 @@ exports[`Box_1 1`] = `
27582758
style={
27592759
Object {
27602760
"backgroundColor": "rgba(103,198,187,1.00)",
2761+
"borderBottomLeftRadius": "4px",
2762+
"borderBottomRightRadius": "4px",
2763+
"borderTopLeftRadius": "4px",
2764+
"borderTopRightRadius": "4px",
2765+
"boxShadow": "5px 5px 8px rgba(167,167,167,0.35)",
27612766
"height": "200px",
2762-
"paddingBottom": "40px",
2763-
"paddingLeft": "40px",
2764-
"paddingRight": "40px",
2765-
"paddingTop": "40px",
27662767
"width": "200px",
27672768
}
27682769
}

0 commit comments

Comments
 (0)