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

Theme switching with dark being the default requires an extra initial click on the ToggleThemeButton to switch to light #8426

Closed
christiaanwesterbeek opened this issue Nov 23, 2022 · 3 comments · Fixed by #8455
Labels

Comments

@christiaanwesterbeek
Copy link
Contributor

christiaanwesterbeek commented Nov 23, 2022

The documentation of ToggleThemeButton state that:

The component lets users switch from light to dark mode, and persists that choice by leveraging the store.

So me trying to have darkTheme be the default theme seems out of scope, but it works, but requires an additional click.

What you were expecting:
Hoping to have dark theme the default theme, and switch to light theme using the ToggleThemeButton.

What happened instead:
It works, but requires an additional initial click on the button.

Steps to reproduce:

  • Go to https://stackblitz.com/edit/github-vymxfy
  • It opens in dark, which is intentional (see theme=darkTheme in Admin)
  • Click once on the ToggleThemeButton --> nothing happens
  • Click again on the ToggleThemeButton --> no theme switches to light
const lightTheme = { palette: { mode: 'light' } };
const darkTheme = { palette: { mode: 'dark' } };

<Admin
    theme={darkTheme}
    store={memoryStore()}
    ...
/>

<AppBar {...props}>
      <Typography flex="1" variant="h6" id="react-admin-title" />
      <div style={{ width: 300 }}>{`theme: ${theme.palette.mode}`}</div>
      <ToggleThemeButton lightTheme={lightTheme} darkTheme={darkTheme} />
</AppBar>

Environment

  • React-admin version: 4.5.2
  • Last version that did not exhibit the issue (if applicable): -
@christiaanwesterbeek
Copy link
Contributor Author

I made a copy of ToggleThemeButton and made only the change of removing the default parameter of lightTheme that is passed to useTheme. Then theme switching works without the extra click. However it will break the app (on load) if no theme is passed to Admin.

@WiXSL
Copy link
Contributor

WiXSL commented Nov 25, 2022

This seems to happen when you pass a custom memoryStore only to the Admin,
I'm marking it as a bug

@WiXSL WiXSL added the bug label Nov 25, 2022
@christiaanwesterbeek
Copy link
Contributor Author

The bug is there with memoryStore and with the default localStorageStore.
With using localStorageStore the bug is camouflaged after loading the app more than once.
With using memoryStore the bug is never camouflaged, so it may seem that memoryStore is the cause when you try a few times in stackblitz, but it's not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants