Skip to content

Commit c268326

Browse files
committed
Feat: Rules - rules.raw() API supports #231
1 parent 216f6e1 commit c268326

File tree

2 files changed

+151
-2
lines changed

2 files changed

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

packages/css/src/rules/index.ts

Lines changed: 144 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ function setCSSProperty(
4747
styles[property] = value;
4848
}
4949

50-
export function rules<
50+
export function rulesImpl<
5151
Variants extends VariantGroups | undefined = undefined,
5252
ToggleVariants extends VariantDefinitions | undefined = undefined,
5353
Props extends ComplexPropDefinitions<PropTarget> | undefined = undefined
@@ -185,6 +185,18 @@ export function rules<
185185
);
186186
}
187187

188+
function rulesRaw<
189+
Variants extends VariantGroups | undefined = undefined,
190+
ToggleVariants extends VariantDefinitions | undefined = undefined,
191+
Props extends ComplexPropDefinitions<PropTarget> | undefined = undefined
192+
>(options: PatternOptions<Variants, ToggleVariants, Props>) {
193+
return options;
194+
}
195+
196+
export const rules = Object.assign(rulesImpl, {
197+
raw: rulesRaw
198+
});
199+
188200
function processPropObject<Target extends PropTarget>(
189201
props: PropDefinition<Target>,
190202
propVars: Record<string, PureCSSVarKey>,
@@ -224,7 +236,7 @@ function processCompoundStyle(
224236
if (import.meta.vitest) {
225237
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
226238
// @ts-ignore error TS1343: The 'import.meta' meta-property is only allowed when the '--module' option is 'es2020', 'es2022', 'esnext', 'system', 'node16', or 'nodenext'.
227-
const { describe, it, assert, expect } = import.meta.vitest;
239+
const { describe, it, assert, expect, assertType } = import.meta.vitest;
228240

229241
const debugId = "myCSS";
230242
setFileScope("test");
@@ -883,4 +895,134 @@ if (import.meta.vitest) {
883895
});
884896
});
885897
});
898+
899+
describe.concurrent("rules.raw()", () => {
900+
it("handles simple Rule properties", () => {
901+
const ruleObj1 = rules.raw({
902+
base: { color: "red" }
903+
});
904+
assertType<PatternOptions<undefined, undefined, undefined>>(ruleObj1);
905+
expect(ruleObj1).toStrictEqual({
906+
base: { color: "red" }
907+
});
908+
rules(ruleObj1); // Ensure it can be used with rules()
909+
910+
const ruleObj2 = rules.raw({
911+
variants: {
912+
color: {
913+
brand: { color: "#FFFFA0" },
914+
accent: { color: "#FFE4B5" }
915+
},
916+
size: {
917+
small: { padding: 12 },
918+
medium: { padding: 16 },
919+
large: { padding: 24 }
920+
},
921+
outlined: {
922+
true: { border: "1px solid black" },
923+
false: { border: "1px solid transparent" }
924+
}
925+
}
926+
});
927+
assertType<
928+
PatternOptions<
929+
{
930+
color: {
931+
brand: { color: "#FFFFA0" };
932+
accent: { color: "#FFE4B5" };
933+
};
934+
size: {
935+
small: { padding: number };
936+
medium: { padding: number };
937+
large: { padding: number };
938+
};
939+
outlined: {
940+
true: { border: "1px solid black" };
941+
false: { border: "1px solid transparent" };
942+
};
943+
},
944+
undefined,
945+
undefined
946+
>
947+
>(ruleObj2);
948+
expect(ruleObj2).toStrictEqual({
949+
variants: {
950+
color: {
951+
brand: { color: "#FFFFA0" },
952+
accent: { color: "#FFE4B5" }
953+
},
954+
size: {
955+
small: { padding: 12 },
956+
medium: { padding: 16 },
957+
large: { padding: 24 }
958+
},
959+
outlined: {
960+
true: { border: "1px solid black" },
961+
false: { border: "1px solid transparent" }
962+
}
963+
}
964+
});
965+
rules(ruleObj2); // Ensure it can be used with rules()
966+
967+
const ruleObj3 = rules.raw({
968+
toggles: {
969+
disabled: { textDecoration: "line-through" },
970+
rounded: { borderRadius: 999 }
971+
}
972+
});
973+
assertType<
974+
PatternOptions<
975+
undefined,
976+
{
977+
disabled: { textDecoration: "line-through" };
978+
rounded: { borderRadius: number };
979+
},
980+
undefined
981+
>
982+
>(ruleObj3);
983+
expect(ruleObj3).toStrictEqual({
984+
toggles: {
985+
disabled: { textDecoration: "line-through" },
986+
rounded: { borderRadius: 999 }
987+
}
988+
});
989+
rules(ruleObj3); // Ensure it can be used with rules()
990+
991+
const ruleObj4 = rules.raw({
992+
props: [
993+
"color",
994+
"background",
995+
{
996+
rounded: { targets: ["borderRadius"] },
997+
size: { base: 0, targets: ["padding", "margin"] }
998+
}
999+
]
1000+
});
1001+
assertType<
1002+
PatternOptions<
1003+
undefined,
1004+
undefined,
1005+
Array<
1006+
| "color"
1007+
| "background"
1008+
| {
1009+
rounded: { targets: Array<"borderRadius"> };
1010+
size: { base: number; targets: Array<"padding" | "margin"> };
1011+
}
1012+
>
1013+
>
1014+
>(ruleObj4);
1015+
expect(ruleObj4).toStrictEqual({
1016+
props: [
1017+
"color",
1018+
"background",
1019+
{
1020+
rounded: { targets: ["borderRadius"] },
1021+
size: { base: 0, targets: ["padding", "margin"] }
1022+
}
1023+
]
1024+
});
1025+
rules(ruleObj4); // Ensure it can be used with rules()
1026+
});
1027+
});
8861028
}

0 commit comments

Comments
 (0)