|
1 | 1 | # @theme-ui/custom-properties
|
2 | 2 |
|
3 |
| -Generate CSS custom properties for use with Theme UI. |
| 3 | +Extend [ThemeUI](https://theme-ui.com)'s core functionality with CSS Custom Properties. |
4 | 4 |
|
5 |
| -https://theme-ui.com |
6 | 5 |
|
7 | 6 | ## Installation
|
8 | 7 |
|
9 | 8 | ```
|
10 | 9 | yarn add @theme-ui/custom-properties
|
11 | 10 | ```
|
12 | 11 |
|
| 12 | +## API |
13 | 13 |
|
14 |
| -## Usage |
| 14 | +### toCustomProperties |
15 | 15 |
|
16 |
| -Transform your Theme UI compliant theme config with the library: |
| 16 | +Transform your Theme UI compliant theme to an object of CSS Custom Properties. |
17 | 17 |
|
18 |
| -```js |
19 |
| -const toCustomProperties = require('@theme-ui/custom-properties') |
20 |
| -const theme = require('../theme'); |
| 18 | +**Type**: `Function` |
21 | 19 |
|
22 |
| -module.exports = () => { |
23 |
| - const customProperties = toCustomProperties(theme, '🍭'); |
| 20 | +**Parameters**: |
| 21 | +1. theme - The theme ui specification object |
| 22 | +2. prefix - An optional string prefix for the css custom property (_optional_) |
24 | 23 |
|
25 |
| - return customProperties; |
| 24 | +**Returns**: `Object` |
| 25 | +```js |
| 26 | +{ |
| 27 | + '--color-primary': '#2980b9', |
| 28 | + '--color-secondary': '#f7df1e', |
| 29 | + '--fontSize-0': 12, |
| 30 | + ' -fontSize-1': 14, |
| 31 | + '--fontSize-2': 16, |
| 32 | + '--fontSize-3': 24, |
| 33 | + '--fontSize-4': 32, |
| 34 | + '--fontSize-5': 48, |
| 35 | + '--fontSize-6': 64 |
26 | 36 | }
|
27 | 37 | ```
|
28 | 38 |
|
| 39 | +**Example**: |
| 40 | +```js |
| 41 | +import { toCustomProperties } from '@theme-ui/custom-properties'; |
| 42 | +import theme from '../theme'; |
29 | 43 |
|
30 |
| -## Parameters |
| 44 | +const customProperties = toCustomProperties(theme, '🍭'); |
| 45 | +console.log(customProperties); |
| 46 | +``` |
31 | 47 |
|
32 |
| -The @theme-ui/custom-properties function takes two parameters: |
| 48 | +### withCustomProperties |
| 49 | +Extend the base `ThemeProvider` to allow native styling by using CSS Custom Properties. |
33 | 50 |
|
34 |
| -```js |
35 |
| -toCustomProperties( $theme, $prefix ); |
| 51 | +**Type**: `Function` |
| 52 | + |
| 53 | +**Parameters**: |
| 54 | +1. prefix - An optional string prefix for the css custom property (_optional_) |
| 55 | + |
| 56 | +**Returns** a React Component which extends the default `ThemeProvider` by adding CSS Custom Properties to the wrapper element. |
| 57 | + |
| 58 | +For example: |
| 59 | + |
| 60 | +```jsx |
| 61 | +const ExtendedThemeProvider = withCustomProperties('app-name'); |
| 62 | + |
| 63 | +ReactDOM.render( |
| 64 | + <ExtendedThemeProvider theme={theme}> |
| 65 | + <p> Hello world! </p> |
| 66 | + </ExtendedThemeProvider>, |
| 67 | + root |
| 68 | + ); |
36 | 69 | ```
|
37 | 70 |
|
38 |
| -1. theme - The theme ui specification object |
39 |
| -1. prefix - An optional prefix for the css custom property _optional_ |
| 71 | +Then in CSS we can do something like: |
40 | 72 |
|
| 73 | +```css |
| 74 | +p { |
| 75 | + color: var(--app-name-color-primary); |
| 76 | + background: var(--app-name-color-secondary); |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +These CSS Custom Properties are in total sync with the theme. Also, sub-theming works as expected. |
| 81 | + |
| 82 | +```jsx |
| 83 | +const theme = { |
| 84 | + colors: { |
| 85 | + primary: 'red', |
| 86 | + secondary: 'blue' |
| 87 | + } |
| 88 | +}; |
| 89 | + |
| 90 | +const subTheme = { |
| 91 | + colors: { |
| 92 | + primary: 'orange' |
| 93 | + } |
| 94 | +}; |
| 95 | + |
| 96 | +const ExtendedThemeProvider = withCustomProperties('app-name'); |
| 97 | + |
| 98 | +ReactDOM.render( |
| 99 | + <ExtendedThemeProvider theme={theme}> |
| 100 | + <p> Hello world! </p> // red on a blue background |
| 101 | + |
| 102 | + <ExtendedThemeProvider theme={subTheme}> |
| 103 | + <p> Hello Aliens! </p> // orange on a blue background |
| 104 | + </ExtendedThemeProvider> |
| 105 | + |
| 106 | + </ExtendedThemeProvider>, |
| 107 | + root |
| 108 | +); |
| 109 | +``` |
| 110 | + |
| 111 | +```css |
| 112 | +p { |
| 113 | + color: var(--app-name-color-primary); |
| 114 | + background: var(--app-name-color-secondary); |
| 115 | +} |
| 116 | +``` |
0 commit comments