11import { variants } from './design-token-options' ;
2+ import { generateRadixColors } from './generateRadixColors' ;
23import { VariantsMap } from './types' ;
3- import { cssVariablesToString , toCssVariables } from './utils' ;
4+ import { cssVariablesToString , setCssVariables , toCssVariables } from './utils' ;
45
56interface FlatTokens {
67 [ index : string ] : string ;
@@ -28,17 +29,46 @@ class BasisThemeStylesheet extends HTMLElement {
2829 this . parameters = new URLSearchParams ( ) ;
2930 this . variantsMap = new Map ( variants . map ( ( group ) => [ group . id , group ] ) ) ;
3031
31- for ( const [ key , value ] of new URL ( location . href ) . searchParams ) {
32+ const initialParams = new URL ( location . href ) . searchParams ;
33+ for ( const [ key , value ] of initialParams ) {
3234 if ( this . variantsMap . has ( key ) && this . variantsMap . get ( key ) ?. variants . some ( ( { id } ) => id === value ) ) {
3335 this . setGroupOption ( key , value ) ;
3436 }
3537 }
3638
37- console . log (
38- Array . from ( new Map < string , FlatTokens > ( [ [ '' , { 'basis.foo.bar' : '42px' } ] ] ) . values ( ) ) . map ( ( tokens ) =>
39- cssVariablesToString ( toCssVariables ( tokens ) ) ,
40- ) ,
41- ) ;
39+ const knownTokens = [
40+ 'basis.typography.font-family.default' ,
41+ 'basis.typography.font-family.heading' ,
42+ 'basis.typography.font-family.code' ,
43+ ] ;
44+
45+ knownTokens . forEach ( ( tokenName ) => {
46+ const tokenValue = initialParams . get ( tokenName ) ;
47+ if ( tokenValue ) {
48+ this . setToken ( tokenName , tokenValue ) ;
49+ }
50+ } ) ;
51+
52+ [
53+ 'basis.color.primary' ,
54+ 'basis.color.secondary' ,
55+ 'basis.color.text' ,
56+ 'basis.color.info' ,
57+ 'basis.color.warning' ,
58+ 'basis.color.error' ,
59+ 'basis.color.success' ,
60+ 'basis.color.highlight' ,
61+ 'basis.color.mark' ,
62+ 'basis.color.selected' ,
63+ ] . forEach ( ( scale ) => {
64+ if ( typeof initialParams . get ( `${ scale } .seed` ) === 'string' ) {
65+ this . setSeedColor ( {
66+ name : `${ scale } ` ,
67+ inverseName : `${ scale } -inverse` ,
68+ value : initialParams . get ( `${ scale } .seed` ) || '' ,
69+ } ) ;
70+ }
71+ } ) ;
4272 }
4373
4474 connectedCallback ( ) {
@@ -55,9 +85,9 @@ class BasisThemeStylesheet extends HTMLElement {
5585 let properties = Array . from ( this . map . values ( ) )
5686 . map ( ( tokens ) => cssVariablesToString ( toCssVariables ( tokens ) ) )
5787 . join ( ';\n' ) ;
58- console . log ( Array . from ( this . map . values ( ) ) ) ;
88+
5989 let css = `.basis-theme {\n${ properties } \n}` ;
60- console . log ( css ) ;
90+
6191 this . sheet ?. replaceSync ( css ) ;
6292 }
6393
@@ -71,6 +101,11 @@ class BasisThemeStylesheet extends HTMLElement {
71101 this . toggleTokens ( input . name , tokens ) ;
72102 }
73103
104+ setToken ( name : string , value : string ) {
105+ this . toggleTokens ( name , { [ name ] : value } ) ;
106+ this . setParameter ( name , value ) ;
107+ }
108+
74109 setGroupOption ( groupId : string , optionId : string ) {
75110 const group = this . variantsMap . get ( groupId ) ;
76111
@@ -83,17 +118,90 @@ class BasisThemeStylesheet extends HTMLElement {
83118 if ( ! option ) {
84119 return ;
85120 }
86- this . parameters . set ( groupId , optionId ) ;
87- history . replaceState ( { } , document . title , `?${ this . parameters } ` ) ;
121+ this . setParameter ( groupId , optionId ) ;
88122
89123 this . toggleTokens ( groupId , option . flatTokens ) ;
90124 }
91125
126+ setParameter ( key : string , value : string ) {
127+ this . parameters . set ( key , value ) ;
128+ history . replaceState ( { } , document . title , `?${ this . parameters } ` ) ;
129+ }
130+
92131 clickGroupOption ( groupId : string , optionId : string ) {
93132 this . setGroupOption ( groupId , optionId ) ;
94133
95134 history . replaceState ( { } , document . title , `?${ this . parameters } ` ) ;
96135 }
136+
137+ handleColorInput ( target : HTMLInputElement , name : string , inverseName : string ) {
138+ const value = target . value ;
139+ if ( typeof value !== 'string' ) {
140+ return ;
141+ }
142+
143+ this . setParameter ( `${ name } .seed` , value ) ;
144+
145+ this . setSeedColor ( {
146+ name,
147+ inverseName,
148+ value,
149+ } ) ;
150+ }
151+
152+ setSeedColor ( { name, value, inverseName } : { name : string ; value : string ; inverseName ?: string } ) {
153+ const radixTheme = generateRadixColors ( {
154+ appearance : 'light' ,
155+ accent : value ,
156+ gray : '#EEEEEE' ,
157+ background : '#FFFFFF' ,
158+ } ) ;
159+
160+ const inverseTheme = generateRadixColors ( {
161+ appearance : 'dark' ,
162+ accent : value ,
163+ gray : '#111111' ,
164+ background : '#000000' ,
165+ } ) ;
166+ const { accentScale } = radixTheme ;
167+ const { accentScale : inverseAccentScale } = inverseTheme ;
168+
169+ const createScaleObject = ( scale : Array < string > , prefix = '' ) : { [ index : string ] : string } =>
170+ scale . reduce ( ( obj , color , index ) => {
171+ let colorNumber = index + 1 ;
172+ let scalePrefix = 'color' ;
173+ if ( [ 0 , 1 ] . includes ( index ) ) {
174+ scalePrefix = 'bg-' ;
175+ colorNumber = index + 1 ;
176+ } else if ( [ 2 , 3 , 4 ] . includes ( index ) ) {
177+ scalePrefix = 'interactive-' ;
178+ colorNumber = index - 1 ;
179+ } else if ( [ 5 , 6 , 7 ] . includes ( index ) ) {
180+ scalePrefix = 'border-' ;
181+ colorNumber = index - 4 ;
182+ } else if ( [ 8 , 9 ] . includes ( index ) ) {
183+ scalePrefix = 'fill-' ;
184+ colorNumber = index - 7 ;
185+ } else if ( [ 10 , 11 ] . includes ( index ) ) {
186+ scalePrefix = 'text-' ;
187+ colorNumber = index - 9 ;
188+ }
189+ return {
190+ ...obj ,
191+ [ `${ prefix } ${ scalePrefix } ${ colorNumber } ` ] : color ,
192+ } ;
193+ } , { } ) ;
194+
195+ const scaleTokens = createScaleObject ( accentScale , `${ name } .` ) ;
196+ const inverseScaleTokens = createScaleObject ( inverseAccentScale , `${ inverseName } .` ) ;
197+
198+ const tokens = {
199+ ...scaleTokens ,
200+ ...inverseScaleTokens ,
201+ } ;
202+
203+ setCssVariables ( toCssVariables ( tokens ) ) ;
204+ }
97205}
98206
99207customElements . define ( 'basis-theme-stylesheet' , BasisThemeStylesheet ) ;
0 commit comments