Skip to content

Commit

Permalink
feat(global): add swipe event publish helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
colinmeinke committed Feb 10, 2017
1 parent b5811b6 commit c6b9d2e
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 5 deletions.
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
A simple publish/subscribe pattern

- Plain old vanilla JS
- Just 0.6kb gzipped
- Just 1kb gzipped

## Installation

Expand Down Expand Up @@ -58,7 +58,7 @@ const event = new Event()
publishClick({ event })

event.subscribe('global.click', ({ target }) => {
console.log('Somebody clicked', target)
console.log('Somebody clicked on', target)
})
```

Expand All @@ -76,6 +76,20 @@ event.subscribe('global.escape', () => {
})
```

#### Swipe

```js
import Event, { publishSwipe } from 'core-events'

const event = new Event()

publishSwipe({ event, sensitivity: { x: 10, y: 50 } })

event.subscribe('global.swipe', ({ direction, target }) => {
console.log(`Somebody swiped ${direction} on`, target)
})
```

## Browser support

Core Events is packaged with Babel, and
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { publishClick, publishEscape } from './publish'
import { publishClick, publishEscape, publishSwipe } from './publish'

const _ = new WeakMap()

Expand Down Expand Up @@ -46,6 +46,6 @@ class Event {
}
}

export { publishClick, publishEscape }
export { publishClick, publishEscape, publishSwipe }

export default Event
55 changes: 54 additions & 1 deletion src/publish.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { supportsPassive } from './support'

const publishClick = ({ event, name = 'global.click' }) => {
window.addEventListener('click', ({ target }) => {
event.publish(name, { target })
Expand All @@ -12,4 +14,55 @@ const publishEscape = ({ event, name = 'global.escape' }) => {
})
}

export { publishClick, publishEscape }
const publishSwipe = ({
event,
name = 'global.swipe',
sensitivity = { x: 10, y: 10 }
}) => {
let swipeStarted = false
let swipeTarget = null
let swipeStartX = null
let swipeStartY = null

const swipeStart = e => {
swipeStarted = true
swipeTarget = e.target
swipeStartX = e.screenX
swipeStartY = e.screenY
}

const swipeEnd = e => {
if (swipeStarted) {
if (Math.abs(e.screenX - swipeStartX) >= sensitivity.x) {
e.screenX < swipeStartX
? event.publish(name, { direction: 'left', target: swipeTarget })
: event.publish(name, { direction: 'right', target: swipeTarget })
}

if (Math.abs(e.screenY - swipeStartY) >= sensitivity.y) {
e.screenY < swipeStartY
? event.publish(name, { direction: 'up', target: swipeTarget })
: event.publish(name, { direction: 'down', target: swipeTarget })
}

swipeStarted = false
swipeTarget = null
swipeStartX = null
swipeStartY = null
}
}

window.addEventListener('touchstart', e => {
swipeStart(e.changedTouches[ 0 ])
}, supportsPassive ? { passive: true } : false)

window.addEventListener('touchend', e => {
swipeEnd(e.changedTouches[ 0 ])
}, supportsPassive ? { passive: true } : false)

window.addEventListener('mousedown', e => swipeStart(e))

window.addEventListener('mouseup', e => swipeEnd(e))
}

export { publishClick, publishEscape, publishSwipe }
25 changes: 25 additions & 0 deletions src/support.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
let testedPassiveSupport = false
let passive = false

const testPassiveSupport = () => {
try {
const opts = Object.defineProperty({}, 'passive', {
get: () => {
passive = true
}
})

window.addEventListener('test', null, opts)
} catch (e) {}
}

const supportsPassive = () => {
if (!testedPassiveSupport) {
testPassiveSupport()
testedPassiveSupport = true
}

return passive
}

export { supportsPassive }

0 comments on commit c6b9d2e

Please sign in to comment.