@@ -4,11 +4,12 @@ import { spaceScaleKeys } from '../foundations/sizes';
44import type { fontSizes , fontWeights } from '../foundations/typography' ;
55import { colors } from '../utils/colors' ;
66import { colorOptionToThemedAlphaScale , colorOptionToThemedLightnessScale } from '../utils/colors/scales' ;
7+ import { cssSupports } from '../utils/cssSupports' ;
78import { fromEntries } from '../utils/fromEntries' ;
89import { removeUndefinedProps } from '../utils/removeUndefinedProps' ;
910
1011export const createColorScales = ( theme : Theme ) => {
11- const variables = theme . variables || { } ;
12+ const variables = removeInvalidValues ( theme . variables || { } ) ;
1213
1314 const dangerScale = colorOptionToThemedLightnessScale ( variables . colorDanger , 'danger' ) ;
1415 const primaryScale = colorOptionToThemedLightnessScale ( variables . colorPrimary , 'primary' ) ;
@@ -43,6 +44,49 @@ export const createColorScales = (theme: Theme) => {
4344 } ) ;
4445} ;
4546
47+ export const removeInvalidValues = ( variables : NonNullable < Theme [ 'variables' ] > ) : NonNullable < Theme [ 'variables' ] > => {
48+ // Check for modern color support. If present, we can simply return the variables as-is since we support everything
49+ // CSS supports.
50+ if ( cssSupports . modernColor ( ) ) {
51+ return variables ;
52+ }
53+
54+ // If not, we need to remove any values that are specified with CSS variables, as our color scale generation only
55+ // supports CSS variables using modern CSS functionality.
56+ const validVariables : Theme [ 'variables' ] = Object . fromEntries (
57+ Object . entries ( variables ) . filter ( ( [ key , value ] ) => {
58+ if ( typeof value === 'string' ) {
59+ const isValid = ! value . startsWith ( 'var(' ) ;
60+ if ( ! isValid ) {
61+ console . warn (
62+ `Invalid color value: ${ value } for key: ${ key } , using default value instead. Using CSS variables is not supported in this browser.` ,
63+ ) ;
64+ }
65+ return isValid ;
66+ }
67+
68+ if ( typeof value === 'object' ) {
69+ return Object . entries ( value ) . every ( ( [ key , value ] ) => {
70+ if ( typeof value !== 'string' ) return true ;
71+
72+ const isValid = ! value . startsWith ( 'var(' ) ;
73+ if ( ! isValid ) {
74+ console . warn (
75+ `Invalid color value: ${ value } for key: ${ key } , using default value instead. Using CSS variables is not supported in this browser.` ,
76+ ) ;
77+ }
78+
79+ return isValid ;
80+ } ) ;
81+ }
82+
83+ return false ;
84+ } ) ,
85+ ) ;
86+
87+ return validVariables ;
88+ } ;
89+
4690export const createRadiiUnits = ( theme : Theme ) => {
4791 const { borderRadius } = theme . variables || { } ;
4892 if ( borderRadius === undefined ) {
0 commit comments