@@ -42,6 +42,11 @@ export interface TokenContext<Z extends TokenTicket> extends RegistryContext<Z>
42
42
resolve : ( token : string | TokenAlias ) => unknown | undefined
43
43
}
44
44
45
+ export interface TokenOptions {
46
+ depth ?: number
47
+ prefix ?: string
48
+ }
49
+
45
50
/**
46
51
* Creates a token registry for managing design token collections with alias resolution.
47
52
* Returns the token context directly for simple usage.
@@ -50,18 +55,23 @@ export interface TokenContext<Z extends TokenTicket> extends RegistryContext<Z>
50
55
* @template Z The structure of the registry token items.
51
56
* @template E The available methods for the token's context.
52
57
* @returns The token context object.
58
+ *
59
+ * @see https://0.vuetifyjs.com/composables/registration/use-tokens
53
60
* @see https://www.designtokens.org/tr/drafts/format/
54
61
*/
55
62
export function useTokens <
56
63
Z extends TokenTicket = TokenTicket ,
57
64
E extends TokenContext < Z > = TokenContext < Z > ,
58
- > ( tokens : TokenCollection = { } ) : E {
65
+ > (
66
+ tokens : TokenCollection = { } ,
67
+ options : TokenOptions = { } ,
68
+ ) : E {
59
69
const logger = useLogger ( )
60
70
const registry = useRegistry < Z , E > ( )
61
71
62
72
const cache = new Map < string , unknown | undefined > ( )
63
73
64
- registry . onboard ( flatten ( tokens ) as Partial < Z > [ ] )
74
+ registry . onboard ( flatten ( tokens , options . prefix , options . depth ) as Partial < Z > [ ] )
65
75
66
76
function isAlias ( token : unknown ) : token is string {
67
77
return isString ( token ) && token . length > 2 && token [ 0 ] === '{' && token . at ( - 1 ) === '}'
@@ -185,12 +195,12 @@ export function createTokensContext<
185
195
* @param prefix An optional prefix to prepend to each token ID.
186
196
* @returns An array of flattened tokens, each with an ID and value.
187
197
*/
188
- function flatten ( tokens : TokenCollection , prefix = '' ) : FlatTokenCollection [ ] {
198
+ function flatten ( tokens : TokenCollection , prefix = '' , depth = Infinity ) : FlatTokenCollection [ ] {
189
199
const flattened : FlatTokenCollection [ ] = [ ]
190
- const stack : Array < { tokens : TokenCollection , prefix : string } > = [ { tokens, prefix } ]
200
+ const stack : { tokens : TokenCollection , prefix : string , depth : number } [ ] = [ { tokens, prefix, depth } ]
191
201
192
202
while ( stack . length > 0 ) {
193
- const { tokens : currentTokens , prefix : currentPrefix } = stack . pop ( ) !
203
+ const { tokens : currentTokens , prefix : currentPrefix , depth : currentDepth } = stack . pop ( ) !
194
204
195
205
const meta : Record < string , unknown > = { }
196
206
for ( const k in currentTokens ) {
@@ -216,7 +226,7 @@ function flatten (tokens: TokenCollection, prefix = ''): FlatTokenCollection[] {
216
226
flattened . push ( { id, value : value as TokenAlias } )
217
227
218
228
const inner = value . $value
219
- if ( isObject ( inner ) ) {
229
+ if ( isObject ( inner ) && currentDepth > 0 ) {
220
230
for ( const innerKey in inner ) {
221
231
if ( innerKey . startsWith ( '$' ) ) continue
222
232
@@ -227,14 +237,19 @@ function flatten (tokens: TokenCollection, prefix = ''): FlatTokenCollection[] {
227
237
} else if ( '$value' in child ) {
228
238
flattened . push ( { id : childId , value : child as TokenAlias } )
229
239
} else {
230
- stack . push ( { tokens : child as TokenCollection , prefix : childId } )
240
+ stack . push ( { tokens : child as TokenCollection , prefix : childId , depth : currentDepth - 1 } )
231
241
}
232
242
}
233
243
}
234
244
continue
235
245
}
236
246
237
- stack . push ( { tokens : value as TokenCollection , prefix : id } )
247
+ if ( currentDepth <= 0 ) {
248
+ flattened . push ( { id, value : value as unknown as TokenValue } )
249
+ continue
250
+ }
251
+
252
+ stack . push ( { tokens : value as TokenCollection , prefix : id , depth : currentDepth - 1 } )
238
253
}
239
254
}
240
255
0 commit comments