Skip to content

Commit d2f01e8

Browse files
committed
refactor to support composite feature checks
1 parent 59f007e commit d2f01e8

File tree

1 file changed

+46
-18
lines changed

1 file changed

+46
-18
lines changed

packages/clerk-js/src/ui/utils/cssSupports.ts

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
1-
type CSSFeature = 'relativeColorSyntax' | 'colorMix';
1+
type CSSFeature = 'relativeColorSyntax' | 'colorMix' | 'modernColorSupport';
22

3-
const CSS_FEATURE_TESTS: Record<CSSFeature, string> = {
3+
type CompositeOperation = 'or' | 'and';
4+
5+
type CompositeFeatureDefinition = {
6+
operation: CompositeOperation;
7+
features: CSSFeature[];
8+
};
9+
10+
const CSS_FEATURE_TESTS: Record<string, string> = {
411
relativeColorSyntax: 'hsl(from white h s l)',
512
colorMix: 'color-mix(in srgb, white, black)',
613
} as const;
714

15+
const COMPOSITE_FEATURES: Record<string, CompositeFeatureDefinition> = {
16+
modernColorSupport: {
17+
operation: 'or',
18+
features: ['relativeColorSyntax', 'colorMix'],
19+
},
20+
} as const;
21+
822
const supportCache = new Map<CSSFeature, boolean>();
9-
// Cache for composite checks
10-
let modernColorSupportCache: boolean | undefined;
1123

1224
/**
1325
* CSS feature detection
@@ -32,11 +44,14 @@ const testCSSFeature = (feature: CSSFeature, property: string = 'color'): boolea
3244
let isSupported = false;
3345

3446
try {
35-
const testValue = CSS_FEATURE_TESTS[feature];
36-
37-
const testProperty = getPropertyForFeature(feature, property);
38-
39-
isSupported = CSS.supports(testProperty, testValue);
47+
// Check if this is a composite feature
48+
if (feature in COMPOSITE_FEATURES) {
49+
isSupported = evaluateCompositeFeature(feature);
50+
} else {
51+
const testValue = CSS_FEATURE_TESTS[feature];
52+
const testProperty = getPropertyForFeature(feature, property);
53+
isSupported = CSS.supports(testProperty, testValue);
54+
}
4055
} catch {
4156
isSupported = false;
4257
}
@@ -46,6 +61,27 @@ const testCSSFeature = (feature: CSSFeature, property: string = 'color'): boolea
4661
return isSupported;
4762
};
4863

64+
/**
65+
* Evaluate a composite feature based on its definition
66+
*/
67+
const evaluateCompositeFeature = (feature: string): boolean => {
68+
const definition = COMPOSITE_FEATURES[feature];
69+
if (!definition) {
70+
return false;
71+
}
72+
73+
const results = definition.features.map(f => testCSSFeature(f));
74+
75+
switch (definition.operation) {
76+
case 'or':
77+
return results.some(Boolean);
78+
case 'and':
79+
return results.every(Boolean);
80+
default:
81+
return false;
82+
}
83+
};
84+
4985
/**
5086
* Get the appropriate CSS property for testing a feature
5187
*/
@@ -70,19 +106,11 @@ export const cssSupports = {
70106
* Check if the browser supports modern color syntax (color-mix or relative color syntax)
71107
* @returns true if the browser supports modern color syntax
72108
*/
73-
hasModernColorSupport: () => {
74-
if (modernColorSupportCache !== undefined) {
75-
return modernColorSupportCache;
76-
}
77-
78-
modernColorSupportCache = testCSSFeature('relativeColorSyntax') || testCSSFeature('colorMix');
79-
return modernColorSupportCache;
80-
},
109+
hasModernColorSupport: () => testCSSFeature('modernColorSupport'),
81110
} as const;
82111

83112
export const clearCSSSupportsCache = (): void => {
84113
supportCache.clear();
85-
modernColorSupportCache = undefined;
86114
};
87115

88116
export const getCachedSupports = (): Record<string, boolean> => {

0 commit comments

Comments
 (0)