Skip to content

Commit

Permalink
feat: adjust theme structure (#3084)
Browse files Browse the repository at this point in the history
  • Loading branch information
Drakeoon authored and lukewalczak committed Jun 8, 2022
1 parent 2a9eff3 commit 329b775
Show file tree
Hide file tree
Showing 55 changed files with 513 additions and 465 deletions.
1 change: 0 additions & 1 deletion babel.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
module.exports = {
presets: ['module:metro-react-native-babel-preset'],
plugins: ['lodash'],
};
14 changes: 6 additions & 8 deletions example/src/Examples/ButtonExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { Button, List, useTheme } from 'react-native-paper';
import ScreenWrapper from '../ScreenWrapper';

const ButtonExample = () => {
const { colors } = useTheme();
const theme = useTheme();

const color = theme.isV3 ? theme.colors.secondary : theme.colors.accent;

return (
<ScreenWrapper>
Expand All @@ -13,11 +15,7 @@ const ButtonExample = () => {
<Button onPress={() => {}} style={styles.button}>
Default
</Button>
<Button
color={colors?.accent}
onPress={() => {}}
style={styles.button}
>
<Button color={color} onPress={() => {}} style={styles.button}>
Custom
</Button>
<Button disabled onPress={() => {}} style={styles.button}>
Expand Down Expand Up @@ -46,7 +44,7 @@ const ButtonExample = () => {
</Button>
<Button
mode="outlined"
color={colors?.accent}
color={color}
onPress={() => {}}
style={styles.button}
>
Expand Down Expand Up @@ -94,7 +92,7 @@ const ButtonExample = () => {
</Button>
<Button
mode="contained"
color={colors?.accent}
color={color}
onPress={() => {}}
style={styles.button}
>
Expand Down
32 changes: 16 additions & 16 deletions example/src/Examples/TextExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import ScreenWrapper from '../ScreenWrapper';

const SectionHeader = ({ children }: React.PropsWithChildren<{}>) => {
return (
<Text variant="title-large" style={styles.sectionHeader}>
<Text variant="titleLarge" style={styles.sectionHeader}>
{children}
</Text>
);
Expand All @@ -32,53 +32,53 @@ const TextExample = () => {

<SectionHeader>Text component:</SectionHeader>

<Text style={styles.text} variant="display-large">
<Text style={styles.text} variant="displayLarge">
Display Large
</Text>
<Text style={styles.text} variant="display-medium">
<Text style={styles.text} variant="displayMedium">
Display Medium
</Text>
<Text style={styles.text} variant="display-small">
<Text style={styles.text} variant="displaySmall">
Display small
</Text>

<Text style={styles.text} variant="headline-large">
<Text style={styles.text} variant="headlineLarge">
Headline Large
</Text>
<Text style={styles.text} variant="headline-medium">
<Text style={styles.text} variant="headlineMedium">
Headline Medium
</Text>
<Text style={styles.text} variant="headline-small">
<Text style={styles.text} variant="headlineSmall">
Headline Small
</Text>

<Text style={styles.text} variant="title-large">
<Text style={styles.text} variant="titleLarge">
Title Large
</Text>
<Text style={styles.text} variant="title-medium">
<Text style={styles.text} variant="titleMedium">
Title Medium
</Text>
<Text style={styles.text} variant="title-small">
<Text style={styles.text} variant="titleSmall">
Title Small
</Text>

<Text style={styles.text} variant="body-large">
<Text style={styles.text} variant="bodyLarge">
Body Large
</Text>
<Text style={styles.text} variant="body-medium">
<Text style={styles.text} variant="bodyMedium">
Body Medium
</Text>
<Text style={styles.text} variant="body-small">
<Text style={styles.text} variant="bodySmall">
Body Small
</Text>

<Text style={styles.text} variant="label-large">
<Text style={styles.text} variant="labelLarge">
Label Large
</Text>
<Text style={styles.text} variant="label-medium">
<Text style={styles.text} variant="labelMedium">
Label Medium
</Text>
<Text style={styles.text} variant="label-small">
<Text style={styles.text} variant="labelSmall">
Label Small
</Text>
</View>
Expand Down
10 changes: 7 additions & 3 deletions example/src/Examples/TextInputExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ const TextInputExample = () => {

const _isUsernameValid = (name: string) => /^[a-zA-Z]*$/.test(name);

const { colors } = useTheme();
const theme = useTheme();

const inputActionHandler = (type: keyof State, payload: string) =>
dispatch({
Expand All @@ -121,7 +121,11 @@ const TextInputExample = () => {

const newColors = {
...state.iconsColor,
[name]: !color ? colors?.accent : undefined,
[name]: !color
? theme.isV3
? theme.colors.primary
: theme.colors?.accent
: undefined,
};

dispatch({
Expand Down Expand Up @@ -230,7 +234,7 @@ const TextInputExample = () => {
right={
<TextInput.Icon
name="chevron-up"
color={(focused) => (focused ? colors?.primary : undefined)}
color={(focused) => (focused ? theme.colors?.primary : undefined)}
/>
}
/>
Expand Down
6 changes: 2 additions & 4 deletions example/src/ScreenWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,14 @@ export default function ScreenWrapper({
contentContainerStyle,
...rest
}: Props) {
const { colors, md, isV3 } = useTheme();
const theme = useTheme();

const insets = useSafeAreaInsets();

const containerStyle = [
styles.container,
{
backgroundColor: isV3
? (md('md.sys.color.background') as string)
: colors?.background,
backgroundColor: theme.colors.background,
paddingBottom: insets.bottom,
paddingLeft: insets.left,
paddingRight: insets.left,
Expand Down
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
"dependencies": {
"@callstack/react-theme-provider": "^3.0.7",
"color": "^3.1.2",
"lodash": "^4.17.21",
"react-native-iphone-x-helper": "^1.3.1"
},
"devDependencies": {
Expand All @@ -59,7 +58,6 @@
"@release-it/conventional-changelog": "^1.1.0",
"@types/color": "^3.0.0",
"@types/jest": "^24.0.13",
"@types/lodash": "^4.14.178",
"@types/node": "^13.1.0",
"@types/react-dom": "^16.8.4",
"@types/react-native": "^0.66.1",
Expand Down
8 changes: 6 additions & 2 deletions src/components/Badge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,12 @@ const Badge = ({
}).start();
}, [visible, opacity, scale]);

const { backgroundColor = theme.colors?.notification, ...restStyle } =
(StyleSheet.flatten(style) || {}) as TextStyle;
const {
backgroundColor = theme.isV3
? theme.colors.error
: theme.colors?.notification,
...restStyle
} = (StyleSheet.flatten(style) || {}) as TextStyle;

const textColor = getContrastingColor(backgroundColor || white, white, black);

Expand Down
9 changes: 8 additions & 1 deletion src/components/Banner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,14 @@ const Banner = ({
</View>
) : null}
<Text
style={[styles.message, { color: theme.colors?.text }]}
style={[
styles.message,
{
color: theme.isV3
? theme.colors.onSurface
: theme.colors.text,
},
]}
accessibilityLiveRegion={visible ? 'polite' : 'none'}
accessibilityRole="alert"
>
Expand Down
17 changes: 13 additions & 4 deletions src/components/Checkbox/CheckboxAndroid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,28 @@ const CheckboxAndroid = ({

const checked = status === 'checked';
const indeterminate = status === 'indeterminate';
const checkedColor = rest.color || theme?.colors?.accent;

const themeTextColor = theme.isV3
? theme.colors.onSurface
: theme.colors.text;
const themeDisabledColor = theme.isV3
? theme.colors.onSurfaceDisabled
: theme.colors.text;

const checkedColor =
rest.color || theme.isV3 ? theme.colors.primary : theme?.colors?.accent;
const uncheckedColor =
rest.uncheckedColor ||
color(theme?.colors?.text)
color(themeTextColor)
.alpha(theme.dark ? 0.7 : 0.54)
.rgb()
.string();

let rippleColor, checkboxColor;

if (disabled) {
rippleColor = color(theme?.colors?.text).alpha(0.16).rgb().string();
checkboxColor = theme?.colors?.disabled;
rippleColor = color(themeTextColor).alpha(0.16).rgb().string();
checkboxColor = themeDisabledColor;
} else {
rippleColor = color(checkedColor).fade(0.32).rgb().string();
checkboxColor = checked ? checkedColor : uncheckedColor;
Expand Down
19 changes: 15 additions & 4 deletions src/components/Checkbox/CheckboxIOS.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,25 @@ const CheckboxIOS = ({
const checked = status === 'checked';
const indeterminate = status === 'indeterminate';

const checkedColor = disabled
? theme?.colors?.disabled
: rest.color || theme?.colors?.accent;
const disabledColor = theme.isV3
? theme.colors.onSurfaceDisabled
: theme?.colors?.disabled;

const defaultColor = theme.isV3
? theme.colors.primary
: theme?.colors?.accent;

const checkedColor = disabled ? disabledColor : rest.color || defaultColor;

let rippleColor;

if (disabled) {
rippleColor = color(theme?.colors?.text).alpha(0.16).rgb().string();
rippleColor = color(
theme.isV3 ? theme.colors.onSurface : theme?.colors?.text
)
.alpha(0.16)
.rgb()
.string();
} else {
rippleColor = color(checkedColor).fade(0.32).rgb().string();
}
Expand Down
7 changes: 6 additions & 1 deletion src/components/Checkbox/CheckboxItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ const CheckboxItem = ({
checkbox = <Checkbox {...checkboxProps} />;
}

const textColor = theme.isV3 ? theme.colors.onSurface : theme.colors.text;
const disabledTextColor = theme.isV3
? theme.colors.onSurfaceDisabled
: theme.colors.disabled;

return (
<TouchableRipple
accessibilityLabel={label}
Expand All @@ -134,7 +139,7 @@ const CheckboxItem = ({
style={[
styles.label,
{
color: disabled ? theme.colors.disabled : theme.colors.text,
color: disabled ? disabledTextColor : textColor,
textAlign: isLeading ? 'right' : 'left',
},
labelStyle,
Expand Down
15 changes: 11 additions & 4 deletions src/components/Chip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,13 @@ const Chip = ({
const { backgroundColor = defaultBackgroundColor, borderRadius = 16 } =
(StyleSheet.flatten(style) || {}) as ViewStyle;

const themeTextColor = theme.isV3
? theme.colors.onSurface
: theme.colors.text;
const themeDisabledColor = theme.isV3
? theme.colors.onSurfaceDisabled
: theme.colors.text;

const borderColor =
mode === 'outlined'
? color(
Expand All @@ -184,14 +191,14 @@ const Chip = ({
.string()
: backgroundColor;
const textColor = disabled
? colors?.disabled
: color(selectedColor !== undefined ? selectedColor : colors?.text)
? themeDisabledColor
: color(selectedColor !== undefined ? selectedColor : themeTextColor)
.alpha(0.87)
.rgb()
.string();
const iconColor = disabled
? colors?.disabled
: color(selectedColor !== undefined ? selectedColor : colors?.text)
? themeDisabledColor
: color(selectedColor !== undefined ? selectedColor : themeTextColor)
.alpha(0.54)
.rgb()
.string();
Expand Down
19 changes: 13 additions & 6 deletions src/components/DataTable/DataTablePagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ const PaginationControls = ({
onPageChange,
showFastPaginationControls,
}: PaginationControlsProps) => {
const { colors } = useTheme();
const theme = useTheme();

const textColor = theme.isV3 ? theme.colors.onSurface : theme.colors.text;

return (
<>
Expand All @@ -95,7 +97,7 @@ const PaginationControls = ({
direction={I18nManager.isRTL ? 'rtl' : 'ltr'}
/>
)}
color={colors?.text}
color={textColor}
disabled={page === 0}
onPress={() => onPageChange(0)}
accessibilityLabel="page-first"
Expand All @@ -110,7 +112,7 @@ const PaginationControls = ({
direction={I18nManager.isRTL ? 'rtl' : 'ltr'}
/>
)}
color={colors?.text}
color={textColor}
disabled={page === 0}
onPress={() => onPageChange(page - 1)}
accessibilityLabel="chevron-left"
Expand All @@ -124,7 +126,7 @@ const PaginationControls = ({
direction={I18nManager.isRTL ? 'rtl' : 'ltr'}
/>
)}
color={colors?.text}
color={textColor}
disabled={numberOfPages === 0 || page === numberOfPages - 1}
onPress={() => onPageChange(page + 1)}
accessibilityLabel="chevron-right"
Expand All @@ -139,7 +141,7 @@ const PaginationControls = ({
direction={I18nManager.isRTL ? 'rtl' : 'ltr'}
/>
)}
color={colors?.text}
color={textColor}
disabled={numberOfPages === 0 || page === numberOfPages - 1}
onPress={() => onPageChange(numberOfPages - 1)}
accessibilityLabel="page-last"
Expand Down Expand Up @@ -270,7 +272,12 @@ const DataTablePagination = ({
selectPageDropdownAccessibilityLabel,
...rest
}: Props) => {
const labelColor = color(theme?.colors?.text).alpha(0.6).rgb().string();
const labelColor = color(
theme.isV3 ? theme.colors.onSurface : theme?.colors.text
)
.alpha(0.6)
.rgb()
.string();

return (
<View
Expand Down
Loading

0 comments on commit 329b775

Please sign in to comment.