Skip to content

Commit

Permalink
Feat: Transform style initial implemented with simply important (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
black7375 authored Jan 10, 2024
1 parent 7d09369 commit 5300482
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
48 changes: 48 additions & 0 deletions packages/transform-to-vanilla/src/transform-object/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { simplyImportant } from "@/transform-values/simply-important";
import type { StyleRule } from "@vanilla-extract/css";
import type {
CSSRule,
CSSRuleKey,
CSSRuleValue,
StyleRuleValue
} from "@/types/style-rule";

// == Interface ================================================================
export function transformStyle(style: CSSRule) {
const result: {
// key in StyleRuleKey occur a error
// Type '{}' is missing the following properties from type
// '{ accentColor: StyleRuleValue; alignContent: StyleRuleValue; alignItems: StyleRuleValue; alignSelf: StyleRuleValue; ... 904 more ...;
// vars: StyleRuleValue; }': accentColor, alignContent, alignItems, alignSelf, and 905 more.ts(2740)
[key in string]: StyleRuleValue;
} = {};

for (const [key, value] of Object.entries(style) as [
CSSRuleKey,
CSSRuleValue
][]) {
if (typeof value === "string") {
result[key] = simplyImportant(value);
continue;
}
result[key] = value;
}
return result as StyleRule;
}

// == Tests ====================================================================
if (import.meta.vitest) {
const { describe, it, expect } = import.meta.vitest;

describe.concurrent("transform", () => {
it("Simply Important", () => {
expect(
transformStyle({
color: "red!"
})
).toStrictEqual({
color: "red !important"
} satisfies StyleRule);
});
});
}
7 changes: 1 addition & 6 deletions packages/transform-to-vanilla/src/transform.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { transformStyle } from "./transform-object/index";
import type { ComplexStyleRule } from "@vanilla-extract/css";
import type {
ComplexCSSRule,
CSSRule,
VanillaStyleArray,
VanillaClassNames
} from "./types/style-rule";
Expand All @@ -23,11 +23,6 @@ function isClassNames(
return typeof style === "string" || Array.isArray(style);
}

// TODO: Need traverse
function transformStyle(style: CSSRule) {
return style;
}

// == Tests ====================================================================
if (import.meta.vitest) {
const { describe, it, expect } = import.meta.vitest;
Expand Down
5 changes: 5 additions & 0 deletions packages/transform-to-vanilla/src/types/style-rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,10 @@ import type { ComplexStyleRule, StyleRule } from "@vanilla-extract/css";
export type ComplexCSSRule = ComplexStyleRule;
export type CSSRule = StyleRule;

export type StyleRuleKey = keyof StyleRule;
export type StyleRuleValue = StyleRule[StyleRuleKey];
export type CSSRuleKey = keyof CSSRule;
export type CSSRuleValue = CSSRule[CSSRuleKey];

export type VanillaStyleArray = Exclude<ComplexStyleRule, StyleRule>;
export type VanillaClassNames = Exclude<VanillaStyleArray[number], StyleRule>;

0 comments on commit 5300482

Please sign in to comment.