react-native-web-hover
Advanced tools
Comparing version
@@ -94,9 +94,5 @@ "use strict"; | ||
function createHoverListener() { | ||
//@ts-ignore | ||
// eslint-disable-next-line no-undef | ||
let hasMouse = isServer ? false : matchMedia('(pointer:fine)').matches; // would be nice if this could be kind of map | ||
// with the reference as key for fast lookup | ||
let pressableRefs = []; | ||
let previousHoveredRef; | ||
let hasMouse = isServer ? false : window.matchMedia('(pointer:fine)').matches; | ||
let refs = new Map(); | ||
let previousHoverFunc; | ||
let mousePosition = { | ||
@@ -108,15 +104,17 @@ x: 0, | ||
function hover(target) { | ||
let hoveredRef = pressableRefs.find(r => { | ||
return containsEvent(r.ref, target); | ||
}); | ||
// hover is targeted directly | ||
let hoverFunc = refs.get(target); // let's try to see if any of the children of the hover event are hovered | ||
if (previousHoveredRef) { | ||
previousHoveredRef.setHovered(false); | ||
if (!hoverFunc) { | ||
for (let r of refs.keys()) { | ||
if (containsEvent(r, target)) { | ||
hoverFunc = refs.get(r); | ||
} | ||
} | ||
} | ||
if (hoveredRef) { | ||
hoveredRef.setHovered(true); | ||
} | ||
previousHoverFunc && previousHoverFunc(false); | ||
hoverFunc && hoverFunc(true); // cache the previous hover so we can un-hover this later on | ||
previousHoveredRef = hoveredRef; | ||
previousHoverFunc = hoverFunc; | ||
} | ||
@@ -129,5 +127,3 @@ | ||
function unhover() { | ||
if (previousHoveredRef) { | ||
previousHoveredRef.setHovered(false); | ||
} | ||
previousHoverFunc && previousHoverFunc(false); | ||
} | ||
@@ -144,13 +140,7 @@ | ||
if (hasMouse) { | ||
//@ts-ignore | ||
document.onmouseover = hoverEvent; //@ts-ignore | ||
document.onmousemove = captureMousePosition; //@ts-ignore | ||
document.ontouchstart = unhover; //@ts-ignore | ||
document.ontouchend = unhover; //@ts-ignore | ||
document.ontouchcancel = unhover; //@ts-ignore | ||
document.onmouseover = hoverEvent; | ||
document.onmousemove = captureMousePosition; | ||
document.ontouchstart = unhover; | ||
document.ontouchend = unhover; | ||
document.ontouchcancel = unhover; | ||
document.ontouchmove = unhover; | ||
@@ -160,10 +150,7 @@ } | ||
function add(ref, setHovered) { | ||
pressableRefs.push({ | ||
ref, | ||
setHovered | ||
}); | ||
refs.set(ref, setHovered); | ||
} | ||
function remove(ref) { | ||
pressableRefs = pressableRefs.filter(r => r.ref !== ref); | ||
refs.delete(ref); | ||
} | ||
@@ -209,6 +196,4 @@ | ||
hover | ||
} = hoverListener; //@ts-ignore | ||
let element = document.elementFromPoint(mousePosition.x, mousePosition.y); | ||
hover(element); | ||
} = hoverListener; | ||
hover(document.elementFromPoint(mousePosition.x, mousePosition.y)); | ||
}; | ||
@@ -219,3 +204,3 @@ | ||
onScroll: onScrollInner, | ||
scrollEventThrottle: scrollEventThrottle || 60 | ||
scrollEventThrottle: scrollEventThrottle || 25 | ||
}, rest)); | ||
@@ -222,0 +207,0 @@ }; |
@@ -80,9 +80,5 @@ function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } | ||
function createHoverListener() { | ||
//@ts-ignore | ||
// eslint-disable-next-line no-undef | ||
let hasMouse = isServer ? false : matchMedia('(pointer:fine)').matches; // would be nice if this could be kind of map | ||
// with the reference as key for fast lookup | ||
let pressableRefs = []; | ||
let previousHoveredRef; | ||
let hasMouse = isServer ? false : window.matchMedia('(pointer:fine)').matches; | ||
let refs = new Map(); | ||
let previousHoverFunc; | ||
let mousePosition = { | ||
@@ -94,15 +90,17 @@ x: 0, | ||
function hover(target) { | ||
let hoveredRef = pressableRefs.find(r => { | ||
return containsEvent(r.ref, target); | ||
}); | ||
// hover is targeted directly | ||
let hoverFunc = refs.get(target); // let's try to see if any of the children of the hover event are hovered | ||
if (previousHoveredRef) { | ||
previousHoveredRef.setHovered(false); | ||
if (!hoverFunc) { | ||
for (let r of refs.keys()) { | ||
if (containsEvent(r, target)) { | ||
hoverFunc = refs.get(r); | ||
} | ||
} | ||
} | ||
if (hoveredRef) { | ||
hoveredRef.setHovered(true); | ||
} | ||
previousHoverFunc && previousHoverFunc(false); | ||
hoverFunc && hoverFunc(true); // cache the previous hover so we can un-hover this later on | ||
previousHoveredRef = hoveredRef; | ||
previousHoverFunc = hoverFunc; | ||
} | ||
@@ -115,5 +113,3 @@ | ||
function unhover() { | ||
if (previousHoveredRef) { | ||
previousHoveredRef.setHovered(false); | ||
} | ||
previousHoverFunc && previousHoverFunc(false); | ||
} | ||
@@ -130,13 +126,7 @@ | ||
if (hasMouse) { | ||
//@ts-ignore | ||
document.onmouseover = hoverEvent; //@ts-ignore | ||
document.onmousemove = captureMousePosition; //@ts-ignore | ||
document.ontouchstart = unhover; //@ts-ignore | ||
document.ontouchend = unhover; //@ts-ignore | ||
document.ontouchcancel = unhover; //@ts-ignore | ||
document.onmouseover = hoverEvent; | ||
document.onmousemove = captureMousePosition; | ||
document.ontouchstart = unhover; | ||
document.ontouchend = unhover; | ||
document.ontouchcancel = unhover; | ||
document.ontouchmove = unhover; | ||
@@ -146,10 +136,7 @@ } | ||
function add(ref, setHovered) { | ||
pressableRefs.push({ | ||
ref, | ||
setHovered | ||
}); | ||
refs.set(ref, setHovered); | ||
} | ||
function remove(ref) { | ||
pressableRefs = pressableRefs.filter(r => r.ref !== ref); | ||
refs.delete(ref); | ||
} | ||
@@ -195,6 +182,4 @@ | ||
hover | ||
} = hoverListener; //@ts-ignore | ||
let element = document.elementFromPoint(mousePosition.x, mousePosition.y); | ||
hover(element); | ||
} = hoverListener; | ||
hover(document.elementFromPoint(mousePosition.x, mousePosition.y)); | ||
}; | ||
@@ -205,3 +190,3 @@ | ||
onScroll: onScrollInner, | ||
scrollEventThrottle: scrollEventThrottle || 60 | ||
scrollEventThrottle: scrollEventThrottle || 25 | ||
}, rest)); | ||
@@ -208,0 +193,0 @@ }; |
{ | ||
"name": "react-native-web-hover", | ||
"version": "0.2.5", | ||
"version": "0.2.6", | ||
"description": "Some enhanced elements for react-native-web to support hover on the web", | ||
@@ -5,0 +5,0 @@ "main": "lib/commonjs/index", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
57262
-1.61%700
-3.58%