1
mirror of https://github.com/jakejarvis/dark-mode.git synced 2025-04-29 07:00:27 -04:00

Compare commits

...

8 Commits
0.8.1 ... main

11 changed files with 2071 additions and 2403 deletions

17
.editorconfig Normal file
View File

@ -0,0 +1,17 @@
# http://editorconfig.org
# this file is the top-most editorconfig file
root = true
# all files
[*]
charset = utf-8
end_of_line = lf
indent_style = space
indent_size = 2
trim_trailing_whitespace = true
insert_final_newline = true
# site content
[*.md]
trim_trailing_whitespace = false

View File

@ -1,18 +1,16 @@
{
"extends": [
"@jakejarvis/eslint-config"
"@jakejarvis/eslint-config",
"plugin:@typescript-eslint/recommended"
],
"parserOptions": {
"ecmaVersion": 2015,
"sourceType": "module"
},
"plugins": [
"@typescript-eslint"
],
"parser": "@typescript-eslint/parser",
"env": {
"browser": true,
"node": true
"browser": true
},
"ignorePatterns": [
"*.d.ts",
"dist/**",
"rollup.config.js"
"dist/**"
]
}

2
.gitattributes vendored Normal file
View File

@ -0,0 +1,2 @@
# Set default behavior to automatically normalize line endings.
* text=auto eol=lf

View File

@ -13,7 +13,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '14.x'
node-version: 16.x
- run: yarn install --frozen-lockfile
- run: yarn lint
- run: yarn build

View File

@ -3,20 +3,18 @@ name: Release
on:
push:
tags:
- '*'
- 'v*'
jobs:
npm:
name: Publish to NPM
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 14
node-version: 16.x
registry-url: https://registry.npmjs.org/
- env:
- run: yarn install --frozen-lockfile
- run: yarn publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
run: |
yarn install --frozen-lockfile
yarn publish

View File

@ -1,8 +1,7 @@
# 🌓 Dark Mode Switcheroo™
[![CI](https://github.com/jakejarvis/dark-mode/actions/workflows/ci.yml/badge.svg)](https://github.com/jakejarvis/dark-mode/actions/workflows/ci.yml)
[![npm](https://img.shields.io/npm/v/dark-mode-switcheroo?logo=npm)](https://www.npmjs.com/package/dark-mode-switcheroo)
[![MIT License](https://img.shields.io/github/license/jakejarvis/dark-mode?color=violet)](LICENSE)
[![npm](https://img.shields.io/npm/v/dark-mode-switcheroo)](https://www.npmjs.com/package/dark-mode-switcheroo)
Very simple CSS dark/light mode toggler with saved preference via local storage & dynamic OS setting detection. Zero dependencies and [only ~500 bytes gzipped!](https://bundlephobia.com/package/dark-mode-switcheroo)
@ -21,6 +20,7 @@ Very simple CSS dark/light mode toggler with saved preference via local storage
- **`default`**: The initial `<body>` class hard-coded into the HTML template. (optional, default: `"light"`)
- **`storageKey`**: Name of the `localStorage` key holding the user's preference. (optional, default: `"dark_mode_pref"`)
- **`onInit([toggle])`**: Callback function executed at the end of initialization. The toggle above is passed in if set. (optional, default: `null`)
- **`onUserToggle([toggle])`**: Callback function executed when a user manually interacts with the toggle button. The toggle above (if set) is passed in. (optional, default: `null`)
- **`onChange([theme, toggle])`**: Callback function executed when theme is switched. The new theme and the toggle above (if set) are passed in. (optional, default: `null`)
### Browser

View File

@ -1,8 +1,9 @@
{
"name": "dark-mode-switcheroo",
"version": "0.8.1",
"version": "0.10.0",
"description": "🌓 Simple CSS theme switching with saved preferences and automatic OS setting detection",
"license": "MIT",
"homepage": "https://jrvs.io/darkmode",
"author": {
"name": "Jake Jarvis",
"email": "jake@jarv.is",
@ -13,40 +14,32 @@
"url": "https://github.com/jakejarvis/dark-mode.git"
},
"type": "module",
"sideEffects": false,
"files": [
"dist"
],
"main": "./dist/dark-mode.cjs.js",
"source": "./src/dark-mode.ts",
"main": "./dist/dark-mode.cjs",
"module": "./dist/dark-mode.esm.js",
"unpkg": "./dist/dark-mode.min.js",
"types": "./dist/dark-mode.d.ts",
"exports": {
"require": "./dist/dark-mode.cjs.js",
"require": "./dist/dark-mode.cjs",
"import": "./dist/dark-mode.esm.js",
"browser": "./dist/dark-mode.min.js"
},
"types": "./dist/dark-mode.d.ts",
"scripts": {
"build": "rollup -c",
"build": "microbundle --format cjs,esm,umd --name 'darkMode'",
"lint": "eslint .",
"prepublishOnly": "yarn build"
},
"dependencies": {},
"devDependencies": {
"@babel/core": "^7.15.5",
"@babel/preset-env": "^7.15.6",
"@jakejarvis/eslint-config": "*",
"@rollup/plugin-babel": "^5.3.0",
"@rollup/plugin-eslint": "^8.0.1",
"@rollup/plugin-node-resolve": "^13.0.4",
"eslint": "^7.32.0",
"eslint-plugin-compat": "^3.13.0",
"eslint-plugin-import": "^2.24.2",
"rollup": "^2.56.3",
"rollup-plugin-copy": "^3.4.0",
"rollup-plugin-delete": "^2.0.0",
"rollup-plugin-filesize": "^9.1.1",
"rollup-plugin-terser": "^7.0.2"
"@typescript-eslint/eslint-plugin": "^5.3.1",
"@typescript-eslint/parser": "^5.3.1",
"eslint": "^8.2.0",
"microbundle": "^0.14.1",
"typescript": "^4.4.4"
},
"keywords": [
"front-end",

View File

@ -1,90 +0,0 @@
import pkg from "./package.json";
import resolve from "@rollup/plugin-node-resolve";
import { babel } from "@rollup/plugin-babel";
import { terser } from "rollup-plugin-terser";
import eslint from "@rollup/plugin-eslint";
import filesize from "rollup-plugin-filesize";
import copy from "rollup-plugin-copy";
import del from "rollup-plugin-delete";
const banner = `/*! Dark Mode Switcheroo v${pkg.version} | MIT License | jrvs.io/darkmode */`;
export default [
{
// universal (browser and node)
input: "src/index.js",
output: [
{
name: "darkMode",
file: "dist/dark-mode.js",
format: "umd",
exports: "named",
esModule: false,
banner: banner,
},
{
name: "darkMode",
file: "dist/dark-mode.min.js",
format: "umd",
exports: "named",
esModule: false,
plugins: [
terser({
output: {
preamble: banner,
},
}),
],
},
],
plugins: [
del({ targets: "dist/*" }),
copy({
// clearly this isn't really typescript, so we need to manually copy the type definition file
targets: [
{
src: "src/index.d.ts",
dest: "dist",
rename: "dark-mode.d.ts",
},
],
}),
resolve(),
eslint(),
babel({
babelHelpers: "bundled",
presets: [["@babel/preset-env"]],
exclude: ["node_modules/**"],
}),
filesize(),
],
},
{
// modules
input: "src/index.js",
output: [
{
// ES6 module (import)
file: "dist/dark-mode.esm.js",
format: "esm",
exports: "named",
banner: banner,
},
{
// commonjs (require)
file: "dist/dark-mode.cjs.js",
format: "cjs",
exports: "named",
banner: banner,
},
],
plugins: [
resolve(),
babel({
babelHelpers: "bundled",
exclude: ["node_modules/**"],
}),
filesize(),
],
},
];

View File

@ -1,4 +1,12 @@
const init = function (options) {
export function init(options: {
toggle?: Element | null;
classes?: { dark: string, light: string };
default?: string;
storageKey?: string;
onInit?: (toggle?: Element | null) => unknown;
onUserToggle?: (toggle?: Element | null) => unknown;
onChange?: (theme: string, toggle?: Element | null) => unknown;
}): void {
options = options || {};
// use a specified element(s) to trigger swap when clicked
@ -19,7 +27,12 @@ const init = function (options) {
let active = defaultTheme === dark;
// receives a class name and switches <body> to it
const activateTheme = function (theme, remember) {
const activateTheme = function (theme: string, remember?: boolean) {
// optional onChange callback function passed as option
if (typeof options.onChange === "function") {
options.onChange(theme, toggle);
}
document.body.classList.remove(dark, light);
document.body.classList.add(theme);
active = theme === dark;
@ -27,17 +40,17 @@ const init = function (options) {
if (remember) {
localStorage.setItem(storageKey, theme);
}
// optional onChange callback function passed as option
if (typeof options.onChange === "function") {
options.onChange(theme, toggle);
}
};
// optional onInit callback function passed as option
if (typeof options.onInit === "function") {
options.onInit(toggle);
}
// user has never clicked the button, so go by their OS preference until/if they do so
if (!pref) {
// returns media query selector syntax
const prefers = function (colorScheme) {
const prefers = function (colorScheme: "dark" | "light") {
// https://drafts.csswg.org/mediaqueries-5/#prefers-color-scheme
return `(prefers-color-scheme: ${colorScheme})`;
};
@ -75,6 +88,11 @@ const init = function (options) {
if (toggle !== null) {
// handle toggle click
toggle.addEventListener("click", function () {
// optional onUserToggle callback function passed as option
if (typeof options.onUserToggle === "function") {
options.onUserToggle(toggle);
}
// switch to the opposite theme & save preference in local storage
if (active) {
activateTheme(light, true);
@ -83,11 +101,4 @@ const init = function (options) {
}
});
}
// optional onInit callback function passed as option
if (typeof options.onInit === "function") {
options.onInit(toggle);
}
};
export { init };

8
src/index.d.ts vendored
View File

@ -1,8 +0,0 @@
export function init(options?: {
toggle?: Element | null;
classes?: { dark: string, light: string };
default?: string;
storageKey?: string;
onInit?: (toggle?: Element | null) => unknown;
onChange?: (theme?: string, toggle?: Element | null) => unknown;
}): void;

4223
yarn.lock

File diff suppressed because it is too large Load Diff