Skip to content

Commit 5e067ed

Browse files
committed
Enable to augment the MUI theme
1 parent 36c0654 commit 5e067ed

File tree

3 files changed

+69
-30
lines changed

3 files changed

+69
-30
lines changed

src/lib/colors.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
import { useMemo } from "react";
21
import { getColorOptions } from "./generatedFromCss/getColorOptions";
32
import type { ColorOptions } from "./generatedFromCss/getColorOptions";
43
import { getColorDecisions } from "./generatedFromCss/getColorDecisions";
54
import type { ColorDecisions } from "./generatedFromCss/getColorDecisions";
65
import { useIsDark } from "./darkMode";
6+
import { memoize } from "./tools/memoize";
77

88
export type ColorTheme = {
99
isDark: boolean;
1010
decisions: ColorDecisions;
1111
options: ColorOptions;
1212
};
1313

14-
export function useColors(): ColorTheme {
15-
const { isDark } = useIsDark();
16-
17-
const options = useMemo(() => getColorOptions({ isDark }), [isDark]);
18-
19-
const decisions = useMemo(() => getColorDecisions({ "colorOptions": options }), [options]);
14+
export const getColors = memoize(
15+
(isDark: boolean): ColorTheme => {
16+
const options = getColorOptions({ isDark });
2017

21-
const colorTheme = useMemo(
22-
(): ColorTheme => ({
18+
return {
2319
isDark,
2420
options,
25-
decisions
26-
}),
27-
[isDark]
28-
);
21+
"decisions": getColorDecisions({ "colorOptions": options })
22+
};
23+
},
24+
{ "max": 1 }
25+
);
26+
27+
export function useColors(): ColorTheme {
28+
const { isDark } = useIsDark();
2929

30-
return colorTheme;
30+
return getColors(isDark);
3131
}

src/lib/tools/memoize.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,31 @@ export function memoize<Args extends (number | boolean | string)[], R>(
22
fn: (...args: Args) => R,
33
options?: {
44
argsLength?: number;
5+
max?: number;
56
}
67
): (...args: Args) => R {
7-
const cache: Record<string, R> = {};
8+
const cache = new Map<string, R>();
89

9-
const { argsLength = fn.length } = options ?? {};
10+
const { argsLength = fn.length, max = Infinity } = options ?? {};
1011

1112
return ((...args: Args) => {
1213
const key = JSON.stringify(args.slice(0, argsLength).join("-sIs9sAslOdeWlEdIos3-"));
1314

14-
if (key in cache) {
15-
return cache[key];
15+
if (cache.has(key)) {
16+
return cache.get(key);
1617
}
1718

18-
return (cache[key] = fn(...args));
19+
if (max === cache.size) {
20+
for (const key of cache.keys()) {
21+
cache.delete(key);
22+
break;
23+
}
24+
}
25+
26+
const value = fn(...args);
27+
28+
cache.set(key, value);
29+
30+
return value;
1931
}) as any;
2032
}

src/mui.tsx

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,21 @@ import type { ReactNode } from "react";
44
import { breakpointValues, breakpointValuesUnit } from "./lib/generatedFromCss/breakpoints";
55
import type { Theme as MuiTheme } from "@mui/material/styles";
66
import { createTheme, ThemeProvider as MuiThemeProvider } from "@mui/material/styles";
7-
import { getColorDecisions } from "./lib/generatedFromCss/getColorDecisions";
8-
import { getColorOptions } from "./lib/generatedFromCss/getColorOptions";
7+
import { getColors } from "./lib/colors";
98
import { useIsDark } from "./lib/darkMode";
109
import { typography } from "./lib/generatedFromCss/typography";
1110
import { spacingTokenByValue } from "./lib/generatedFromCss/spacing";
11+
import type { ColorTheme } from "./lib/colors";
12+
//import type { Components } from "@mui/material/styles";
13+
14+
/*
15+
type NonAugmentedBaseMuiTheme = NonNullable<MuiTheme["components"]> extends Components<infer BaseMuiTheme> ? BaseMuiTheme : undefined;
16+
17+
18+
export type NonAugmentedMuiTheme = NonAugmentedBaseMuiTheme & {
19+
components?: Components<NonAugmentedBaseMuiTheme>;
20+
};
21+
*/
1222

1323
function createMuiDsfrTheme(params: { isDark: boolean }): MuiTheme {
1424
const { isDark } = params;
@@ -22,18 +32,17 @@ function createMuiDsfrTheme(params: { isDark: boolean }): MuiTheme {
2232
"values": breakpointValues
2333
},
2434
"palette": (() => {
25-
const colorOptions = getColorOptions({ isDark });
26-
const colorDecisions = getColorDecisions({ colorOptions });
35+
const { decisions } = getColors(isDark);
2736

2837
return {
2938
"mode": isDark ? "dark" : "light",
3039
"primary": {
31-
"main": colorDecisions.background.actionHigh.blueFrance.default,
32-
"light": colorDecisions.background.actionLow.blueFrance.default
40+
"main": decisions.background.actionHigh.blueFrance.default,
41+
"light": decisions.background.actionLow.blueFrance.default
3342
},
3443
"secondary": {
35-
"main": colorDecisions.background.actionHigh.redMarianne.default,
36-
"light": colorDecisions.background.actionLow.redMarianne.default
44+
"main": decisions.background.actionHigh.redMarianne.default,
45+
"light": decisions.background.actionLow.redMarianne.default
3746
}
3847
/*
3948
"primary": {
@@ -82,18 +91,36 @@ function createMuiDsfrTheme(params: { isDark: boolean }): MuiTheme {
8291
return muiTheme;
8392
}
8493

94+
export type NonAugmentedMuiTheme = MuiTheme;
95+
8596
export type MuiDsfrThemeProviderProps = {
8697
children: ReactNode;
98+
/** If you have implemented theme augmentation */
99+
augmentMuiTheme?: (params: {
100+
nonAugmentedMuiTheme: NonAugmentedMuiTheme;
101+
frColorTheme: ColorTheme;
102+
}) => MuiTheme;
87103
};
88104

89105
export function MuiDsfrThemeProvider(props: MuiDsfrThemeProviderProps) {
90-
const { children } = props;
106+
const { children, augmentMuiTheme } = props;
91107

92108
const { isDark } = useIsDark();
93109

94-
const muiTheme = useMemo(() => createMuiDsfrTheme({ isDark }), [isDark]);
110+
const augmentedMuiTheme = useMemo(() => {
111+
const muiTheme = createMuiDsfrTheme({ isDark });
112+
113+
if (augmentMuiTheme === undefined) {
114+
return muiTheme;
115+
}
116+
117+
return augmentMuiTheme({
118+
"frColorTheme": getColors(isDark),
119+
"nonAugmentedMuiTheme": muiTheme
120+
});
121+
}, [isDark, augmentMuiTheme]);
95122

96-
return <MuiThemeProvider theme={muiTheme}>{children}</MuiThemeProvider>;
123+
return <MuiThemeProvider theme={augmentedMuiTheme}>{children}</MuiThemeProvider>;
97124
}
98125

99126
/*

0 commit comments

Comments
 (0)