-
Notifications
You must be signed in to change notification settings - Fork 675
Implement Theme UI to Custom properties utility transformer #235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
28ec6f3
Add css variable package
46ad043
Remove unecessary async in documentation
e856c58
Update packages/css-variables/test/test.js
268de5c
multi level theme obj, non plural props, prefix test, return obj
96383d1
Fix up readme documentation
bc34f57
Remove babel-polyfill
73c27fa
Only remove plural from key
b16e98f
Replace css variables with css custom props
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
babel.config.js | ||
test | ||
coverage |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# @theme-ui/custom-properties | ||
|
||
Generate CSS variables for use with Theme UI. | ||
|
||
|
||
## Installation | ||
|
||
``` | ||
yarn add @theme-ui/custom-properties | ||
``` | ||
|
||
## Usage | ||
|
||
Transform your Theme UI compliant theme config with the library: | ||
|
||
```js | ||
const toCustomProperties = require('@theme-ui/custom-properties') | ||
const theme = require('../theme'); | ||
|
||
module.exports = () => { | ||
const customProperties = toCustomProperties(theme, '🍭'); | ||
|
||
return customProperties; | ||
} | ||
``` | ||
|
||
## Parameters | ||
|
||
The @theme-ui/custom-properties function takes two parameters: | ||
|
||
feColor( $theme, $prefix ); | ||
|
||
1. theme - The theme ui specification object | ||
1. prefix - An optional prefix for the css variables _optional_ | ||
|
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('../../babel.config') |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"name": "@theme-ui/custom-properties", | ||
"description": "Generate CSS variables for use with Theme UI", | ||
"version": "0.0.0", | ||
"main": "dist/index.js", | ||
"module": "dist/index.esm.js", | ||
"author": "Alex Page <[email protected]>", | ||
"license": "MIT", | ||
"scripts": { | ||
"prepare": "microbundle --no-compress", | ||
"watch": "microbundle watch --no-compress" | ||
}, | ||
"devDependencies": { | ||
"babel-polyfill": "^6.26.0" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"dependencies": { | ||
"pluralize": "^8.0.0" | ||
} | ||
} |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import pluralize from 'pluralize' | ||
|
||
const formatKey = key => { | ||
return pluralize(key, 1) | ||
.replace(/([a-z0-9])([A-Z])/g, '$1-$2') | ||
.toLowerCase() | ||
} | ||
|
||
export default (theme, prefix) => { | ||
const customProperties = {} | ||
|
||
const generateProperties = (object, previousKey) => { | ||
Object.entries(object).forEach(([key, value]) => { | ||
let formattedKey = formatKey(key) | ||
|
||
if (prefix && !previousKey) { | ||
formattedKey = `${prefix}-${formattedKey}` | ||
} | ||
|
||
const newKey = previousKey | ||
? previousKey + '-' + formattedKey | ||
: formattedKey | ||
|
||
if (Array.isArray(value)) { | ||
value.forEach((item, i) => { | ||
customProperties[`--${newKey}-${i}`] = item | ||
}) | ||
} else if (Object(value) === value) { | ||
generateProperties(value, newKey) | ||
} else { | ||
customProperties[`--${newKey}`] = value | ||
} | ||
}) | ||
} | ||
|
||
generateProperties(theme) | ||
|
||
return customProperties | ||
} |
63 changes: 63 additions & 0 deletions
63
packages/custom-properties/test/__snapshots__/test.js.snap
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`transforms a theme config to css variables 1`] = ` | ||
Object { | ||
"--color-accent": "#609", | ||
"--color-background": "#fff", | ||
"--color-dark-background-surface": "#fff", | ||
"--color-dark-foreground-text": "#000", | ||
"--color-muted": "#f6f6f6", | ||
"--color-primary": "#07c", | ||
"--color-secondary": "#05a", | ||
"--color-text": "#000", | ||
"--font-body": "system-ui, sans-serif", | ||
"--font-heading": "system-ui, sans-serif", | ||
"--font-monospace": "Menlo, monospace", | ||
"--font-weight-body": 400, | ||
"--font-weight-bold": 700, | ||
"--font-weight-heading": 700, | ||
"--line-height-0": 1.5, | ||
"--line-height-1": 1.125, | ||
"--size-0": "10em", | ||
"--size-1": "20em", | ||
"--size-2": "30em", | ||
"--size-3": "40em", | ||
"--space-0": 0, | ||
"--space-1": 2, | ||
"--space-2": 3, | ||
"--space-3": 4, | ||
"--space-4": 5, | ||
"--space-5": 6, | ||
} | ||
`; | ||
|
||
exports[`transforms a theme config to css variables with prefix 1`] = ` | ||
Object { | ||
"--🍭-color-accent": "#609", | ||
"--🍭-color-background": "#fff", | ||
"--🍭-color-dark-background-surface": "#fff", | ||
"--🍭-color-dark-foreground-text": "#000", | ||
"--🍭-color-muted": "#f6f6f6", | ||
"--🍭-color-primary": "#07c", | ||
"--🍭-color-secondary": "#05a", | ||
"--🍭-color-text": "#000", | ||
"--🍭-font-body": "system-ui, sans-serif", | ||
"--🍭-font-heading": "system-ui, sans-serif", | ||
"--🍭-font-monospace": "Menlo, monospace", | ||
"--🍭-font-weight-body": 400, | ||
alex-page marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"--🍭-font-weight-bold": 700, | ||
"--🍭-font-weight-heading": 700, | ||
"--🍭-line-height-0": 1.5, | ||
"--🍭-line-height-1": 1.125, | ||
"--🍭-size-0": "10em", | ||
"--🍭-size-1": "20em", | ||
"--🍭-size-2": "30em", | ||
"--🍭-size-3": "40em", | ||
"--🍭-space-0": 0, | ||
"--🍭-space-1": 2, | ||
"--🍭-space-2": 3, | ||
"--🍭-space-3": 4, | ||
"--🍭-space-4": 5, | ||
"--🍭-space-5": 6, | ||
} | ||
`; |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import 'babel-polyfill' | ||
alex-page marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import toCustomProperties from '../src' | ||
|
||
const theme = { | ||
colors: { | ||
text: '#000', | ||
background: '#fff', | ||
primary: '#07c', | ||
secondary: '#05a', | ||
accent: '#609', | ||
muted: '#f6f6f6', | ||
dark: { | ||
foreground: { | ||
text: '#000' | ||
}, | ||
background: { | ||
surface: '#fff' | ||
} | ||
} | ||
}, | ||
fonts: { | ||
body: 'system-ui, sans-serif', | ||
heading: 'system-ui, sans-serif', | ||
monospace: 'Menlo, monospace', | ||
}, | ||
fontWeights: { | ||
body: 400, | ||
heading: 700, | ||
bold: 700, | ||
}, | ||
lineHeights: [1.5, 1.125], | ||
space: [0, 2, 3, 4, 5, 6], | ||
size: ['10em', '20em', '30em', '40em'], | ||
} | ||
|
||
it('transforms a theme config to css variables', () => { | ||
const result = toCustomProperties(theme); | ||
|
||
expect(result).toMatchSnapshot(); | ||
}) | ||
|
||
it('transforms a theme config to css variables with prefix', () => { | ||
const result = toCustomProperties(theme, '🍭'); | ||
|
||
expect(result).toMatchSnapshot(); | ||
}) |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.