Skip to content

Commit f8e5879

Browse files
committed
main 🧊 add enabled for use permission
1 parent ba06635 commit f8e5879

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

‎src/hooks/usePermission/usePermission.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ export type UsePermissionName =
2121
| 'push'
2222
| 'speaker';
2323

24+
/** The use permission options type */
25+
export interface UsePermissionOptions {
26+
/** Whether the permission is enabled */
27+
enabled: boolean;
28+
}
29+
2430
/** The use permission return type */
2531
export interface UsePermissionReturn {
2632
/** The permission state */
@@ -41,9 +47,13 @@ export interface UsePermissionReturn {
4147
* @example
4248
* const { state, supported, query } = usePermission('microphone');
4349
*/
44-
export const usePermission = (permissionDescriptorName: UsePermissionName) => {
50+
export const usePermission = (
51+
permissionDescriptorName: UsePermissionName,
52+
options?: UsePermissionOptions
53+
) => {
4554
const [state, setState] = useState<PermissionState>('prompt');
4655
const supported = navigator && 'permissions' in navigator;
56+
const enabled = options?.enabled ?? true;
4757

4858
const permissionDescriptor = { name: permissionDescriptorName };
4959

@@ -61,13 +71,13 @@ export const usePermission = (permissionDescriptorName: UsePermissionName) => {
6171
});
6272

6373
useEffect(() => {
64-
if (!supported) return;
74+
if (!supported || !enabled) return;
6575
query();
6676
window.addEventListener('change', query);
6777
return () => {
6878
window.removeEventListener('change', query);
6979
};
70-
}, [permissionDescriptorName]);
80+
}, [permissionDescriptorName, enabled]);
7181

7282
return {
7383
state,

0 commit comments

Comments
 (0)