Skip to content

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 8 commits into from
Jul 31, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/custom-properties/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
babel.config.js
test
coverage
35 changes: 35 additions & 0 deletions packages/custom-properties/README.md
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_

1 change: 1 addition & 0 deletions packages/custom-properties/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('../../babel.config')
22 changes: 22 additions & 0 deletions packages/custom-properties/package.json
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"
}
}
39 changes: 39 additions & 0 deletions packages/custom-properties/src/index.js
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 packages/custom-properties/test/__snapshots__/test.js.snap
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,
"--🍭-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,
}
`;
47 changes: 47 additions & 0 deletions packages/custom-properties/test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import 'babel-polyfill'

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();
})
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -15988,6 +15988,11 @@ please-upgrade-node@^3.1.1:
dependencies:
semver-compare "^1.0.0"

pluralize@^8.0.0:
version "8.0.0"
resolved "https://registry.yarnpkg.com/pluralize/-/pluralize-8.0.0.tgz#1a6fa16a38d12a1901e0320fa017051c539ce3b1"
integrity sha512-Nc3IT5yHzflTfbjgqWcCPpo7DaKy4FnpB0l/zCAW0Tc7jxAiuqSxHasntB3D7887LSrA93kDJ9IXovxJYxyLCA==

pn@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/pn/-/pn-1.1.0.tgz#e2f4cef0e219f463c179ab37463e4e1ecdccbafb"
Expand Down