Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stack timeout listener #23

Merged
merged 4 commits into from
Jul 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 38 additions & 29 deletions src/event.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,43 @@
import { isTouch } from './utils'

export const customEvents = popup => {
const onMouseLeave = e => {

let eventListenerStack = [],
timeoutStack = []
const addTimeout = ( func, timeout ) => {
const id = setTimeout( func, timeout )
timeoutStack.push( id )
return id
},
clearAllTimeout = () => {
timeoutStack.forEach( timeoutId => {
clearTimeout( timeoutId )
} )
timeoutStack = []
},
addEventListener = ( target, type, listener, options = undefined ) => {
target.addEventListener( type, listener, options )
eventListenerStack.push( [ target, type, listener, options ] )
},
clearAllEventListener = () => {
eventListenerStack.forEach( eventListener => {
const [ target, type, listener, options ] = eventListener
target.removeEventListener( type, listener, options )
} )
eventListenerStack = []
},
onMouseLeave = e => {
const toElement = e.toElement || e.relatedTarget || e.target,
previewElement = popup.element.currentTargetElement

if ( toElement !== previewElement && !popup.element.contains( toElement ) ) {
let timeoutId
const persistPopup = () => {
popup.element.removeEventListener( 'mouseenter', persistPopup )
popup.timeoutId = null
clearTimeout( timeoutId )
},
clearTimeout( timeoutId )
}

hidePopup = () => {
popup.element.removeEventListener( 'mouseenter', persistPopup )
popup.timeoutId = null
popup.hide()
}

timeoutId = setTimeout( hidePopup, 300 )
popup.element.addEventListener( 'mouseenter', persistPopup )
popup.timeoutId = timeoutId
timeoutId = addTimeout( popup.hide, 300 )
addEventListener( popup.element, 'mouseenter', persistPopup )
}
},

Expand All @@ -36,16 +52,9 @@ export const customEvents = popup => {
}
},

onHide = element => {
element.component.closeBtn.removeEventListener( 'click', popup.hide )
element.component.readMore.removeEventListener( 'click', onExpand )

if ( isTouch ) {
document.removeEventListener( 'touchstart', onTouchStart, true )
} else {
element.removeEventListener( 'mouseleave', onMouseLeave )
element.currentTargetElement.removeEventListener( 'mouseleave', onMouseLeave )
}
onHide = () => {
clearAllEventListener()
clearAllTimeout()
},

onShow = element => {
Expand All @@ -61,14 +70,14 @@ export const customEvents = popup => {
onExpand( element )
}

element.component.closeBtn.addEventListener( 'click', popup.hide )
element.component.readMore.addEventListener( 'click', onExpand )
addEventListener( element.component.closeBtn, 'click', popup.hide )
addEventListener( element.component.readMore, 'click', onExpand )

if ( isTouch ) {
document.addEventListener( 'touchstart', onTouchStart, true )
addEventListener( document, 'touchstart', onTouchStart, true )
} else {
element.addEventListener( 'mouseleave', onMouseLeave )
element.currentTargetElement.addEventListener( 'mouseleave', onMouseLeave )
addEventListener( element, 'mouseleave', onMouseLeave )
addEventListener( element.currentTargetElement, 'mouseleave', onMouseLeave )
}
}

Expand Down
5 changes: 1 addition & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ function init( {
createPopup( popupContainer ),
events = customEvents( popup ),
showPopup = ( { target } ) => {
if ( popup.timeoutId ) {
clearTimeout( popup.timeoutId )
popup.timeoutId = null
}
popup.hide()

const title = target.getAttribute( 'data-wp-title' ) || target.textContent,
lang = target.getAttribute( 'data-wp-lang' ) || globalLang
Expand Down