Skip to content

Instantly share code, notes, and snippets.

@Raynos Raynos/0readme.md
Last active Dec 11, 2015

Embed
What would you like to do?

mouseenter / leave

Get the mouseenter / mouseleave events for an elem

Example

var fold = require("reducers/fold")
var Stream = require("stream-reduce/stream")
var mouseleave = require("mouseleave")
// var mouseenter = require("mouseenter")

var elem = document.getElementById("my-element")

var leaves = mouseleave(elem)

// consume leaves an handle each mouseleave event
fold(leaves, function (event) {
  
})

// or convert to stream and handle as stream
var leaves = Stream(mouseleave(elem))

leaves.on("data", function (event) {
  
})
var DOCUMENT_POSITION_CONTAINED_BY = 16
module.exports = contains
function contains(container, elem) {
if (container.contains) {
return container.contains(elem)
}
var comparison = container.compareDocumentPosition(elem)
return comparison === 0 || comparison & DOCUMENT_POSITION_CONTAINED_BY
}
var events = require("dom-reduce/event")
var filter = require("reducers/filter")
var map = require("reducers/map")
var contains = require("./contains")
module.exports = mouseenter
function mouseenter(elem) {
var overs = events(elem, "mouseover")
var correctElems = filter(overs, function isCorrectElement(event) {
var target = event.target
var elem = event.currentTarget
return target === elem || contains(elem, target)
})
var correctMoves = filter(correctElems, function notMovedFromChild(event) {
var related = event.relatedTarget || event.fromElement
return !contains(event.currentTarget, related)
})
var withType = map(correctMoves, function mouseenter(event) {
event.type = "mouseenter"
return event
})
return withType
}
var events = require("dom-reduce/event")
var synthesize = require("synthesize")
var filter = require("reducers/filter")
var map = require("reducers/map")
var contains = require("./contains")
module.exports = (synthesize)
(events, "mouseout")
(filter, function isCorrectElement(event) {
var target = event.target
var elem = event.currentTarget
return target === elem || contains(elem, target)
})
(filter, function notLeftFromChild(event) {
var related = event.relatedTarget || event.toElement
return !contains(event.currentTarget, related)
})
(map, function mouseleave(event) {
event.type = "mouseleave"
return event
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.