|
1 |
| -# react-theme-utils |
2 |
| -Utilities for theming and theme switching via React 16.8 hooks and CSS custom properties. |
| 1 | +# react-theme-context |
| 2 | + |
| 3 | +[](https://travis-ci.com/jedmao/react-theme-context) |
| 4 | +[](https://codecov.io/gh/jedmao/react-theme-context) |
| 5 | +[](https://www.npmjs.com/package/react-theme-context) |
| 6 | + |
| 7 | +Provides theme [context](https://reactjs.org/docs/context.html) and |
| 8 | +[hooks](https://reactjs.org/docs/hooks-reference.html). Supports theme switching |
| 9 | +via |
| 10 | +[CSS custom properties](https://developer.mozilla.org/en-US/docs/Web/CSS/--*). |
| 11 | + |
| 12 | +## Usage |
| 13 | + |
| 14 | +_The following example uses [TypeScript](http://www.typescriptlang.org/)._ |
| 15 | + |
| 16 | +- You only want to create a single theme context and reuse it throughout your |
| 17 | + application. |
| 18 | +- You can create as many themes as you want. |
| 19 | +- A `Theme` is expected to be of type: |
| 20 | + `Record<string, string | number | boolean>`. |
| 21 | + |
| 22 | +### themeContext.tsx |
| 23 | + |
| 24 | +```tsx |
| 25 | +import ThemeContext from 'react-theme-context' |
| 26 | + |
| 27 | +const defaultTheme = { primaryColor: 'red' } |
| 28 | + |
| 29 | +export default new ThemeContext(defaultTheme) |
| 30 | +``` |
| 31 | + |
| 32 | +### App.tsx |
| 33 | + |
| 34 | +```tsx |
| 35 | +import React, { FC } from 'react' |
| 36 | + |
| 37 | +import themeContext from './themeContext' |
| 38 | + |
| 39 | +const App: FC = () => { |
| 40 | + themeContext.useLayoutEffect() |
| 41 | + const [theme, setTheme] = themeContext.use() |
| 42 | + return ( |
| 43 | + <div style={`background: ${themeContext.prop('primaryColor')}`}> |
| 44 | + <button |
| 45 | + onClick={() => { |
| 46 | + setTheme({ primaryColor: 'blue' }) |
| 47 | + }} |
| 48 | + > |
| 49 | + {theme.primaryColor} |
| 50 | + </button> |
| 51 | + </div> |
| 52 | + ) |
| 53 | +} |
| 54 | + |
| 55 | +export default App |
| 56 | +``` |
| 57 | + |
| 58 | +### index.tsx |
| 59 | + |
| 60 | +```tsx |
| 61 | +import React from 'react' |
| 62 | +import ReactDOM from 'react-dom' |
| 63 | + |
| 64 | +import themeContext from './themeContext' |
| 65 | +import App from './App' |
| 66 | + |
| 67 | +const renderApp = () => |
| 68 | + ReactDOM.render( |
| 69 | + <themeContext.Provider> |
| 70 | + <App /> |
| 71 | + </themeContext.Provider>, |
| 72 | + document.getElementById('root'), |
| 73 | + ) |
| 74 | + |
| 75 | +renderApp() |
| 76 | +``` |
| 77 | + |
| 78 | +## ThemeContext API |
| 79 | + |
| 80 | +### `#Provider` |
| 81 | + |
| 82 | +_See: |
| 83 | +[React Docs | Context Provider](https://reactjs.org/docs/context.html#contextprovider)_ |
| 84 | + |
| 85 | +### `#prop(property: keyof Theme): string` |
| 86 | + |
| 87 | +Converts `property` into |
| 88 | +[CSS custom property](https://developer.mozilla.org/en-US/docs/Web/CSS/--*) |
| 89 | +syntax. In TypeScript, it also prevents you from using a theme property that is |
| 90 | +not defined. |
| 91 | + |
| 92 | +#### Example |
| 93 | + |
| 94 | +```ts |
| 95 | +themeContext.prop('primaryColor') |
| 96 | +// 'var(--primary-color)' |
| 97 | +``` |
| 98 | + |
| 99 | +### `#useLayoutEffect(element = document.documentElement)` |
| 100 | + |
| 101 | +_Returns: `[T, Dispatch<SetStateAction<T>>]`_ |
| 102 | + |
| 103 | +Sets theme properties as |
| 104 | +[CSS custom properties](https://developer.mozilla.org/en-US/docs/Web/CSS/--*) on |
| 105 | +the provided `element` or the root `documentElement` by default. If the theme is |
| 106 | +updated, these props are updated too. This enables live theme switching! |
| 107 | + |
| 108 | +_See: |
| 109 | +[React Hooks API Reference | useLayoutEffect](https://reactjs.org/docs/hooks-reference.html#uselayouteffect)_ |
| 110 | + |
| 111 | +### `#use()` |
| 112 | + |
| 113 | +_Returns: the result of |
| 114 | +[`React.useContext`](https://reactjs.org/docs/hooks-reference.html#usecontext)_ |
| 115 | + |
| 116 | +## Available Scripts |
| 117 | + |
| 118 | +In the project directory, you can run: |
| 119 | + |
| 120 | +### `npm test` |
| 121 | + |
| 122 | +Launches the [Jest][https://jestjs.io/] test runner in the interactive |
| 123 | +[watch](https://jestjs.io/docs/en/cli#watch) mode. |
| 124 | + |
| 125 | +For a coverage report, run `npm test -- --coverage`. |
| 126 | + |
| 127 | +### `npm run lint` |
| 128 | + |
| 129 | +Runs [ESLint](https://eslint.org/). |
| 130 | + |
| 131 | +### `npm run commit` |
| 132 | + |
| 133 | +Runs [commitizen](https://www.npmjs.com/package/commitizen), prompting you to |
| 134 | +fill out any required commit fields at commit time. |
| 135 | + |
| 136 | +### `npm run build` |
| 137 | + |
| 138 | +Builds the library for [npm](https://www.npmjs.com/) into the `dist` folder. |
0 commit comments