Skip to content

Commit aafcd1c

Browse files
committed
Feat: rules.with() API implement #234
1 parent 10a0dac commit aafcd1c

File tree

3 files changed

+99
-3
lines changed

3 files changed

+99
-3
lines changed

.changeset/eager-lemons-wink.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@mincho-js/css": minor
3+
---
4+
5+
**css**
6+
7+
## New
8+
- Add `rules.with()` API

packages/css/src/css/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,7 @@ function cssWith<const T extends CSSRule>(
134134
>(styleMap: StyleMap, debugId?: string): Record<keyof StyleMap, string>;
135135
function cssWithMultiple<
136136
Data extends Record<string | number, RestrictedCSSRule>,
137-
Key extends keyof Data,
138-
MapData extends (value: Data[Key], key: Key) => ComplexCSSRule
137+
MapData extends (value: Data[keyof Data], key: keyof Data) => ComplexCSSRule
139138
>(data: Data, mapData: MapData, debugId?: string): Record<keyof Data, string>;
140139
function cssWithMultiple<
141140
Data extends Record<string | number, RestrictedCSSRule>,

packages/css/src/rules/index.ts

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type {
99
} from "@mincho-js/transform-to-vanilla";
1010

1111
import { css } from "../css/index.js";
12+
import type { CSSRuleWith } from "../css/types.js";
1213
import { className, getDebugName, getVarName } from "../utils.js";
1314
import { createRuntimeFn } from "./createRuntimeFn.js";
1415
import type {
@@ -39,7 +40,8 @@ import {
3940
// == Rules ====================================================================
4041
export const rules = Object.assign(rulesImpl, {
4142
multiple: rulesMultiple,
42-
raw: rulesRaw
43+
raw: rulesRaw,
44+
with: rulesWith
4345
});
4446

4547
function 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 ===============================================================
54143
const mergeObject = deepmerge();
55144

0 commit comments

Comments
 (0)