|
| 1 | +--- |
| 2 | +sidebar_position: 3 |
| 3 | +--- |
| 4 | + |
| 5 | +# Getting Started |
| 6 | + |
| 7 | +This section will guide you through getting started with `design-token-library`. |
| 8 | + |
| 9 | +> The following instructions assume TypeScript. If you're using JavaScript, skip the steps related to TypeScript and remove typings from code-samples where necessary. |
| 10 | +
|
| 11 | +## Library Interface |
| 12 | + |
| 13 | +The first step is to define the interface for the design token library. This is done using `DesignToken`. |
| 14 | + |
| 15 | +First, import `DesignToken` |
| 16 | + |
| 17 | +```ts |
| 18 | +import { type DesignToken } from "design-token-library"; |
| 19 | +``` |
| 20 | + |
| 21 | +Next, define the hierarchy of the library. This hierarchy is arbitrary, so choose whichever hierarchy works for you. Hierarchies can also have arbitrary levels of nesting, and can be divided into sub-interfaces if desired. |
| 22 | + |
| 23 | +> For a complete list of available token-types, see [here](/design-token-library/api-reference/design-token-library.designtoken#type-aliases). |
| 24 | +
|
| 25 | +```ts |
| 26 | +export interface IMyLibrary { |
| 27 | + colors: { |
| 28 | + foreground: DesignToken.Color; |
| 29 | + background: DesignToken.Color; |
| 30 | + }; |
| 31 | + typography: { |
| 32 | + base: DesignToken.FontFamily; |
| 33 | + body: DesignToken.Typography; |
| 34 | + }; |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +## Library Config |
| 39 | + |
| 40 | +With the Library interface defined, the concrete library config can be constructed. This config will serve as the initial configuration of the library. |
| 41 | + |
| 42 | +```ts |
| 43 | +import { Library } from "design-token-library"; |
| 44 | + |
| 45 | +const myLibraryConfig: Library.Config<IMyLibrary> = { |
| 46 | + colors: { |
| 47 | + foreground: { |
| 48 | + type: DesignToken.Type.Color, |
| 49 | + value: "#010101", |
| 50 | + }, |
| 51 | + background: { |
| 52 | + type: DesignToken.Type.Color, |
| 53 | + value: "#FEFEFE", |
| 54 | + }, |
| 55 | + }, |
| 56 | + typography: { |
| 57 | + fonts: { |
| 58 | + type: DesignToken.Type.FontFamily, |
| 59 | + value: "Helvetica" |
| 60 | + }, |
| 61 | + body: { |
| 62 | + type: DesignToken.Type.Typography |
| 63 | + value: { |
| 64 | + fontFamily: (tokens) => tokens.typography.fonts.base, // Alias |
| 65 | + fontSize: "14px", |
| 66 | + fontWeight: 400; |
| 67 | + letterSpacing: "0px"; |
| 68 | + lineHeight: "18px"; |
| 69 | + } |
| 70 | + }, |
| 71 | + } |
| 72 | +}; |
| 73 | +``` |
| 74 | + |
| 75 | +### Tokens |
| 76 | + |
| 77 | +Each token in the library must have a `value` property, and groups must not have a value. Tokens can be assigned three types of values: **static**, **alias**, and **computed**. |
| 78 | + |
| 79 | +```ts |
| 80 | +interface Colors { |
| 81 | + static: DesignToken.Color; |
| 82 | + alias: DesignToken.Color; |
| 83 | + computed: DesignToken.Color; |
| 84 | +} |
| 85 | + |
| 86 | +const config: Library.Config<Colors> = { |
| 87 | + static: { |
| 88 | + type: DesignToken.Type.Color, |
| 89 | + value: "#FFFFFF", |
| 90 | + }, |
| 91 | + alias: { |
| 92 | + type: DesignToken.Type.Color, |
| 93 | + value: (tokens) => tokens.static, // alias to the 'static' token |
| 94 | + }, |
| 95 | + computed: { |
| 96 | + type: DesignToken.Type.Color, |
| 97 | + // Operate on the value of the 'alias' token |
| 98 | + value: (tokens) => darken(tokens.alias.value, 0.3), |
| 99 | + }, |
| 100 | +}; |
| 101 | +``` |
| 102 | + |
| 103 | +### Groups |
| 104 | + |
| 105 | +In alignment with the [DTCG Group](https://design-tokens.github.io/community-group/format/#type-1) specification, token groups _may_ define a `type` field. All tokens part of the group will infer their type from the group unless they define their own type. |
| 106 | + |
| 107 | +## Creating a Library |
| 108 | + |
| 109 | +With the configuration defined, the library can be created. The purpose of the library is to enable changes to token values, notify subscribers to changes, and reconciling alias and computed values with those changes. |
| 110 | + |
| 111 | +```ts |
| 112 | +const library = Library.create(myLibraryConfig); |
| 113 | +``` |
| 114 | + |
| 115 | +### Reading Token Values |
| 116 | + |
| 117 | +The value of a token can easily be read: |
| 118 | + |
| 119 | +```ts |
| 120 | +const value = library.tokens.colors.foreground.value; |
| 121 | +``` |
| 122 | + |
| 123 | +### Setting Token Values |
| 124 | + |
| 125 | +The value of a token can be set via the `.set()` method: |
| 126 | + |
| 127 | +```ts |
| 128 | +library.tokens.colors.foreground.set("#EEEEEE"); |
| 129 | +``` |
| 130 | + |
| 131 | +### Subscribing to Changes |
| 132 | + |
| 133 | +```ts |
| 134 | +const subscriber: Library.Subscriber<IMyLibrary> = { |
| 135 | + onChange: (tokens) => { |
| 136 | + tokens.forEach((token) => { |
| 137 | + /* do something with tokens */ |
| 138 | + }); |
| 139 | + }, |
| 140 | +}; |
| 141 | +library.subscribe(subscriber); |
| 142 | + |
| 143 | +// Will notify subscribers |
| 144 | +library.tokens.foreground.set("#878787"); |
| 145 | +``` |
| 146 | + |
| 147 | +Change notifications are batched and subscribers get notified each microtask. It's important to note that token values are lazily evaluated. If a computed or alias token has not been accessed, it will notify itself to subscribers even if it's dependencies change: |
| 148 | + |
| 149 | +```ts |
| 150 | +const library = Library.create({ |
| 151 | + a: { type: DesignToken.Type.Color, value: "#000000" }, |
| 152 | + b: { type: DesignToken.Type.Color, value: (tokens) => tokens.a }, |
| 153 | +}); |
| 154 | + |
| 155 | +library.subscribe({ |
| 156 | + onChange(tokens) { |
| 157 | + /* ... */ |
| 158 | + }, |
| 159 | +}); |
| 160 | + |
| 161 | +// Will only notify 'library.tokens.a' |
| 162 | +library.tokens.a.set("#FFFFFF"); |
| 163 | + |
| 164 | +const b = library.tokens.b.value; |
| 165 | + |
| 166 | +// Will now notify with 'library.tokens.a' and 'library.tokens.b' |
| 167 | +library.tokens.a.set("#111111"); |
| 168 | +``` |
0 commit comments