Skip to content

Commit

Permalink
Merge pull request react-native-elements#2650 from vividh/fix/2647
Browse files Browse the repository at this point in the history
fix(ThemeProvider): Custom theme lost on dark mode switch
  • Loading branch information
flyingcircle committed Dec 14, 2020
2 parents a19f521 + 894f21c commit d8c51e5
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/config/ThemeProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,15 @@ export default class ThemeProvider extends React.Component {
if (useDark !== state.useDark) {
const defaultColors = useDark ? darkColors : colors;
return {
theme: deepmerge(state.theme, {
colors: defaultColors,
}),
theme: deepmerge(
state.theme,
deepmerge(
{
colors: defaultColors,
},
props.theme
)
),
useDark,
};
}
Expand Down
39 changes: 39 additions & 0 deletions src/config/__tests__/ThemeProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,43 @@ describe('ThemeProvider', () => {
useDark: false,
});
});

it('should retain custom theme when switching between light / dark mode', () => {
const customTheme = { colors: { primary: 'cyan' } };

const component = shallow(
<ThemeProvider theme={customTheme}>
<View />
</ThemeProvider>
);
const instance = component.instance();

expect(instance.state).toMatchObject({
theme: {
...theme,
colors: {
...theme.colors,
...customTheme.colors,
},
},
});

// Switch to dark mode
component.setProps({ useDark: true });
const retrievedDarkTheme = instance.getTheme();

expect(retrievedDarkTheme).toBeTruthy();
expect(retrievedDarkTheme.colors.primary).toEqual(
customTheme.colors.primary
);

// Switch to light mode
component.setProps({ useDark: false });
const retrievedLightTheme = instance.getTheme();

expect(retrievedLightTheme).toBeTruthy();
expect(retrievedLightTheme.colors.primary).toEqual(
customTheme.colors.primary
);
});
});

0 comments on commit d8c51e5

Please sign in to comment.