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 2 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 @@
</ThemeProvider>
```

## Setting the default mode

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

Check warning on line 137 in docs/data/material/customization/dark-mode/dark-mode.md

View workflow job for this annotation

GitHub Actions / runner / vale

[vale] reported by reviewdog 🐶 [Google.Will] Avoid using 'will'. Raw Output: {"message": "[Google.Will] Avoid using 'will'.", "location": {"path": "docs/data/material/customization/dark-mode/dark-mode.md", "range": {"start": {"line": 137, "column": 84}}}, "severity": "WARNING"}
siriwatknp marked this conversation as resolved.
Show resolved Hide resolved

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('should be dark by default', () => {
siriwatknp marked this conversation as resolved.
Show resolved Hide resolved
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 storage is empty,
Copy link
Member

@aarongarciah aarongarciah Oct 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does "storage" mean? Could we specify a bit more?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be local storage is more direct?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

siriwatknp marked this conversation as resolved.
Show resolved Hide resolved
* require the theme to have `colorSchemes` with light and dark.
siriwatknp marked this conversation as resolved.
Show resolved Hide resolved
* @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
Loading