@@ -639,6 +639,28 @@ const buildPatterns = ({
639639 return current ;
640640} ;
641641
642+ /**
643+ * Derive a semantic CSS variable name from a slot's grammar position label.
644+ * The label looks like "border:term:2:type:color:branch:0:type:hex-color".
645+ * We extract the first `type:XXX` segment to build `--slot-XXX`.
646+ * When multiple slots in the same pattern share the same type we append
647+ * a counter suffix: `--slot-color`, `--slot-color-2`, etc.
648+ * Falling back to `--slot-N` when no type can be extracted.
649+ */
650+ const slotVarName = (
651+ label : string | undefined ,
652+ fallbackIndex : number ,
653+ used : Map < string , number >
654+ ) : string => {
655+ const typeMatch = label ?. match ( / (?: ^ | : ) t y p e : ( [ ^ : ] + ) / ) ;
656+ const base = typeMatch
657+ ? `--slot-${ typeMatch [ 1 ] } `
658+ : `--slot-${ fallbackIndex + 1 } ` ;
659+ const count = ( used . get ( base ) ?? 0 ) + 1 ;
660+ used . set ( base , count ) ;
661+ return count === 1 ? base : `${ base } -${ count } ` ;
662+ } ;
663+
642664const buildCases = ( {
643665 patterns,
644666 property,
@@ -658,7 +680,12 @@ const buildCases = ({
658680 }
659681
660682 for ( const [ slotOrder , slot ] of slots . entries ( ) ) {
661- const variableName = `--slot-${ slotOrder + 1 } ` ;
683+ const used = new Map < string , number > ( ) ;
684+ // pre-register names for earlier slots so this slot gets the right suffix
685+ for ( let i = 0 ; i < slotOrder ; i ++ ) {
686+ slotVarName ( slots [ i ] . part . label , i , used ) ;
687+ }
688+ const variableName = slotVarName ( slot . part . label , slotOrder , used ) ;
662689 const parts = pattern . parts . map ( ( part , index ) => {
663690 if ( index !== slot . index || part . kind !== "slot" ) {
664691 return part ;
@@ -685,12 +712,13 @@ const buildCases = ({
685712 }
686713
687714 const variables : Record < string , string > = { } ;
715+ const allUsed = new Map < string , number > ( ) ;
688716 const parts = pattern . parts . map ( ( part , index ) => {
689717 const slotOrder = slots . findIndex ( ( slot ) => slot . index === index ) ;
690718 if ( slotOrder === - 1 || part . kind !== "slot" ) {
691719 return part ;
692720 }
693- const variableName = `--slot- ${ slotOrder + 1 } ` ;
721+ const variableName = slotVarName ( part . label , slotOrder , allUsed ) ;
694722 variables [ variableName ] = part . text ;
695723 return {
696724 kind : "text" as const ,
0 commit comments