• Stars
    star
    218
  • Rank 181,805 (Top 4 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 4 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

πŸŒ‘ Enable dark mode for Next.js apps

next-dark-mode

πŸŒ“ Theme your Next.js apps with a Dark Mode

license npm bundle size npm version

Contents:

Features

Auto mode

next-dark-mode optionally supports auto mode which automatically switches the user's theme as per the color mode selected on their operating system.

Windows and macOS both support setting the dark or light mode based on the time of the day.

It is achieved via prefers-color-scheme media query.

No page load glitch

next-dark-mode uses configurable cookies to persist the state of the current theme, one for the auto mode and the other for the dark mode.

This prevents the common page load glitch with the local storage approach where the app loads on the client and then the state of the user's theme is fetched.

You can see it in this implementation by Pantaley Stoyanov.

NOTE: This library is not compatible with Next.js 9's Auto Partial Static Export feature as it has to read the cookies in getInitialProps function, which makes all pages incompatible with Automatic Partial Static Export feature.

Requirements

To use next-dark-mode, you must use [email protected] or greater which includes Hooks.

Installation

$ yarn add next-dark-mode

or

$ npm install next-dark-mode

Usage

  1. Wrap your _app.js component (located in /pages) with the HOC withDarkMode

    // _app.js
    import App from 'next/app'
    import withDarkMode from 'next-dark-mode'
    
    export default withDarkMode(App)
  2. You can now use the useDarkMode hook anywhere in your app

    import { useDarkMode } from 'next-dark-mode'
    
    const MyComponent = props => {
      const {
        autoModeActive,    // boolean - whether the auto mode is active or not
        autoModeSupported, // boolean - whether the auto mode is supported on this browser
        darkModeActive,    // boolean - whether the dark mode is active or not
        switchToAutoMode,  // function - toggles the auto mode on
        switchToDarkMode,  // function - toggles the dark mode on
        switchToLightMode, // function - toggles the light mode on
      } = useDarkMode()
    
     ...
    }

With CSS-in-JS libraries (like emotion or styled-components)

  1. Wrap your _app.js component (located in /pages) with the HOC withDarkMode and pass the values to the ThemeProvider so that you can use it in your components

    // _app.js
    import { ThemeProvider } from '@emotion/react' // or styled-components
    import withDarkMode from 'next-dark-mode'
    
    function MyApp({ Component, darkMode, pageProps }) {
      const { autoModeActive, autoModeSupported, darkModeActive } = darkMode
    
      return (
        <ThemeProvider theme={{ darkMode: darkModeActive, ...(other values) }}>
          <Component {...pageProps} />
        </ThemeProvider>
      )
    }
    
    export default withDarkMode(MyApp)

Configuration

The withDarkMode function accepts a config object as its second argument. Every key is optional with default values mentioned:

  • autoModeCookieName: string - Name of the cookie used to determine whether the auto preset is enabled. Defaults to 'autoMode'.
  • cookieOptions: object - Configuration options for the cookies that gets set on the client. Defaults to { sameSite: 'lax' }.
  • darkModeCookieName: string - Name of the cookie used to determine whether the dark preset is enabled. Defaults to 'darkMode'.
  • defaultMode: string - Determines the default color mode when there's no cookie set on the client. This usually happens on the first ever page load. It can either be 'dark' or 'light' and it defaults to 'light'.

Resources

More Repositories

1

30-Days-of-Code

πŸ‘¨β€πŸ’» 30 Days of Code by HackerRank Solutions in C, C++, C#, F#, Go, Java, JavaScript, Python, Ruby, Swift & TypeScript. PRs Welcome! πŸ˜„
Java
974
star
2

pianoforte

 🎹 Pianoforte: Synthesia for Everyone.
JavaScript
16
star
3

TypeMonPress

πŸ… A Boilerplate Starter Application For Building RESTful APIs Microservice in Node.js using Express and Mongoose in TypeScript
TypeScript
15
star
4

generation-blockchain-crypto-tracker

πŸ’Έ Track your favourite cryptocurrencies. React based starter repo for my Generation Blockchain talk at IIT Delhi https://speakerdeck.com/xeoneux/front-end-web-development-in-react
JavaScript
5
star
5

treva

🌌 Information about the planets in Flutter and Dart
Dart
4
star
6

midiate

🎼 A Tool To Generate Running Notes Data From MIDI Files.
TypeScript
4
star
7

pianolive

🎢 A Real-Time Piano For All That Works Over WebSockets!
JavaScript
4
star
8

roundaround

Find nearby places on map based on place type through Google and Mapbox APIs
JavaScript
4
star
9

nexa

πŸŽ– A Boilerplate Application for Building RESTful APIs in Node.js using Nest and PostgreSQL in TypeScript with Code Coverage and Authentication using JSON Web Tokens.
TypeScript
3
star
10

typeliber

πŸ’  A Simple TypeScript Library Starter Project
TypeScript
2
star
11

tetrisdl

C++
2
star
12

cropchat

πŸ’¬ A Progressive Web Application with Vue JS, Webpack & Material Design
JavaScript
2
star
13

ruby_koans

Ruby
2
star
14

advanced-react

βš›οΈ Take a deep dive into full-stack JavaScript with React.js and learn advanced concepts like the context API, HOCs, external state, performance optimization, asynchronous APIs, testing, deployment, and more.
JavaScript
2
star
15

key_server

Rails implementation of a simple key server challenge.
Ruby
2
star
16

chomp

πŸ’Ύ Quick solution to transfer files over local network
TypeScript
2
star
17

Voting

πŸ—³οΈ Full-stack real-time react and redux voting application.
JavaScript
2
star
18

react-native-super-hero

🀘 A super duper hero unit react-native component with support for dynamic image, dynamic sizing, color overlays, and more.
TypeScript
2
star
19

Vue-Mastery

JavaScript
2
star
20

caravansary

🏨 Hotel reservation app using Spring Boot in Kotlin
CSS
1
star
21

startup_namer

πŸ“› A Startup Name Generator App in Flutter using Dart.
Dart
1
star
22

TheWorld

πŸ—ΊοΈ Web App with ASP.NET Core RC1, MVC 6, EF7 & AngularJS
C#
1
star
23

Welp

πŸ“ A simple Yelp clone built using React and React Router.
JavaScript
1
star
24

ttt-test-react

TTT: terribly tiny tales test in react
JavaScript
1
star
25

member_teams

Rust
1
star
26

cloudamqp-demo

Simple Cloudamqp demo for learning
JavaScript
1
star
27

HelloPhoenix

🐀 A sample blog built using Phoenix Framework
Elixir
1
star
28

nymp

🎧 NYMP: Not Your Music Player - A real-time music player that plays songs on other people's devices
Java
1
star
29

yoco

πŸ“Ή A real-time video conferencing application based on WebRTC
TypeScript
1
star
30

AcmeProductManagement

πŸ›’ Angular 2: Getting Started
TypeScript
1
star
31

Popular-Movies

Project for Android Developer Nanodegree by Udacity
Java
1
star
32

Silly-Song

🎀 Project for Beginning iOS App Development Nanodegree by Udacity
Swift
1
star
33

Frames-Per-Second

πŸ“½οΈ Project for iOS Developer Nanodegree by Udacity
Swift
1
star
34

On-the-Map

πŸ—Ύ Project for iOS Developer Nanodegree by Udacity
Swift
1
star
35

Sunshine

β˜€οΈ Walk-through of Android Nanodegree tutorial
Java
1
star
36

learning-opengl

Getting up and running with OpenGL in C++
C++
1
star
37

OdeToFood

🍱 ASP.NET Core 1.0 Fundamentals
C#
1
star
38

express-openapi-validator-default-query-issue

JavaScript
1
star
39

sick-fits

JavaScript
1
star
40

JavaScript30

30 Day Vanilla JS Challenge
HTML
1
star
41

PopularMovies

🍿 P1 for Android Developer Nanodegree by Udacity
Java
1
star
42

Tower-Defense

πŸ•ΉοΈ Tower Defense game in Unity Engine
C#
1
star
43

tic-tac-toe

πŸ€„ Intro to React to build an interactive tic-tac-toe game.
JavaScript
1
star
44

xeoneux.github.io

πŸ€™ The life and times of Aayush Kapoor
CSS
1
star
45

paytm-insider-app

JavaScript
1
star
46

Quiz-Game

🧠 A Quiz Game built using Unity Game Engine
C#
1
star
47

MemeMe

🀦 Project for iOS Developer Nanodegree by Udacity
Swift
1
star
48

watermarx

πŸ“Έ iOS app to add your watermark to your favorite snaps
Swift
1
star
49

My-App-Portfolio

Project for Android Developer Nanodegree by Udacity
Java
1
star
50

Reduxstagram

🀳 A simple photo app to simplify core ideas behind Redux, React and React Router.
JavaScript
1
star
51

Pitch-Perfect

🎚️ Project for iOS Developer Nanodegree by Udacity
Swift
1
star
52

ScaryBugs

🐞 OS X Application to load a list of bugs and display them in a table view
Objective-C
1
star
53

KotlinKoans

This tutorial walks you through a series of exercises to get familiar with Kotlin.
Kotlin
1
star
54

reason-to-do

πŸ“‡ To Do Application in Reason & React by Facebook
OCaml
1
star
55

vyng-backend-challenge

Vyng Backend Challenge
JavaScript
1
star
56

LearnOpenGL

C++
1
star
57

fastify-root-schema-issue

JavaScript
1
star
58

rusty-journal

Rust
1
star
59

Virtual-Tourist

πŸŽ’ Project for iOS Developer Nanodegree by Udacity
Swift
1
star
60

GhostText-for-VisualStudioCode

Allows live editing of Chrome/Firefox text area or JS Code editor content with Visual Studio Code using WebSockets
TypeScript
1
star
61

react-native-super-injector

πŸ’‰ An "injectable" react-native component that allows an external component to be injected during run-time while falling back to a default component
TypeScript
1
star
62

magnum

C++
1
star