question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add a prop to change the color mode key in local storage

See original GitHub issue

Description

The way Chakra persists the color mode in an application is by storing it in the browser’s local storage under the key name chakra-ui-color-mode. It would be great if we could pass a prop to the ColorModeScript component to give a custom key name for storing the color mode.

Problem Statement/Justification

Adding more customizations can help in really personalizing the application. Since the prefix for css variables can be passed as a prop, I feel like changing the color mode key name in the persistent storage can be great addition too.

Proposed Solution or API

Add a property to the ColorModeScriptProps interface.

interface ColorModeScriptProps {
  initialColorMode?: ConfigColorMode;
  nonce?: string;
  storageKeyName?: string;
}

Not sure if the name storageKeyName is the most appropriate, but that is just the idea.

Alternatives

No response

Additional Information

Image of where/how Chakra stores the color mode in the browser.

chakra-ui-color-mode

Additionally, I’d love to make a contribution if this approved!

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
segunadebayocommented, May 17, 2022

We now provide a way to customize the storage keys in Chakra UI v2.

  • Use the createLocalStorageManager or createCookieStorageManager to create a new storage for the color mode
  • Pass the new manager to the ChakraProvider
  • If you use ColorModeScript, pass the storageKey prop and set it to the key that matches the step above
const manager = createLocalStorageManager("my-key")

// in root
<ChakraProvider  colorModeManager={manager} />

// in script
<ColorModeScript storageKey="my-key" />
2reactions
TimKolbergercommented, Feb 10, 2022

In 1.4+ we added a theme & ColorModeScript prop cssVarPrefix Chakra UI Docs | Theme | Config.

What do you think about a new property on theme & ColorModeScriptProps localStoragePrefix?

const theme = extendTheme({
  config: {
    cssVarPrefix: 'ck',
    // what about 
    localStoragePrefix: 'ck'
  },
})
<ColorModeScript localStoragePrefix="ck" />
Read more comments on GitHub >

github_iconTop Results From Across the Web

React - Should I use localstorage when changing a theme?
So in my opinion, localstorage should be the way to go. ... Show activity on this post. you could use css-variable to switch...
Read more >
Dark Mode w/ Custom React Hooks using Local Storage
Creating a custom hook in React to store state in local storage, synchronize it across browser tabs, and have its value retrieved on...
Read more >
Color Modes - Theme UI
Use the useColorMode hook in your application to change the color mode. This value will be stored in localStorage and used whenever the...
Read more >
Color Mode - Chakra UI
Tip: Chakra stores the color mode in localStorage and appends a className to the body to ensure the color mode is persistent.
Read more >
Window localStorage Property - W3Schools
Syntax. window.localStorage. or just: localStorage. Save Data to Local Storage. localStorage.setItem(key, value); ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found