|
| 1 | +# Basic Style Dictionary |
| 2 | + |
| 3 | +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: |
| 4 | +```bash |
| 5 | +style-dictionary build |
| 6 | +``` |
| 7 | + |
| 8 | +You should see something like this output: |
| 9 | +``` |
| 10 | +Copying starter files... |
| 11 | +
|
| 12 | +Source style dictionary starter files created! |
| 13 | +
|
| 14 | +Running `style-dictionary build` for the first time to generate build artifacts. |
| 15 | +
|
| 16 | +
|
| 17 | +scss |
| 18 | +✔︎ build/scss/_variables.scss |
| 19 | +
|
| 20 | +android |
| 21 | +✔︎ build/android/font_dimens.xml |
| 22 | +✔︎ build/android/colors.xml |
| 23 | +
|
| 24 | +compose |
| 25 | +✔︎ build/compose/StyleDictionaryColor.kt |
| 26 | +✔︎ build/compose/StyleDictionarySize.kt |
| 27 | +
|
| 28 | +ios |
| 29 | +✔︎ build/ios/StyleDictionaryColor.h |
| 30 | +✔︎ build/ios/StyleDictionaryColor.m |
| 31 | +✔︎ build/ios/StyleDictionarySize.h |
| 32 | +✔︎ build/ios/StyleDictionarySize.m |
| 33 | +
|
| 34 | +ios-swift |
| 35 | +✔︎ build/ios-swift/StyleDictionary.swift |
| 36 | +
|
| 37 | +ios-swift-separate-enums |
| 38 | +✔︎ build/ios-swift/StyleDictionaryColor.swift |
| 39 | +✔︎ build/ios-swift/StyleDictionarySize.swift |
| 40 | +``` |
| 41 | + |
| 42 | +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: |
| 43 | +``` |
| 44 | +├── README.md |
| 45 | +├── config.json |
| 46 | +├── tokens/ |
| 47 | +│ ├── color/ |
| 48 | +│ ├── base.json |
| 49 | +│ ├── font.json |
| 50 | +│ ├── size/ |
| 51 | +│ ├── font.json |
| 52 | +├── build/ |
| 53 | +│ ├── android/ |
| 54 | +│ ├── font_dimens.xml |
| 55 | +│ ├── colors.xml |
| 56 | +│ ├── compose/ |
| 57 | +│ ├── StyleDictionaryColor.kt |
| 58 | +│ ├── StyleDictionarySize.kt |
| 59 | +│ ├── scss/ |
| 60 | +│ ├── _variables.scss |
| 61 | +│ ├── ios/ |
| 62 | +│ ├── StyleDictionaryColor.h |
| 63 | +│ ├── StyleDictionaryColor.m |
| 64 | +│ ├── StyleDictionarySize.h |
| 65 | +│ ├── StyleDictionarySize.m |
| 66 | +│ ├── ios-swift/ |
| 67 | +│ ├── StyleDictionary.swift |
| 68 | +│ ├── StyleDictionaryColor.swift |
| 69 | +│ ├── StyleDictionarySize.swift |
| 70 | +``` |
| 71 | + |
| 72 | +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: |
| 73 | + |
| 74 | +**Android** |
| 75 | +```xml |
| 76 | +<!-- font_dimens.xml --> |
| 77 | +<resources> |
| 78 | + <dimen name="size_font_small">12.00sp</dimen> |
| 79 | + <dimen name="size_font_medium">16.00sp</dimen> |
| 80 | + <dimen name="size_font_large">32.00sp</dimen> |
| 81 | + <dimen name="size_font_base">16.00sp</dimen> |
| 82 | +</resources> |
| 83 | + |
| 84 | +<!-- colors.xml --> |
| 85 | +<resources> |
| 86 | + <color name="color_base_gray_light">#ffcccccc</color> |
| 87 | + <color name="color_base_gray_medium">#ff999999</color> |
| 88 | + <color name="color_base_gray_dark">#ff111111</color> |
| 89 | + <color name="color_base_red">#ffff0000</color> |
| 90 | + <color name="color_base_green">#ff00ff00</color> |
| 91 | + <color name="color_font_base">#ffff0000</color> |
| 92 | + <color name="color_font_secondary">#ff00ff00</color> |
| 93 | + <color name="color_font_tertiary">#ffcccccc</color> |
| 94 | +</resources> |
| 95 | +``` |
| 96 | + |
| 97 | +**Compose** |
| 98 | +```kotlin |
| 99 | +object StyleDictionaryColor { |
| 100 | + val colorBaseGrayDark = Color(0xff111111) |
| 101 | + val colorBaseGrayLight = Color(0xffcccccc) |
| 102 | + val colorBaseGrayMedium = Color(0xff999999) |
| 103 | + val colorBaseGreen = Color(0xff00ff00) |
| 104 | + val colorBaseRed = Color(0xffff0000) |
| 105 | + val colorFontBase = Color(0xffff0000) |
| 106 | + val colorFontSecondary = Color(0xff00ff00) |
| 107 | + val colorFontTertiary = Color(0xffcccccc) |
| 108 | +} |
| 109 | + |
| 110 | +object StyleDictionarySize { |
| 111 | + /** the base size of the font */ |
| 112 | + val sizeFontBase = 16.00.sp |
| 113 | + /** the large size of the font */ |
| 114 | + val sizeFontLarge = 32.00.sp |
| 115 | + /** the medium size of the font */ |
| 116 | + val sizeFontMedium = 16.00.sp |
| 117 | + /** the small size of the font */ |
| 118 | + val sizeFontSmall = 12.00.sp |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | +**SCSS** |
| 123 | +```scss |
| 124 | +// variables.scss |
| 125 | +$color-base-gray-light: #cccccc; |
| 126 | +$color-base-gray-medium: #999999; |
| 127 | +$color-base-gray-dark: #111111; |
| 128 | +$color-base-red: #ff0000; |
| 129 | +$color-base-green: #00ff00; |
| 130 | +$color-font-base: #ff0000; |
| 131 | +$color-font-secondary: #00ff00; |
| 132 | +$color-font-tertiary: #cccccc; |
| 133 | +$size-font-small: 0.75rem; |
| 134 | +$size-font-medium: 1rem; |
| 135 | +$size-font-large: 2rem; |
| 136 | +$size-font-base: 1rem; |
| 137 | +``` |
| 138 | + |
| 139 | +**iOS** |
| 140 | +```objc |
| 141 | +#import "StyleDictionaryColor.h" |
| 142 | + |
| 143 | +@implementation StyleDictionaryColor |
| 144 | + |
| 145 | ++ (UIColor *)color:(StyleDictionaryColorName)colorEnum{ |
| 146 | + return [[self values] objectAtIndex:colorEnum]; |
| 147 | +} |
| 148 | + |
| 149 | ++ (NSArray *)values { |
| 150 | + static NSArray* colorArray; |
| 151 | + static dispatch_once_t onceToken; |
| 152 | + |
| 153 | + dispatch_once(&onceToken, ^{ |
| 154 | + colorArray = @[ |
| 155 | +[UIColor colorWithRed:0.800f green:0.800f blue:0.800f alpha:1.000f], |
| 156 | +[UIColor colorWithRed:0.600f green:0.600f blue:0.600f alpha:1.000f], |
| 157 | +[UIColor colorWithRed:0.067f green:0.067f blue:0.067f alpha:1.000f], |
| 158 | +[UIColor colorWithRed:1.000f green:0.000f blue:0.000f alpha:1.000f], |
| 159 | +[UIColor colorWithRed:0.000f green:1.000f blue:0.000f alpha:1.000f], |
| 160 | +[UIColor colorWithRed:1.000f green:0.000f blue:0.000f alpha:1.000f], |
| 161 | +[UIColor colorWithRed:0.000f green:1.000f blue:0.000f alpha:1.000f], |
| 162 | +[UIColor colorWithRed:0.800f green:0.800f blue:0.800f alpha:1.000f] |
| 163 | + ]; |
| 164 | + }); |
| 165 | + |
| 166 | + return colorArray; |
| 167 | +} |
| 168 | + |
| 169 | +@end |
| 170 | +``` |
| 171 | +
|
| 172 | +Pretty nifty! This shows a few things happening: |
| 173 | +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. |
| 174 | +1. The build system resolves references to other design tokens. `{size.font.medium.value}` gets resolved properly. |
| 175 | +1. The build system handles references to token values in other files as well as you can see in `tokens/color/font.json`. |
| 176 | +
|
| 177 | +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. |
| 178 | +
|
| 179 | +**Huzzah!** |
| 180 | +
|
| 181 | +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). |
0 commit comments