Skip to content

Commit

Permalink
🌅
Browse files Browse the repository at this point in the history
  • Loading branch information
bsonntag committed Oct 28, 2018
0 parents commit 4f12aa6
Show file tree
Hide file tree
Showing 14 changed files with 8,123 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["@babel/env", "@babel/react"]
}
42 changes: 42 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
version: 2
jobs:
build:
docker:
- image: circleci/node:8.12

working_directory: ~/repo

steps:
- checkout

- restore_cache:
name: Restore node_modules cache
keys:
- v1-dependencies-{{ checksum "package.json" }}
- v1-dependencies-

- run:
name: Node version
command: node --version

- run:
name: Install dependencies
command: yarn install --frozen-lockfile

- run:
name: Lint
command: yarn lint

- run:
name: Tests
command: yarn test

- run:
name: Coverage
command: yarn coveralls

- save_cache:
name: Save node_modules cache
paths:
- node_modules
key: v1-dependencies-{{ checksum "package.json" }}
6 changes: 6 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
env: {
browser: 1,
},
extends: 'seegno'
};
62 changes: 62 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# Parcel folders
build
.cache
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Benjamim Sonntag

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
72 changes: 72 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# react-use-user-media

[![CircleCI](https://circleci.com/gh/bsonntag/react-use-user-media.svg?style=svg)](https://circleci.com/gh/bsonntag/react-use-user-media)
[![Coverage Status](https://coveralls.io/repos/github/bsonntag/react-use-user-media/badge.svg?branch=master)](https://coveralls.io/github/bsonntag/react-use-user-media?branch=master)

React hook for accessing user media.

## Installation

Using npm:

```sh
$ npm install --save react-use-user-media
```

Using yarn:

```sh
$ yarn add react-use-user-media
```

This module uses React's upcoming hooks feature.
To try this out you'll also need to install the 16.7.0-alpha.0 version
of `react` and `react-dom`:

```sh
$ yarn add react@16.7.0-alpha.0 react-dom@16.7.0-alpha.0
```

## Usage

```js
import React from 'react';
import Video from '@bsonntag/react-video';
import useUserMedia from 'react-use-user-media';

function Example() {
const { stream } = useUserMedia({ video: true });

if (!stream) {
return <p>Waiting...</p>;
}

return (
<Video
play
srcObject={stream}
/>
);
}
```

## API

```js
useUserMedia(Object): {
error: Error,
stream: MediaStream
}
```

Receives a [constraints object](https://developer.mozilla.org/en-US/docs/Web/API/MediaStreamConstraints)
to call [`getUserMedia`](https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia)
with and returns an object with the error and [stream](https://developer.mozilla.org/en-US/docs/Web/API/MediaStream/MediaStream).

## Contributing

Please feel free to submit any issues or pull requests.

## License

MIT
28 changes: 28 additions & 0 deletions example/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
body {
font-size: 20px;
margin: 0;
}

#root {
align-content: center;
display: grid;
grid-gap: 1em;
height: 100vh;
justify-content: center;
width: 100vw;
}

button {
border: 0;
border-radius: 1em;
box-shadow: 0px 2px 4px #0008;
cursor: pointer;
font-size: 1em;
min-width: 6em;
padding: 1em 2em;
}

button:focus,
button:hover {
opacity: 0.8;
}
13 changes: 13 additions & 0 deletions example/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>react-use-user-media example</title>
<link rel="stylesheet" href="./index.css" type="text/css">
</head>
<body>
<div id="root"></div>
<script src="./index.js" type="text/javascript"></script>
</body>
</html>
25 changes: 25 additions & 0 deletions example/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { render } from 'react-dom';
import React from 'react';
import Video from '@bsonntag/react-video';
import useUserMedia from '../src';

const Example = () => {
const { stream } = useUserMedia({ video: true });

if (!stream) {
return (
<p>
{'Waiting...'}
</p>
);
}

return (
<Video
play
srcObject={stream}
/>
);
};

render(<Example />, document.getElementById('root'));
1 change: 1 addition & 0 deletions jest-setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import 'jest-dom/extend-expect';
63 changes: 63 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"name": "react-use-user-media",
"version": "0.0.0-alpha.0",
"description": "React hook for accessing user media.",
"keywords": [
"hook",
"hooks",
"media",
"react",
"user"
],
"repository": "git@github.com:bsonntag/react-use-user-media.git",
"license": "MIT",
"author": "Benjamim Sonntag <benjamimsonntag@gmail.com>",
"main": "dist/index.js",
"scripts": {
"bundle": "rm -rf dist && babel src --out-dir dist --ignore **/*.test.js",
"bundle:watch": "babel src --out-dir dist --watch --ignore **/*.test.js",
"coveralls": "jest --coverage --silent --coverageReporters=text-lcov | coveralls",
"example": "parcel --out-dir build example/index.html",
"lint": "eslint src",
"test": "jest",
"test:watch": "jest --watch --notify",
"version": "npm run bundle && git add dist"
},
"jest": {
"coverageDirectory": "coverage",
"coveragePathIgnorePatterns": [
"jest-setup.js"
],
"coverageReporters": [
"html",
"lcov",
"text"
],
"setupTestFrameworkScriptFile": "./jest-setup.js"
},
"devDependencies": {
"@babel/cli": "^7.1.2",
"@babel/core": "^7.1.2",
"@babel/preset-env": "^7.1.0",
"@babel/preset-react": "^7.0.0",
"@bsonntag/react-video": "^0.0.1",
"babel-core": "^7.0.0-bridge",
"babel-jest": "^23.6.0",
"coveralls": "^3.0.2",
"eslint": "^5.7.0",
"eslint-config-seegno": "^11.0.1",
"jest": "^23.6.0",
"jest-dom": "^2.1.0",
"parcel-bundler": "^1.10.3",
"react": "^16.7.0-alpha.0",
"react-dom": "^16.7.0-alpha.0",
"react-testing-library": "^5.2.3"
},
"peerDependencies": {
"react": "^16.7.0-alpha.0"
},
"dependencies": {
"react-use-promise": "^0.0.0-alpha.0",
"stop-media-stream": "^0.0.2"
}
}
15 changes: 15 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { useEffect } from 'react';
import stopMediaStream from 'stop-media-stream';
import usePromise from 'react-use-promise';

function useUserMedia(constraints) {
const [stream, error] = usePromise(() => {
return navigator.mediaDevices.getUserMedia(constraints);
});

useEffect(() => () => stopMediaStream(stream), [stream]);

return { error, stream };
}

export default useUserMedia;
Loading

0 comments on commit 4f12aa6

Please sign in to comment.