@@ -14,30 +14,30 @@ import { useTheme } from "@storybook/theming";
14
14
* @returns {string|void } - The value of the token
15
15
*/
16
16
function parseData ( data , { key, color, platform } ) {
17
- // If nothing exists for that key, report that the key is missing
18
- if ( ! data ) {
19
- console . log ( `⚠️ Token ${ key } can't be found in the spectrum token data` ) ;
20
- return ;
21
- }
17
+ // If nothing exists for that key, report that the key is missing
18
+ if ( ! data ) {
19
+ console . log ( `⚠️ Token ${ key } can't be found in the spectrum token data` ) ;
20
+ return ;
21
+ }
22
22
23
- // Check if the key has a value
24
- if ( data . value ) return data . value ;
23
+ // Check if the key has a value
24
+ if ( data . value ) return data . value ;
25
25
26
- if ( Object . keys ( data . sets ) . length === 0 ) {
27
- console . log ( `⚠️ Token ${ key } has no value or sets` ) ;
28
- return ;
29
- }
26
+ if ( Object . keys ( data . sets ) . length === 0 ) {
27
+ console . log ( `⚠️ Token ${ key } has no value or sets` ) ;
28
+ return ;
29
+ }
30
30
31
- // Check if one of the contexts is a key in the sets
32
- if ( color in data . sets ) {
33
- return parseData ( data . sets [ color ] , { key, color, platform } ) ;
34
- }
31
+ // Check if one of the contexts is a key in the sets
32
+ if ( color in data . sets ) {
33
+ return parseData ( data . sets [ color ] , { key, color, platform } ) ;
34
+ }
35
35
36
- if ( platform in data . sets ) {
37
- return parseData ( data . sets [ platform ] , { key, color, platform } ) ;
38
- }
36
+ if ( platform in data . sets ) {
37
+ return parseData ( data . sets [ platform ] , { key, color, platform } ) ;
38
+ }
39
39
40
- return ;
40
+ return ;
41
41
}
42
42
43
43
/**
@@ -49,23 +49,25 @@ function parseData(data, { key, color, platform }) {
49
49
* @returns {string|undefined } - The value of the token or a fallback value
50
50
*/
51
51
export function fetchToken ( key , fallback = undefined , { color, scale } = { } ) {
52
- if ( typeof key !== "string" ) return fallback ;
52
+ if ( typeof key !== "string" ) return fallback ;
53
53
54
- // Fetch the theme if it exists; this data exists if wrapped in a ThemeProvider
54
+ // Fetch the theme if it exists; this data exists if wrapped in a ThemeProvider
55
55
const theme = useTheme ( ) ?? { } ;
56
56
57
- // If the color or scale is not provided, use the theme values or a fallback
58
- if ( typeof color !== "string" && typeof theme . color == "string" ) color = theme . color ;
59
- else if ( ! color ) color = "light" ;
57
+ // If the color or scale is not provided, use the theme values or a fallback
58
+ if ( typeof color !== "string" && typeof theme . color == "string" )
59
+ color = theme . color ;
60
+ else if ( ! color ) color = "light" ;
60
61
61
- if ( typeof scale !== "string" && typeof theme . scale == "string" ) scale = theme . scale ;
62
- else if ( ! scale ) scale = "medium" ;
62
+ if ( typeof scale !== "string" && typeof theme . scale == "string" )
63
+ scale = theme . scale ;
64
+ else if ( ! scale ) scale = "medium" ;
63
65
64
- // Create a platform context based on the scale (platform used in the token data)
65
- const platform = scale === "medium" ? "desktop" : "mobile" ;
66
+ // Create a platform context based on the scale (platform used in the token data)
67
+ const platform = scale === "medium" ? "desktop" : "mobile" ;
66
68
67
- // Check if the spectrum data is available
68
- if ( ! spectrum || typeof spectrum !== "object" ) return fallback ;
69
+ // Check if the spectrum data is available
70
+ if ( ! spectrum || typeof spectrum !== "object" ) return fallback ;
69
71
70
- return parseData ( spectrum [ key ] , { color, platform } ) ?? fallback ;
72
+ return parseData ( spectrum [ key ] , { color, platform } ) ?? fallback ;
71
73
}
0 commit comments