Skip to content

Commit c5fcdf3

Browse files
committed
[IMPL] useShare hook
1 parent 255b166 commit c5fcdf3

8 files changed

Lines changed: 118 additions & 23 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { useRef } from "react"
2+
import { useShare } from "../../../../../../packages/react-tools/src"
3+
4+
/**
5+
The component uses _useShare_ hook to know if Web share API is supported and returns a button to share link to this library.
6+
*/
7+
export const UseShare = () => {
8+
const sharedDate = useRef<ShareData>({
9+
title: "React-tools",
10+
text: "React tools",
11+
url: window.location.origin + "/index.html"
12+
})
13+
const { isSupported, share } = useShare();
14+
15+
const onClick = () => share(sharedDate.current);
16+
17+
return <div style={{textAlign: "center"}}>
18+
<p>Is supported: {isSupported.toString()}</p>
19+
<button onClick={onClick}>Share</button>
20+
</div>
21+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ export const COMPONENTS = [
8080
"useIdle",
8181
"useFullscreen",
8282
"useBattery",
83-
"useGeolocation"
83+
"useGeolocation",
84+
"useShare"
8485
]
8586
],
8687
//UTILS

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

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,32 @@ Hook to use user's geographic location. Refer to [GeoLocation API](https://devel
66
```tsx
77
export const UseGeolocation = () => {
88
const [error, setError] = useState("")
9-
const [location] = useGeolocation({onError(error) {
10-
setError(error.message);
11-
}});
9+
const [location, currentPosition] = useGeolocation({
10+
mode: "manual",
11+
onError(error) {
12+
setError(error.message);
13+
}
14+
});
1215

13-
return (<div style={{ textAlign: "center" }}>
16+
return (<div style={{ textAlign: "left", width: 'fit-content', margin:'0 auto' }}>
1417
{
1518
error && <p style={{ color: 'red' }}>{error}</p>
1619
}
17-
<p >isSupported: {location?.isSupported}</p>
20+
<br/>
21+
<button onClick={currentPosition}>Get Location</button>
22+
<br />
23+
<p >isSupported: {location.isSupported ? "true" : "false"}</p>
1824
<p >Timestamp: {location?.position?.timestamp}</p>
1925
<p >Coords:</p>
20-
<ul>
21-
{
22-
Object.entries(location?.position?.coords ?? []).map(([key, value]) => {
23-
return <li>
24-
{key}: {value}
25-
</li>
26-
})
27-
}
28-
</ul>
26+
<div style={{paddingLeft: 10, textAlign: 'left', width: 'fit-content', margin: '0 auto'}}>
27+
<p>accuracy: {location.position?.coords.accuracy}</p>
28+
<p>altitude: {location.position?.coords.altitude}</p>
29+
<p>altitudeAccuracy: {location.position?.coords.altitudeAccuracy}</p>
30+
<p>heading: {location.position?.coords.heading}</p>
31+
<p>latitude: {location.position?.coords.latitude}</p>
32+
<p>longitude: {location.position?.coords.longitude}</p>
33+
<p>speed: {location.position?.coords.speed}</p>
34+
</div>
2935
</div>)
3036
}
3137
```
@@ -45,8 +51,8 @@ useGeolocation * - _first element_: is the location object with two properties:
4551
options to use geolocation.
4652
> - __opts.locationOptions?__: _PositionOptions_
4753
An optional object which provides options for retrieval of the position data.
48-
> - __opts.observe?__: _boolean_
49-
if true returns current position and observes position change, otherwise returns only position in that time.
54+
> - __opts.mode?__: _boolean_
55+
it establishes how to obtain the geographic location:
5056
> - __opts.onError?__: _GeolocationPositionError_
5157
callback that will be executed if there will be errors.
5258
>
@@ -59,6 +65,6 @@ callback that will be executed if there will be errors.
5965
- _()=>void_
6066
> Array with:
6167
> - _first element_: is the location object with two properties: __isSupported__ and __position__.
62-
> - _second element_: function to observe location changes and it receives one param __successCallback__ and two optional params __errorCallback__ and __options__.
63-
> - _third element_: function to cancel previous observing location changes.
68+
> - _second element_: function to obtain manually current location.
69+
> - _third element_: function to obtain location on every changes.
6470
>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# useShare
2+
Hook to use [Web Share Api](https://developer.mozilla.org/en-US/docs/Web/API/Web_Share_API).
3+
4+
## Usage
5+
6+
```tsx
7+
export const UseShare = () => {
8+
const sharedDate = useRef<ShareData>({
9+
title: "React-tools",
10+
text: "React tools",
11+
url: window.location.origin + "/index.html"
12+
})
13+
const { isSupported, share } = useShare();
14+
15+
const onClick = () => share(sharedDate.current);
16+
17+
return <div style={{textAlign: "center"}}>
18+
<p>Is supported: {isSupported.toString()}</p>
19+
<button onClick={onClick}>Share</button>
20+
</div>
21+
}
22+
```
23+
24+
> The component uses _useShare_ hook to know if Web share API is supported and returns a button to share link to this library.
25+
26+
27+
## API
28+
29+
```tsx
30+
useShare (): {isSupported: boolean, share: (data?: ShareData)=>Promise<void>}
31+
```
32+
33+
> ### Params
34+
>
35+
>
36+
>
37+
38+
> ### Returns
39+
>
40+
> __object__: __isSupported__ to known if share API is supported and __share__ function to use Web share API.
41+
> - __Object__:
42+
> - __isSupported__ : _boolean_
43+
> - __share__ : _(data?: ShareData) => Promise<void>_
44+
>

packages/react-tools/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,12 @@
9494
- [x] useFullscreen (check browser compatibility)
9595
- [x] useBattery
9696
- [x] useGeolocation
97-
- [ ] useShare
97+
- [x] useShare
9898
- [ ] useScreenShare
9999
- [ ] useFetch (with suspense ???)
100100
- [ ] useAsync
101101
- [ ] useAudio (???)
102102
- [ ] useVideo (???)
103-
- [ ] useOs
104103
- [ ] useEyeDropper (refer to https://developer.mozilla.org/en-US/docs/Web/API/EyeDropper_API)
105104
- [ ] useMedia
106105
- [ ] useMediaDevices

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,5 @@ export { useScrollIntoView } from './useScrollIntoView';
6161
export { useMouse } from './useMouse';
6262
export { useLongPress } from './useLongPress';
6363
export { useBattery } from './useBattery';
64-
export { useGeolocation } from './useGeolocation';
64+
export { useGeolocation } from './useGeolocation';
65+
export { useShare } from './useShare';
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { useCallback } from "react"
2+
3+
/**
4+
* **`useShare`**: Hook to use [Web Share Api](https://developer.mozilla.org/en-US/docs/Web/API/Web_Share_API).
5+
* @returns {{isSupported: boolean, share: (data?: ShareData) => Promise<void>}} object - __isSupported__ to known if share API is supported and __share__ function to use Web share API.
6+
*/
7+
export const useShare = (): {isSupported: boolean, share: (data?: ShareData)=>Promise<void>} => {
8+
const isSupported = "share" in navigator;
9+
10+
const share = useCallback((data?: ShareData) => {
11+
if ("share" in navigator) {
12+
return navigator.share(data);
13+
} else {
14+
return Promise.resolve(void 0);
15+
}
16+
}, []);
17+
18+
return {
19+
isSupported,
20+
share
21+
}
22+
}

packages/react-tools/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ export {
7777
useMouse,
7878
useLongPress,
7979
useBattery,
80-
useGeolocation
80+
useGeolocation,
81+
useShare
8182
} from './hooks'
8283

8384
export {

0 commit comments

Comments
 (0)