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 abillity to prevent CssVarsProvider from using local storage #35837

Open
zroug opened this issue Jan 15, 2023 · 8 comments · May be fixed by #45136
Open

Add abillity to prevent CssVarsProvider from using local storage #35837

zroug opened this issue Jan 15, 2023 · 8 comments · May be fixed by #45136
Assignees
Labels
customization: css Design CSS customizability package: system Specific to @mui/system

Comments

@zroug
Copy link

zroug commented Jan 15, 2023

Summary 💡

The current implementation of CssVarsProvider requires the use of local storage to store the theme settings. My suggestion is to make the use of local storage optional. One idea is to add a mode property to CssVarsProvider, in addition to the existing defaultMode property. With this new property, the theme would be controlled solely by the property and would not be affected by local storage. Another suggestion is to delay writing to local storage until the theme is explicitly changed using the setter of the useColorScheme hook. That way, if the setter is not used, nothing would be written to local storage.

Examples 🌈

No response

Motivation 🔦

The current version of CssVarsProvider requires the use of local storage, even though it is unnecessary in certain situations. For example, if the theme is only controlled by the system theme, or if there is only one theme, there is no need to use local storage. Local storage adds unnecessary complexity due to statefulness. For example, it adds the need to check how divergent states in local storage and in code interact.

@zroug zroug added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Jan 15, 2023
@zannager zannager added package: styles Specific to @mui/styles. Legacy package, @material-ui/styled-engine is taking over in v5. customization: css Design CSS customizability and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Jan 16, 2023
@kingpalethe
Copy link

Has anyone found a solution for this for usage in Joy? I need to change the color scheme programmatically for certain templates, and it would seem I would need to interact with the local storage and CSSVarsProvider in order to do this, in a complex way?

@tuckergordon
Copy link

I'm not sure if this belongs as a comment on this ticket or if it should be its own ticket, but using localstorage like this creates some developer headaches when developing apps with different themes.

Say I have an app foo and another app bar and each app has a different theme. I first open foo locally at localhost:8080. Later I switch to working on bar and I open it at localhost:8080 and it shows up with foo's theme.

This seems to require manually going in and clearing localstorage, so having an ability to disable that feature would be useful!

At minimum, I would suggest adding a note to the documentation instructing developers to clear localstorage if switching between sites with different themes.

@NotYoojun
Copy link

The same issue, I don't want MUI to save the modes into localStorage. Instead we have our own theme manager.

@acomanescu
Copy link

Maybe the ThemeProvider can expose a prop to set a StorageProvider that's an object/class instance that has getKey setKey where setMode can write into and read from. It's a really important feature if you need to have the same mode across multiple browsers/devices for the same user.

@ggascoigne
Copy link

I hate to jump on this with a me-too comment, but since there's been no obvious movement here I'd like to add my use case to the list.

We provide an internal library that use @mui, we expose our own provider that configures themes etc. and as part of that we manage themes in our own code. This include setting the preferred theme.

Having @mui explicitly try and do all of this for us just adds a list of features to work around, and honestly I've spent more times dealing with this than I have getting everything else related to css-variables working nicely.

I don't want this library to automatically persist state. I don't particularly want this library to sync themes across multiple tabs (by reading that variable), at least make this opt-in.

Can we please have an option to disable this behavior, it's such an intrusive change in behavior when compared to the previous interface.

@NotYoojun
Copy link

Is anybody caring about this? These items are really messing up my app. I don't want these crap to appear here.

image

@mnajdova mnajdova added package: system Specific to @mui/system and removed package: styles Specific to @mui/styles. Legacy package, @material-ui/styled-engine is taking over in v5. labels Jan 20, 2025
@mnajdova
Copy link
Member

Maybe the ThemeProvider can expose a prop to set a StorageProvider that's an object/class instance that has getKey setKey where setMode can write into and read from. It's a really important feature if you need to have the same mode across multiple browsers/devices for the same user.

This makes sense to me. cc @siriwatknp can you look into this please?

@mnajdova mnajdova assigned siriwatknp and unassigned mnajdova Jan 20, 2025
@mnajdova mnajdova moved this to Selected in Material UI Jan 20, 2025
@siriwatknp
Copy link
Member

Yes, I'm looking into it.

@DiegoAndai DiegoAndai moved this from Selected to In progress in Material UI Jan 27, 2025
@siriwatknp siriwatknp linked a pull request Jan 29, 2025 that will close this issue
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
customization: css Design CSS customizability package: system Specific to @mui/system
Projects
Status: In progress
Development

Successfully merging a pull request may close this issue.

9 participants