Skip to content

Commit

Permalink
feat: Export ThemeContext for usage with hooks (#2049)
Browse files Browse the repository at this point in the history
Resolves #2007
  • Loading branch information
iRoachie committed Sep 3, 2019
1 parent 80d0070 commit 49ca4d0
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 3 deletions.
18 changes: 18 additions & 0 deletions docs/customization.md
Expand Up @@ -277,6 +277,24 @@ const MyComponent = () => (
)
```

You can also use the ThemeContext directly if you use hooks.

```jsx
import React, { useContext } from 'react';
import { Text } from 'react-native';
import { ThemeContext } from 'react-native-elements';

const MyComponent = () => {
const { theme } = useContext(ThemeContext);

return (
<View style={styles.container}>
<Text style={{ color: theme.colors.primary }}>Yo!</Text>
</View>
);
};
```

---

### Using the respective platform's native colors
Expand Down
4 changes: 3 additions & 1 deletion src/config/ThemeProvider.js
Expand Up @@ -4,7 +4,9 @@ import deepmerge from 'deepmerge';

import colors from './colors';

const ThemeContext = React.createContext();
export const ThemeContext = React.createContext({
colors,
});

export default class ThemeProvider extends React.Component {
constructor(props) {
Expand Down
3 changes: 2 additions & 1 deletion src/config/index.js
Expand Up @@ -5,7 +5,7 @@ import BackgroundImage from './BackgroundImage';
import colors from './colors';
import ViewPropTypes from './ViewPropTypes';
import fonts from './fonts';
import ThemeProvider, { ThemeConsumer } from './ThemeProvider';
import ThemeProvider, { ThemeConsumer, ThemeContext } from './ThemeProvider';
import withTheme from './withTheme';

const TextPropTypes = Text.propTypes;
Expand All @@ -19,5 +19,6 @@ export {
fonts,
ThemeProvider,
ThemeConsumer,
ThemeContext,
withTheme,
};
2 changes: 2 additions & 0 deletions src/index.d.ts
Expand Up @@ -2063,6 +2063,8 @@ export interface ThemeConsumerProps<T> {

export class ThemeConsumer<T> extends React.Component<ThemeConsumerProps<T>> {}

export const ThemeContext: React.Context<ThemeProps<{}>>;

export function withTheme<P = {}, T = {}>(
component: React.ComponentType<P & ThemeProps<T>>
): React.ComponentClass<P>;
9 changes: 8 additions & 1 deletion src/index.js
Expand Up @@ -36,7 +36,13 @@ import {

// helpers
import Text from './text/Text';
import { colors, ThemeProvider, ThemeConsumer, withTheme } from './config';
import {
colors,
ThemeProvider,
ThemeConsumer,
ThemeContext,
withTheme,
} from './config';
import getIconType, { registerCustomIconType } from './helpers/getIconType';
import normalize from './helpers/normalizeText';

Expand Down Expand Up @@ -71,6 +77,7 @@ export {
Overlay,
ThemeProvider,
ThemeConsumer,
ThemeContext,
withBadge,
withTheme,
Image,
Expand Down

0 comments on commit 49ca4d0

Please sign in to comment.