@@ -28,6 +28,18 @@ function contrastRatio(first: string, second: string): number {
2828 return ( lighter + 0.05 ) / ( darker + 0.05 ) ;
2929}
3030
31+ function mixThemeColors ( first : string , second : string , firstWeight : number ) : string {
32+ const channels = [ first , second ] . map ( ( value ) => {
33+ const hex = themeColorToHex ( value ) ?. slice ( 1 , 7 ) ;
34+ if ( ! hex ) throw new Error ( `Expected a theme color, received ${ value } ` ) ;
35+ return [ 0 , 1 , 2 ] . map ( ( channel ) => Number . parseInt ( hex . slice ( channel * 2 , channel * 2 + 2 ) , 16 ) ) ;
36+ } ) ;
37+ const mixed = channels [ 0 ] ! . map ( ( channel , index ) =>
38+ Math . round ( channel * firstWeight + channels [ 1 ] ! [ index ] ! * ( 1 - firstWeight ) ) ,
39+ ) ;
40+ return `#${ mixed . map ( ( channel ) => channel . toString ( 16 ) . padStart ( 2 , "0" ) ) . join ( "" ) } ` ;
41+ }
42+
3143const builtInThemeModes = [
3244 T3_CHAT_THEME ,
3345 GROVE_THEME ,
@@ -39,29 +51,38 @@ const builtInThemeModes = [
3951) ;
4052
4153describe ( "clerkAppearance" , ( ) => {
42- it ( "maps colors without overriding Clerk's component structure" , ( ) => {
54+ it ( "maps theme colors without overriding Clerk's component structure" , ( ) => {
4355 expect ( clerkAppearance ) . toEqual ( {
4456 variables : {
4557 colorPrimary : "var(--update-foreground)" ,
4658 colorPrimaryForeground : "var(--card)" ,
47- colorDanger : "var(--error-foreground )" ,
48- colorSuccess : "var(--success-foreground )" ,
49- colorWarning : "var(--warning-foreground )" ,
59+ colorDanger : "var(--error)" ,
60+ colorSuccess : "var(--success)" ,
61+ colorWarning : "var(--warning)" ,
5062 colorNeutral : "var(--foreground)" ,
5163 colorForeground : "var(--foreground)" ,
52- colorMuted : "var(--muted )" ,
64+ colorMuted : "color-mix(in srgb, var(--card) 98%, var(--foreground) )" ,
5365 colorMutedForeground : "var(--muted-foreground)" ,
5466 colorBackground : "var(--card)" ,
5567 colorInputForeground : "var(--foreground)" ,
5668 colorInput : "var(--secondary)" ,
5769 colorRing : "var(--ring)" ,
5870 } ,
71+ elements : {
72+ formFieldErrorText : { color : "var(--error-foreground)" } ,
73+ formFieldWarningText : { color : "var(--warning-foreground)" } ,
74+ formFieldSuccessText : { color : "var(--success-foreground)" } ,
75+ otpCodeFieldErrorText : { color : "var(--error-foreground)" } ,
76+ otpCodeFieldSuccessText : { color : "var(--success-foreground)" } ,
77+ } ,
5978 } ) ;
6079 } ) ;
6180
6281 it . each ( builtInThemeModes ) ( "keeps Clerk text readable across a built-in palette" , ( colors ) => {
82+ const mutedSurface = mixThemeColors ( colors . surface , colors . text , 0.98 ) ;
83+
6384 expect ( contrastRatio ( colors . text , colors . surface ) ) . toBeGreaterThanOrEqual ( 4.5 ) ;
64- expect ( contrastRatio ( colors . mutedForeground , colors . surface ) ) . toBeGreaterThanOrEqual ( 4.5 ) ;
85+ expect ( contrastRatio ( colors . mutedForeground , mutedSurface ) ) . toBeGreaterThanOrEqual ( 4.5 ) ;
6586 expect ( contrastRatio ( colors . text , colors . secondary ) ) . toBeGreaterThanOrEqual ( 4.5 ) ;
6687 expect ( contrastRatio ( colors . updateForeground , colors . surface ) ) . toBeGreaterThanOrEqual ( 4.5 ) ;
6788 expect ( contrastRatio ( colors . errorForeground , colors . surface ) ) . toBeGreaterThanOrEqual ( 4.5 ) ;
0 commit comments