Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for useTheme hook #84

Merged
merged 16 commits into from
Feb 11, 2019
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
### Next

- Improve bundle size ([#83](https://github.com/cssinjs/theming/pull/83))
- Add useTheme hook ([#84](https://github.com/cssinjs/theming/pull/84))

### 3.0.3 (2019-1-20)

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@
"eslint-config-jss": "^5.0.1",
"flow-bin": "^0.88.0",
"nyc": "^13.1.0",
"react": "^16.6.0",
"react-test-renderer": "^16.6.0",
"react": "^16.8.0",
"react-test-renderer": "^16.8.0",
"rimraf": "^2.6.1",
"rollup": "^0.66.6",
"rollup-plugin-babel": "^4.0.3",
Expand Down
15 changes: 15 additions & 0 deletions src/create-use-theme.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// @flow

import React, { type Context } from 'react';
import warning from 'tiny-warning';
import isObject from './is-object';

export default function createUseTheme<Theme>(context: Context<Theme>) {
HenriBeck marked this conversation as resolved.
Show resolved Hide resolved
return () => {
HenriBeck marked this conversation as resolved.
Show resolved Hide resolved
const theme = React.useContext(context);

warning(isObject(theme), '[theming] Please use useTheme only with the ThemeProvider');

return theme;
};
}
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { createContext, type Context } from 'react';

import createThemeProvider, { type ThemeProviderProps } from './create-theme-provider';
import createWithTheme from './create-with-theme';
import createUseTheme from './create-use-theme';

type ExtractReturnType<Theme> = <ReturnType>(
(context: Context<Theme>) => ReturnType
Expand All @@ -21,6 +22,7 @@ function createTheming<Theme>(context: Context<Theme>): Theming<Theme> {
return {
context,
withTheme: createWithTheme(context),
useTheme: createUseTheme(context),
ThemeProvider: createThemeProvider(context),
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ test('createTheming()\'s key names', (t) => {
const context = createContext({});
const theming = createTheming(context);
const actual = Object.keys(theming);
const expected = ['context', 'withTheme', 'ThemeProvider'];
const expected = ['context', 'withTheme', 'useTheme', 'ThemeProvider'];

t.deepEqual(
actual,
Expand Down