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

Is there any way to implement useDarkMode config static or server-side? (NextJS) #64

Open
tendan opened this issue Dec 4, 2020 · 5 comments

Comments

@tendan
Copy link

tendan commented Dec 4, 2020

I'm trying to implement the configuration for useDarkMode hook, it works when NextJS uses CSR, but has an error when renders in server-side.
Any way to implement that configuration in SSG or SSR?
Also i want to use the element for <html> anchor tag, because of using Tailwind CSS, so it's crucial for me to implement element attribute.

My code:

function MyApp({ Component, pageProps }: AppProps) {
  useEffect(() => {
    // @ts-ignore
    const darkMode = useDarkMode(false, {
      classNameDark: "dark",
      classNameLight: "light",
      element: document.getElementsByTagName("html")[0]
    });
  }, [])


  return (
    <Fragment>
      <Head>
        <title>Oficjalna strona Nostalgawki</title>
        <meta name="viewport" content="initial-scale=1, width=device-width"/>
      </Head>
      <div className={"main"}>
        <AnimatePresence exitBeforeEnter>
            <Navbar key={"navbar"}/>
            <Component key={"component"} {...pageProps} />
            <Footer key={"footer"}/>
        </AnimatePresence>
      </div>
    </Fragment>
  );
}
@djD-REK
Copy link

djD-REK commented Dec 6, 2020 via email

@vacekj
Copy link

vacekj commented Dec 17, 2020

Also running into the same issue. What are your thoughts on this @donavon ?

@donavon
Copy link
Owner

donavon commented Dec 18, 2020

@tendan
useDarkMode (or any hook for that matter) can NOT be used inside of a useEffect hook. There's also no need.

As to using html instead, you can always specify an onChange handler and do the work yourself.

For example:

  const { value } = useDarkMode(false, { 
    onChange: (state: boolean)  => {
      const htmlElement = document.documentElement;
      if (state) {
        htmlElement.classList.add('dark');
        htmlElement.classList.remove('light');
      } else {
        htmlElement.classList.add('light');
        htmlElement.classList.remove('dark');
      }
    }
  });

@donavon
Copy link
Owner

donavon commented Dec 18, 2020

Can you give a short example of this failing in SSR that I can clone and run locally?

@kelvindecosta
Copy link

Hey!

Since the localStorage variable doesn't really exist during SSR, the hook might have some unintended behavior.

I managed to get it working well by using use-ssr.

// ...other imports
import { useSSR } from "use-ssr"

// Prints whether to dark mode is on / off
const DarkThemeTruthy = () => {
  // Get theme value
  const { value } = useDarkMode(false, {
    classNameDark: "dark",
    classNameLight: "light",
    storageKey: "dark-theme",
  })

  // Get rendering mode
  const { isBrowser } = useSSR()

  return <Fragment>{isBrowser && <div>{value}</div>}</Fragment>
}

I've tested this on a Gatsby site, using both React and Preact.
It works quite well but there is one caveat.
Since useSSR is being used, the <div> is Client Side Rendered.

Hope this helps!

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

No branches or pull requests

5 participants