Git Product home page Git Product logo

use-prefers-color-scheme's Introduction

use-prefers-color-scheme

React hook for determining the preferred color scheme

NPM JavaScript Style Guide

Features:

  • SSR support
  • Written in Typescript

Install

npm install --save use-prefers-color-scheme

Usage

Tip When rendered in node (SSR) the hook returns no-preference.

import React from 'react'

import usePrefersColorScheme from 'use-prefers-color-scheme'

const App = () => {
  const prefersColorScheme = usePrefersColorScheme()
  const isDarkMode = prefersColorScheme === 'dark'

  return (
    <div>You are using {isDarkMode ? 'Dark Mode ๐ŸŒš' : 'Light Mode ๐ŸŒž'}!</div>
  )
}

Online Demo

Open Codesanbox

License

MIT ยฉ rfoel

use-prefers-color-scheme's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

use-prefers-color-scheme's Issues

Next.js error on first load

From the version v1.1.3, users may get the following error:

Unhandled Runtime Error
Error: Text content does not match server-rendered HTML.

See more info here: https://nextjs.org/docs/messages/react-hydration-error

This happens because after the version v1.1.2, the first client render will return the users preffered color scheme, but on the server side render it will return no-preference, thus causing this mismatch from the content if it depends on the response from the hook.

I personally do not use Next.js nor other SSR framework, and I am not really sure how to handle such error. If anyone has an anwser please contribute!

import fails in next.js

@rfoel thanks for creating this hook. it looks great and exactly what i need, but i get the following error when trying to use it in a next.js app :

error - pages/index.tsx (6:49) @ Home
TypeError: use_prefers_color_scheme__WEBPACK_IMPORTED_MODULE_1___default(...) is not a function
  4 | 
  5 | const Home: NextPage = () => {
> 6 |   const prefersColorScheme = usePrefersColorScheme();
    |                                                  ^
  7 |   const isDarkMode = prefersColorScheme === "dark";
  8 | 
  9 |   return (

i've tried importing both the default and named exports and neither way works

here's a reproduction : https://replit.com/@ptrkcsk/use-prefers-color-scheme-nextjs

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.