Skip to content

Commit

Permalink
Actively prevent navigation on preview links
Browse files Browse the repository at this point in the history
  • Loading branch information
Sbisson committed Jul 18, 2024
1 parent 5c9fb0e commit ee172ed
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,25 +190,29 @@ function init( {
} )
}

const onPointerUp = ( pointerEvent ) => {
if ( pointerEvent.pointerType === 'touch' ) {
// This click event was triggered on a touch screen
showPopup( pointerEvent )
}
}

const onPointerEnter = ( pointerEvent ) => {
if ( pointerEvent.pointerType === 'mouse' ) {
// This hover event was triggered by a mouse
showPopup( pointerEvent )
}
showPopup( pointerEvent )
}

const registerPreviewEvents = ( node ) => {
node.addEventListener( 'pointerup', onPointerUp )
node.addEventListener( 'pointerenter', onPointerEnter )
}

const preventTapFromNavigatingLink = ( node ) => {
// The click event still receives a MouseEvent instead of the newer PointerEvent
// in some browsers so we have to grab the pointerType from the preceding pointerdown event.
let currentPointerType = null
node.addEventListener( 'pointerdown', ( e ) => {
currentPointerType = e.pointerType
} )
node.addEventListener( 'click', ( e ) => {
if ( currentPointerType === 'touch' ) {
e.preventDefault()
e.stopPropagation()
}
} )
}

forEachRoot( root, ( localRoot ) => {
Array.prototype.forEach.call(
localRoot.querySelectorAll( selector ),
Expand All @@ -233,6 +237,7 @@ function init( {
node.setAttribute( 'data-wp-title', matches.title )
node.setAttribute( 'data-wp-lang', matches.lang )
registerPreviewEvents( node )
preventTapFromNavigatingLink( node )

foundDetectLinks.push( {
text: node.textContent,
Expand Down

0 comments on commit ee172ed

Please sign in to comment.