diff --git a/docs/customization.md b/docs/customization.md index 14c1e4649d..71fad9e2a8 100644 --- a/docs/customization.md +++ b/docs/customization.md @@ -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 ( + + Yo! + + ); +}; +``` + --- ### Using the respective platform's native colors diff --git a/src/config/ThemeProvider.js b/src/config/ThemeProvider.js index 47639b3e1d..a36137afd6 100644 --- a/src/config/ThemeProvider.js +++ b/src/config/ThemeProvider.js @@ -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) { diff --git a/src/config/index.js b/src/config/index.js index 6d82a5cbec..c9286482d9 100644 --- a/src/config/index.js +++ b/src/config/index.js @@ -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; @@ -19,5 +19,6 @@ export { fonts, ThemeProvider, ThemeConsumer, + ThemeContext, withTheme, }; diff --git a/src/index.d.ts b/src/index.d.ts index b086ebc606..92994ac480 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -2063,6 +2063,8 @@ export interface ThemeConsumerProps { export class ThemeConsumer extends React.Component> {} +export const ThemeContext: React.Context>; + export function withTheme

( component: React.ComponentType

> ): React.ComponentClass

; diff --git a/src/index.js b/src/index.js index 99d6b2eda7..17a5817908 100644 --- a/src/index.js +++ b/src/index.js @@ -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'; @@ -71,6 +77,7 @@ export { Overlay, ThemeProvider, ThemeConsumer, + ThemeContext, withBadge, withTheme, Image,