Skip to content

Commit 2ea304f

Browse files
authored
Merge pull request #3 from nicholasrice/users/nickrice/update-docs
chore: adding docs
2 parents 943d411 + a254cae commit 2ea304f

File tree

3 files changed

+187
-0
lines changed

3 files changed

+187
-0
lines changed

doc-site/docs/getting-started.md

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
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+
```

doc-site/docs/intro.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,21 @@ sidebar_position: 1
44
---
55

66
# Intro
7+
8+
`design-token-library` is a TypeScript library for managing Design Tokens. It's interface is based off of the [Design Token Community Group (DTCG)](https://www.designtokens.org/), and aims to be a lightweight, type-safe, polymorphic library that can be used in any JavaScript runtime.
9+
10+
`design-token-library` supports:
11+
12+
1. Design token aliasing
13+
2. Computed values
14+
3. Lazy value evaluation
15+
4. Change subscription
16+
5. Library extension
17+
18+
## Installation
19+
20+
Install using your package manager of choice from the NPM registry.
21+
22+
```
23+
npm install design-token-library
24+
```

doc-site/sidebars.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const sidebars: SidebarsConfig = {
1616
// But you can create a sidebar manually
1717
tutorialSidebar: [
1818
"intro",
19+
"getting-started",
1920
// {
2021
// type: "category",
2122
// label: "API Reference",

0 commit comments

Comments
 (0)