npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-mic-cam-permissions

v1.0.13

Published

Managing camera and microphone permissions with React

Downloads

56

Readme

React-Mic-Cam-Permissions

React-Mic-Cam-Permissions is a React library for managing camera and microphone permissions in web applications. It allows users to request and manage permissions seamlessly, while displaying status messages to inform the user of the results.

To install the library, run the following command:

npm i react-mic-cam-permissions

Use

1. Microphone permission request

You can use the MicrophonePermissionButton component to request permission to use the microphone. Just add it to your component and pass the events to find out if permission was granted or denied.

Example code:

import React from 'react';
import { MicrophonePermissionButton } from 'media-permissions';

const App = () => {
  const handleGranted = () => {
    console.log('Microphone permission granted');
  };

  const handleDenied = () => {
    console.log('Microphone permission denied');
  };

  return (
    <div>
      <h1>Microphone Permission</h1>
      <MicrophonePermissionButton
        buttonText="Request Microphone Permission"
        onGranted={handleGranted}
        onDenied={handleDenied}
      />
    </div>
  );
};

export default App;

2. Camera permission request

The CameraPermissionButton component works the same as the one for the microphone, but for the camera.

Example code:

import React from 'react';
import { CameraPermissionButton } from 'media-permissions';

const App = () => {
  const handleGranted = () => {
    console.log('Camera permission granted');
  };

  const handleDenied = () => {
    console.log('Camera permission denied');
  };

  return (
    <div>
      <h1>Camera Permission</h1>
      <CameraPermissionButton
        buttonText="Request Camera Permission"
        onGranted={handleGranted}
        onDenied={handleDenied}
      />
    </div>
  );
};

export default App;

3. Viewing permission status

The PermissionStatus component allows you to display the current status of permissions for the camera or microphone, with icons adapted to indicate whether the permission has been granted, denied, or is pending.

Example code:

import React from 'react';
import { PermissionStatus } from 'media-permissions';

const App = () => {
  return (
    <div>
      <h1>Permission Status</h1>
      <PermissionStatus permissionType="microphone" state="granted" />
      <PermissionStatus permissionType="camera" state="denied" />
    </div>
  );
};

export default App;

4. Full display with device selection

You can also display available devices (cameras and microphones) and allow users to select them using the PermissionStatusDisplay component.

Example code:

import React from 'react';
import { PermissionStatusDisplay } from 'media-permissions';

const App = () => {
  return (
    <div>
      <h1>Media Permissions</h1>
      <PermissionStatusDisplay />
    </div>
  );
};

export default App;

5. Managing permissions via the hook

You can also manage permissions directly with the useCameraPermission and useMicrophonePermission hooks.

Example of using the microphone hook:

import React, { useEffect } from 'react';
import { useMicrophonePermission } from 'media-permissions';

const App = () => {
  const { permissionState, requestPermission } = useMicrophonePermission();

  useEffect(() => {
    console.log('Microphone permission state:', permissionState);
  }, [permissionState]);

  return (
    <div>
      <h1>Microphone Permission</h1>
      <button onClick={requestPermission}>Request Microphone Permission</button>
      <p>Current Permission State: {permissionState}</p>
    </div>
  );
};

export default App;

Example of using the camera hook:

import React, { useEffect } from 'react';
import { useCameraPermission } from 'media-permissions';

const App = () => {
  const { permissionState, requestPermission } = useCameraPermission();

  useEffect(() => {
    console.log('Camera permission state:', permissionState);
  }, [permissionState]);

  return (
    <div>
      <h1>Camera Permission</h1>
      <button onClick={requestPermission}>Request Camera Permission</button>
      <p>Current Permission State: {permissionState}</p>
    </div>
  );
};

export default App;

Features

  • Permission request: You can request permissions for the camera and microphone.
  • Status display: The PermissionStatus component allows you to display the permission status.
  • Device Selection: Display available video and audio devices for the user to select.
  • Custom hooks: Manage permissions directly in your components with the useCameraPermission and useMicrophonePermission hooks.

API

Components

MicrophonePermissionButton

  • className: Customizes the style of the button.
  • buttonText: The text to display on the button.
  • onGranted: Callback when permission is granted.
  • onDenied: Callback when permission is denied.

CameraPermissionButton

Works the same as MicrophonePermissionButton, but for the camera.

PermissionStatus

  • permissionType: Can be camera or microphone.
  • state: The state of the permission, can be granted, denied, prompt or unavailable.
  • className: Customizes the display style.

PermissionStatusDisplay

Displays a set of controls for managing permissions and selecting devices.

Hooks

useMicrophonePermission

Returns the permission status for the microphone as well as a function to request this permission.

useCameraPermission

Returns the permission status for the camera as well as a function to request this permission.