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

Add ability to default to dark mode when users prefer it #8874

Merged
merged 21 commits into from
May 9, 2023
Merged

Conversation

fzaninotto
Copy link
Member

@fzaninotto fzaninotto commented May 2, 2023

Problems

  • When using an app with two themes, it defaults to the light theme even though the user has a system preference for dark theme
  • When developers change the theme, if a user has used the theme toggle in the past, they won't get the theme change
  • Themes must be serializable to JSON to use the ToggleThemeButton, which forbids themes with functions

Solution

  • If the developer specifies an <Admin darkTheme> prop, and a user prefers dark mode, the app defaults to dark mode
  • The useTheme hook now saves a theme mode ('light' or 'dark') in the preferences rather than a theme object
  • <ToggleThemeButton> no longer needs theme props

The solution is made backwards compatible by keeping the old <ToggleThemeButton>.

Example

// in src/MyAppBar.js
import { AppBar, TitlePortal, ToggleThemeButton } from 'react-admin';

export const MyAppBar = () => (
    <AppBar>
        <TitlePortal />
        <ToggleThemeButton />
    </AppBar>>
);

// in src/App.js
import { Admin, Layout } from 'react-admin';
import { MyAppBar } from './MyAppBar';
import { darkTheme, lightTheme } from './themes';

const MyLayout = (props) => <Layout {...props} appBar={MyAppBar} />;

const App = () => (
    <Admin
        dataProvider={dataProvider}
        layout={MyLayout} 
        theme={lightTheme}
        darkTheme={darkTheme}
    >
        ...
    </Admin>
);

Closes #8670

  • Add <Admin darkTheme> prop
  • Add <Admin defaultToLighTheme> prop
  • Create ThemesContext
  • Update <ToggleThemeButton> to use it
  • Update the AppBar to display the <ToggleThemeButton> by default when a dark mode is defined
  • Use a media query to default to the user preferred theme
  • Make it backwards compatible
  • Write documentation
  • Add tests
  • Use the <ToggleThemeButton> in one of our demos (ecommerce)

Copy link
Member Author

Choose a reason for hiding this comment

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

I moved and rewrote a lot of things in there. Best to read the new version rather than read the diff.

return (
<CoreAdminContext {...rest}>
<ThemeProvider theme={theme}>{children}</ThemeProvider>
<ThemesContext.Provider
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not fond of this syntax (ThemesContextProvider wraps ThemeProvider), but it's a good way to make the whole change backwards compatible.

Copy link
Contributor

Choose a reason for hiding this comment

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

Add a fixme in v5?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think the existing FIXMEs introduced in this PR will suffice

@@ -85,10 +85,10 @@ const theme = createTheme({

declare module '@mui/material/styles' {
interface Palette {
userDefined: PaletteColor;
userDefined?: PaletteColor;
Copy link
Member Author

Choose a reason for hiding this comment

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

This was making TS compilation fail in other tests

@fzaninotto fzaninotto added the RFR Ready For Review label May 7, 2023
@fzaninotto
Copy link
Member Author

Switching to RFR

@djhi djhi removed the WIP Work In Progress label May 9, 2023
Copy link
Contributor

@djhi djhi left a comment

Choose a reason for hiding this comment

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

Looks good!

return (
<CoreAdminContext {...rest}>
<ThemeProvider theme={theme}>{children}</ThemeProvider>
<ThemesContext.Provider
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a fixme in v5?

docs/Theming.md Outdated Show resolved Hide resolved
docs/Admin.md Outdated Show resolved Hide resolved
export interface ThemesContextValue {
darkTheme?: RaThemeOptions;
lightTheme?: RaThemeOptions;
defaultToLightTheme?: boolean;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why no a defaultTheme instead, allowing you to default to the dark theme if you want?

Copy link
Member Author

Choose a reason for hiding this comment

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

👍 You're right, it's better. Added in the next commit.

Co-authored-by: Gildas Garcia <1122076+djhi@users.noreply.github.com>
@djhi djhi added this to the 4.11.0 milestone May 9, 2023
@djhi djhi merged commit 906af62 into next May 9, 2023
8 checks passed
@djhi djhi deleted the theme-dark-mode branch May 9, 2023 12:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RFR Ready For Review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants