@bamboocss/token-dictionary 1.11.1 → 1.11.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,235 @@
1
+ import { CssVar, CssVarOptions } from "@bamboocss/shared";
2
+ import { ColorPaletteOptions, SemanticTokens, ThemeVariantsMap, Token as Token$1, Tokens } from "@bamboocss/types";
3
+
4
+ //#region src/token.d.ts
5
+ interface ExtensionData {
6
+ category?: string;
7
+ references?: TokenReferences;
8
+ condition?: string;
9
+ conditions?: TokenConditions;
10
+ theme?: string;
11
+ }
12
+ interface TokenConditions {
13
+ [key: string]: string;
14
+ }
15
+ interface TokenReferences {
16
+ [key: string]: Token;
17
+ }
18
+ type TokenExtensions<T = {}> = ExtensionData & {
19
+ [key: string]: any;
20
+ } & T;
21
+ interface TokenInput {
22
+ name: string;
23
+ value: any;
24
+ type?: string;
25
+ path?: string[];
26
+ description?: string;
27
+ deprecated?: boolean | string;
28
+ extensions?: TokenExtensions;
29
+ }
30
+ /**
31
+ * Represents a design token in the dictionary
32
+ */
33
+ declare class Token {
34
+ name: string;
35
+ value: any;
36
+ originalValue: any;
37
+ path: string[];
38
+ type?: string;
39
+ description?: string;
40
+ deprecated?: boolean | string;
41
+ extensions: TokenExtensions;
42
+ constructor(input: TokenInput);
43
+ /**
44
+ * The unique identifier of the token.
45
+ */
46
+ get id(): string;
47
+ /**
48
+ * Whether the token is a conditional token.
49
+ * Conditional tokens are tokens that have multiple values based on a condition.
50
+ */
51
+ get isConditional(): boolean;
52
+ /**
53
+ * Whether the token has a reference in its value.
54
+ * e.g. {color.gray.100}
55
+ */
56
+ get hasReference(): boolean;
57
+ /**
58
+ * Whether the token is a complex or composite token.
59
+ */
60
+ get isComposite(): boolean;
61
+ /**
62
+ * Returns the token value with the references expanded.
63
+ * e.g. {color.gray.100} => var(--colors-gray-100)
64
+ *
65
+ */
66
+ expandReferences(): string;
67
+ /**
68
+ * Whether this token has a reference to another token
69
+ */
70
+ get isReference(): boolean;
71
+ /**
72
+ * Returns the list of references in the token value
73
+ */
74
+ get references(): string[];
75
+ clone(): Token;
76
+ /**
77
+ * Returns an array of tokens per conditions.
78
+ * It is commonly used in semantic tokens, and can have multiple values based on a condition.
79
+ * e.g. primary: { light: '#000', dark: '#fff' }
80
+ */
81
+ getConditionTokens(): Token[] | undefined;
82
+ /**
83
+ * Add more extensions to the token
84
+ */
85
+ setExtensions(extensions: TokenExtensions): this;
86
+ setType(): void;
87
+ }
88
+ declare const TOKEN_TYPES: {
89
+ readonly colors: "color";
90
+ readonly spacing: "dimension";
91
+ readonly sizes: "dimension";
92
+ readonly shadows: "shadow";
93
+ readonly fonts: "fontFamily";
94
+ readonly fontSizes: "fontSize";
95
+ readonly fontWeights: "fontWeight";
96
+ readonly letterSpacings: "letterSpacing";
97
+ readonly lineHeights: "lineHeight";
98
+ readonly durations: "duration";
99
+ readonly transitions: "transition";
100
+ readonly radii: "borderRadius";
101
+ readonly gradients: "gradient";
102
+ readonly easings: "cubicBezier";
103
+ readonly borders: "border";
104
+ readonly borderWidths: "borderWidth";
105
+ readonly zIndex: "zIndex";
106
+ readonly opacity: "opacity";
107
+ readonly blurs: "blur";
108
+ readonly aspectRatios: "aspectRatio";
109
+ readonly cursor: "cursor";
110
+ readonly assets: "asset";
111
+ };
112
+ type TokenCategory = keyof typeof TOKEN_TYPES;
113
+ declare const TOKEN_CATEGORIES: TokenCategory[];
114
+ //#endregion
115
+ //#region src/dictionary.d.ts
116
+ type EnforcePhase = 'pre' | 'post';
117
+ interface TokenTransformer {
118
+ name: string;
119
+ enforce?: EnforcePhase;
120
+ type?: 'value' | 'name' | 'extensions';
121
+ match?: (token: Token) => boolean;
122
+ transform: (token: Token, dictionary: TokenDictionary) => any;
123
+ }
124
+ interface TokenDictionaryOptions {
125
+ tokens?: Tokens;
126
+ semanticTokens?: SemanticTokens;
127
+ breakpoints?: Record<string, string>;
128
+ themes?: ThemeVariantsMap | undefined;
129
+ prefix?: string;
130
+ hash?: boolean;
131
+ colorPalette?: ColorPaletteOptions;
132
+ }
133
+ interface TokenMiddleware {
134
+ enforce?: EnforcePhase;
135
+ transform: (dict: TokenDictionary) => void;
136
+ }
137
+ declare class TokenDictionary {
138
+ private options;
139
+ allTokens: Token[];
140
+ byName: Map<string, Token>;
141
+ private deprecated;
142
+ constructor(options: TokenDictionaryOptions);
143
+ init(): this;
144
+ get prefix(): string | undefined;
145
+ get hash(): boolean | undefined;
146
+ get colorPalette(): ColorPaletteOptions | undefined;
147
+ getByName: (path: string) => Token | undefined;
148
+ formatTokenName: (path: string[]) => string;
149
+ formatCssVar: (path: string[], options: CssVarOptions) => CssVar;
150
+ registerTokens(): this;
151
+ registerToken: (token: Token, transformPhase?: "pre" | "post") => void;
152
+ private transforms;
153
+ registerTransform(...transforms: TokenTransformer[]): this;
154
+ private execTransform;
155
+ execTransformOnToken(transform: TokenTransformer, token: Token): void;
156
+ transformTokens(enforce: EnforcePhase): this;
157
+ private middlewares;
158
+ registerMiddleware(...middlewares: TokenMiddleware[]): this;
159
+ applyMiddlewares(enforce: EnforcePhase): void;
160
+ getReferences(value: string): Token[];
161
+ usesReference(value: any): boolean;
162
+ addReferences(): this;
163
+ filter(pattern: Partial<Token> | ((token: Token) => boolean)): Token[];
164
+ addConditionalTokens(): this;
165
+ expandTokenReferences(): this;
166
+ colorMix: (value: string, tokenFn: (path: string) => string) => {
167
+ invalid: boolean;
168
+ value: string;
169
+ color?: undefined;
170
+ } | {
171
+ invalid: boolean;
172
+ color: string;
173
+ value: string;
174
+ };
175
+ /**
176
+ * Expand token references to their CSS variable
177
+ */
178
+ expandReferenceInValue(value: string): string;
179
+ /**
180
+ * Get the value of a token reference
181
+ */
182
+ resolveReference(value: string): string;
183
+ /**
184
+ * Resolve token references to their actual raw value (recursively resolves references)
185
+ */
186
+ deepResolveReference(originalValue: string): string | undefined;
187
+ isDeprecated(name: string): boolean;
188
+ build(): this;
189
+ get isEmpty(): boolean;
190
+ view: ReturnType<TokenDictionaryView['getTokensView']>;
191
+ setComputedView(): this;
192
+ }
193
+ declare class TokenDictionaryView {
194
+ private dictionary;
195
+ constructor(dictionary: TokenDictionary);
196
+ getTokensView(): {
197
+ conditionMap: Map<string, Set<Token>>;
198
+ categoryMap: Map<keyof import("@bamboocss/types").TokenDataTypes, Map<string, Token>>;
199
+ colorPalettes: Map<string, Map<string, string>>;
200
+ vars: Map<string, Map<string, string>>;
201
+ values: Map<string, string>;
202
+ nameByVar: Map<string, string>;
203
+ json: Record<string, Record<string, string>>;
204
+ valuesByCategory: Map<keyof import("@bamboocss/types").TokenDataTypes, Map<string, string>>;
205
+ get: (path: string, fallback?: string | number) => string;
206
+ getVar: (path: string, fallback?: string | number) => string;
207
+ getCategoryValues: (category: string) => Record<string, string> | undefined;
208
+ getColorPaletteValues: (palette: string) => string[];
209
+ };
210
+ private processCondition;
211
+ private processColorPalette;
212
+ private processCategory;
213
+ private processValue;
214
+ private processVars;
215
+ }
216
+ //#endregion
217
+ //#region src/utils.d.ts
218
+ /**
219
+ * Returns all references in a string
220
+ *
221
+ * @example
222
+ *
223
+ * `{colors.red.300} {sizes.sm}` => ['colors.red.300', 'sizes.sm']
224
+ */
225
+ declare function getReferences(value: string): string[];
226
+ declare const hasReference: (value: string) => boolean;
227
+ declare function expandReferences(value: string, fn: (key: string) => string): string;
228
+ /**
229
+ * Converts a JS Map to an object
230
+ */
231
+ declare function mapToJson(map: Map<string, any>): Record<string, unknown>;
232
+ declare const isToken: (value: any) => value is Token$1;
233
+ declare function assertTokenFormat(token: any): asserts token is Token$1;
234
+ //#endregion
235
+ export { TOKEN_CATEGORIES, Token, type TokenCategory, TokenDictionary, type TokenExtensions, assertTokenFormat, expandReferences, getReferences, hasReference, isToken, mapToJson };
@@ -0,0 +1,235 @@
1
+ import { CssVar, CssVarOptions } from "@bamboocss/shared";
2
+ import { ColorPaletteOptions, SemanticTokens, ThemeVariantsMap, Token as Token$1, Tokens } from "@bamboocss/types";
3
+
4
+ //#region src/token.d.ts
5
+ interface ExtensionData {
6
+ category?: string;
7
+ references?: TokenReferences;
8
+ condition?: string;
9
+ conditions?: TokenConditions;
10
+ theme?: string;
11
+ }
12
+ interface TokenConditions {
13
+ [key: string]: string;
14
+ }
15
+ interface TokenReferences {
16
+ [key: string]: Token;
17
+ }
18
+ type TokenExtensions<T = {}> = ExtensionData & {
19
+ [key: string]: any;
20
+ } & T;
21
+ interface TokenInput {
22
+ name: string;
23
+ value: any;
24
+ type?: string;
25
+ path?: string[];
26
+ description?: string;
27
+ deprecated?: boolean | string;
28
+ extensions?: TokenExtensions;
29
+ }
30
+ /**
31
+ * Represents a design token in the dictionary
32
+ */
33
+ declare class Token {
34
+ name: string;
35
+ value: any;
36
+ originalValue: any;
37
+ path: string[];
38
+ type?: string;
39
+ description?: string;
40
+ deprecated?: boolean | string;
41
+ extensions: TokenExtensions;
42
+ constructor(input: TokenInput);
43
+ /**
44
+ * The unique identifier of the token.
45
+ */
46
+ get id(): string;
47
+ /**
48
+ * Whether the token is a conditional token.
49
+ * Conditional tokens are tokens that have multiple values based on a condition.
50
+ */
51
+ get isConditional(): boolean;
52
+ /**
53
+ * Whether the token has a reference in its value.
54
+ * e.g. {color.gray.100}
55
+ */
56
+ get hasReference(): boolean;
57
+ /**
58
+ * Whether the token is a complex or composite token.
59
+ */
60
+ get isComposite(): boolean;
61
+ /**
62
+ * Returns the token value with the references expanded.
63
+ * e.g. {color.gray.100} => var(--colors-gray-100)
64
+ *
65
+ */
66
+ expandReferences(): string;
67
+ /**
68
+ * Whether this token has a reference to another token
69
+ */
70
+ get isReference(): boolean;
71
+ /**
72
+ * Returns the list of references in the token value
73
+ */
74
+ get references(): string[];
75
+ clone(): Token;
76
+ /**
77
+ * Returns an array of tokens per conditions.
78
+ * It is commonly used in semantic tokens, and can have multiple values based on a condition.
79
+ * e.g. primary: { light: '#000', dark: '#fff' }
80
+ */
81
+ getConditionTokens(): Token[] | undefined;
82
+ /**
83
+ * Add more extensions to the token
84
+ */
85
+ setExtensions(extensions: TokenExtensions): this;
86
+ setType(): void;
87
+ }
88
+ declare const TOKEN_TYPES: {
89
+ readonly colors: "color";
90
+ readonly spacing: "dimension";
91
+ readonly sizes: "dimension";
92
+ readonly shadows: "shadow";
93
+ readonly fonts: "fontFamily";
94
+ readonly fontSizes: "fontSize";
95
+ readonly fontWeights: "fontWeight";
96
+ readonly letterSpacings: "letterSpacing";
97
+ readonly lineHeights: "lineHeight";
98
+ readonly durations: "duration";
99
+ readonly transitions: "transition";
100
+ readonly radii: "borderRadius";
101
+ readonly gradients: "gradient";
102
+ readonly easings: "cubicBezier";
103
+ readonly borders: "border";
104
+ readonly borderWidths: "borderWidth";
105
+ readonly zIndex: "zIndex";
106
+ readonly opacity: "opacity";
107
+ readonly blurs: "blur";
108
+ readonly aspectRatios: "aspectRatio";
109
+ readonly cursor: "cursor";
110
+ readonly assets: "asset";
111
+ };
112
+ type TokenCategory = keyof typeof TOKEN_TYPES;
113
+ declare const TOKEN_CATEGORIES: TokenCategory[];
114
+ //#endregion
115
+ //#region src/dictionary.d.ts
116
+ type EnforcePhase = 'pre' | 'post';
117
+ interface TokenTransformer {
118
+ name: string;
119
+ enforce?: EnforcePhase;
120
+ type?: 'value' | 'name' | 'extensions';
121
+ match?: (token: Token) => boolean;
122
+ transform: (token: Token, dictionary: TokenDictionary) => any;
123
+ }
124
+ interface TokenDictionaryOptions {
125
+ tokens?: Tokens;
126
+ semanticTokens?: SemanticTokens;
127
+ breakpoints?: Record<string, string>;
128
+ themes?: ThemeVariantsMap | undefined;
129
+ prefix?: string;
130
+ hash?: boolean;
131
+ colorPalette?: ColorPaletteOptions;
132
+ }
133
+ interface TokenMiddleware {
134
+ enforce?: EnforcePhase;
135
+ transform: (dict: TokenDictionary) => void;
136
+ }
137
+ declare class TokenDictionary {
138
+ private options;
139
+ allTokens: Token[];
140
+ byName: Map<string, Token>;
141
+ private deprecated;
142
+ constructor(options: TokenDictionaryOptions);
143
+ init(): this;
144
+ get prefix(): string | undefined;
145
+ get hash(): boolean | undefined;
146
+ get colorPalette(): ColorPaletteOptions | undefined;
147
+ getByName: (path: string) => Token | undefined;
148
+ formatTokenName: (path: string[]) => string;
149
+ formatCssVar: (path: string[], options: CssVarOptions) => CssVar;
150
+ registerTokens(): this;
151
+ registerToken: (token: Token, transformPhase?: "pre" | "post") => void;
152
+ private transforms;
153
+ registerTransform(...transforms: TokenTransformer[]): this;
154
+ private execTransform;
155
+ execTransformOnToken(transform: TokenTransformer, token: Token): void;
156
+ transformTokens(enforce: EnforcePhase): this;
157
+ private middlewares;
158
+ registerMiddleware(...middlewares: TokenMiddleware[]): this;
159
+ applyMiddlewares(enforce: EnforcePhase): void;
160
+ getReferences(value: string): Token[];
161
+ usesReference(value: any): boolean;
162
+ addReferences(): this;
163
+ filter(pattern: Partial<Token> | ((token: Token) => boolean)): Token[];
164
+ addConditionalTokens(): this;
165
+ expandTokenReferences(): this;
166
+ colorMix: (value: string, tokenFn: (path: string) => string) => {
167
+ invalid: boolean;
168
+ value: string;
169
+ color?: undefined;
170
+ } | {
171
+ invalid: boolean;
172
+ color: string;
173
+ value: string;
174
+ };
175
+ /**
176
+ * Expand token references to their CSS variable
177
+ */
178
+ expandReferenceInValue(value: string): string;
179
+ /**
180
+ * Get the value of a token reference
181
+ */
182
+ resolveReference(value: string): string;
183
+ /**
184
+ * Resolve token references to their actual raw value (recursively resolves references)
185
+ */
186
+ deepResolveReference(originalValue: string): string | undefined;
187
+ isDeprecated(name: string): boolean;
188
+ build(): this;
189
+ get isEmpty(): boolean;
190
+ view: ReturnType<TokenDictionaryView['getTokensView']>;
191
+ setComputedView(): this;
192
+ }
193
+ declare class TokenDictionaryView {
194
+ private dictionary;
195
+ constructor(dictionary: TokenDictionary);
196
+ getTokensView(): {
197
+ conditionMap: Map<string, Set<Token>>;
198
+ categoryMap: Map<keyof import("@bamboocss/types").TokenDataTypes, Map<string, Token>>;
199
+ colorPalettes: Map<string, Map<string, string>>;
200
+ vars: Map<string, Map<string, string>>;
201
+ values: Map<string, string>;
202
+ nameByVar: Map<string, string>;
203
+ json: Record<string, Record<string, string>>;
204
+ valuesByCategory: Map<keyof import("@bamboocss/types").TokenDataTypes, Map<string, string>>;
205
+ get: (path: string, fallback?: string | number) => string;
206
+ getVar: (path: string, fallback?: string | number) => string;
207
+ getCategoryValues: (category: string) => Record<string, string> | undefined;
208
+ getColorPaletteValues: (palette: string) => string[];
209
+ };
210
+ private processCondition;
211
+ private processColorPalette;
212
+ private processCategory;
213
+ private processValue;
214
+ private processVars;
215
+ }
216
+ //#endregion
217
+ //#region src/utils.d.ts
218
+ /**
219
+ * Returns all references in a string
220
+ *
221
+ * @example
222
+ *
223
+ * `{colors.red.300} {sizes.sm}` => ['colors.red.300', 'sizes.sm']
224
+ */
225
+ declare function getReferences(value: string): string[];
226
+ declare const hasReference: (value: string) => boolean;
227
+ declare function expandReferences(value: string, fn: (key: string) => string): string;
228
+ /**
229
+ * Converts a JS Map to an object
230
+ */
231
+ declare function mapToJson(map: Map<string, any>): Record<string, unknown>;
232
+ declare const isToken: (value: any) => value is Token$1;
233
+ declare function assertTokenFormat(token: any): asserts token is Token$1;
234
+ //#endregion
235
+ export { TOKEN_CATEGORIES, Token, type TokenCategory, TokenDictionary, type TokenExtensions, assertTokenFormat, expandReferences, getReferences, hasReference, isToken, mapToJson };