-
Notifications
You must be signed in to change notification settings - Fork 671
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
custom-properties: extend ThemeProvider to insert CSS Custom Props
- Loading branch information
Showing
5 changed files
with
192 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,126 @@ | ||
# @theme-ui/custom-properties | ||
|
||
Generate CSS custom properties for use with Theme UI. | ||
Extend [ThemeUI](https://theme-ui.com)'s core functionality with CSS Custom Properties. | ||
|
||
https://theme-ui.com | ||
|
||
## Installation | ||
|
||
``` | ||
yarn add @theme-ui/custom-properties | ||
``` | ||
|
||
## API | ||
|
||
## Usage | ||
### toCustomProperties | ||
|
||
Transform your Theme UI compliant theme config with the library: | ||
Transform your Theme UI compliant theme to an object of CSS Custom Properties. | ||
|
||
```js | ||
const toCustomProperties = require('@theme-ui/custom-properties') | ||
const theme = require('../theme'); | ||
**Type**: `Function` | ||
|
||
module.exports = () => { | ||
const customProperties = toCustomProperties(theme, '🍭'); | ||
**Parameters**: | ||
1. theme - The theme ui specification object | ||
2. prefix - An optional string prefix for the css custom property (_optional_) | ||
|
||
return customProperties; | ||
**Returns**: `Object` | ||
```js | ||
// Example response | ||
{ | ||
'--color-primary': '#2980b9', | ||
'--color-secondary': '#f7df1e', | ||
'--fontSize-0': 12, | ||
' -fontSize-1': 14, | ||
'--fontSize-2': 16, | ||
'--fontSize-3': 24, | ||
'--fontSize-4': 32, | ||
'--fontSize-5': 48, | ||
'--fontSize-6': 64 | ||
} | ||
``` | ||
|
||
**Example**: | ||
```js | ||
import toCustomProperties from '@theme-ui/custom-properties'; | ||
import theme from '../theme'; | ||
|
||
## Parameters | ||
const customProperties = toCustomProperties(theme, '🍭'); | ||
console.log(customProperties); | ||
``` | ||
|
||
The @theme-ui/custom-properties function takes two parameters: | ||
### withCustomProperties | ||
Extend the base `ThemeProvider` to allow native styling by using CSS Custom Properties. | ||
|
||
```js | ||
toCustomProperties( $theme, $prefix ); | ||
**Type**: `Function` | ||
|
||
**Parameters**: | ||
1. prefix - An optional string prefix for the css custom property (_optional_) | ||
2. className - An optional class name to add onto the wrapper. All CSS Custom Properties will be defined on this element. | ||
|
||
**Returns** a React Component which extends the default `ThemeProvider` by adding CSS Custom Properties to the wrapper element. | ||
|
||
For example: | ||
|
||
```jsx | ||
const ExtendedThemeProvider = withCustomProperties('app-name', 'extended-theme-provider'); | ||
|
||
ReactDOM.render( | ||
<ExtendedThemeProvider theme={theme}> | ||
<p> Hello world! </p> | ||
</ExtendedThemeProvider>, | ||
root | ||
); | ||
``` | ||
|
||
1. theme - The theme ui specification object | ||
1. prefix - An optional prefix for the css custom property _optional_ | ||
will render: | ||
|
||
```jsx | ||
<div class="extended-theme-provider"> | ||
<p> Hello world! </p> | ||
</div> | ||
``` | ||
|
||
Then in CSS we can do something like: | ||
|
||
```css | ||
p { | ||
color: var(--app-name-color-primary); | ||
background: var(--app-name-color-secondary); | ||
} | ||
``` | ||
|
||
These CSS Custom Properties are in total sync with the theme. Also, sub-theming works as expected. | ||
|
||
```jsx | ||
const theme = { | ||
colors: { | ||
primary: 'red', | ||
secondary: 'blue' | ||
} | ||
}; | ||
|
||
const subTheme = { | ||
colors: { | ||
primary: 'orange' | ||
} | ||
}; | ||
|
||
const ExtendedThemeProvider = withCustomProperties('app-name'); | ||
|
||
ReactDOM.render( | ||
<ExtendedThemeProvider theme={theme}> | ||
<p> Hello world! </p> // red on a blue background | ||
|
||
<ExtendedThemeProvider theme={subTheme}> | ||
<p> Hello Aliens! </p> // orange on a blue background | ||
</ExtendedThemeProvider> | ||
|
||
</ExtendedThemeProvider>, | ||
root | ||
); | ||
``` | ||
|
||
```css | ||
p { | ||
color: var(--app-name-color-primary); | ||
background: var(--app-name-color-secondary); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
packages/custom-properties/test/__snapshots__/test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters