@@ -9,6 +9,7 @@ import type {
99} from "@mincho-js/transform-to-vanilla" ;
1010
1111import { css } from "../css/index.js" ;
12+ import type { CSSRuleWith } from "../css/types.js" ;
1213import { className , getDebugName , getVarName } from "../utils.js" ;
1314import { createRuntimeFn } from "./createRuntimeFn.js" ;
1415import type {
@@ -39,7 +40,8 @@ import {
3940// == Rules ====================================================================
4041export const rules = Object . assign ( rulesImpl , {
4142 multiple : rulesMultiple ,
42- raw : rulesRaw
43+ raw : rulesRaw ,
44+ with : rulesWith
4345} ) ;
4446
4547function rulesRaw <
@@ -50,6 +52,93 @@ function rulesRaw<
5052 return options ;
5153}
5254
55+ function rulesWith <
56+ const T extends CSSRule ,
57+ Variants extends VariantGroups | undefined = undefined ,
58+ ToggleVariants extends VariantDefinitions | undefined = undefined ,
59+ Props extends ComplexPropDefinitions < PropTarget > | undefined = undefined
60+ > (
61+ callback : (
62+ style : CSSRuleWith < T >
63+ ) => PatternOptions < Variants , ToggleVariants , Props >
64+ ) {
65+ type RestrictedCSSRule = CSSRuleWith < T > ;
66+ type RulePattern = PatternOptions < Variants , ToggleVariants , Props > ;
67+ type RuntimeFnType = RuntimeFn <
68+ ConditionalVariants < Variants , ToggleVariants > ,
69+ Exclude < Props , undefined >
70+ > ;
71+ type TransformRuleMap < T extends Record < string | number , RestrictedCSSRule > > =
72+ {
73+ [ K in keyof T ] : RuntimeFnType ;
74+ } ;
75+
76+ function rulesWithImpl ( style : RestrictedCSSRule , debugId ?: string ) {
77+ return rulesImpl ( callback ( style ) , debugId ) as RuntimeFnType ;
78+ }
79+ function rulesWithRaw ( style : RestrictedCSSRule ) {
80+ return rulesRaw ( callback ( style ) ) as RulePattern ;
81+ }
82+
83+ function rulesWithMultiple <
84+ RuleMap extends Record < string | number , RestrictedCSSRule >
85+ > ( ruleMap : RuleMap , debugId ?: string ) : TransformRuleMap < RuleMap > ;
86+ function rulesWithMultiple <
87+ Data extends Record < string | number , RestrictedCSSRule > ,
88+ MapData extends (
89+ value : Data [ keyof Data ] ,
90+ key : keyof Data
91+ ) => PatternOptions <
92+ VariantGroups | undefined ,
93+ VariantDefinitions | undefined ,
94+ ComplexPropDefinitions < PropTarget > | undefined
95+ >
96+ > (
97+ data : Data ,
98+ mapData : MapData ,
99+ debugId ?: string
100+ ) : TransformDataMapping < Data , MapData > ;
101+ function rulesWithMultiple <
102+ Data extends Record < string | number , RestrictedCSSRule > ,
103+ MapData extends (
104+ value : unknown ,
105+ key : string | number | symbol
106+ ) => PatternOptions <
107+ VariantGroups | undefined ,
108+ VariantDefinitions | undefined ,
109+ ComplexPropDefinitions < PropTarget > | undefined
110+ >
111+ > (
112+ ruleMapOrData : Data ,
113+ mapDataOrDebugId ?: MapData | string ,
114+ debugId ?: string
115+ ) : TransformRuleMap < Data > | TransformDataMapping < Data , MapData > {
116+ if ( isMapDataFunction ( mapDataOrDebugId ) ) {
117+ const data = ruleMapOrData as Data ;
118+ const mapData = mapDataOrDebugId ;
119+ return rulesMultiple (
120+ data ,
121+ ( value : Parameters < typeof callback > [ 0 ] , key : keyof Data ) =>
122+ mapData ( callback ( value ) as Parameters < MapData > [ 0 ] , key ) ,
123+ debugId
124+ ) as TransformDataMapping < Data , MapData > ;
125+ } else {
126+ const ruleMap = ruleMapOrData as Data ;
127+ const debugId = mapDataOrDebugId ;
128+ return rulesMultiple (
129+ ruleMap ,
130+ callback as unknown as MapData ,
131+ debugId
132+ ) as TransformRuleMap < Data > ;
133+ }
134+ }
135+
136+ return Object . assign ( rulesWithImpl , {
137+ raw : rulesWithRaw ,
138+ multiple : rulesWithMultiple
139+ } ) ;
140+ }
141+
53142// == Rules Impl ===============================================================
54143const mergeObject = deepmerge ( ) ;
55144
0 commit comments