-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
161de6e
commit 36082c2
Showing
16 changed files
with
5,755 additions
and
5,619 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ docs/node_modules | |
docs/.docusaurus | ||
docs/build | ||
docs/src/hooks | ||
docs/blog | ||
dist | ||
example | ||
test | ||
test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# useIsOnline | ||
|
||
A hook for detecting network status of the user. This hook will return a boolean value indicating whether the user is online or not. The value will be automatically updated when the user's network status changes. | ||
|
||
<pre>{`import {useIsOnline} from 'react-use-custom-hooks';`}</pre> | ||
|
||
:::caution | ||
The hook works based on value of `navigator.onLine` property, so this hook returns `true` does not always mean the user connected to the internet, it can also just a connection to some network. | ||
|
||
If the browser doesn't support `navigator.onLine`, the hook will return `false/undefined`. | ||
|
||
Early versions of Chrome and Safari always reported "true" for navigator.onLine | ||
|
||
Desktop Firefox responds to the status of its "Work Offline" mode. If not in that mode, `navigator.onLine` is always true, regardless of the actual network connectivity status. See [ firefox bug](https://bugzilla.mozilla.org/show_bug.cgi?id=654579) for details. | ||
|
||
::: | ||
|
||
### Usage example | ||
|
||
```typescript | ||
const isOnline: boolean = useIsOnline(); | ||
``` | ||
|
||
### Playground | ||
|
||
Try disconnecting and then connecting your network and see the value changes. | ||
|
||
```jsx live | ||
function IsOnlineExample(props) { | ||
const isOnline = useIsOnline(); | ||
return <>User is online: {isOnline.toString()}</>; | ||
} | ||
``` | ||
|
||
### API | ||
|
||
```typescript | ||
export function useIsOnline(): Boolean; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# useTitle | ||
|
||
A hook to manage title value of document. | ||
|
||
<pre>{`import {useTitle} from 'react-use-custom-hooks';`}</pre> | ||
|
||
### Usage example | ||
|
||
```typescript | ||
useTitle('My title'); | ||
``` | ||
|
||
### Playground | ||
|
||
You can pass static value or computed value, state or prop to `useTitle` hook. The title of the document will get's updated whenever the value changes. Also you can restore the previous title if the component using this hook unmounts. Pass `restoreOnUnmount` value `true` in `options` object to configure the behavior. | ||
|
||
```jsx live | ||
function TitleExample(props) { | ||
const [title, setTitle] = useState('My Title!'); | ||
useTitle(title); | ||
return ( | ||
<> | ||
<input | ||
type="text" | ||
value={title} | ||
onChange={e => setTitle(e.target.value)} | ||
/> | ||
</> | ||
); | ||
} | ||
``` | ||
|
||
### API | ||
|
||
```typescript | ||
interface Options { | ||
restoreOnUnmount?: boolean; | ||
} | ||
|
||
export function useTitle( | ||
title: string, | ||
options: Options = DEFAULT_OPTIONS | ||
): void; | ||
``` | ||
|
||
#### Options | ||
|
||
| Property | Description | Type | Default | | ||
| ---------------- | ---------------------------------------------- | --------- | ------- | | ||
| restoreOnUnmount | Indicate to restore the title value on unmount | `boolean` | false | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file was deleted.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { useCallback } from 'react'; | ||
import { useState } from 'react'; | ||
import { useEffect } from 'react'; | ||
|
||
export function useIsOnline(): Boolean { | ||
const isNavigatorOnLineDefined: boolean = | ||
typeof window.navigator.onLine === 'boolean'; | ||
const [isOnline, setIsOnline] = useState<boolean>(window.navigator.onLine); | ||
|
||
const onStatusChange = useCallback( | ||
() => setIsOnline(window.navigator.onLine), | ||
[] | ||
); | ||
|
||
useEffect(() => { | ||
if (!isNavigatorOnLineDefined) { | ||
return; | ||
} | ||
window.addEventListener('online', onStatusChange); | ||
window.addEventListener('offline', onStatusChange); | ||
|
||
return () => { | ||
window.removeEventListener('online', onStatusChange); | ||
window.removeEventListener('offline', onStatusChange); | ||
}; | ||
}, []); | ||
|
||
return isOnline; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { useEffect, useRef } from 'react'; | ||
|
||
interface Options { | ||
restoreOnUnmount?: boolean; | ||
} | ||
|
||
const DEFAULT_OPTIONS: Options = { | ||
restoreOnUnmount: false, | ||
}; | ||
|
||
export function useTitle( | ||
title: string, | ||
options: Options = DEFAULT_OPTIONS | ||
): void { | ||
const isDocumentDefined = typeof document !== 'undefined'; | ||
const originalTitle = useRef(isDocumentDefined ? document.title : ''); | ||
|
||
useEffect(() => { | ||
if (!isDocumentDefined) return; | ||
if (document.title !== title) document.title = title; | ||
return () => { | ||
if (options?.restoreOnUnmount) document.title = originalTitle.current; | ||
}; | ||
}, [title]); | ||
} |
Oops, something went wrong.