Skip to content

Commit

Permalink
feat(themes): add themes package (carbon-design-system#239)
Browse files Browse the repository at this point in the history
* refactor(colors): move tokens out of colors package

* feat(themes): add themes package

* docs: run doctoc and update colors guide

* chore: run sync

* chore(project): update offline mirror

* chore(project): add scss to gitignore in themes
  • Loading branch information
joshblack authored Jan 10, 2019
1 parent 070f34d commit fb19540
Show file tree
Hide file tree
Showing 26 changed files with 5,783 additions and 122 deletions.
Binary file added .yarn-offline-mirror/prettier-1.15.3.tgz
Binary file not shown.
69 changes: 62 additions & 7 deletions docs/guides/colors.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
- [Contributing](#contributing)
- [Colors](#colors)
- [Gotchas](#gotchas)
- [Tokens](#tokens)
- [Themes](#themes)
- [Using another theme](#using-another-theme)
- [`rgba` values](#rgba-values)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->
<!-- prettier-ignore-end -->
Expand Down Expand Up @@ -58,12 +60,15 @@ otherwise the JavaScript program will not run.
- For example, `export const red10 = '#fff0f1;` would not compile without the
ending `'` quote.

### Tokens
### Themes

When working with token names and values, you will be working in the
[`tokens.js`](/packages/colors/src/tokens.js) file. This file is written in
JavaScript and exports all the tokens that developers could use when working in
the Carbon Design System.
[`themes`](/packages/themes/src) folder. This folder will have files that are
named for each theme we currently support. The [`white`](/packages/themes/src/white.js) theme is considered the default theme.

Each file in this directly (outside of `index.js`) will have all of the theme
tokens defined inside of them. These files are written in JavaScript exports all
the tokens that developers could use when working in the Carbon Design System.

A token is exported by writing the following:

Expand All @@ -87,7 +92,7 @@ import {
blue80,

// ...
} from './colors';
} from '@carbon/colors';
```

In the above code snippet, we can see that several blue values have been
Expand All @@ -103,7 +108,7 @@ import {
blue80,

// ...
} from './colors';
} from '@carbon/colors';
```

Afterwards, we could use `blue20` as the value in any of our tokens 🎉
Expand All @@ -115,3 +120,53 @@ from `blue60` to `blue20` we would need to do the following:
```js
export const interactive01 = blue20;
```

#### Using another theme

If a theme that you are currently editing relies on another theme value, you can
re-export that token at the end of the file. As an example, let's
say we are working in the `gray10`, or `g10`, theme. This theme has a token,
`hoverSelectedUI`, that is the same value as the `white` theme.

Following our examples above, we would normally write a token value like:

```js
export const hoverSelectedUI = 'some-value';
```

However, since this token relies on the `white` theme, we can re-export this
value at the bottom of the file. We can do this by writing the following:

```js
export { hoverSelectedUI } from './white';
```

Writing the line above will allow us to get the `hoverSelectedUI` value from the
`white` theme and include it as part of the `g10` theme.

#### `rgba` values

When working with values that have opacity, you can import and use the `rgba` helper to get the proper representation of the value in code.

We can use this helper by including the following at the top of the file in our
imports:

```js
import {
blue60,
blue80,
blue100,
// ...

// Include the `rgba` helper
rgba,
} from '@carbon/colors';

// ...

export const overlay01 = rgba(gray100, 0.5);
```

In this example, we have included the `rgba` helper and called in for the token
`overlay01`. This helper takes in a color, in this case `gray100`, and the
decimal value for the opacity.
6 changes: 3 additions & 3 deletions packages/bundler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"private": true,
"version": "0.0.1-alpha.26",
"license": "Apache-2.0",
"bin": {
"bundler": "./bin/bundler.js"
},
"repository": "https://github.com/IBM/carbon-elements/tree/master/packages/bundler",
"bugs": "https://github.com/IBM/carbon-elements/issues",
"keywords": [
Expand Down Expand Up @@ -39,8 +42,5 @@
},
"devDependencies": {
"change-case": "^3.0.2"
},
"bin": {
"bundler": "./bin/bundler.js"
}
}
2 changes: 1 addition & 1 deletion packages/colors/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"access": "public"
},
"scripts": {
"build": "yarn clean && bundler bundle src/index.js --name CarbonColors && node tasks/build.js && bundler check 'scss/*.scss' && bundler bundle:scss scss/bundle.scss -n colors",
"build": "yarn clean && bundler bundle src/index.js --name CarbonColors && node tasks/build.js && bundler check 'scss/*.scss'",
"clean": "rimraf css es lib umd scss"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/colors/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
*/

export * from './colors';
export * from './tokens';
export * from './rgb';
21 changes: 21 additions & 0 deletions packages/colors/src/rgb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Copyright IBM Corp. 2018, 2018
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

/**
* Parse a given hexcode string into an rgba statement with the given opacity
* @param {string} hexcode
* @param {number} opacity
* @return {string}
*/
export function rgba(hexcode, opacity) {
const values = [
hexcode.substring(1, 3),
hexcode.substring(3, 5),
hexcode.substring(5, 7),
].map(string => parseInt(string, 16));
return `rgba(${values[0]}, ${values[1]}, ${values[2]}, ${opacity})`;
}
41 changes: 1 addition & 40 deletions packages/colors/tasks/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ const {

const SCSS_DIR = path.resolve(__dirname, '../scss');
const COLORS_ENTRYPOINT = path.join(SCSS_DIR, 'colors.scss');
const TOKEN_ENTRYPOINT = path.join(SCSS_DIR, 'tokens.scss');
const MIXINS_ENTRYPOINT = path.join(SCSS_DIR, 'mixins.scss');
const BUNDLE_ENTRYPOINT = path.join(SCSS_DIR, 'bundle.scss');
const GENERATED_COMMENT = '// Code generated by @carbon/colors. DO NOT EDIT.';
Expand Down Expand Up @@ -51,7 +50,7 @@ const colorsToMap = {
};

async function build() {
reporter.info('Building scss files for colors and tokens...');
reporter.info('Building scss files for colors...');

const colorVariables = Object.keys(colorsToMap).reduce((acc, key) => {
const swatch = paramCase(key);
Expand All @@ -74,21 +73,10 @@ async function build() {
return acc.concat(...values);
}, []);

const tokenVariables = Object.keys(tokens).map(key => {
const token = paramCase(key);
const [type, grade] = token.split(/(\d+)/g).filter(Boolean);
const name = grade ? `${type}-${grade}` : type;
return createVariable(name, tokens[key]);
});

const mixins = `${GENERATED_COMMENT}
@mixin color-values {
${colorVariables.map(variable => '' + variable).join('\n ')}
}
@mixin token-values {
${tokenVariables.map(variable => '' + variable).join('\n ')}
}`;

await fs.ensureDir(SCSS_DIR);
Expand All @@ -97,18 +85,6 @@ async function build() {
prettier.format(mixins, prettierOptions)
);

const tokensFile = `${GENERATED_COMMENT}
@import './mixins';
@include token-values();
`;

await fs.writeFile(
TOKEN_ENTRYPOINT,
prettier.format(tokensFile, prettierOptions)
);

const colorsFile = `${GENERATED_COMMENT}
@import './mixins';
Expand All @@ -121,21 +97,6 @@ async function build() {
prettier.format(colorsFile, prettierOptions)
);

const variables = [...colorVariables, ...tokenVariables];
const bundleFile = `${GENERATED_COMMENT}
:root {
${variables
.map(({ unprefixed, value }) => `--${unprefixed}: ${value};`)
.join('\n ')}
}
`;

await fs.writeFile(
BUNDLE_ENTRYPOINT,
prettier.format(bundleFile, prettierOptions)
);

reporter.success('Done! 🎉');
}

Expand Down
1 change: 1 addition & 0 deletions packages/layout/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ If you're looking for more examples on how to use `@carbon/layout`, we
have some examples that you can check out:

- [Basic](./examples/basic)
- [Styled-components](./examples/styled-components)

## 🙌 Contributing

Expand Down
1 change: 1 addition & 0 deletions packages/themes/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
scss
30 changes: 30 additions & 0 deletions packages/themes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# @carbon/themes

> Themes for applying color in the Carbon Design System
## Getting started

To install `@carbon/themes` in your project, you will need to run the
following command using [npm](https://www.npmjs.com/):

```bash
npm install -S @carbon/themes
```

If you prefer [Yarn](https://yarnpkg.com/en/), use the following
command instead:

```bash
yarn add @carbon/themes
```

## 🙌 Contributing

We're always looking for contributors to help us fix bugs, build new
features, or help us improve the project documentation. If you're
interested, definitely check out our [Contributing Guide](/.github/CONTRIBUTING.md)
! 👀

## 📝 License

Licensed under the [Apache 2.0 License](/LICENSE).
1 change: 1 addition & 0 deletions packages/themes/examples/.yarnrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
yarn-offline-mirror false
31 changes: 31 additions & 0 deletions packages/themes/examples/preview/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Preview</title>
<link href="https://fonts.googleapis.com/css?family=IBM+Plex+Mono" rel="stylesheet">
<link rel="stylesheet" href="./styles.scss">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="./styles.scss" rel="stylesheet">
<style>
*,
*::before,
*::after {
box-sizing: border-box;
}

body {
margin: 0;
padding: 0;
}

body, code {
font-family: 'IBM Plex Mono', monospace;
}
</style>
</head>
<body>
<div id="root"></div>
<script src="./index.js"></script>
</body>
</html>
78 changes: 78 additions & 0 deletions packages/themes/examples/preview/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* Copyright IBM Corp. 2018, 2018
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

import React from 'react';
import ReactDOM from 'react-dom';
import * as colors from '@carbon/colors';
import { themes } from '@carbon/themes';

const mountNode = document.getElementById('root');
function render(element) {
ReactDOM.render(element, mountNode);
}

const colorNameLookup = Object.keys(colors).reduce(
(acc, color) => ({
...acc,
[colors[color]]: color,
}),
{}
);
const tokens = Object.keys(themes[Object.keys(themes)[0]]);

function App() {
return (
<React.Fragment>
<h1>Preview</h1>
<section>
<h2>Themes</h2>
<table>
<thead>
<tr>
<th>Token</th>
{Object.keys(themes).map(theme => (
<th key={theme}>{theme} theme</th>
))}
</tr>
</thead>
<tbody>
{tokens.map(token => (
<tr key={token}>
<td>
<pre>
<code>{token}</code>
</pre>
</td>
{Object.keys(themes).map(theme => {
const color = themes[theme][token];
return (
<td key={theme}>
<div style={{ display: 'flex', alignItems: 'center' }}>
<div
style={{
width: 50,
height: 50,
background: color,
outline: '1px solid #8a3ffc',
marginRight: '1rem',
}}
/>
{colorNameLookup[color] || color}
</div>
</td>
);
})}
</tr>
))}
</tbody>
</table>
</section>
</React.Fragment>
);
}

render(<App />);
Loading

0 comments on commit fb19540

Please sign in to comment.