Skip to content

Commit ef4c185

Browse files
add Dark Mode API to detect user preference and listen for theme changes
1 parent 4b90ce7 commit ef4c185

File tree

6 files changed

+200
-1
lines changed

6 files changed

+200
-1
lines changed

README.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,89 @@ startIdleTimer(10, () => {
590590
* Lock app after idle timeout in sensitive dashboards
591591
---
592592

593+
# 🌙 Dark Mode API – webdev-power-kit
594+
595+
This module detects whether the user prefers dark or light mode, and lets you listen to theme changes in real-time using the system’s color scheme preference.
596+
597+
---
598+
599+
## ✨ Features
600+
601+
* Detect if dark mode is enabled by system/browser
602+
* Listen for real-time theme changes
603+
* Useful for automatic theming or toggles
604+
* Uses native `window.matchMedia`
605+
606+
---
607+
608+
## ✅ Functions
609+
610+
### 1. `isDarkMode()`
611+
612+
📌 Returns whether the user’s system prefers dark mode.
613+
614+
```js
615+
import { isDarkMode } from "webdev-power-kit";
616+
617+
if (isDarkMode()) {
618+
console.log("🌙 Dark mode is enabled");
619+
} else {
620+
console.log("☀️ Light mode is enabled");
621+
}
622+
```
623+
624+
#### 🔁 Returns:
625+
626+
* `true` → if user prefers dark mode
627+
* `false` → if light mode is preferred
628+
629+
---
630+
631+
### 2. `listenDarkMode(callback)`
632+
633+
📌 Listen to changes in the system theme (dark ↔️ light) in real-time.
634+
635+
```js
636+
import { listenDarkMode } from "webdev-power-kit";
637+
638+
listenDarkMode((isDark) => {
639+
console.log("Theme changed:", isDark ? "Dark" : "Light");
640+
});
641+
```
642+
643+
#### 📥 Parameters:
644+
645+
| Param | Type | Description |
646+
| ---------- | --------------------------- | -------------------------------------------------------- |
647+
| `callback` | `(isDark: boolean) => void` | Called with `true` for dark mode, `false` for light mode |
648+
649+
#### 🔁 Returns:
650+
651+
* A function to unsubscribe and stop listening
652+
653+
---
654+
655+
## 🔐 Browser Support
656+
657+
| Browser | Supported? | Notes |
658+
| ------- | ---------- | -------------------------------- |
659+
| Chrome || Fully supported |
660+
| Firefox || Fully supported |
661+
| Edge || Fully supported |
662+
| Safari || Fully supported (macOS/iOS only) |
663+
| Mobile || Most modern devices supported |
664+
665+
---
666+
667+
## 💡 Use Cases
668+
669+
* Auto-switch theme on page load
670+
* Show theme toggle suggestion
671+
* Sync app theme with OS settings
672+
* Apply different CSS styles dynamically
673+
674+
---
675+
593676

594677

595678

cnddocs.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
2+
# 🌙 Dark Mode API – webdev-power-kit
3+
4+
This module detects whether the user prefers dark or light mode, and lets you listen to theme changes in real-time using the system’s color scheme preference.
5+
6+
---
7+
8+
## ✨ Features
9+
10+
* Detect if dark mode is enabled by system/browser
11+
* Listen for real-time theme changes
12+
* Useful for automatic theming or toggles
13+
* Uses native `window.matchMedia`
14+
15+
---
16+
17+
## ✅ Functions
18+
19+
### 1. `isDarkMode()`
20+
21+
📌 Returns whether the user’s system prefers dark mode.
22+
23+
```js
24+
import { isDarkMode } from "webdev-power-kit";
25+
26+
if (isDarkMode()) {
27+
console.log("🌙 Dark mode is enabled");
28+
} else {
29+
console.log("☀️ Light mode is enabled");
30+
}
31+
```
32+
33+
#### 🔁 Returns:
34+
35+
* `true` → if user prefers dark mode
36+
* `false` → if light mode is preferred
37+
38+
---
39+
40+
### 2. `listenDarkMode(callback)`
41+
42+
📌 Listen to changes in the system theme (dark ↔️ light) in real-time.
43+
44+
```js
45+
import { listenDarkMode } from "webdev-power-kit";
46+
47+
listenDarkMode((isDark) => {
48+
console.log("Theme changed:", isDark ? "Dark" : "Light");
49+
});
50+
```
51+
52+
#### 📥 Parameters:
53+
54+
| Param | Type | Description |
55+
| ---------- | --------------------------- | -------------------------------------------------------- |
56+
| `callback` | `(isDark: boolean) => void` | Called with `true` for dark mode, `false` for light mode |
57+
58+
#### 🔁 Returns:
59+
60+
* A function to unsubscribe and stop listening
61+
62+
---
63+
64+
## 🔐 Browser Support
65+
66+
| Browser | Supported? | Notes |
67+
| ------- | ---------- | -------------------------------- |
68+
| Chrome || Fully supported |
69+
| Firefox || Fully supported |
70+
| Edge || Fully supported |
71+
| Safari || Fully supported (macOS/iOS only) |
72+
| Mobile || Most modern devices supported |
73+
74+
---
75+
76+
## 💡 Use Cases
77+
78+
* Auto-switch theme on page load
79+
* Show theme toggle suggestion
80+
* Sync app theme with OS settings
81+
* Apply different CSS styles dynamically
82+

src/darkMode.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Detect if user prefers dark mode based on system/browser settings.
3+
* @returns {boolean} - true if dark mode is preferred
4+
*/
5+
export function isDarkMode() {
6+
return window.matchMedia &&
7+
window.matchMedia("(prefers-color-scheme: dark)").matches;
8+
}
9+
10+
/**
11+
* Listen for system theme changes in real-time.
12+
* @param {(isDark: boolean) => void} callback - Called when theme changes
13+
* @returns {() => void} - Function to remove listener
14+
*/
15+
export function listenDarkMode(callback) {
16+
const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
17+
const handler = (e) => callback(e.matches);
18+
19+
mediaQuery.addEventListener("change", handler);
20+
21+
// Trigger once initially
22+
callback(mediaQuery.matches);
23+
24+
return () => {
25+
mediaQuery.removeEventListener("change", handler);
26+
};
27+
}

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ export * from './network.js';
66
export * from './tabVisibility.js';
77
export * from './preventClose.js';
88
export * from './idleTimer.js';
9+
export * from './darkMode.js';

test/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ <h2>Clipboard Test</h2>
2323
<hr>
2424
<button id="enable">Enable Tab Protection</button>
2525
<button id="disable">Disable Tab Protection</button>
26-
26+
<hr>
27+
<p id="theme-status">Checking theme...</p>
2728
<script type="module" src="testScript.js"> </script>
2829
</body>
2930
</html>

test/testScript.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ import { isOnline, listenNetworkStatus } from "../src/index.js";
55
import { listenTabVisibility } from "../src/index.js";
66
import { preventTabClose } from "../src/index.js";
77
import { startIdleTimer } from "../src/index.js";
8+
import { isDarkMode, listenDarkMode } from "../src/index.js";
89

10+
listenDarkMode((isDark) => {
11+
document.getElementById("theme-status").textContent =
12+
isDark ? "🌙 Dark mode enabled" : "☀️ Light mode enabled";
13+
});
914
startIdleTimer(5, () => {
1015
document.getElementById("idle").textContent = "💤 You are idle!";
1116
});

0 commit comments

Comments
 (0)