Skip to content

Commit

Permalink
Add base feature branch for the style dictionary experiment work (#1)
Browse files Browse the repository at this point in the history
* Bug 1850611 - Create a JSON file source of truth for our design tokens

Introducing a JSON file that holds our design tokens source of truth in an agnostic format.
The plan is to generate desktop and mobile tokens from this file.
We will be exploring a build process as a next step, potentially relying on Style Dictionary.
This is the format we're playing with for now, but not set in stone.

Differential Revision: https://phabricator.services.mozilla.com/D189153

Depends on D194165

* working example of style dictionary with very small amount of design tokens

Differential Revision: https://phabricator.services.mozilla.com/D198707

* Reduced JSON design tokens to get Style Dictionary to build

Note: We still need to determine how to get "prefers-contrast" and
"light-dark" transforms to work. We should also determine how to get
aliases to work as expected so that tokens that reference other tokens
use CSS variables instead of raw values.

Differential Revision: https://phabricator.services.mozilla.com/D198702

---------

Co-authored-by: Jules Simplicio <jsimplicio@mozilla.com>
  • Loading branch information
TGiles and Jules Simplicio committed Mar 5, 2024
1 parent 6b0b8c1 commit c2d26cd
Show file tree
Hide file tree
Showing 7 changed files with 1,839 additions and 1 deletion.
181 changes: 181 additions & 0 deletions toolkit/themes/shared/design-system/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
# Basic Style Dictionary

This example code is bare-bones to show you what this framework can do. If you have the style-dictionary module installed globally, you can `cd` into this directory and run:
```bash
style-dictionary build
```

You should see something like this output:
```
Copying starter files...
Source style dictionary starter files created!
Running `style-dictionary build` for the first time to generate build artifacts.
scss
✔︎ build/scss/_variables.scss
android
✔︎ build/android/font_dimens.xml
✔︎ build/android/colors.xml
compose
✔︎ build/compose/StyleDictionaryColor.kt
✔︎ build/compose/StyleDictionarySize.kt
ios
✔︎ build/ios/StyleDictionaryColor.h
✔︎ build/ios/StyleDictionaryColor.m
✔︎ build/ios/StyleDictionarySize.h
✔︎ build/ios/StyleDictionarySize.m
ios-swift
✔︎ build/ios-swift/StyleDictionary.swift
ios-swift-separate-enums
✔︎ build/ios-swift/StyleDictionaryColor.swift
✔︎ build/ios-swift/StyleDictionarySize.swift
```

Good for you! You have now built your first style dictionary! Moving on, take a look at what we have built. This should have created a build directory and it should look like this:
```
├── README.md
├── config.json
├── tokens/
│ ├── color/
│ ├── base.json
│ ├── font.json
│ ├── size/
│ ├── font.json
├── build/
│ ├── android/
│ ├── font_dimens.xml
│ ├── colors.xml
│ ├── compose/
│ ├── StyleDictionaryColor.kt
│ ├── StyleDictionarySize.kt
│ ├── scss/
│ ├── _variables.scss
│ ├── ios/
│ ├── StyleDictionaryColor.h
│ ├── StyleDictionaryColor.m
│ ├── StyleDictionarySize.h
│ ├── StyleDictionarySize.m
│ ├── ios-swift/
│ ├── StyleDictionary.swift
│ ├── StyleDictionaryColor.swift
│ ├── StyleDictionarySize.swift
```

If you open `config.json` you will see there are 5 platforms defined: scss, android, compose, ios, and ios-swift. Each platform has a transformGroup, buildPath, and files. The buildPath and files of the platform should match up to the files what were built. The files built should look like these:

**Android**
```xml
<!-- font_dimens.xml -->
<resources>
<dimen name="size_font_small">12.00sp</dimen>
<dimen name="size_font_medium">16.00sp</dimen>
<dimen name="size_font_large">32.00sp</dimen>
<dimen name="size_font_base">16.00sp</dimen>
</resources>

<!-- colors.xml -->
<resources>
<color name="color_base_gray_light">#ffcccccc</color>
<color name="color_base_gray_medium">#ff999999</color>
<color name="color_base_gray_dark">#ff111111</color>
<color name="color_base_red">#ffff0000</color>
<color name="color_base_green">#ff00ff00</color>
<color name="color_font_base">#ffff0000</color>
<color name="color_font_secondary">#ff00ff00</color>
<color name="color_font_tertiary">#ffcccccc</color>
</resources>
```

**Compose**
```kotlin
object StyleDictionaryColor {
val colorBaseGrayDark = Color(0xff111111)
val colorBaseGrayLight = Color(0xffcccccc)
val colorBaseGrayMedium = Color(0xff999999)
val colorBaseGreen = Color(0xff00ff00)
val colorBaseRed = Color(0xffff0000)
val colorFontBase = Color(0xffff0000)
val colorFontSecondary = Color(0xff00ff00)
val colorFontTertiary = Color(0xffcccccc)
}

object StyleDictionarySize {
/** the base size of the font */
val sizeFontBase = 16.00.sp
/** the large size of the font */
val sizeFontLarge = 32.00.sp
/** the medium size of the font */
val sizeFontMedium = 16.00.sp
/** the small size of the font */
val sizeFontSmall = 12.00.sp
}
```

**SCSS**
```scss
// variables.scss
$color-base-gray-light: #cccccc;
$color-base-gray-medium: #999999;
$color-base-gray-dark: #111111;
$color-base-red: #ff0000;
$color-base-green: #00ff00;
$color-font-base: #ff0000;
$color-font-secondary: #00ff00;
$color-font-tertiary: #cccccc;
$size-font-small: 0.75rem;
$size-font-medium: 1rem;
$size-font-large: 2rem;
$size-font-base: 1rem;
```

**iOS**
```objc
#import "StyleDictionaryColor.h"

@implementation StyleDictionaryColor

+ (UIColor *)color:(StyleDictionaryColorName)colorEnum{
return [[self values] objectAtIndex:colorEnum];
}

+ (NSArray *)values {
static NSArray* colorArray;
static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{
colorArray = @[
[UIColor colorWithRed:0.800f green:0.800f blue:0.800f alpha:1.000f],
[UIColor colorWithRed:0.600f green:0.600f blue:0.600f alpha:1.000f],
[UIColor colorWithRed:0.067f green:0.067f blue:0.067f alpha:1.000f],
[UIColor colorWithRed:1.000f green:0.000f blue:0.000f alpha:1.000f],
[UIColor colorWithRed:0.000f green:1.000f blue:0.000f alpha:1.000f],
[UIColor colorWithRed:1.000f green:0.000f blue:0.000f alpha:1.000f],
[UIColor colorWithRed:0.000f green:1.000f blue:0.000f alpha:1.000f],
[UIColor colorWithRed:0.800f green:0.800f blue:0.800f alpha:1.000f]
];
});

return colorArray;
}

@end
```
Pretty nifty! This shows a few things happening:
1. The build system does a deep merge of all the token JSON files defined in the `source` attribute of `config.json`. This allows you to split up the token JSON files however you want. There are 2 JSON files with `color` as the top level key, but they get merged properly.
1. The build system resolves references to other design tokens. `{size.font.medium.value}` gets resolved properly.
1. The build system handles references to token values in other files as well as you can see in `tokens/color/font.json`.
Now let's make a change and see how that affects things. Open up `tokens/color/base.json` and change `"#111111"` to `"#000000"`. After you make that change, save the file and re-run the build command `style-dictionary build`. Open up the build files and take a look.
**Huzzah!**
Now go forth and create! Take a look at all the built-in [transforms](https://amzn.github.io/style-dictionary/#/transforms?id=pre-defined-transforms) and [formats](https://amzn.github.io/style-dictionary/#/formats?id=pre-defined-formats).
51 changes: 51 additions & 0 deletions toolkit/themes/shared/design-system/build/css/variables.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Do not edit directly
* Generated on Fri, 12 Jan 2024 19:25:11 GMT
*/

:root {
--color-background-critical-light: #ffe8e8;
--color-background-critical-dark: #690f22;
--color-background-information-light: #deeafc;
--color-background-information-dark: #003070;
--color-background-success-light: #d8eedc;
--color-background-success-dark: #5a3100;
--color-background-warning-light: #ffebcd;
--color-background-warning-dark: #003070;
--color-black-a10: rgba(0, 0, 0, 0.1);
--color-blue-30: #73a7f3;
--color-blue-50: #0060df;
--color-blue-60: #0250bb;
--color-blue-70: #054096;
--color-blue-80: #003070;
--color-blue-05: #deeafc;
--color-cyan-20: #aaf2ff;
--color-cyan-30: #aaf2ff;
--color-cyan-50: #00ddff;
--color-gray-50: #bfbfc9;
--color-gray-60: #8f8f9d;
--color-gray-70: #5b5b66;
--color-gray-80: #23222b;
--color-gray-90: #1c1b22;
--color-gray-100: #15141a;
--color-gray-05: #fbfbfe;
--color-green-30: #4dbc87;
--color-green-50: #017a40;
--color-green-80: #004725;
--color-green-05: #d8eedc;
--color-red-30: #f37f98;
--color-red-50: #d7264c;
--color-red-80: #690f22;
--color-red-05: #ffe8e8;
--color-yellow-30: #e49c49;
--color-yellow-50: #cd411e;
--color-yellow-80: #5a3100;
--color-yellow-05: #ffebcd;
--text-color-prefers-contrast: CanvasText;
--text-color-deemphasized-prefers-contrast: inherit;
--text-color-brand-light: #15141a;
--text-color-brand-dark: #fbfbfe;
--text-color-brand-deemphasized-light: color-mix(in srgb, currentColor 69%, transparent);
--text-color-brand-deemphasized-dark: color-mix(in srgb, currentColor 75%, transparent);
--platform: currentColor;
}
13 changes: 13 additions & 0 deletions toolkit/themes/shared/design-system/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"source": ["design-tokens.json"],
"platforms": {
"css": {
"transformGroup": "css",
"buildPath": "build/css/",
"files": [{
"destination": "variables.css",
"format": "css/variables"
}]
}
}
}
Loading

0 comments on commit c2d26cd

Please sign in to comment.