A dark mode toggle component for React.
npm install @anatoliygatt/dark-mode-toggle
- 🚀 Getting Started
- ⚡️ Quick Start
- 💻 Live Demo
- ⚙️ Configuration
- ♿️ Accessibility
- 👨🏼⚖️ License
``shell`
npm install @anatoliygatt/dark-mode-toggle @emotion/react @emotion/styled
`jsx
import { useState } from 'react';
import { DarkModeToggle } from '@anatoliygatt/dark-mode-toggle';
function Example() {
const [mode, setMode] = useState('dark');
return (
dark="Dark"
light="Light"
size="lg"
inactiveTrackColor="#e2e8f0"
inactiveTrackColorOnHover="#f8fafc"
inactiveTrackColorOnActive="#cbd5e1"
activeTrackColor="#334155"
activeTrackColorOnHover="#1e293b"
activeTrackColorOnActive="#0f172a"
inactiveThumbColor="#1e293b"
activeThumbColor="#e2e8f0"
onChange={(mode) => {
setMode(mode);
}}
/>
);
}
`

DarkModeToggle supports the following props:
| Prop | Type | Default value | Description |
| -------------------------- | -------- | ----------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| mode | string | dark | The color scheme mode. |undefined
| dark | string | | The dark mode label text. |undefined
| light | string | | The light mode label text. |undefined
| onChange | Function | | The callback invoked when the mode changes. |sm
| size | string | | The size of the toggle switch (w/o labels). There are 3 available sizes:sm — 48x28pxmd — 72x42pxlg — 96x56pxN.B. If label(s) are used, their font size is going to scale proportionally to the toggle switch as follows:sm — 12pxmd — 18pxlg — 24px |#b9bdc1
| inactiveLabelColor | string | | The color of the label(s) when the toggle switch is in an inactive/off state. |#fcfefe
| inactiveLabelColorOnHover | string | | The color of the label(s) on hover, when the toggle switch is in an inactive/off state. |#cdd1d5
| inactiveLabelColorOnActive | string | | The color of the label(s) on active, when the toggle switch is in an inactive/off state. |#5b5e62
| activeLabelColor | string | | The color of the label(s) when the toggle switch is in an active/on state. |#404346'
| activeLabelColorOnHover | string | | The color of the label(s) on hover, when the toggle switch is in an active/on state. |#010101
| activeLabelColorOnActive | string | | The color of the label(s) on active, when the toggle switch is in an active/on state. |#dce0e3
| inactiveTrackColor | string | | The color of the track when the toggle switch is in an inactive/off state. |#fcfefe
| inactiveTrackColorOnHover | string | | The color of the track on hover, when the toggle switch is in an inactive/off state. |#cdd1d5
| inactiveTrackColorOnActive | string | | The color of the track on active, when the toggle switch is in an inactive/off state. |#404346
| activeTrackColor | string | | The color of the track when the toggle switch is in an active/on state. |#2d2f31
| activeTrackColorOnHover | string | | The color of the track on hover, when the toggle switch is in an active/on state. |#141516
| activeTrackColorOnActive | string | | The color of the track on active, when the toggle switch is in an active/on state. |#2d2f31
| inactiveThumbColor | string | | The color of the thumb when the toggle switch is in an inactive/off state. |#dce0e3
| activeThumbColor | string | | The color of the thumb when the toggle switch is in an active/on state. |rgb(59 130 246 / 0.5)
| focusRingColor | string | | The color of the toggle switch's focus ring. |
In order to comply with the web accessibility standards, we must make use of the ariaLabel prop, like so:
`jsx``
function AccessibleExample() {
return
}