File tree Expand file tree Collapse file tree 4 files changed +71
-1
lines changed Expand file tree Collapse file tree 4 files changed +71
-1
lines changed Original file line number Diff line number Diff line change @@ -3,4 +3,7 @@ export * from './notify';
3
3
export * from './battery' ;
4
4
export * from './dark-mode' ;
5
5
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" ;
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments