Skip to content

Commit 936f99b

Browse files
committed
feat: add @withTheme decorator
1 parent 4260276 commit 936f99b

File tree

6 files changed

+48
-11
lines changed

6 files changed

+48
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ const MyComponent = mock();
6868
- `FocusSensor`, `withFocus()`, and `@withFocus`
6969
- [Context](./docs/en/Context.md)
7070
- [`<Provider>`](./docs/en/Provider.md#provider), [`<Consumer>`](./docs/en/Provider.md#consumer), [`withContext()`](./docs/en/Provider.md#withcontext-hoc), and [`@withContext`](./docs/en/Provider.md#withcontext-decorator)
71-
- [`<Theme>`](./docs/en/theme.md#theme), [`<Themed>`](./docs/en/theme.md#themed), [`withTheme()`](./docs/en/theme.md#withtheme), and `@withTheme`
71+
- [`<Theme>`](./docs/en/theme.md#theme), [`<Themed>`](./docs/en/theme.md#themed), [`withTheme()`](./docs/en/theme.md#withtheme-hoc), and [`@withTheme`](./docs/en/theme.md#withtheme-decorator)
7272
- `<CssVars>`
7373
- [`<Router>`](./docs/en/routing.md#router), [`<Route>`](./docs/en/routing.md#route), [`withRoute()`](./docs/en/routing.md#withroute), `@withRoute`, `go()`, and `<Go>`
7474
- [`<Translations>`](./docs/en/translate.md#translations), [`<Translate>`](./docs/en/translate.md#translate-or-t), [`<T>`](./docs/en/translate.md#translate-or-t), [`withT()`](./docs/en/translate.md#witht-hoc), and [`@withT`](./docs/en/translate.md#witht-decorator)

docs/en/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
- `FocusSensor`, `withFocus()`, and `@withFocus`
3838
- [Context](./Context.md)
3939
- [`<Provider>`](./Provider.md#provider), [`<Consumer>`](./Provider.md#consumer), [`withContext()`](./Provider.md#withcontext-hoc), and [`@withContext`](./Provider.md#withcontext-decorator)
40-
- [`<Theme>`](./theme.md#theme), [`<Themed>`](./theme.md#themed), [`withTheme()`](./theme.md#withtheme), and `@withTheme`
40+
- [`<Theme>`](./theme.md#theme), [`<Themed>`](./theme.md#themed), [`withTheme()`](./theme.md#withtheme-hoc), and [`@withTheme`](./theme.md#withtheme-decorator)
4141
- `<CssVars>`
4242
- [`<Router>`](./routing.md#router), [`<Route>`](./routing.md#route), [`withRoute()`](./routing.md#withroute), `@withRoute`, `go()`, and `<Go>`
4343
- [`<Translations>`](./translate.md#translations), [`<Translate>`](./translate.md#translate-or-t), [`<T>`](./translate.md#translate-or-t), [`withT()`](./translate.md#witht-hoc), and [`@withT`](./translate.md#witht-decorator)

docs/en/theme.md

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,19 @@ FaCC theme consumer, re-renders on theme update.
3737
- `name` - optional, theme name.
3838

3939

40-
## `withTheme()`
40+
## `withTheme()` HOC
4141

42-
HOC theme consumer that ensures your component is enhanced with `theme` property.
42+
Theme enhancer that ensures your component is injected with `theme` property.
4343

4444
```ts
45-
withTheme: (Comp: React.Component, name?: string) => React.Component;
45+
withTheme: (Comp: React.Component, propName?: string, props?: object) => React.Component;
4646
```
4747

4848
, where
4949

5050
- `Comp` - your React component.
51-
- `name` - optional, theme name.
51+
- `propName` - optional, string, injected prop name.
52+
- `props` - optional, object, props to provided to [`<Themed>`](#themed).
5253

5354
Returns a *themed* component that will have a prop named `theme` containing
5455
theme object.
@@ -69,3 +70,20 @@ const theme = {
6970
<BlockThemed />
7071
</Theme>
7172
```
73+
74+
75+
## `@withTheme` decorator
76+
77+
React component class decorator that injects `theme` into props. You can optionally,
78+
specify injected prop name and theme name.
79+
80+
Usage
81+
82+
```js
83+
@withTheme
84+
@withTheme('')
85+
@withTheme('specialTheme', {name: 'specialTheme'})
86+
class Button extends Component {
87+
88+
}
89+
``

src/shim/__story__/createRef.story.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Example extends Component<any, any> {
1313
};
1414

1515
render () {
16-
return <div ref={this.divRef} onClick={this.onClick}>foobar</div>;
16+
return <div ref={this.divRef} onClick={this.onClick}>{'See <div> ref in console.'}</div>;
1717
}
1818
}
1919

src/theme/story.tsx renamed to src/theme/__story__/story.tsx

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import {createElement as h} from 'react';
1+
import {createElement as h, Component} from 'react';
22
import {storiesOf} from '@storybook/react';
33
import {action} from '@storybook/addon-actions';
44
import {linkTo} from '@storybook/addon-links';
5-
import {Theme, Themed, withTheme} from '.';
5+
import {Theme, Themed, withTheme} from '..';
66

77
const theme = {
88
color: 'white',
@@ -14,6 +14,19 @@ const Block = ({theme: {color, background}}) =>
1414

1515
const BlockThemed = withTheme(Block);
1616

17+
@withTheme
18+
class Decorator1 extends Component<any, any> {
19+
render () {
20+
const {color, background} = this.props.theme;
21+
22+
return (
23+
<div style={{color, background}}>
24+
Background: {background}, color: {color}
25+
</div>
26+
);
27+
}
28+
}
29+
1730
storiesOf('Context/theme', module)
1831
.add('FaCC', () =>
1932
<Theme value={theme}>
@@ -26,4 +39,9 @@ storiesOf('Context/theme', module)
2639
<Theme value={theme}>
2740
<BlockThemed />
2841
</Theme>
29-
);
42+
)
43+
.add('Decorator 1', () =>
44+
<Theme value={theme}>
45+
<Decorator1 />
46+
</Theme>
47+
)

src/theme/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {Provider, Consumer, withContext} from '../context';
22
import {h, THoc} from '../util';
3+
import faccToHocc from '../util/faccToHoc';
34

45
export interface IThemeProps {
56
name?: string;
@@ -22,4 +23,4 @@ export const Themed: React.StatelessComponent<IThemedProps> = (props) => {
2223
return h(Consumer, {name}, children);
2324
};
2425

25-
export const withTheme: THoc<any, any> = (Comp, name = 'theme') => withContext(Comp, name, {name});
26+
export const withTheme = faccToHocc(Themed, 'theme');

0 commit comments

Comments
 (0)