Skip to content

Commit 841ce71

Browse files
add feature network, prevent-close, and vibration modules; implement online status checks and tab close warnings
1 parent 89124cf commit 841ce71

File tree

4 files changed

+71
-1
lines changed

4 files changed

+71
-1
lines changed

src/browser/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@ export * from './notify';
33
export * from './battery';
44
export * from './dark-mode';
55
export * from "./tab-visibility";
6-
export * from "./idle-timer";
6+
export * from "./idle-timer";
7+
export * from "./prevent-close";
8+
export * from "./network";
9+
export * from "./vibration";

src/browser/network/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Checks if the browser is currently online.
3+
* @returns {boolean} True if online, false if offline.
4+
*/
5+
export function isOnline(): boolean {
6+
return navigator.onLine;
7+
}
8+
9+
/**
10+
* Listen for changes in network status (online/offline).
11+
* @param callback - A function that receives the current status.
12+
*/
13+
export function onNetworkChange(callback: (online: boolean) => void): void {
14+
const updateStatus = () => callback(navigator.onLine);
15+
16+
window.addEventListener("online", updateStatus);
17+
window.addEventListener("offline", updateStatus);
18+
}

src/browser/prevent-close/index.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
let listenerAttached = false;
2+
let currentMessage = "Are you sure you want to leave? Changes may not be saved.";
3+
4+
/**
5+
* Enables the tab close or refresh warning popup.
6+
* @param message Optional custom message for the prompt.
7+
*/
8+
export function enablePreventClose(message?: string): void {
9+
if (listenerAttached) return;
10+
listenerAttached = true;
11+
12+
if (message) currentMessage = message;
13+
14+
window.addEventListener("beforeunload", beforeUnloadHandler);
15+
}
16+
17+
function beforeUnloadHandler(e: BeforeUnloadEvent): void {
18+
e.preventDefault();
19+
e.returnValue = currentMessage; // Most browsers use this for legacy
20+
}
21+
22+
/**
23+
* Disables the warning popup and allows normal tab closing.
24+
*/
25+
export function disablePreventClose(): void {
26+
if (!listenerAttached) return;
27+
window.removeEventListener("beforeunload", beforeUnloadHandler);
28+
listenerAttached = false;
29+
}

src/browser/vibration/index.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Vibrates the device using the given pattern.
3+
* @param pattern - A single duration or array of vibration timings in ms.
4+
* @returns True if vibration was triggered successfully.
5+
*/
6+
export function vibrate(pattern: number | number[]): boolean {
7+
if ("vibrate" in navigator) {
8+
return navigator.vibrate(pattern);
9+
}
10+
return false;
11+
}
12+
13+
/**
14+
* Stops any ongoing vibration.
15+
*/
16+
export function stopVibration(): void {
17+
if ("vibrate" in navigator) {
18+
navigator.vibrate(0);
19+
}
20+
}

0 commit comments

Comments
 (0)