Git Product home page Git Product logo

react-dark-mode-toggle's Introduction

react-dark-mode-toggle

Hello! ๐Ÿ‘‹ This package is now deprecated. Please migrate over to react-dark-mode-toggle-2.

A super cutesy dark mode toggle button for React. Inspired by overreacted.io.

code style: prettier

๐Ÿš€ Getting Started

Using npm:

npm i react-dark-mode-toggle

Using yarn:

yarn add react-dark-mode-toggle

โœจ Usage

import React, {useState} from "react";
import DarkModeToggle from "react-dark-mode-toggle";

export default () => {
  const [isDarkMode, setIsDarkMode] = useState(() => false);
  return (
    <DarkModeToggle
      onChange={setIsDarkMode}
      checked={isDarkMode}
      size={80}
    />
  );
};

๐Ÿ“Œ Props

Prop Type Default Required
onChange func value => null No
checked boolean false No
size number (defaults to px) or string (px, em, rem, %, etc.) 85 No
speed number 1.3 No
className string null No

Note, this is not a dark mode theme implementation; it's just a button! You'll need to mix this with a management solution such as use-dark-mode.

โœŒ๏ธ License

MIT

react-dark-mode-toggle's People

Contributors

cawfree avatar jorgepasco1 avatar tianenpang avatar

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  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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

react-dark-mode-toggle's Issues

Flash when dark mode is the default mode

If the CSS is setup to default to light mode, but the user selects dark mode, the next time they visit the app, they will be in dark mode. However, the user will see a flash of light mode before the app is spun up and useDarkMode is called.

TypeScript support

This component looks and performs great, but does not work at all in TypeScript due to the lack of a definitions file.

I plan on making a PR to include the definitions file in the library directly, but until then TypeScript users can create a definition file (e.g. declaration.d.ts) in their project and paste in the following to get up and running:

declare module "react-dark-mode-toggle" {
  import React from "react";
  declare type Props = {
    size?: number | string;
    checked?: boolean;
    onChange?: (isChecked: boolean) => void;
    speed?: number;
    className?: string;
  };
  declare const _default: React.MemoExoticComponent<(props: Props) => JSX.Element>;
  export default _default;
}

TypeError: Cannot set property 'fillStyle' of null

Hi. First of all, great work!

I have a problem with testing components which use this button.

Simple test:

import React from 'react'
import { render } from '@testing-library/react'
import App from './App'

test('renders learn react link', () => {
  const { queryByTestId } = render(<App />)
  expect(queryByTestId('App')).toBeTruthy()
  expect(queryByTestId('App').classList.contains('active')).toBeFalsy()
})

has output:

https://i.imgur.com/2MxFLSa.png

App was created with create-react-app. I've prepared simple repository to reproduce this problem: https://github.com/karlosos/dark-mode-toggle-failed-tests

Maybe there's something wrong with testing config?

Add className

I have to use a wrapper in order to manipulate the element like for adding margin. Coudn't we pass it as a prop?
thank you

Disable animation when dark mode default is true

Hello @cawfree

I set the dark mode is true, and the animation will be executed again every time the page refreshes or reloads.

Is there get some functions to not repeat the animation when the dark mode is true by default?

Just like the dark mode is false, render and display toggle directly.

dark

Interaction bug

Hello @cawfree hope that you can help me here, I'm using tsx and every time I interact with the nav bar (where the dark mode toggle is), by clicking on any of the links or writing in the search box, the position of the toggle changes.
Im using mobX to manage the state of the dark mode.

Seems like a bug, correct me if I'm wrong. Attached you will find a gif of the "bug".
You can also check the code here

darkToggle

Thanks a lot and great work on the awesome component!
Joao Felicio

Too much bottom margin bottom

Hi @cawfree ,
I congratulate you on the module, I'm having some problems.
As you can see from the image, there is too much distance between the component and the object below.

Would it be possible to do something to remove this margin?

Link: codesandbox

Schermata 2020-08-15 alle 16 07 59

[Bug] Behaves weirdly as a child of Transition component (React-Transition-Group)

I have used this button in my blog (https://blog.krharsh17.vision) and it works absolutely fine as a static element on the homepage. However, while trying to put it in a collapsible navbar in one of the pages, it started automatically changing its state. I've used a Transition component, inside of it, there's another div and finally, inside this div, there is this button. I am animating the navbar out of the page when the user scrolls down, and back in when the user scrolls up. Here's the code for the top navbar:


const TopNav = (props) => {
    return(
    <Transition in={animate} timeout={1000}>
                {(state) => (
                    <NavContainer state={state}>
                        <NavBrand href={'/'}>brand-name</NavBrand>
                        {props.title}
                            <DarkModeToggle
                                onChange={props.toggleDarkMode}
                                checked={props.darkMode}
                                size={56}
                            />
                     </NavContainer>
                )}
    </Transition>
    );
}

NavContainer is a styled div element, while NavBrand is a styled a element. Upon scrolling up and down the page a number of times, the button changes from light to dark automatically gradually.

Here's a GIF about what exactly is happening on scroll
ezgif-7-1c8069a5d898

Outline on button :focus

Clicking on the toggle button produces an outline around the button in Chrome:

button-outline

Adding outline: "none" to the button element fixes the issue, however that will cause confusion to anyone who is strictly using the keyboard because when they tab to the element on the page that outline won't be present. Some articles online suggest listening for key presses and, if the element is focused and the last key pressed was tab, showing the outline. While that pleases both types of users, it complicates the component.

I'm not sure what makes the most sense for this library and I defer to you @cawfree

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.