Skip to content

Commit 06be59f

Browse files
committed
[IMPL] useIsOnline hook
1 parent 914f064 commit 06be59f

20 files changed

Lines changed: 170 additions & 33 deletions

File tree

apps/react-tools-demo/scripts/generateMarkdown.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ function buildHooksUtilsMarkdownObject(file) {
251251
obj.description = titleDescription.join(":").trim();
252252
obj.description = obj.description.charAt(0).toUpperCase() + obj.description.substring(1);
253253
let lastDescriptionIndex = index+1;
254-
while([" * @param", " * @returns", "//", " */", "*/"].filter(key => lines[lastDescriptionIndex].startsWith(key)).length === 0) {
254+
while([" * @", "//", " */", "*/"].filter(key => lines[lastDescriptionIndex].startsWith(key)).length === 0) {
255255
lastDescriptionIndex++;
256256
}
257257
if(lastDescriptionIndex !== (index+1)) {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { useIsOnline } from "../../../../../../packages/react-tools/src"
2+
3+
/**
4+
The component display the network connection informations. Try to change connection type from console to see changes.
5+
*/
6+
export const UseIsOnline = () => {
7+
const connectionState = useIsOnline();
8+
9+
return (<div style={{ textAlign: "center" }}>
10+
<p>Online: {JSON.stringify(connectionState)}</p>
11+
</div>)
12+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { useNetwork } from "../../../../../../packages/react-tools/src"
2+
3+
/**
4+
The component display the network connection informations. Try to change connection type from console to see changes.
5+
*/
6+
export const UseNetwork = () => {
7+
const connectionState = useNetwork();
8+
9+
return (<div style={{ textAlign: "center" }}>
10+
{
11+
Object.keys(connectionState).map(el => (
12+
<p key={el}>{el}: {JSON.stringify(connectionState[el as keyof typeof connectionState])}</p>
13+
))
14+
}
15+
</div>)
16+
}

apps/react-tools-demo/src/constants/components.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ export const COMPONENTS = [
4949
"useDocumentVisibility",
5050
"useHover",
5151
"useResponsive",
52-
"useClickOutside"
52+
"useClickOutside",
53+
"useNetwork",
54+
"useIsOnline"
5355
],
5456
//API DOM
5557
[
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# useIsOnline
2+
Hook to detect network connection status.
3+
4+
## Usage
5+
6+
```tsx
7+
export const UseIsOnline = () => {
8+
const connectionState = useIsOnline();
9+
10+
return (<div style={{ textAlign: "center" }}>
11+
<p>Online: {JSON.stringify(connectionState)}</p>
12+
</div>)
13+
}
14+
```
15+
16+
> The component display the network connection informations. Try to change connection type from console to see changes.
17+
18+
19+
## API
20+
21+
```tsx
22+
useIsOnline (): boolean
23+
```
24+
25+
> ### Params
26+
>
27+
>
28+
>
29+
30+
> ### Returns
31+
>
32+
> __isOnline__
33+
> - _boolean_
34+
>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# useNetwork
2+
Hook to detect network connection infos, refer to [Network Information API](https://developer.mozilla.org/en-US/docs/Web/API/NetworkInformation). It takes optinally a parameter __selectedInfo__ to specify a subset of connection status property.
3+
4+
## Usage
5+
6+
```tsx
7+
export const UseNetwork = () => {
8+
const connectionState = useNetwork();
9+
10+
return (<div style={{ textAlign: "center" }}>
11+
{
12+
Object.keys(connectionState).map(el => (
13+
<p key={el}>{el}: {JSON.stringify(connectionState[el as keyof typeof connectionState])}</p>
14+
))
15+
}
16+
</div>)
17+
}
18+
```
19+
20+
> The component display the network connection informations. Try to change connection type from console to see changes.
21+
22+
23+
## API
24+
25+
```tsx
26+
useNetwork<T extends keyof ConnectionState>(selectedInfo?: ArrayMinLength1<T>): ConnectionState | {[k in T] : ConnectionState[k]}
27+
```
28+
29+
> ### Params
30+
>
31+
> - __selectedInfo?__: _ArrayMinLength1<T>_
32+
array of connection property.
33+
>
34+
35+
> ### Returns
36+
>
37+
> __object__: Network connection property or a subset if __selectedInfo__ is specified.
38+
> - _ConnectionState|{[k in T] : ConnectionState[k]}_
39+
>

apps/react-tools-demo/src/markdown/useStateGetReset.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,6 @@ const UseStateGetReset = () => {
2323
});
2424
}, [state, setState])
2525

26-
const fullState = useMemo(() => {
27-
console.log("fullState")
28-
const state = getState();
29-
return <p>{state.id} - {state.name} - {state.eta}</p>
30-
}, [getState])
31-
3226
return (
3327
<div style={{ display: "grid", gridTemplateColumns: "auto auto", justifyContent: "center", gap: 50 }}>
3428
<div>
@@ -48,7 +42,6 @@ const UseStateGetReset = () => {
4842
<Input id="eta" name="eta" value={state.eta} onChange={onChange} />
4943
</div>
5044
</div>
51-
{fullState}
5245
</div>
5346
);
5447
};

packages/react-tools/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@
4747
- [x] useHover
4848
- [x] useResponsive
4949
- [x] useClickOutside
50-
- [-] useNetwork
51-
- [ ] useOnline
50+
- [x] useNetwork
51+
- [x] useOnline
5252
- [ ] useSize (=useMeasuree?)
5353
- [ ] useScrollIntoView
5454
- [ ] useMouse

packages/react-tools/src/hooks/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,6 @@ export { useLazyRef } from './useLazyRef';
4545
export { useId } from './useId';
4646
export { useHover } from './useHover';
4747
export { useResponsive } from './useResponsive';
48-
export { useClickOutside } from './useClickOutside';
48+
export { useClickOutside } from './useClickOutside';
49+
export { useNetwork } from './useNetwork';
50+
export { useIsOnline } from './useIsOnline';

packages/react-tools/src/hooks/useActiveElement.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const useActiveElement = ():Element|null => {
1111
return useSyncExternalStore(
1212
useCallback(notif => {
1313
previousElem.current = document.activeElement;
14-
const idTimeout: { id: number | NodeJS.Timeout } = { id: 0 };
14+
const idTimeout: { id: number } = { id: 0 };
1515
const listener = (e: Event) => {
1616
/**
1717
* INFO
@@ -27,7 +27,7 @@ export const useActiveElement = ():Element|null => {
2727
if (e.type === "focusout") {
2828
idTimeout.id = setTimeout(() => {
2929
notif()
30-
}, 0);
30+
}, 0) as unknown as number;
3131
} else {
3232
clearTimeout(idTimeout.id);
3333
notif();

0 commit comments

Comments
 (0)