Skip to content

Commit 829eca2

Browse files
committed
Complete typograpy parser
1 parent 05986a2 commit 829eca2

File tree

6 files changed

+95
-8
lines changed

6 files changed

+95
-8
lines changed

src/bin/css_to_ts/cssVariable.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import type { Equals } from "tsafe";
55
import { exclude } from "tsafe/exclude";
66
import memoize from "memoizee";
77
import type { ColorScheme } from "./colorOptions";
8+
import { objectKeys } from "tsafe/objectKeys";
9+
import { same } from "evt/tools/inDepth/same";
810

911
const orderedColorScheme = ["light", "dark"] as const;
1012

@@ -101,3 +103,27 @@ export const createGetCssVariable = memoize((rawCssCode: string) => {
101103

102104
return { getCssVariable };
103105
});
106+
107+
export function isInvariantAcrossTheme(cssVariableValue: CssVariableValue): boolean {
108+
for (const breakpoint of objectKeys(cssVariableValue)) {
109+
const { light, dark } = cssVariableValue[breakpoint];
110+
111+
if (light !== dark) {
112+
return false;
113+
}
114+
}
115+
116+
return true;
117+
}
118+
119+
export function isInvariantAcrossScreenSizes(cssVariableValue: CssVariableValue): boolean {
120+
const lightAndDark = cssVariableValue["root"];
121+
122+
for (const breakpoint of objectKeys(cssVariableValue).filter(exclude("root"))) {
123+
if (!same(lightAndDark, cssVariableValue[breakpoint])) {
124+
return false;
125+
}
126+
}
127+
128+
return true;
129+
}

src/bin/css_to_ts/typography.ts

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { getRulesByBreakpoint, parseBreakpointsValues } from "./breakpoints";
2-
import { createGetCssVariable } from "./cssVariable";
2+
import {
3+
createGetCssVariable,
4+
isInvariantAcrossScreenSizes,
5+
isInvariantAcrossTheme
6+
} from "./cssVariable";
37
import { objectKeys } from "tsafe/objectKeys";
8+
import { assert } from "tsafe/assert";
9+
import { is } from "tsafe/is";
410

511
// Object that represent a set of CSS rules, see https://www.npmjs.com/package/csstype
612
// It's the argument of the `css()` function of @emotion/css.
@@ -20,8 +26,6 @@ export function parseTypographyVariants(rawCssCode: string): TypographyVariant[]
2026

2127
const { getCssVariable } = createGetCssVariable(rawCssCode);
2228

23-
console.log("TODO use", getCssVariable);
24-
2529
objectKeys(rulesByBreakpoint).forEach(breakpoint => {
2630
const mediaQuery = breakpoint === "root" ? undefined : mediaQueryByBreakpoint[breakpoint];
2731

@@ -34,7 +38,39 @@ export function parseTypographyVariants(rawCssCode: string): TypographyVariant[]
3438

3539
const style: CSSObject = {};
3640

37-
//TODO: fill up style
41+
rule.declarations.forEach(
42+
declaration =>
43+
(style[(declaration as any).property] = (() => {
44+
const value = declaration.value as string;
45+
46+
int: {
47+
const n = parseInt(value);
48+
49+
if (isNaN(n)) {
50+
break int;
51+
}
52+
53+
return n;
54+
}
55+
56+
return value.replace(/var\((--[^)]+)\)/g, (...[, cssVariableName]) => {
57+
assert(is<`--${string}`>(cssVariableName));
58+
59+
const cssVariableValue = getCssVariable(cssVariableName);
60+
61+
assert(
62+
isInvariantAcrossTheme(cssVariableValue),
63+
"CSS variable for TypoGraphy that depends on the theme have been introduce, the alg need to be made a bit more sophisticated."
64+
);
65+
assert(
66+
isInvariantAcrossScreenSizes(cssVariableValue),
67+
"CSS variable for TypoGraphy that depends on screen width have been introduce, the alg need to be made a bit more sophisticated."
68+
);
69+
70+
return cssVariableValue.root.light;
71+
});
72+
})())
73+
);
3874

3975
matchedSelectors.forEach(selector => {
4076
const typographyVariant = typographyVariants.find(

src/test/bin/cssVariable.test.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { createGetCssVariable } from "../../bin/css_to_ts/cssVariable";
1+
import {
2+
createGetCssVariable,
3+
isInvariantAcrossTheme,
4+
isInvariantAcrossScreenSizes
5+
} from "../../bin/css_to_ts/cssVariable";
26
import type { CssVariableValue } from "../../bin/css_to_ts/cssVariable";
37
import { same } from "evt/tools/inDepth";
48
import { assert } from "tsafe/assert";
@@ -59,6 +63,9 @@ const { getCssVariable } = createGetCssVariable(rawCssCode);
5963
}
6064
};
6165

66+
assert(isInvariantAcrossScreenSizes(expected));
67+
assert(isInvariantAcrossTheme(expected));
68+
6269
const got = getCssVariable("--my-var");
6370

6471
assert(same(got, expected));
@@ -88,6 +95,9 @@ const { getCssVariable } = createGetCssVariable(rawCssCode);
8895
}
8996
};
9097

98+
assert(isInvariantAcrossScreenSizes(expected));
99+
assert(!isInvariantAcrossTheme(expected));
100+
91101
const got = getCssVariable("--my-var-2");
92102

93103
assert(same(got, expected));
@@ -117,6 +127,9 @@ const { getCssVariable } = createGetCssVariable(rawCssCode);
117127
}
118128
};
119129

130+
assert(!isInvariantAcrossScreenSizes(expected));
131+
assert(isInvariantAcrossTheme(expected));
132+
120133
const got = getCssVariable("--my-var-3");
121134

122135
assert(same(got, expected));
@@ -146,6 +159,9 @@ const { getCssVariable } = createGetCssVariable(rawCssCode);
146159
}
147160
};
148161

162+
assert(!isInvariantAcrossScreenSizes(expected));
163+
assert(!isInvariantAcrossTheme(expected));
164+
149165
const got = getCssVariable("--my-var-4");
150166

151167
assert(same(got, expected));

src/test/bin/main.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import "./breakpoints";
22
import "./colorDecisions";
33
import "./colorOptions";
4-
//import "./typography";
4+
import "./cssVariable.test";
5+
import "./typography";

src/test/bin/typography/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
import "./parseTypographyVariants.test.ts";
2-
import "./generateTypographyTsCode.test.ts.disabled";
1+
import "./generateTypographyTsCode.test";
2+
import "./parseTypographyVariants.test";

src/test/bin/typography/parseTypographyVariants.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,14 @@ sup {
347347
}
348348
349349
}
350+
351+
@media (min-width: 36em) { }
352+
353+
354+
@media (min-width: 78em) { }
355+
356+
@media (min-width: 62em) { }
357+
350358
`;
351359

352360
const got = parseTypographyVariants(rawCssCode);

0 commit comments

Comments
 (0)