UNPKG

3.52 kBMarkdownView Raw
1# react-base16-styling [![Latest Stable Version](https://img.shields.io/npm/v/react-base16-styling.svg)](https://www.npmjs.com/package/react-base16-styling)
2
3React styling with base16 color scheme support
4
5Inspired by [react-themeable](https://github.com/markdalgleish/react-themeable), this utility provides flexible theming for your component with [base16](https://github.com/chriskempson/base16) theme support.
6
7## Install
8
9```
10yarn add react-base16-styling
11```
12
13## Usage
14
15```jsx
16import { createStyling } from 'react-base16-styling';
17import base16Themes from './base16Themes';
18
19function getStylingFromBase16(base16Theme) {
20 return {
21 myComponent: {
22 backgroundColor: base16Theme.base00,
23 },
24
25 myComponentToggleColor({ style, className }, clickCount) {
26 return {
27 style: {
28 ...style,
29 backgroundColor: clickCount % 2 ? 'red' : 'blue',
30 },
31 };
32 },
33 };
34}
35
36const createStylingFromTheme = createStyling(getStylingFromBase16, {
37 defaultBase16: base16Themes.solarized,
38 base16Themes,
39});
40
41class MyComponent extends Component {
42 state = { clickCount: 0 };
43 render() {
44 const { theme } = this.props;
45 const { clickCount } = this.state;
46
47 const styling = createStylingFromTheme(theme);
48
49 return (
50 <div {...styling('myComponent')}>
51 <a onClick={() => this.setState({ clickCount: clickCount + 1 })}>
52 Click Me
53 </a>
54 <div {...styling('myComponentToggleColor', clickCount)}>
55 {clickCount}
56 </div>
57 </div>
58 );
59 }
60}
61```
62
63## `createStyling`
64
65```js
66function(getStylingFromBase16, defaultStylingOptions, themeOrStyling)
67```
68
69| Argument | Signature | Description |
70| ----------------------- | --------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
71| `getStylingFromBase16` | `function(base16Theme)` | creates object with default stylings for your component, using provided base16 theme. |
72| `defaultStylingOptions` | `{ defaultBase16, base16Themes }` | optional parameters, allow to set default `base16` theme and additional `base16` themes for component. |
73| `themeOrStyling` | `string` or `object` | `base16` theme name, `base16` theme object or styling object. Theme name can have a modifier: `"themeName:inverted"` to invert theme colors (see [[#invertTheme]]) |
74
75Styling object values could be: - objects (treated as style definitions) - strings (class names) - functions (they may be provided with additional parameters and should return object { style, className })
76
77## `getBase16Theme`
78
79```js
80function(themeOrStyling, base16Themes)
81```
82
83Helper method that returns `base16` theme object if `themeOrStyling` is `base16` theme name or theme object.
84
85## `invertBase16Theme`
86
87```js
88function(base16Theme)
89```
90
91Helper method that inverts `base16` theme colors, creating light theme out of dark one or vice versa.
92
93## `invertTheme`
94
95```js
96function(theme)
97```
98
99Helper method that inverts a theme or styling object to be passed into the `themeOrStyling` parameter of [createStyling](#createstyling).