1.0.3 • Published 3 years ago

react-toggle-dark-mode-uid v1.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

🌃 Animated dark mode toggle as seen in blogs!

Interactive sun and moon transition

Prerequisites

  • node >=10

Installation

npm i react-toggle-dark-mode

or with Yarn:

yarn add react-toggle-dark-mode

Usage

import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { DarkModeSwitch } from 'react-toggle-dark-mode';

const App = () => {
  const [isDarkMode, setDarkMode] = React.useState(false);

  const toggleDarkMode = (checked: boolean) => {
    setDarkMode(checked);
  };

  return (
    <DarkModeSwitch
      style={{ marginBottom: '2rem' }}
      checked={isDarkMode}
      onChange={toggleDarkMode}
      size={120}
    />
  );
};

API

DarkModeSwitch

Props

NameTypeDefault ValueDescription
onChange(checked: boolean) => voidEvent that triggers when icon is clicked.
checkedbooleanfalseCurrent icon state.
styleObjectCSS properties object.
sizenumber24SVG size.
animationPropertiesObjectdefaultProperties (see below)Override default animation properties.
moonColorstringwhiteColor of the moon.
sunColorstringblackColor of the sun.

Default Animation Properties

const defaultProperties = {
  dark: {
    circle: {
      r: 9,
    },
    mask: {
      cx: '50%',
      cy: '23%',
    },
    svg: {
      transform: 'rotate(40deg)',
    },
    lines: {
      opacity: 0,
    },
  },
  light: {
    circle: {
      r: 5,
    },
    mask: {
      cx: '100%',
      cy: 0,
    },
    svg: {
      transform: 'rotate(90deg)',
    },
    lines: {
      opacity: 1,
    },
  },
  springConfig: { mass: 4, tension: 250, friction: 35 },
};

Contributors

Thanks goes to these wonderful people (emoji key):

This project follows the all-contributors specification. Contributions of any kind are welcome!

Show your support

Give a ⭐️ if this project helped you!