Skip to content

Commit 89124cf

Browse files
add TypeScript support and new APIs; remove obsolete files
1 parent 83cf58c commit 89124cf

File tree

27 files changed

+268
-292
lines changed

27 files changed

+268
-292
lines changed

.npmignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
src/
2+
*.ts
3+
tsconfig.json
4+
*.log
5+
node_modules/
6+
.vscode/
7+
examples/

package-lock.json

Lines changed: 19 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"name": "webdev-power-kit",
3-
"version": "1.1.3",
2+
"name": "web-power-kit",
3+
"version": "1.0.0-beta.0",
44
"description": "A powerful toolkit that simplifies access to browser features like clipboard, notifications, battery, vibration, and more — perfect for modern web developers.",
55
"keywords": [
66
"webdev",
@@ -13,6 +13,7 @@
1313
"frontend",
1414
"html5-api",
1515
"javascript",
16+
"typescript",
1617
"web-utilities",
1718
"productivity"
1819
],
@@ -31,21 +32,28 @@
3132
"url": "https://github.com/dev-aditya-lab"
3233
},
3334
"type": "module",
34-
"main": "./src/index.js",
35+
"main": "./dist/index.js",
36+
"types": "./dist/index.d.ts",
3537
"exports": {
36-
".": "./src/index.js"
38+
".": {
39+
"import": "./dist/index.js",
40+
"types": "./dist/index.d.ts"
41+
}
3742
},
3843
"files": [
39-
"src",
44+
"dist",
4045
"README.md",
4146
"LICENSE"
4247
],
4348
"scripts": {
44-
"start": "echo 'Open ./test/index.html to test in browser'",
45-
"test": "./test/testScript.js",
46-
"build": "echo 'No build step required for pure JS'"
49+
"start": "echo 'Use test/index.html to run in browser.'",
50+
"test": "node ./test/testScript.js",
51+
"build": "tsc"
4752
},
4853
"engines": {
4954
"node": ">=14"
55+
},
56+
"devDependencies": {
57+
"typescript": "^5.8.3"
5058
}
5159
}

src/battery.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/browser/battery/index.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
interface BatteryManager extends EventTarget {
2+
charging: boolean;
3+
level: number;
4+
chargingTime: number;
5+
dischargingTime: number;
6+
addEventListener(
7+
type: "chargingchange" | "levelchange",
8+
listener: (this: BatteryManager, ev: Event) => any,
9+
options?: boolean | AddEventListenerOptions
10+
): void;
11+
}
12+
13+
/**
14+
* Returns the battery status using the Navigator API.
15+
* @returns Promise that resolves to a BatteryManager object
16+
*/
17+
export async function getBattery(): Promise<BatteryManager> {
18+
if (!("getBattery" in navigator)) {
19+
throw new Error("Battery API not supported.");
20+
}
21+
return await (navigator as any).getBattery();
22+
}
23+
24+
/**
25+
* Returns current battery level as percentage (0–100).
26+
*/
27+
export async function getBatteryLevel(): Promise<number> {
28+
const battery = await getBattery();
29+
return battery.level * 100;
30+
}
31+
32+
/**
33+
* Returns current charging status (true/false).
34+
*/
35+
export async function isBatteryCharging(): Promise<boolean> {
36+
const battery = await getBattery();
37+
return battery.charging;
38+
}
39+
40+
/**
41+
* Subscribes to battery level or charging change events.
42+
* @param onChange Callback that runs when battery info updates
43+
*/
44+
export async function onBatteryChange(
45+
onChange: (battery: BatteryManager) => void
46+
): Promise<void> {
47+
const battery = await getBattery();
48+
49+
battery.addEventListener("levelchange", () => onChange(battery));
50+
battery.addEventListener("chargingchange", () => onChange(battery));
51+
}

src/browser/clipboard/index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Copies a given string to the clipboard.
3+
* @param text - The text to copy
4+
* @returns A Promise that resolves when the copy is complete
5+
*/
6+
export function copyToClipboard(text: string): Promise<void> {
7+
return navigator.clipboard.writeText(text);
8+
}
9+
10+
/**
11+
* Reads the current string from the clipboard.
12+
* @returns A Promise that resolves to clipboard content
13+
*/
14+
export function readClipboard(): Promise<string> {
15+
return navigator.clipboard.readText();
16+
}

src/browser/dark-mode/index.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Checks if user's OS prefers dark mode.
3+
* @returns boolean - true if dark mode is preferred
4+
*/
5+
export function isDarkMode(): boolean {
6+
return window.matchMedia('(prefers-color-scheme: dark)').matches;
7+
}
8+
9+
/**
10+
* Toggle dark mode by adding/removing a class on <html>.
11+
* @param className Optional CSS class to toggle (default: "dark")
12+
*/
13+
export function toggleDarkMode(className = "dark"): void {
14+
document.documentElement.classList.toggle(className);
15+
}
16+
17+
/**
18+
* Subscribe to changes in system theme (dark/light).
19+
* @param callback Function triggered on change
20+
*/
21+
export function onThemeChange(callback: (isDark: boolean) => void): void {
22+
const media = window.matchMedia('(prefers-color-scheme: dark)');
23+
media.addEventListener("change", () => {
24+
callback(media.matches);
25+
});
26+
}

src/browser/idle-timer/index.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
let idleTimeoutID: ReturnType<typeof setTimeout> | null = null;
2+
let isListening = false;
3+
4+
/**
5+
* Starts the idle timer.
6+
* @param timeout - Time in milliseconds before user is considered idle.
7+
* @param callback - Function to call when user becomes idle.
8+
*/
9+
export function startIdleTimer(timeout: number, callback: () => void): void {
10+
if (isListening) return; // Avoid duplicate listeners
11+
isListening = true;
12+
13+
const resetTimer = () => {
14+
if (idleTimeoutID) clearTimeout(idleTimeoutID);
15+
idleTimeoutID = setTimeout(callback, timeout);
16+
};
17+
18+
// Events that indicate activity
19+
const events = [
20+
"mousemove", "keydown", "mousedown", "touchstart", "scroll"
21+
];
22+
23+
events.forEach((event) =>
24+
window.addEventListener(event, resetTimer, { passive: true })
25+
);
26+
27+
resetTimer(); // start on load
28+
}
29+
30+
/**
31+
* Manually reset the idle timer.
32+
*/
33+
export function resetIdleTimer(): void {
34+
if (idleTimeoutID) {
35+
clearTimeout(idleTimeoutID);
36+
idleTimeoutID = null;
37+
}
38+
}
39+
40+
/**
41+
* Stops the idle timer and removes event listeners.
42+
*/
43+
export function stopIdleTimer(): void {
44+
if (!isListening) return;
45+
46+
const events = [
47+
"mousemove", "keydown", "mousedown", "touchstart", "scroll"
48+
];
49+
50+
events.forEach((event) =>
51+
window.removeEventListener(event, resetIdleTimer)
52+
);
53+
54+
if (idleTimeoutID) {
55+
clearTimeout(idleTimeoutID);
56+
idleTimeoutID = null;
57+
}
58+
59+
isListening = false;
60+
}

src/browser/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export * from "./clipboard";
2+
export * from './notify';
3+
export * from './battery';
4+
export * from './dark-mode';
5+
export * from "./tab-visibility";
6+
export * from "./idle-timer";
File renamed without changes.

0 commit comments

Comments
 (0)