├── .babelrc ├── .gitignore ├── index.js ├── license ├── package-lock.json ├── package.json ├── readme.md └── rollup.config.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["@babel/preset-env", "@babel/preset-react"] 3 | } 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (https://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # TypeScript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | # next.js build output 61 | .next 62 | dist 63 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import React, { Fragment, useState, useEffect, useRef } from 'react'; 2 | import IdleTimer from 'react-idle-timer'; 3 | import PropTypes from 'prop-types'; 4 | 5 | function LockScreen(props) { 6 | const [isLocked, setLock] = useState(false); 7 | const init = useRef(0); 8 | 9 | const handleOnIdle = () => { 10 | if (!isLocked) { 11 | setLock(!isLocked); 12 | } 13 | }; 14 | 15 | useEffect(() => { 16 | if (!init.current) { 17 | init.current = 1; 18 | return; 19 | } 20 | isLocked ? props.onScreenLocked() : props.onScreenUnlocked(); 21 | }, [isLocked]); 22 | 23 | const renderLockScreenUi = () => { 24 | if (isLocked) return props.ui(setLock); 25 | }; 26 | 27 | const defaultLockScreenStyles = () => 28 | isLocked && !props.className 29 | ? { 30 | filter: 'blur(4px)', 31 | userSelect: 'none', 32 | height: '100vh', 33 | overflow: 'hidden' 34 | } 35 | : {}; 36 | 37 | return ( 38 | 39 | 40 |
44 | {props.children} 45 |
46 | {renderLockScreenUi()} 47 |
48 | ); 49 | } 50 | 51 | LockScreen.defaultProps = { 52 | onScreenLocked: () => {}, 53 | onScreenUnlocked: () => {} 54 | }; 55 | 56 | LockScreen.propTypes = { 57 | timeout: PropTypes.number.isRequired, 58 | ui: PropTypes.func.isRequired, 59 | className: PropTypes.string, 60 | onScreenLocked: PropTypes.func, 61 | onScreenUnlocked: PropTypes.func 62 | }; 63 | 64 | export default LockScreen; 65 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Luqman Olushi O. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-lock-screen", 3 | "version": "0.1.1", 4 | "description": "Simple lock-screen for idle React applications", 5 | "source": "index.js", 6 | "main": "dist/react-lock-screen.js", 7 | "module": "dist/react-lock-screen.m.js", 8 | "scripts": { 9 | "clean": "rimraf dist", 10 | "bundle": "rollup -c", 11 | "build": "npm-run-all clean bundle", 12 | "release": "npm run build && np" 13 | }, 14 | "repository": { 15 | "type": "git", 16 | "url": "git+https://github.com/codeshifu/react-lock-screen.git" 17 | }, 18 | "files": [ 19 | "dist", 20 | "readme.md" 21 | ], 22 | "keywords": [ 23 | "react", 24 | "lock-screen", 25 | "react-lock-screen", 26 | "lock", 27 | "screen", 28 | "idle", 29 | "session" 30 | ], 31 | "author": "", 32 | "license": "MIT", 33 | "bugs": { 34 | "url": "https://github.com/codeshifu/react-lock-screen/issues" 35 | }, 36 | "homepage": "https://github.com/codeshifu/react-lock-screen#readme", 37 | "dependencies": { 38 | "prop-types": "15.7.2", 39 | "react-idle-timer": "4.2.11" 40 | }, 41 | "peerDependencies": { 42 | "react": "16.8.x", 43 | "react-dom": "16.8.x" 44 | }, 45 | "devDependencies": { 46 | "@babel/cli": "7.7.0", 47 | "@babel/core": "7.7.2", 48 | "@babel/preset-env": "7.7.1", 49 | "@babel/preset-react": "7.7.0", 50 | "np": "3.1.0", 51 | "npm-run-all": "4.1.5", 52 | "react": "16.11.0", 53 | "react-dom": "16.11.0", 54 | "rimraf": "3.0.0", 55 | "rollup": "1.26.3", 56 | "rollup-plugin-babel": "4.3.3", 57 | "rollup-plugin-commonjs": "10.1.0", 58 | "rollup-plugin-node-resolve": "5.2.0", 59 | "rollup-plugin-peer-deps-external": "2.2.0", 60 | "rollup-plugin-terser": "5.1.2" 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # react-lock-screen 2 | 3 | > Simple lock-screen for idle React applications 4 | 5 | Display a custom lock-screen when your React applications becomes idle 6 | 7 | ## Demo 8 | See it in action [here](https://codesandbox.io/s/react-lock-screen-demo-q2zx3) 9 | 10 | ## Prerequisites 11 | 12 | - [React 16.8+](https://reactjs.org/blog/2019/02/06/react-v16.8.0.html) 13 | 14 | ## Installation 15 | 16 | ```bash 17 | $ npm install react-lock-screen 18 | ``` 19 | 20 | ## Usage 21 | 22 | ```jsx 23 | import LockScreen from 'react-lock-screen' 24 | ... 25 | 26 | function App() { 27 | 28 | const getLockScreenUi = (setLock) => { 29 | return 30 | } 31 | 32 | return ( 33 | 36 |

Lorem Ipsum is not simply

37 |
38 | ) 39 | ``` 40 | 41 | ## APIs (props) 42 | 43 | ### timeout 44 | 45 | type: `number` 46 | 47 | The number of idle (milliseconds) time before screen is locked 48 | 49 | ### ui 50 | 51 | type: `function` 52 | 53 | A function that returns some jsx representing the UI to show when screen is locked. Recieves a function argument that can be used to toggle lock-screen state. 54 | 55 | ```jsx 56 | }> 57 | ... 58 | 59 | ``` 60 | 61 | ### className 62 | 63 | type: `string` 64 | 65 | Class name to be applied to the blurred div when screen is locked. This is useful if you want to change the blur amount (or other styles) of the div. 66 | 67 | 68 | ```css 69 | /* default styles */ 70 | .react-lock-screen { 71 | filter: blur(4px); 72 | userselect: none; 73 | height: 100vh; 74 | overflow: hidden; 75 | } 76 | ``` 77 | override default styles using className prop 78 | ```jsx 79 | 80 | ... 81 | ``` 82 | 83 | ```css 84 | .react-lock-screen.lock { 85 | /* your custom styles here */ 86 | } 87 | ``` 88 | 89 | ### onScreenLocked 90 | 91 | type: `function` 92 | 93 | Callback function that gets called when screen is locked 94 | 95 | ### onScreenUnlocked 96 | 97 | type: `function` 98 | 99 | Callback function that gets called when screen is unlocked 100 | 101 | ## Contributing 102 | 103 | Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. 104 | 105 | Please make sure to update tests as appropriate. 106 | 107 | ## License 108 | 109 | [react-lock-screen](https://github.com/codeshifu/react-lock-screen) is licensed under [MIT](https://github.com/codeshifu/react-lock-screen/blob/master/license) 110 | -------------------------------------------------------------------------------- /rollup.config.js: -------------------------------------------------------------------------------- 1 | import babel from "rollup-plugin-babel"; 2 | import resolve from "rollup-plugin-node-resolve"; 3 | import peerDepsExternal from "rollup-plugin-peer-deps-external"; 4 | import commonjs from 'rollup-plugin-commonjs'; 5 | import { terser } from 'rollup-plugin-terser'; 6 | 7 | import pkg from "./package.json"; 8 | 9 | const sourcemap = false; 10 | 11 | export default { 12 | input: "index.js", 13 | plugins: [ 14 | terser(), 15 | babel({ 16 | exclude: "node_modules/**" 17 | }), 18 | resolve(), 19 | commonjs(), 20 | peerDepsExternal() 21 | ], 22 | output: [ 23 | { 24 | file: pkg.module, 25 | format: "es", 26 | sourcemap 27 | }, 28 | { 29 | file: pkg.main, 30 | format: "cjs", 31 | sourcemap 32 | } 33 | ], 34 | external: [ 35 | 'prop-types', 36 | 'react-idle-timer' 37 | ] 38 | }; 39 | --------------------------------------------------------------------------------