Skip to content

Commit 0853cc2

Browse files
committed
Adapt theming documentation
1 parent 2cbc2c3 commit 0853cc2

2 files changed

Lines changed: 60 additions & 28 deletions

File tree

docs/Admin.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ const App = () => (
425425

426426
## `darkTheme`
427427

428-
If you want to support light and dark mode, you can provide a `darkTheme` in addition to the `theme` prop. The `darkTheme` will be used when the user's browser is in dark mode, or when the user manually switches to dark mode using [the `<ToggleThemeButton>` component](./ToggleThemeButton.md).
428+
If you want to support both light and dark mode, you can provide a `darkTheme` in addition to the `theme` prop. The `darkTheme` will be used when the user's browser is in dark mode, or when the user manually switches to dark mode using [the `<ToggleThemeButton>` component](./ToggleThemeButton.md).
429429

430430
![Dark mode](./img/dark-theme.png)
431431

@@ -776,16 +776,16 @@ Check the [Preferences documentation](./Store.md) for more details.
776776

777777
## `theme`
778778

779-
Material UI supports [theming](https://mui.com/material-ui/customization/theming/). This lets you customize the look and feel of an admin by overriding fonts, colors, and spacing. You can provide a custom Material UI theme by using the `theme` prop:
779+
Material UI supports [theming](https://mui.com/material-ui/customization/theming/). This lets you customize the look and feel of an admin by overriding fonts, colors, and spacing. You can provide a custom Material UI theme by using the `theme` prop.
780+
781+
For instance, to use a dark theme by default:
780782

781783
```jsx
782784
import { defaultTheme } from 'react-admin';
783785

784786
const theme = {
785787
...defaultTheme,
786-
palette: {
787-
mode: 'dark', // Switching the dark mode on is a single property value change.
788-
},
788+
palette: { mode: 'dark' },
789789
};
790790

791791
const App = () => (
@@ -797,6 +797,8 @@ const App = () => (
797797

798798
![Dark theme](./img/dark-theme.png)
799799

800+
If you want to support both a light and a dark theme, check out [the `<Admin darkTheme>` prop](#darktheme).
801+
800802
For more details on predefined themes and custom themes, refer to [the Theming chapter](./Theming.md#global-theme-overrides) of the react-admin documentation.
801803

802804
## `title`

docs/Theming.md

Lines changed: 53 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ Note that you don't need to call `createTheme` yourself. React-admin will do it
243243

244244
Again, to guess the name of the subclass to use (like `.RaDatagrid-headerCell` above) for customizing a component, you can use the developer tools of your browser, or check the react-admin documentation for individual components (e.g. the [Datagrid CSS documentation](./Datagrid.md#sx-css-api)).
245245

246-
You can use this technique to override not only styles, but also default for components. That's how react-admin applies the `filled` variant to all `TextField` components. So for instance, to change the variant to `outlined`, create a custom theme as follows:
246+
You can use this technique to override not only styles, but also defaults for components. That's how react-admin applies the `filled` variant to all `TextField` components. So for instance, to change the variant to `outlined`, create a custom theme as follows:
247247

248248
```jsx
249249
import { defaultTheme } from 'react-admin';
@@ -367,58 +367,88 @@ const App = () => (
367367

368368
## Letting Users Choose The Theme
369369

370-
The `<ToggleThemeButton>` component lets users switch from light to dark mode, and persists that choice by leveraging the [store](./Store.md).
370+
It's a common practice to support both a light theme and a dark theme in an application, and let users choose which one they prefer.
371+
372+
React-admin's `<Admin>` component accepts a `darkTheme` mode in addition to the `theme` prop.
373+
374+
```jsx
375+
import { Admin, defaultTheme } from 'react-admin';
376+
377+
const lightTheme = defaultTheme;
378+
const darkTheme = { ...defaultTheme, palette: { mode: 'dark' } };
379+
380+
const App = () => (
381+
<Admin
382+
dataProvider={...}
383+
theme={lightTheme}
384+
darkTheme={darkTheme}
385+
>
386+
// ...
387+
</Admin>
388+
);
389+
```
390+
391+
With this setup, the default application theme will depend on the user's system settings. If the user has chosen a dark mode in their OS, react-admin will use the dark theme. Otherwise, it will use the light theme.
392+
393+
In addition, you can let users switch from one theme to the other: [the `<ToggleThemeButton>` component](./ToggleThemeButton.md) lets users switch from light to dark mode, and persists that choice by leveraging the [store](./Store.md).
371394

372395
<video controls autoplay muted loop>
373396
<source src="./img/ToggleThemeButton.webm" type="video/webm"/>
374397
Your browser does not support the video tag.
375398
</video>
376399

377-
378-
You can add the `<ToggleThemeButton>` to a custom App Bar:
400+
Add the `<ToggleThemeButton>` to a custom App Bar:
379401

380402
```jsx
403+
// in src/MyLayout.jsx
381404
import * as React from 'react';
382-
import { defaultTheme, Layout, AppBar, ToggleThemeButton, TitlePortal } from 'react-admin';
383-
import { createTheme, Box, Typography } from '@mui/material';
384-
385-
const darkTheme = createTheme({
386-
palette: { mode: 'dark' },
387-
});
405+
import { Layout, AppBar, ToggleThemeButton, TitlePortal } from 'react-admin';
406+
import { Box, Typography } from '@mui/material';
388407

389408
const MyAppBar = () => (
390409
<AppBar>
391410
<TitlePortal />
392-
<ToggleThemeButton lightTheme={defaultTheme} darkTheme={darkTheme} />
411+
<ToggleThemeButton />
393412
</AppBar>
394413
);
395414

396415
const MyLayout = props => <Layout {...props} appBar={MyAppBar} />;
397416
```
398417

418+
Then, pass the custom layout to the `<Admin>` component:
419+
420+
```jsx
421+
import { Admin, defaultTheme } from 'react-admin';
422+
423+
const lightTheme = defaultTheme;
424+
const darkTheme = { ...defaultTheme, palette: { mode: 'dark' } };
425+
426+
const App = () => (
427+
<Admin
428+
dataProvider={...}
429+
theme={lightTheme}
430+
darkTheme={darkTheme}
431+
layout={MyLayout}
432+
>
433+
// ...
434+
</Admin>
435+
);
436+
```
437+
399438
## Changing the Theme Programmatically
400439

401-
React-admin provides the `useTheme` hook to read and update the theme programmatically. It uses the same syntax as `useState`.
402-
Its used internally by `ToggleThemeButton` component.
440+
React-admin provides the `useTheme` hook to read and update the theme programmatically. It uses the same syntax as `useState`. Its used internally by [the `<ToggleThemeButton>` component](./ToggleThemeButton.md).
403441

404442
```jsx
405443
import { defaultTheme, useTheme } from 'react-admin';
406444
import { Button } from '@mui/material';
407445

408-
const lightTheme = defaultTheme;
409-
const darkTheme = {
410-
...defaultTheme,
411-
palette: {
412-
mode: 'dark',
413-
},
414-
};
415-
416446
const ThemeToggler = () => {
417447
const [theme, setTheme] = useTheme();
418448

419449
return (
420-
<Button onClick={() => setTheme(theme.palette.mode === 'dark' ? lightTheme : darkTheme)}>
421-
{theme.palette.mode === 'dark' ? 'Switch to light theme' : 'Switch to dark theme'}
450+
<Button onClick={() => setTheme(theme === 'dark' ? 'light' : 'dark')}>
451+
{theme === 'dark' ? 'Switch to light theme' : 'Switch to dark theme'}
422452
</Button>
423453
);
424454
}

0 commit comments

Comments
 (0)