Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
danhayden committed Dec 4, 2017
0 parents commit 93d1e0a
Show file tree
Hide file tree
Showing 10 changed files with 234 additions and 0 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist/**
11 changes: 11 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
parser: 'babel-eslint',
extends: ['unobtrusive', 'unobtrusive/react'],
env: {
browser: true,
node: true
},
globals: {
Map: true
}
}
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# npm dependencies
node_modules

# generated files
/dist

# system files
*.log
.DS_Store
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"bracketSpacing": false,
"semi": false,
"singleQuote": true
}
10 changes: 10 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
The MIT License (MIT)
=====================

Copyright © 2017 Dan Hayden

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.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# React Exiting

[![version][version]](http://npm.im/react-exiting)
[![MIT License][mit license]](http://opensource.org/licenses/MIT)
[![Size][size]](https://unpkg.com/react-exiting)
[![Size gzip][size gzip]](https://unpkg.com/react-exiting)

Notifies your app when the user is exiting.

## Usage

```js
import React from 'react'
import {Exiting} from 'react-exiting'

const App = () => (
<Exiting
onChange={({isExiting}) => console.log({isExiting})}
render={({isExiting}) => (
<h1>User is {isExiting ? 'exiting' : 'not exiting'}</h1>
)}
/>
)
```

### License

MIT

[version]: https://img.shields.io/npm/v/react-exiting.svg
[mit license]: https://img.shields.io/npm/l/react-exiting.svg
[size]: https://badges.herokuapp.com/size/npm/react-exiting
[size gzip]: https://badges.herokuapp.com/size/npm/react-exiting?gzip=true
72 changes: 72 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{
"name": "react-exiting",
"version": "0.0.1",
"description": "Notifies your app when the user is exiting.",
"keywords": [
"react",
"exiting"
],
"author": "Dan Hayden <[email protected]>",
"license": "MIT",
"main": "dist/react-exiting.js",
"module": "dist/react-exiting.es.js",
"umd:main": "dist/react-exiting.umd.js",
"files": [
"dist"
],
"scripts": {
"test": "echo \"Error: no test specified\"",
"lint": "prettier -- 'src/**/*.{js,json}'",
"build": "rollup -c",
"dev": "rollup -c -w",
"changelog": "standard-version",
"size": "npx gzip-size-cli ./dist/react-exiting.js ",
"precommit": "lint-staged",
"prepush": "npm test",
"prepublish": "npm run build"
},
"lint-staged": {
"src/**/*.{js,json}": [
"prettier --list-different --log-level error"
]
},
"peerDependencies": {
"react": ">=15"
},
"dependencies": {
"lodash": "4.17.4",
"prop-types": "15.6.0"
},
"devDependencies": {
"babel-core": "6.26.0",
"babel-eslint": "8.0.3",
"babel-plugin-external-helpers": "6.22.0",
"babel-preset-env": "1.6.1",
"babel-preset-react": "6.24.1",
"babel-preset-stage-2": "6.24.1",
"eslint": "4.12.1",
"eslint-config-unobtrusive": "1.2.0",
"eslint-plugin-react": "7.5.1",
"husky": "0.14.3",
"lint-staged": "6.0.0",
"prettier": "1.8.2",
"react": "16.2.0",
"react-dom": "16.2.0",
"rollup": "0.49.2",
"rollup-plugin-babel": "3.0.2",
"rollup-plugin-commonjs": "8.2.0",
"rollup-plugin-node-resolve": "3.0.0",
"rollup-plugin-uglify": "2.0.1",
"rollup-watch": "4.3.1",
"standard-version": "4.2.0",
"uglify-es": "3.0.28"
},
"repository": {
"type": "git",
"url": "git+https://github.com/danhayden/react-exiting.git"
},
"bugs": {
"url": "https://github.com/danhayden/react-exiting/issues"
},
"homepage": "https://github.com/danhayden/react-exiting#readme"
}
44 changes: 44 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import resolve from 'rollup-plugin-node-resolve'
import commonjs from 'rollup-plugin-commonjs'
import babel from 'rollup-plugin-babel'
import uglify from 'rollup-plugin-uglify'
import {minify} from 'uglify-es'
import fs from 'fs'
const pkg = JSON.parse(fs.readFileSync('./package.json'))

export default {
name: 'ReactExiting',
input: 'src/react-exiting.js',
output: [
{format: 'es', file: pkg.module},
{format: 'cjs', file: pkg.main},
{format: 'umd', file: pkg['umd:main']}
],
external: ['react', 'prop-types'],
globals: {
react: 'React',
'prop-types': 'PropTypes'
},
plugins: [
resolve(),
commonjs(),
babel({
babelrc: false,
exclude: 'node_modules/**',
presets: [
[
'babel-preset-env',
{
modules: false,
loose: true,
targets: {browsers: ['last 2 versions', '> 1%']}
}
],
'babel-preset-react',
'babel-preset-stage-2'
],
plugins: ['external-helpers']
}),
uglify({}, minify)
]
}
48 changes: 48 additions & 0 deletions src/react-exiting.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import throttle from 'lodash/throttle'

export default function ExitIntent(options = {}) {
const defaultOptions = {
threshold: 20,
eventThrottle: 200,
onExitIntent: () => {}
}

return (function() {
const config = {...defaultOptions, ...options}
const eventListeners = new Map()

const addEvent = (eventName, callback) => {
document.addEventListener(eventName, callback, false)
eventListeners.set(`document:${eventName}`, {eventName, callback})
}

const removeEvent = key => {
const {eventName, callback} = eventListeners.get(key)
document.removeEventListener(eventName, callback)
eventListeners.delete(key)
}

const shouldDisplay = position => {
if (position <= config.threshold) {
return true
}
return false
}

const mouseDidMove = event => {
if (shouldDisplay(event.clientY)) {
config.onExitIntent(true)
} else {
config.onExitIntent(false)
}
}

const removeEvents = () => {
eventListeners.forEach((value, key, map) => removeEvent(key))
}

addEvent('mousemove', throttle(mouseDidMove, config.eventThrottle))

return removeEvents
})()
}

0 comments on commit 93d1e0a

Please sign in to comment.