next-themes icon indicating copy to clipboard operation
next-themes copied to clipboard

add option to use sessionStorage instead of localStorage

Open macguirerintoul opened this issue 3 years ago • 2 comments

macguirerintoul avatar Dec 29 '21 20:12 macguirerintoul

Thanks! I'd like to think about how to support this better than just a boolean. I wonder if we're missing any other common storage providers that would warrant this being a generic cache option. Or perhaps caching/syncing can be turned off entirely.

pacocoursey avatar Feb 20 '22 15:02 pacocoursey

@pacocoursey, you could alternatively add a storage?: 'local' | 'session' | Storage; prop to ThemeProvider to allow for local/session/custom storage implementations:

// localStorage (default)
function MyApp({ Component, pageProps }) {
  return (
    <ThemeProvider storage="local">
      <Component {...pageProps} />
    </ThemeProvider>
  )
}
// sessionStorage
function MyApp({ Component, pageProps }) {
  return (
    <ThemeProvider storage="session">
      <Component {...pageProps} />
    </ThemeProvider>
  )
}
// Custom storage implementation
const myCustomStorage: Storage = { /* Implementation here */ };

function MyApp({ Component, pageProps }) {
  return (
    <ThemeProvider storage={myCustomStorage}>
      <Component {...pageProps} />
    </ThemeProvider>
  )
}

jasongerbes avatar Jan 20 '23 05:01 jasongerbes