-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hook to query permission status of browser API's
- Loading branch information
Showing
5 changed files
with
86 additions
and
0 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
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,20 @@ | ||
# `usePermission` | ||
|
||
React side-effect hook that query permission status from the user depends on the specific API. | ||
|
||
|
||
## Usage | ||
|
||
```jsx | ||
import {usePermission} from 'react-use'; | ||
|
||
const Demo = () => { | ||
const state = usePermission({ name: 'microphone' }); | ||
|
||
return ( | ||
<pre> | ||
{JSON.stringify(state, null, 2)} | ||
</pre> | ||
); | ||
}; | ||
``` |
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,14 @@ | ||
import { storiesOf } from '@storybook/react'; | ||
import * as React from 'react'; | ||
import { usePermission } from '..'; | ||
import ShowDocs from './util/ShowDocs'; | ||
|
||
const Demo = () => { | ||
const state = usePermission({ name: 'microphone' }); | ||
|
||
return <pre>{JSON.stringify(state, null, 2)}</pre>; | ||
}; | ||
|
||
storiesOf('Side effects|usePermission', module) | ||
.add('Docs', () => <ShowDocs md={require('../../docs/usePermission.md')} />) | ||
.add('Demo', () => <Demo />); |
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,49 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { off, on } from './util'; | ||
|
||
type PermissionDesc = | ||
| PermissionDescriptor | ||
| DevicePermissionDescriptor | ||
| MidiPermissionDescriptor | ||
| PushPermissionDescriptor; | ||
|
||
type State = PermissionState | ''; | ||
|
||
const noop = () => {}; | ||
|
||
const usePermission = (permissionDesc: PermissionDesc): State => { | ||
let mounted = true; | ||
let permissionStatus: PermissionStatus | null = null; | ||
|
||
const [state, setState] = useState<State>(''); | ||
|
||
const onChange = () => { | ||
if (mounted && permissionStatus) { | ||
setState(permissionStatus.state); | ||
} | ||
}; | ||
|
||
const changeState = () => { | ||
onChange(); | ||
on(permissionStatus, 'change', onChange); | ||
}; | ||
|
||
useEffect(() => { | ||
navigator.permissions | ||
.query(permissionDesc) | ||
.then(status => { | ||
permissionStatus = status; | ||
changeState(); | ||
}) | ||
.catch(noop); | ||
|
||
return () => { | ||
mounted = false; | ||
permissionStatus && off(permissionStatus, 'change', onChange); | ||
}; | ||
}, []); | ||
|
||
return state; | ||
}; | ||
|
||
export default usePermission; |