Created
          February 22, 2020 14:14 
        
      - 
      
- 
        Save skolhustick/0f4b1a46f446f9809c7ea7233025f0b8 to your computer and use it in GitHub Desktop. 
    NextJS Dark Theme
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import React, { useState, useEffect } from 'react' | |
| import useDarkMode from 'use-dark-mode' | |
| import { ThemeProvider } from 'styled-components' | |
| import { lightTheme, darkTheme } from '../theme' | |
| const MyApp = ({ Component, pageProps }) => { | |
| const [isMounted, setIsMounted] = useState(false) | |
| const darkMode = useDarkMode(true) | |
| const theme = darkMode.value ? darkTheme : lightTheme | |
| useEffect(() => { | |
| setIsMounted(true) | |
| }, []) | |
| return ( | |
| <ThemeProvider theme={theme}> | |
| <button onClick={darkMode.enable}>DARK MODE</button> | |
| <button onClick={darkMode.disable}>LIGHT MODE</button> | |
| {isMounted && <Component {...pageProps} />} | |
| </ThemeProvider> | |
| ) | |
| } | |
| export default MyApp | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import React from 'react' | |
| import Button from '../components/Button' | |
| const Home = () => { | |
| return <Button>Theme Test</Button> | |
| } | |
| export default Home | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | const light = { | |
| bg: { | |
| primary: 'white' | |
| }, | |
| fontColor: 'black' | |
| } | |
| const dark = { | |
| bg: { | |
| primary: 'black' | |
| }, | |
| fontColor: 'white' | |
| } | |
| export const darkTheme = { ...dark } | |
| export const lightTheme = { ...light } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment