|
| 1 | +/** |
| 2 | + * @typedef {typeof colorPaths} ColorPaths - All the valid paths for the tokens of type color. |
| 3 | + * To use this type you can do: `@type {import('path/to/myTokensFile').ColorPaths}` |
| 4 | + */ |
| 5 | +export const colorPaths = /** @type {const} */ [ |
| 6 | + "primitive.color.black", |
| 7 | + "primitive.color.blue.0", |
| 8 | + "primitive.color.blue.1", |
| 9 | + "primitive.color.blue.2", |
| 10 | + "primitive.color.blue.3", |
| 11 | + "primitive.color.blue.4", |
| 12 | + "primitive.color.blue.5", |
| 13 | + "primitive.color.blue.6", |
| 14 | + "primitive.color.blue.7", |
| 15 | + "primitive.color.blue.8", |
| 16 | + "primitive.color.blue.9", |
| 17 | + "primitive.color.gray.0", |
| 18 | + "primitive.color.gray.1", |
| 19 | + "primitive.color.gray.2", |
| 20 | + "primitive.color.gray.3", |
| 21 | + "primitive.color.gray.4", |
| 22 | + "primitive.color.gray.5", |
| 23 | + "primitive.color.gray.6", |
| 24 | + "primitive.color.gray.7", |
| 25 | + "primitive.color.gray.8", |
| 26 | + "primitive.color.gray.9", |
| 27 | + "primitive.color.green.0", |
| 28 | + "primitive.color.green.1", |
| 29 | + "primitive.color.green.2", |
| 30 | + "primitive.color.green.3", |
| 31 | + "primitive.color.green.4", |
| 32 | + "primitive.color.green.5", |
| 33 | + "primitive.color.green.6", |
| 34 | + "primitive.color.green.7", |
| 35 | + "primitive.color.green.8", |
| 36 | + "primitive.color.green.9", |
| 37 | + "primitive.color.orange.0", |
| 38 | + "primitive.color.orange.1", |
| 39 | + "primitive.color.orange.2", |
| 40 | + "primitive.color.orange.3", |
| 41 | + "primitive.color.orange.4", |
| 42 | + "primitive.color.orange.5", |
| 43 | + "primitive.color.orange.6", |
| 44 | + "primitive.color.orange.7", |
| 45 | + "primitive.color.orange.8", |
| 46 | + "primitive.color.orange.9", |
| 47 | + "primitive.color.red.0", |
| 48 | + "primitive.color.red.1", |
| 49 | + "primitive.color.red.2", |
| 50 | + "primitive.color.red.3", |
| 51 | + "primitive.color.red.4", |
| 52 | + "primitive.color.red.5", |
| 53 | + "primitive.color.red.6", |
| 54 | + "primitive.color.red.7", |
| 55 | + "primitive.color.red.8", |
| 56 | + "primitive.color.red.9", |
| 57 | + "primitive.color.white", |
| 58 | + "themedColor.surface.1", |
| 59 | + "themedColor.surface.2", |
| 60 | + "themedColor.surface.3", |
| 61 | + "themedColor.surface.error", |
| 62 | + "themedColor.surface.information", |
| 63 | + "themedColor.surface.success", |
| 64 | + "themedColor.surface.warning", |
| 65 | + "themedColor.text.error.default", |
| 66 | + "themedColor.text.error.hover", |
| 67 | + "themedColor.text.highEmphasis.active", |
| 68 | + "themedColor.text.highEmphasis.default", |
| 69 | + "themedColor.text.highEmphasis.hover", |
| 70 | + "themedColor.text.information.default", |
| 71 | + "themedColor.text.information.hover", |
| 72 | + "themedColor.text.lowEmphasis.default", |
| 73 | + "themedColor.text.mediumEmphasis.default", |
| 74 | + "themedColor.text.mediumEmphasis.hover", |
| 75 | + "themedColor.text.success.default", |
| 76 | + "themedColor.text.success.hover", |
| 77 | + "themedColor.text.warning.default", |
| 78 | + "themedColor.text.warning.hover", |
| 79 | +] as const; |
| 80 | +export type ColorPaths = typeof colorPaths; |
| 81 | + |
| 82 | +/** |
| 83 | + * @typedef {typeof themedColorPaths} ThemedcolorPaths - All the valid paths for the collection themedColor. |
| 84 | + * To use this type you can do: `@type {import('path/to/myTokensFile').ThemedcolorPaths}` |
| 85 | + */ |
| 86 | +export const themedColorPaths = /** @type {const} */ [ |
| 87 | + "themedColor.surface.1", |
| 88 | + "themedColor.surface.2", |
| 89 | + "themedColor.surface.3", |
| 90 | + "themedColor.surface.error", |
| 91 | + "themedColor.surface.information", |
| 92 | + "themedColor.surface.success", |
| 93 | + "themedColor.surface.warning", |
| 94 | + "themedColor.text.error.default", |
| 95 | + "themedColor.text.error.hover", |
| 96 | + "themedColor.text.highEmphasis.active", |
| 97 | + "themedColor.text.highEmphasis.default", |
| 98 | + "themedColor.text.highEmphasis.hover", |
| 99 | + "themedColor.text.information.default", |
| 100 | + "themedColor.text.information.hover", |
| 101 | + "themedColor.text.lowEmphasis.default", |
| 102 | + "themedColor.text.mediumEmphasis.default", |
| 103 | + "themedColor.text.mediumEmphasis.hover", |
| 104 | + "themedColor.text.success.default", |
| 105 | + "themedColor.text.success.hover", |
| 106 | + "themedColor.text.warning.default", |
| 107 | + "themedColor.text.warning.hover", |
| 108 | +] as const; |
| 109 | +export type ThemedcolorPaths = typeof themedColorPaths; |
| 110 | + |
| 111 | +/** |
| 112 | + * @typedef {ColorPaths | ThemedcolorPaths} AllPath - All possible paths |
| 113 | + */ |
| 114 | +export type AllPath = ColorPaths | ThemedcolorPaths; |
| 115 | + |
| 116 | +/** |
| 117 | + * @typedef {typeof pathsByType} PathsByType - All the paths for a given token type. Needed for `getTokensByType` |
| 118 | + */ |
| 119 | +const pathsByType = /** @type {const} */ { |
| 120 | + color: colorPaths, |
| 121 | +} as const; |
| 122 | +type PathsByType = typeof pathsByType; |
| 123 | + |
| 124 | +/** |
| 125 | + * @typedef {typeof colorModes[number]} ColorModes - All the valid modes of color. |
| 126 | + * To use this type you can do: `@type {import('path/to/myTokensFile').ColorModes}` |
| 127 | + */ |
| 128 | +export const colorModes = /** @type {const} */ ["dark", "light"] as const; |
| 129 | +export type ColorModes = (typeof colorModes)[number]; |
| 130 | + |
| 131 | +/** |
| 132 | + * @typedef {typeof themedColorModes[number]} ThemedcolorModes - All the valid modes of themedColor. |
| 133 | + * To use this type you can do: `@type {import('path/to/myTokensFile').ThemedcolorModes}` |
| 134 | + */ |
| 135 | +export const themedColorModes = /** @type {const} */ ["light", "dark"] as const; |
| 136 | +export type ThemedcolorModes = (typeof themedColorModes)[number]; |
| 137 | + |
| 138 | +/** |
| 139 | + * @typedef {ColorModes | ThemedcolorModes} AllMode - All the available modes |
| 140 | + */ |
| 141 | +export type AllMode = ColorModes | ThemedcolorModes; |
| 142 | + |
| 143 | +/** |
| 144 | + * @typedef {typeof tokens} Tokens - All the tokens. |
| 145 | + * Use `getTokenByMode` to retrieve one. |
| 146 | + */ |
| 147 | +export const tokens = /** @type {const} */ { |
| 148 | + "primitive.color.black": "#000000", |
| 149 | + "primitive.color.blue.0": "#e7f5ff", |
| 150 | + "primitive.color.blue.1": "#d0ebff", |
| 151 | + "primitive.color.blue.2": "#a5d8ff", |
| 152 | + "primitive.color.blue.3": "#74c0fc", |
| 153 | + "primitive.color.blue.4": "#4dabf7", |
| 154 | + "primitive.color.blue.5": "#339af0", |
| 155 | + "primitive.color.blue.6": "#228be6", |
| 156 | + "primitive.color.blue.7": "#1c7ed6", |
| 157 | + "primitive.color.blue.8": "#1971c2", |
| 158 | + "primitive.color.blue.9": "#1864ab", |
| 159 | + "primitive.color.gray.0": "#f8f9fa", |
| 160 | + "primitive.color.gray.1": "#f1f3f5", |
| 161 | + "primitive.color.gray.2": "#e9ecef", |
| 162 | + "primitive.color.gray.3": "#dee2e6", |
| 163 | + "primitive.color.gray.4": "#ced4da", |
| 164 | + "primitive.color.gray.5": "#adb5bd", |
| 165 | + "primitive.color.gray.6": "#868e96", |
| 166 | + "primitive.color.gray.7": "#495057", |
| 167 | + "primitive.color.gray.8": "#343a40", |
| 168 | + "primitive.color.gray.9": "#212529", |
| 169 | + "primitive.color.green.0": "#ebfbee", |
| 170 | + "primitive.color.green.1": "#d3f9d8", |
| 171 | + "primitive.color.green.2": "#b2f2bb", |
| 172 | + "primitive.color.green.3": "#8ce99a", |
| 173 | + "primitive.color.green.4": "#69db7c", |
| 174 | + "primitive.color.green.5": "#51cf66", |
| 175 | + "primitive.color.green.6": "#40c057", |
| 176 | + "primitive.color.green.7": "#37b24d", |
| 177 | + "primitive.color.green.8": "#2f9e44", |
| 178 | + "primitive.color.green.9": "#2b8a3e", |
| 179 | + "primitive.color.orange.0": "#fff4e6", |
| 180 | + "primitive.color.orange.1": "#ffe8cc", |
| 181 | + "primitive.color.orange.2": "#ffd8a8", |
| 182 | + "primitive.color.orange.3": "#ffc078", |
| 183 | + "primitive.color.orange.4": "#ffa94d", |
| 184 | + "primitive.color.orange.5": "#ff922b", |
| 185 | + "primitive.color.orange.6": "#fd7e14", |
| 186 | + "primitive.color.orange.7": "#f76707", |
| 187 | + "primitive.color.orange.8": "#e8590c", |
| 188 | + "primitive.color.orange.9": "#d9480f", |
| 189 | + "primitive.color.red.0": "#fff5f5", |
| 190 | + "primitive.color.red.1": "#ffe3e3", |
| 191 | + "primitive.color.red.2": "#ffc9c9", |
| 192 | + "primitive.color.red.3": "#ffa8a8", |
| 193 | + "primitive.color.red.4": "#ff8787", |
| 194 | + "primitive.color.red.5": "#ff6b6b", |
| 195 | + "primitive.color.red.6": "#fa5252", |
| 196 | + "primitive.color.red.7": "#f03e3e", |
| 197 | + "primitive.color.red.8": "#e03131", |
| 198 | + "primitive.color.red.9": "#c92a2a", |
| 199 | + "primitive.color.white": "#ffffff", |
| 200 | + "themedColor.surface.1": { dark: "#212529", light: "#f8f9fa" }, |
| 201 | + "themedColor.surface.2": { dark: "#343a40", light: "#f1f3f5" }, |
| 202 | + "themedColor.surface.3": { dark: "#495057", light: "#e9ecef" }, |
| 203 | + "themedColor.surface.error": { dark: "#f03e3e", light: "#ff8787" }, |
| 204 | + "themedColor.surface.information": { dark: "#1c7ed6", light: "#4dabf7" }, |
| 205 | + "themedColor.surface.success": { dark: "#37b24d", light: "#69db7c" }, |
| 206 | + "themedColor.surface.warning": { dark: "#f76707", light: "#ffa94d" }, |
| 207 | + "themedColor.text.error.default": { dark: "#fa5252", light: "#f03e3e" }, |
| 208 | + "themedColor.text.error.hover": { dark: "#ff6b6b", light: "#fa5252" }, |
| 209 | + "themedColor.text.highEmphasis.active": { dark: "#e9ecef", light: "#495057" }, |
| 210 | + "themedColor.text.highEmphasis.default": { |
| 211 | + dark: "#f8f9fa", |
| 212 | + light: "#212529", |
| 213 | + }, |
| 214 | + "themedColor.text.highEmphasis.hover": { dark: "#f1f3f5", light: "#343a40" }, |
| 215 | + "themedColor.text.information.default": { dark: "#228be6", light: "#1c7ed6" }, |
| 216 | + "themedColor.text.information.hover": { dark: "#339af0", light: "#228be6" }, |
| 217 | + "themedColor.text.lowEmphasis.default": { dark: "#ced4da", light: "#868e96" }, |
| 218 | + "themedColor.text.mediumEmphasis.default": { |
| 219 | + dark: "#dee2e6", |
| 220 | + light: "#495057", |
| 221 | + }, |
| 222 | + "themedColor.text.mediumEmphasis.hover": { |
| 223 | + dark: "#e9ecef", |
| 224 | + light: "#343a40", |
| 225 | + }, |
| 226 | + "themedColor.text.success.default": { dark: "#40c057", light: "#37b24d" }, |
| 227 | + "themedColor.text.success.hover": { dark: "#51cf66", light: "#40c057" }, |
| 228 | + "themedColor.text.warning.default": { dark: "#fd7e14", light: "#f76707" }, |
| 229 | + "themedColor.text.warning.hover": { dark: "#ff922b", light: "#fd7e14" }, |
| 230 | +} as const; |
| 231 | +type Tokens = typeof tokens; |
| 232 | + |
| 233 | +/** |
| 234 | +* Retrieve any token for a given mode. If available, the default mode will be: 'default' |
| 235 | +* @template {AllPath} Path - A generic extending all the possible paths |
| 236 | +* @template {Tokens[Path] extends Record<string, any> |
| 237 | + ? keyof Tokens[Path] |
| 238 | + : undefined} Mode - A generic representing all the valid modes for a given path |
| 239 | +* @template {Tokens[Path] extends Record<string, any> |
| 240 | + ? Tokens[Path][Mode extends undefined ? never : Mode] |
| 241 | + : Tokens[Path]} Return - The return type |
| 242 | +* @param {Path} path - The path to the token |
| 243 | +* @param {Mode} mode - The mode of the token you want to retrieve |
| 244 | +* @returns {Return} - The value of a token for a given mode |
| 245 | +*/ |
| 246 | +export function getTokenByMode< |
| 247 | + Path extends keyof Tokens, |
| 248 | + Mode extends Tokens[Path] extends Record<string, any> |
| 249 | + ? keyof Tokens[Path] |
| 250 | + : undefined, |
| 251 | + Return extends Tokens[Path] extends Record<string, any> |
| 252 | + ? Tokens[Path][Mode extends undefined ? never : Mode] |
| 253 | + : Tokens[Path], |
| 254 | +>(path: Path, mode: Mode): Return { |
| 255 | + if (!tokens[path]) { |
| 256 | + throw new Error( |
| 257 | + "Path: '" + |
| 258 | + path + |
| 259 | + "' doesn't exist. Here are all the valid paths:\n- " + |
| 260 | + Object.keys(tokens).join("\n- "), |
| 261 | + ); |
| 262 | + } |
| 263 | + |
| 264 | + if (typeof tokens[path] !== "object") { |
| 265 | + return tokens[path] as Return; |
| 266 | + } |
| 267 | + |
| 268 | + if (!mode) |
| 269 | + throw new Error( |
| 270 | + "Mode is undefined but it should be one of " + |
| 271 | + Object.keys(tokens[path]).join(", ") + |
| 272 | + " for path: " + |
| 273 | + path, |
| 274 | + ); |
| 275 | + |
| 276 | + if (!tokens[path][mode]) { |
| 277 | + throw new Error( |
| 278 | + "Invalid mode '" + |
| 279 | + mode.toString() + |
| 280 | + " at path '" + |
| 281 | + path + |
| 282 | + "', here are all the valid modes:\n- " + |
| 283 | + Object.keys(tokens[path]).join("\n- "), |
| 284 | + ); |
| 285 | + } |
| 286 | + |
| 287 | + return tokens[path][mode] as Return; |
| 288 | +} |
| 289 | + |
| 290 | +/** |
| 291 | + * Retrieve all the tokens for a specific type (color, dimension, etc...). |
| 292 | + * Note that the value will either be a string or an object if the token has modes |
| 293 | + * @template {keyof PathsByType} Type - A generic extending all the possible types |
| 294 | + * @template {Tokens[PathsByType[Type][number]]} Token - A generic representing a union of all the outputs |
| 295 | + * @param {Type} type - The path to the token |
| 296 | + * @returns {Array<Token>} - An array with all the values |
| 297 | + */ |
| 298 | +export function getTokensByType< |
| 299 | + Type extends keyof PathsByType, |
| 300 | + Token extends Tokens[PathsByType[Type][number]], |
| 301 | +>(type: Type): Array<Token> { |
| 302 | + if (!pathsByType[type]) { |
| 303 | + throw new Error("The type: '" + type + "' does not exist"); |
| 304 | + } |
| 305 | + |
| 306 | + return pathsByType[type].map((path) => tokens[path]) as Array<Token>; |
| 307 | +} |
0 commit comments