Skip to content

Commit bce666d

Browse files
committed
Enable to provide own useTheme()
1 parent 03192bf commit bce666d

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

src/mui.tsx

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,7 @@ import defaultMuiShadows from "@mui/material/styles/shadows";
1515
import type { Shadows } from "@mui/material/styles";
1616
import { id } from "tsafe/id";
1717

18-
export interface MuiDsfrThemeParams {
19-
isDark: boolean;
20-
}
21-
22-
export function getMuiDsfrThemeOptions(params: MuiDsfrThemeParams): ThemeOptions {
18+
export function getMuiDsfrThemeOptions(params: { isDark: boolean }): ThemeOptions {
2319
const { isDark } = params;
2420

2521
const { options, decisions } = getColors(isDark);
@@ -236,15 +232,23 @@ export function getMuiDsfrThemeOptions(params: MuiDsfrThemeParams): ThemeOptions
236232
};
237233
}
238234

239-
export function createMuiDsfrTheme(params: MuiDsfrThemeParams, ...args: object[]): MuiTheme {
240-
const options = getMuiDsfrThemeOptions(params);
241-
242-
const muiTheme = createTheme(options, ...args);
235+
/**
236+
*Generate a theme base on the options received.
237+
*
238+
* @param params — Dark or light mode.
239+
*
240+
* @param args — Deep merge the arguments with the about to be returned theme.
241+
*
242+
* @returns — A complete, ready-to-use mui theme object.
243+
*/
244+
export function createMuiDsfrTheme(params: { isDark: boolean }, ...args: object[]): MuiTheme {
245+
const muiTheme = createTheme(getMuiDsfrThemeOptions(params), ...args);
243246

244247
return muiTheme;
245248
}
246249

247250
export function createMuiDsfrThemeProvider(params?: {
251+
useIsDark?: () => { isDark: boolean };
248252
augmentMuiTheme: (params: {
249253
/** WARNING: The types is lying here.
250254
* It's a Theme as defined in import type { Theme } from "@mui/material/styles";
@@ -254,7 +258,7 @@ export function createMuiDsfrThemeProvider(params?: {
254258
frColorTheme: ColorTheme;
255259
}) => MuiTheme;
256260
}) {
257-
const { augmentMuiTheme } = params ?? {};
261+
const { augmentMuiTheme, useIsDark: useIsDark_props = useIsDark } = params ?? {};
258262

259263
type Props = {
260264
children: ReactNode;
@@ -264,7 +268,7 @@ export function createMuiDsfrThemeProvider(params?: {
264268
function MuiDsfrThemeProvider(props: Props) {
265269
const { children } = props;
266270

267-
const { isDark } = useIsDark();
271+
const { isDark } = useIsDark_props();
268272

269273
const theme = useMemo(() => {
270274
const nonAugmentedMuiTheme = createMuiDsfrTheme({ isDark });

0 commit comments

Comments
 (0)