Skip to content

Commit

Permalink
feat: add usePermission
Browse files Browse the repository at this point in the history
  • Loading branch information
gelove committed Jun 26, 2019
1 parent ba6d375 commit 4da40b9
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
20 changes: 20 additions & 0 deletions docs/usePermission.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# `usePermission`

React UI hook that query permission status from the user depends on the specific AP.


## Usage

```jsx
import {usePermission} from 'react-use';

const Demo = () => {
const state = usePermission({ name: 'microphone' });

return (
<pre>
{JSON.stringify(state, null, 2)}
</pre>
);
};
```
14 changes: 14 additions & 0 deletions src/__stories__/usePermission.story.tsx
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('UI|usePermission', module)
.add('Docs', () => <ShowDocs md={require('../../docs/usePermission.md')} />)
.add('Demo', () => <Demo />);
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import useNumber from './useNumber';
import useObservable from './useObservable';
import useOrientation from './useOrientation';
import usePageLeave from './usePageLeave';
import usePermission from './usePermission';
import usePrevious from './usePrevious';
import usePromise from './usePromise';
import useRaf from './useRaf';
Expand Down Expand Up @@ -123,6 +124,7 @@ export {
useObservable,
useOrientation,
usePageLeave,
usePermission,
usePrevious,
usePromise,
useRaf,
Expand Down
46 changes: 46 additions & 0 deletions src/usePermission.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { useEffect, useState } from 'react';
import { off, on } from './util';

type PermissionDesc =
| PermissionDescriptor
| DevicePermissionDescriptor
| MidiPermissionDescriptor
| PushPermissionDescriptor;

const noop = () => {};

const usePermission = (permissionDesc: PermissionDesc): string => {
const [state, setState] = useState('');
let mounted = true;
let permissionStatus: PermissionStatus | null = null;

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;

0 comments on commit 4da40b9

Please sign in to comment.