Repository: JoseRFelix/react-toggle-dark-mode
Branch: master
Commit: a3cc623a837a
Files: 18
Total size: 25.0 KB
Directory structure:
gitextract_1hconnob/
├── .changeset/
│ ├── README.md
│ └── config.json
├── .github/
│ └── workflows/
│ └── main.yml
├── .gitignore
├── .husky/
│ └── pre-commit
├── CHANGELOG.md
├── LICENSE
├── README.md
├── example/
│ ├── .npmignore
│ ├── index.html
│ ├── index.tsx
│ ├── package.json
│ └── tsconfig.json
├── package.json
├── pnpm-workspace.yaml
├── src/
│ └── index.tsx
├── test/
│ └── index.test.tsx
└── tsconfig.json
================================================
FILE CONTENTS
================================================
================================================
FILE: .changeset/README.md
================================================
# Changesets
Hello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works
with multi-package repos, or single-package repos to help you version and publish your code. You can
find the full documentation for it [in our repository](https://github.com/changesets/changesets)
We have a quick list of common questions to get you started engaging with this project in
[our documentation](https://github.com/changesets/changesets/blob/main/docs/common-questions.md)
================================================
FILE: .changeset/config.json
================================================
{
"$schema": "https://unpkg.com/@changesets/config@3.1.2/schema.json",
"changelog": "@changesets/cli/changelog",
"commit": false,
"fixed": [],
"linked": [],
"access": "public",
"baseBranch": "main",
"updateInternalDependencies": "patch",
"ignore": ["example"]
}
================================================
FILE: .github/workflows/main.yml
================================================
name: CI
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Begin CI...
uses: actions/checkout@v4
- name: Setup pnpm
uses: pnpm/action-setup@v4
- name: Use Node 20
uses: actions/setup-node@v4
with:
node-version: 20.x
cache: pnpm
- name: Setup Bun
uses: oven-sh/setup-bun@v2
- name: Install dependencies
run: pnpm install --frozen-lockfile
env:
CI: true
- name: Lint
run: pnpm lint
env:
CI: true
- name: Test
run: pnpm test
env:
CI: true
- name: Build
run: pnpm build
env:
CI: true
react-compatibility:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
react-version: [16.14.0, 17.0.2, 18.3.1, 19.0.0]
steps:
- name: Begin compatibility checks...
uses: actions/checkout@v4
- name: Setup pnpm
uses: pnpm/action-setup@v4
- name: Use Node 20
uses: actions/setup-node@v4
with:
node-version: 20.x
cache: pnpm
- name: Setup Bun
uses: oven-sh/setup-bun@v2
- name: Install dependencies
run: pnpm install --frozen-lockfile
env:
CI: true
- name: Test React ${{ matrix.react-version }} compatibility
run: pnpm add -Dw react@${{ matrix.react-version }} react-dom@${{ matrix.react-version }} --ignore-scripts
- name: Run tests
run: pnpm test
env:
CI: true
- name: Build with matrix React version
run: pnpm build
env:
CI: true
================================================
FILE: .gitignore
================================================
*.log
.DS_Store
node_modules
.pnpm-store
.cache
dist
.parcel-cache
================================================
FILE: .husky/pre-commit
================================================
pnpm lint
================================================
FILE: CHANGELOG.md
================================================
# react-toggle-dark-mode
## 2.0.0
### Major Changes
- Modernize build setup and harden DarkModeSwitch for React 16–19
### Changed
- Migrated project tooling from Yarn to pnpm.
- Updated root scripts and package metadata for pnpm-based workflows.
- Switched CI to modern GitHub Actions (`checkout@v4`, `setup-node@v4`, Node 20) and pnpm caching.
- Added a React compatibility CI matrix for React 16, 17, 18, and 19.
- Replaced `react-spring` import usage with `@react-spring/web`.
- Updated dependencies/devDependencies across root and example app (React 19, newer TypeScript/tooling).
- Modernized example app bootstrapping to `createRoot` and module script loading.
- Updated README install instructions to show pnpm usage.
- Added `.pnpm-store` to `.gitignore`.
### Improved
- Refactored `DarkModeSwitch` animation property merging to avoid mutating defaults.
- Improved prop typing (button-oriented props) and event handling in `DarkModeSwitch`.
- Improved accessibility defaults (`role="switch"`, `aria-checked`, default label behavior).
- Strengthened test suite with coverage for:
- animation-property merge behavior
- non-mutation of defaults
- accessibility attributes
- unique mask IDs across multiple component instances
### Removed
- Removed legacy Yarn lockfiles (`yarn.lock`, `example/yarn.lock`).
================================================
FILE: LICENSE
================================================
MIT License
Copyright (c) 2020 Jose Felix
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
================================================
FILE: README.md
================================================
React Toggle Dark Mode
> 🌃 Animated dark mode toggle as seen in blogs!
Supports React 16.14+ through 19.

## Prerequisites
- node >=20
## Installation
```shell
npm i react-toggle-dark-mode
```
or with pnpm:
```shell
pnpm add react-toggle-dark-mode
```
## Usage
```jsx
import * as React from 'react';
import { createRoot } from 'react-dom/client';
import { DarkModeSwitch } from 'react-toggle-dark-mode';
const App = () => {
const [isDarkMode, setDarkMode] = React.useState(false);
const toggleDarkMode = (checked: boolean) => {
setDarkMode(checked);
};
return (
);
};
const rootElement = document.getElementById('root');
if (!rootElement) {
throw new Error('Unable to find root element');
}
createRoot(rootElement).render();
```
## API
### DarkModeSwitch
#### Props
| Name | Type | Default Value | Description |
| ------------------- | -------------------------------------------------- | ------------------------------- | -------------------------------------------------------------------------------------------- |
| onChange | \(checked: boolean\) => void | | Event that triggers when icon is clicked. |
| checked | boolean | false | Current icon state. |
| style | React.CSSProperties | | CSS properties object applied to the button wrapper. |
| size | number \| string | 24 | SVG size. |
| animationProperties | Partial animation properties object | defaultProperties \(see below\) | Override only the fields you want; missing fields are merged with defaults. |
| moonColor | string | white | Color of the moon. |
| sunColor | string | black | Color of the sun. |
| aria-label | string | Toggle dark mode | Accessible label for the control. Ignored when `aria-labelledby` is provided. |
| aria-labelledby | string | | Links the control to an external label element. |
| onClick | \(event: React.MouseEvent\)=>void | | Optional button click handler. Call `event.preventDefault()` to prevent toggling on click. |
All valid button attributes (except `children`) are forwarded to the underlying button element.
### Default Animation Properties
```javascript
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](https://allcontributors.org/docs/en/emoji-key)):