Skip to content

Commit cb2efe6

Browse files
committed
online binding
1 parent c87d27a commit cb2efe6

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

packages/svelte/src/reactivity/window/index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,21 @@ export const screenTop = new ReactiveValue(
9696
}
9797
);
9898

99+
/**
100+
* `online.current` is a reactive view of `navigator.onLine`. On the server it is `undefined`
101+
*/
102+
export const online = new ReactiveValue(
103+
BROWSER ? () => navigator.onLine : () => undefined,
104+
(update) => {
105+
const unsub_online = on(window, 'online', update);
106+
const unsub_offline = on(window, 'offline', update);
107+
return () => {
108+
unsub_online();
109+
unsub_offline();
110+
};
111+
}
112+
);
113+
99114
/**
100115
* `devicePixelRatio.current` is a reactive view of `window.devicePixelRatio`. On the server it is `undefined`.
101116
* Note that behaviour differs between browsers — on Chrome it will respond to the current zoom level,

packages/svelte/types/index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1995,6 +1995,10 @@ declare module 'svelte/reactivity/window' {
19951995
* `screenTop.current` is a reactive view of `window.screenTop`. On the server it is `undefined`
19961996
*/
19971997
export const screenTop: ReactiveValue<number | undefined>;
1998+
/**
1999+
* `online.current` is a reactive view of `navigator.onLine`. On the server it is `undefined`
2000+
*/
2001+
export const online: ReactiveValue<boolean | undefined>;
19982002
/**
19992003
* `devicePixelRatio.current` is a reactive view of `window.devicePixelRatio`. On the server it is `undefined`.
20002004
* Note that behaviour differs between browsers — on Chrome it will respond to the current zoom level,

0 commit comments

Comments
 (0)