Skip to content

Commit f49dc7b

Browse files
authored
Allow setting of tokens values (#182)
This PR adds a new `set` method so you can update or add values to your tokens.
2 parents fc79a0b + 8e4c769 commit f49dc7b

File tree

6 files changed

+375
-287
lines changed

6 files changed

+375
-287
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "design-system-utils",
3-
"version": "1.4.0",
3+
"version": "1.5.0",
44
"description": "Design-system utils for working with JS-in-CSS",
55
"main": "build/cjs.js",
66
"module": "build/es.js",
@@ -49,7 +49,8 @@
4949
"author": "Zander Martineau",
5050
"license": "MIT",
5151
"dependencies": {
52-
"@ngard/tiny-get": "^1.0.1"
52+
"@ngard/tiny-get": "^1.0.1",
53+
"dset": "^2.0.1"
5354
},
5455
"devDependencies": {
5556
"@types/jest": "^24.0.9",

readme.md

Lines changed: 92 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,7 @@
99

1010
</h1>
1111

12-
Design System Utils is a micro framework that standardises your design-system tokens & provides helpful utility functions to access it’s information. It can be used with [styled-components](https://styled-components.com), [emotion](https://emotion.sh), [glamorous](https://glamorous.rocks/) or any other [CSS-in-JS framework](https://css-in-js-playground.com/).
13-
14-
<details>
15-
<summary>Table of contents</summary>
16-
17-
- [Install](#install)
18-
- [Usage](#usage)
19-
- [Setting up your design system](#setting-up-your-design-system)
20-
- [Initialise the design system framework](#initialise-the-design-system-framework)
21-
- [Accessing the design system data in your app](#accessing-the-design-system-data-in-your-app)
22-
- [Options](#options)
23-
- [API methods](#api-methods)
24-
- [`tokens.get()` - Get any value from the design tokens](#tokensget---get-any-value-from-the-design-tokens)
25-
- [`tokens.fontSize()` or `tokens.fs()` - Get font-size values](#tokensfontsize-or-tokensfs---get-font-size-values)
26-
- [Color palette](#color-palette)
27-
- [`tokens.color()` - Get color palette values](#tokenscolor---get-color-palette-values)
28-
- [`tokens.brand()` - Get brand palette values](#tokensbrand---get-brand-palette-values)
29-
- [`tokens.bp()` - Get responsive breakpoint values](#tokensbp---get-responsive-breakpoint-values)
30-
- [`tokens.z()` - Get `z-index` values](#tokensz---get-z-index-values)
31-
- [`tokens.spacing()` or `tokens.space()` - Get spacing values](#tokensspacing-or-tokensspace---get-spacing-values)
32-
- [Calculations](#calculations)
33-
- [Usage with Typescript](#usage-with-typescript)
34-
- [Demo & examples](#demo--examples)
35-
36-
</details>
12+
Design System Utils is a micro framework that standardises your design-system tokens & provides helpful utility functions to access the information. It can be used with [styled-components](https://styled-components.com), [emotion](https://emotion.sh), [glamorous](https://glamorous.rocks/) or any other [CSS-in-JS framework](https://css-in-js-playground.com/).
3713

3814
## Install
3915

@@ -50,7 +26,7 @@ npm install --save design-system-utils
5026
```
5127
$ size-limit
5228
53-
cjs/index.js
29+
build/cjs.js
5430
Package size: 814 B
5531
Size limit: 1 KB
5632
@@ -61,6 +37,45 @@ $ size-limit
6137
With all dependencies, minified and gzipped
6238
```
6339

40+
<details>
41+
<summary>🤓 Table of contents</summary>
42+
43+
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
44+
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
45+
46+
47+
- [Usage](#usage)
48+
- [Setup](#setup)
49+
- [Initialise the design system framework](#initialise-the-design-system-framework)
50+
- [Accessing the design system data in your app](#accessing-the-design-system-data-in-your%C2%A0app)
51+
- [Options](#options)
52+
- [Basic API methods](#basic-api-methods)
53+
- [`tokens.get()` - Get a token value](#tokensget---get-a-token-value)
54+
- [`tokens.set()` - Set a token value](#tokensset---set-a-token-value)
55+
- [API helper methods](#api-helper-methods)
56+
- [`tokens.fontSize()` or `tokens.fs()` - Get font-size values](#tokensfontsize-or-tokensfs---get-font-size-values)
57+
- [Modular scale](#modular-scale)
58+
- [Color palette](#color-palette)
59+
- [`tokens.color()` - Get color palette values](#tokenscolor---get-color-palette-values)
60+
- [`tokens.brand()` - Get brand palette values](#tokensbrand---get-brand-palette-values)
61+
- [`tokens.bp()` - Get responsive breakpoint values](#tokensbp---get-responsive-breakpoint-values)
62+
- [`tokens.z()` - Get `z-index` values](#tokensz---get-z-index-values)
63+
- [`tokens.spacing()` or `tokens.space()` - Get spacing values](#tokensspacing-or-tokensspace---get-spacing-values)
64+
- [Array example:](#array-example)
65+
- [Object example:](#object-example)
66+
- [Calculations](#calculations)
67+
- [`tokens.multiply()`](#tokensmultiply)
68+
- [`pxTo()`](#pxto)
69+
- [`toPx()`](#topx)
70+
- [`parseUnit()`](#parseunit)
71+
- [Usage with Typescript](#usage-with-typescript)
72+
- [Demo & examples](#demo--examples)
73+
- [Licence](#licence)
74+
75+
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
76+
77+
</details>
78+
6479
## Usage
6580

6681
First create your design system file, this contains all your design tokens that your app or site will use; things like font-sizes, color palette, spacing etc (kind of like a Sass/Less variables file).
@@ -84,7 +99,7 @@ const designTokens = {...}
8499
export default new DesignSystem(designTokens)
85100
```
86101

87-
### Setting up your design system
102+
### Setup
88103

89104
The "shape" and structure of your design tokens object **_can_** actually be anything you want, however, if you want to make use of the shortcut/helper methods like `tokens.fontSize|bp|z|color|brand|spacing` etc, there is a particular shape that your data will need to follow, see below:
90105

@@ -230,18 +245,44 @@ export default new DesignSystem(myDesignSystem, {
230245
})
231246
```
232247

233-
## API methods
248+
## Basic API methods
234249

235-
### `tokens.get()` - Get any value from the design tokens
250+
### `tokens.get()` - Get a token value
236251

237252
The `tokens.get()` function can be used to get any value from the design-system. Use object dot notation to find the value you need from your design system object.
238253

239254
```js
240255
// with the system setup, as above
241256
tokens.get('lineHeight.headings') // 1.1
257+
tokens.get('a.really.deeply.nested.value')
258+
```
259+
260+
### `tokens.set()` - Set a token value
261+
262+
The `tokens.set()` function can be used to set tokens values. This means you can overwrite existing items, or create new items that are specific to your application.
263+
264+
Like the `.get()` method, use object dot notation to find the value you need from your design system object.
265+
266+
This method uses [dset](https://github.com/lukeed/dset#usage) under the hood, so please read the docs there for more info.
267+
268+
```js
269+
// with the system setup, as above
270+
tokens.set('forms.inputBackground', '#fff')
271+
272+
// then use it later like so:
273+
tokens.get('forms.inputBackground')
242274
```
243275

244-
There are a few more helper methods to make finding certain values more simple.
276+
## API helper methods
277+
278+
The helper methods make getting values _much_ more simple.
279+
280+
- [`tokens.fontSize()` or `tokens.fs()`](#tokensfontsize-or-tokensfs---get-font-size-values) - Get font-size values
281+
- [`tokens.color()`](#tokenscolor---get-color-palette-values) - Get color palette values
282+
- [`tokens.brand()`](#tokensbrand---get-brand-palette-values) - Get brand palette values
283+
- [`tokens.bp()`](#tokensbp---get-responsive-breakpoint-values) - Get responsive breakpoint values
284+
- [`tokens.z()`](#tokensz---get-z-index-values) - Get `z-index` values
285+
- [`tokens.spacing()` or `tokens.space()`](#tokensspacing-or-tokensspace---get-spacing-values) - Get spacing values
245286

246287
### `tokens.fontSize()` or `tokens.fs()` - Get font-size values
247288

@@ -464,7 +505,27 @@ parseUnit('18px') // 'px'
464505

465506
## Usage with Typescript
466507

467-
Typescript types and interfaces should be imported as named imports. Below is an example where a new item (`baseline`) is added to the `spacing` object.
508+
Typescript types and interfaces should be imported as named imports.
509+
510+
See all the type definitions in the [types.ts](https://github.com/mrmartineau/design-system-utils/blob/master/src/types.ts) file. Here are all the exported types that can be extended:
511+
512+
```
513+
{
514+
System,
515+
SystemOptions,
516+
SystemBreakpoints,
517+
SystemZIndex,
518+
SystemFontSizes,
519+
SystemSpacing,
520+
SystemScale,
521+
SystemColorPalette,
522+
SystemBrandPalette,
523+
SystemType,
524+
SystemOptionalKey,
525+
}
526+
```
527+
528+
Below is an example where a new item (`baseline`) is added to the `spacing` object.
468529

469530
```ts
470531
import DesignSystem, { System, SystemOptions, SystemSpacing } from '../index'

0 commit comments

Comments
 (0)