Skip to content

Commit da102ad

Browse files
committed
Compat MUI DSFR CSS
1 parent d8d5079 commit da102ad

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

src/scripts/build/build.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
import * as child_process from "child_process";
1212
import { oppa } from "oppa";
1313
import { assert } from "tsafe/assert";
14+
import { patchCssForMui } from "./patchCssForMui";
1415

1516
(async () => {
1617
const { args } = oppa()
@@ -46,6 +47,21 @@ import { assert } from "tsafe/assert";
4647
)
4748
);
4849

50+
{
51+
const { rawDsfrCssCodePatchedForMui, rawDsfrCssCodePatchedForMuiMinified } = patchCssForMui(
52+
{ rawDsfrCssCode }
53+
);
54+
55+
(
56+
[
57+
[rawDsfrCssCodePatchedForMui, ".css"],
58+
[rawDsfrCssCodePatchedForMuiMinified, ".min.css"]
59+
] as const
60+
).forEach(([rawCssCode, ext]) =>
61+
fs.writeFileSync(pathJoin(dsfrDirPath, `dsfr${ext}`), Buffer.from(rawCssCode, "utf8"))
62+
);
63+
}
64+
4965
const icons = await collectIcons({
5066
"remixiconDirPath": pathJoin(nodeModuleDirPath, "remixicon"),
5167
"iconsCssRawCode": fs
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* eslint-disable @typescript-eslint/no-non-null-assertion */
2+
import * as css from "css";
3+
4+
export function patchCssForMui(params: { rawDsfrCssCode: string }) {
5+
const { rawDsfrCssCode } = params;
6+
7+
const parsedCss = css.parse(rawDsfrCssCode);
8+
9+
(function callee(rules: any[], media: string): any[] {
10+
return [
11+
...rules.filter(({ type }) => type === "rule").map(rule => [rule, media]),
12+
...rules
13+
.filter(({ type }) => type === "media")
14+
.map(({ rules, media }) => callee(rules, media))
15+
.flat()
16+
];
17+
})(parsedCss.stylesheet!.rules as any[], "root").forEach(
18+
([rule, media]) =>
19+
(rule.selectors = rule.selectors.map((selector: string) => {
20+
if (media === "(hover: hover) and (pointer: fine)") {
21+
if (
22+
selector === "button:not(:disabled):hover" ||
23+
selector === "button:not(:disabled):active"
24+
) {
25+
return `${selector}:not([class^="Mui"])`;
26+
}
27+
}
28+
29+
return selector;
30+
}))
31+
);
32+
33+
return {
34+
"rawDsfrCssCodePatchedForMui": css.stringify(parsedCss, { "compress": false }),
35+
"rawDsfrCssCodePatchedForMuiMinified": css.stringify(parsedCss, { "compress": true })
36+
};
37+
}

0 commit comments

Comments
 (0)