@@ -14,30 +14,30 @@ import { useTheme } from "@storybook/theming";
1414 * @returns {string|void } - The value of the token
1515 */
1616function 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+ }
2222
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 ;
2525
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+ }
3030
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+ }
3535
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+ }
3939
40- return ;
40+ return ;
4141}
4242
4343/**
@@ -49,23 +49,25 @@ function parseData(data, { key, color, platform }) {
4949 * @returns {string|undefined } - The value of the token or a fallback value
5050 */
5151export function fetchToken ( key , fallback = undefined , { color, scale } = { } ) {
52- if ( typeof key !== "string" ) return fallback ;
52+ if ( typeof key !== "string" ) return fallback ;
5353
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
5555 const theme = useTheme ( ) ?? { } ;
5656
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" ;
6061
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" ;
6365
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" ;
6668
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 ;
6971
70- return parseData ( spectrum [ key ] , { color, platform } ) ?? fallback ;
72+ return parseData ( spectrum [ key ] , { color, platform } ) ?? fallback ;
7173}
0 commit comments