Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ThemeProvider] Support setting default mode #43951

Merged
merged 4 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions docs/data/material/customization/dark-mode/dark-mode.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,20 @@ To instantly switch between color schemes with no transition, apply the `disable
</ThemeProvider>
```

## Setting the default mode

When `colorSchemes` is provided, the default mode is `system`, which means the app uses the system preference when users first visit the site.

To set a different default mode, pass the `defaultMode` prop to the ThemeProvider component:

```js
<ThemeProvider theme={theme} defaultMode="dark">
```

:::info
The `defaultMode` value can be `'light'`, `'dark'`, or `'system'`.
:::

## Styling in dark mode

Use the `theme.applyStyles` utility to apply styles for a specific mode.
Expand Down
13 changes: 13 additions & 0 deletions packages/mui-material/src/styles/ThemeProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,18 @@ describe('ThemeProvider', () => {

expect(getByTestId('mode-switcher')).to.have.property('value', 'dark');
});

it('allows default mode to be changed', () => {
const theme = createTheme({
colorSchemes: { dark: true },
});
const { getByTestId } = render(
<ThemeProvider theme={theme} defaultMode="dark">
<ModeSwitcher />
</ThemeProvider>,
);

expect(getByTestId('mode-switcher')).to.have.property('value', 'dark');
});
});
});
6 changes: 6 additions & 0 deletions packages/mui-material/src/styles/ThemeProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ export interface ThemeProviderProps<Theme = DefaultTheme> extends ThemeProviderC
* @default document
*/
documentNode?: Document | null;
/**
* The default mode when the local storage has no mode yet,
* requires the theme to have `colorSchemes` with light and dark.
* @default 'system'
*/
defaultMode?: 'light' | 'dark' | 'system';
/**
* The window that attaches the 'storage' event listener
* @default window
Expand Down
6 changes: 6 additions & 0 deletions packages/mui-system/src/cssVars/createCssVarsProvider.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ export interface CreateCssVarsProviderResult<
colorSchemeSelector?: 'media' | 'class' | 'data' | string;
}
>;
/**
* The default mode when the storage is empty,
* require the theme to have `colorSchemes` with light and dark.
* @default 'system'
*/
defaultMode?: 'light' | 'dark' | 'system';
/**
* The document used to perform `disableTransitionOnChange` feature
* @default document
Expand Down
8 changes: 7 additions & 1 deletion packages/mui-system/src/cssVars/createCssVarsProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export default function createCssVarsProvider(options) {
colorSchemeNode = typeof document === 'undefined' ? undefined : document.documentElement,
disableNestedContext = false,
disableStyleSheetGeneration = false,
defaultMode: initialMode = 'system',
} = props;
const hasMounted = React.useRef(false);
const upperTheme = muiUseTheme();
Expand Down Expand Up @@ -92,7 +93,7 @@ export default function createCssVarsProvider(options) {
typeof defaultColorScheme === 'string' ? defaultColorScheme : defaultColorScheme.dark;
const defaultMode =
colorSchemes[defaultLightColorScheme] && colorSchemes[defaultDarkColorScheme]
? 'system'
? initialMode
: colorSchemes[restThemeProp.defaultColorScheme]?.palette?.mode ||
restThemeProp.palette?.mode;

Expand Down Expand Up @@ -299,6 +300,11 @@ export default function createCssVarsProvider(options) {
* localStorage key used to store `colorScheme`
*/
colorSchemeStorageKey: PropTypes.string,
/**
* The default mode when the storage is empty,
* require the theme to have `colorSchemes` with light and dark.
*/
defaultMode: PropTypes.string,
/**
* If `true`, the provider creates its own context and generate stylesheet as if it is a root `CssVarsProvider`.
*/
Expand Down