Skip to content

Commit 711fb2b

Browse files
committed
👀 init
0 parents  commit 711fb2b

12 files changed

+6696
-0
lines changed

‎.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
*.log
3+
dist
4+
.cache

‎.travis.yml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
git:
2+
depth: 1
3+
sudo: false
4+
language: node_js
5+
node_js:
6+
- '8'
7+
cache:
8+
yarn: true
9+
directories:
10+
- node_modules
11+
script:
12+
- yarn test

‎LICENSE

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright © 2018-present [Jack Hanford](http://jackhanford.com), [email protected]
2+
3+
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:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
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.

‎README.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# `document-visibility-hook`
2+
3+
> React hook for subscribing to document visibility
4+
5+
> **Note:** This is using the new [React Hooks API Proposal](https://reactjs.org/docs/hooks-intro.html)
6+
> which is subject to change until React 16.7 final.
7+
>
8+
> You'll need to install `react`, `react-dom`, etc at `^16.7.0-alpha.0`
9+
10+
## Install
11+
12+
```sh
13+
yarn add document-visibility-hook
14+
```
15+
16+
## Usage
17+
18+
```js
19+
import useDocumentVisibility from 'document-visibility-hook';
20+
21+
function MyComponent() {
22+
let documentvisibility = useDocumentVisibility();
23+
// {
24+
// visible: true,
25+
// }
26+
27+
// ...
28+
}
29+
```

‎example.html

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
7+
<title>Example</title>
8+
</head>
9+
<body>
10+
<div id="root"></div>
11+
<script src="./example.js"></script>
12+
</body>
13+
</html>

‎example.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import React from 'react';
2+
import { render } from 'react-dom';
3+
import useDocumentVis from './';
4+
5+
function App() {
6+
let documentVisible = useDocumentVis();
7+
console.log(documentVisible)
8+
return <pre>{JSON.stringify(documentVisible)}</pre>;
9+
}
10+
11+
render(<App />, window.root);

‎index.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
let { useState, useEffect } = require('react');
4+
5+
function getVisibility() {
6+
return {
7+
visible: document.visibilityState === 'visible'
8+
}
9+
}
10+
11+
function useDocumentVisibility() {
12+
let [documentVis, setDocumentVis] = useState(getVisibility());
13+
14+
function handleVisChange() {
15+
setDocumentVis(getVisibility());
16+
}
17+
18+
useEffect(() => {
19+
window.addEventListener('visibilitychange', handleVisChange);
20+
return () => {
21+
window.removeEventListener('visibilitychange', handleVisChange);
22+
};
23+
});
24+
25+
return documentVis;
26+
}
27+
28+
module.exports = useDocumentVisibility;

‎index.js.flow

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
interface WindowSize {
2+
innerHeight: number,
3+
innerWidth: number,
4+
outerHeight: number,
5+
outerWidth: number,
6+
}
7+
8+
declare export default function useWindowSize(): WindowSize;

‎package.json

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"name": "document-visibility-hook",
3+
"version": "1.0.0",
4+
"description": "React hook for subscribing to document visibility",
5+
"main": "index.js",
6+
"repository": "https://github.com/hanford/document-visibility-hook",
7+
"author": "Jack Hanford <[email protected]>",
8+
"license": "MIT",
9+
"publishConfig": {
10+
"access": "public"
11+
},
12+
"keywords": [
13+
"react",
14+
"hooks",
15+
"window",
16+
"browser",
17+
"document",
18+
"visibility"
19+
],
20+
"files": [
21+
"index.*"
22+
],
23+
"scripts": {
24+
"test": "ava test.js",
25+
"example": "parcel example.html"
26+
},
27+
"dependencies": {
28+
"react": "^16.7.0-alpha.0"
29+
},
30+
"devDependencies": {
31+
"ava": "^0.25.0",
32+
"browser-env": "^3.2.5",
33+
"parcel": "^1.10.3",
34+
"raf": "^3.4.0",
35+
"react-dom": "^16.7.0-alpha.0",
36+
"react-test-renderer": "^16.7.0-alpha.0"
37+
},
38+
"ava": {
39+
"require": [
40+
"./test-setup.js"
41+
]
42+
}
43+
}

‎test-setup.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require('raf/polyfill');
2+
require('browser-env')();

‎test.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
let test = require('ava');
3+
let { createElement: h } = require('react');
4+
let ReactTestRenderer = require('react-test-renderer');
5+
let useWindowSize = require('./');
6+
7+
function render(val) {
8+
return ReactTestRenderer.create(val).toJSON();
9+
}
10+
11+
test(t => {
12+
function Component() {
13+
return JSON.stringify(useWindowSize());
14+
}
15+
16+
t.is(render(h(Component)), JSON.stringify({
17+
visible: document.visibilityState === 'visible',
18+
}));
19+
});

0 commit comments

Comments
 (0)