Skip to content

Commit

Permalink
fix: detect browser support for passive events (#2325)
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianMitchL authored May 14, 2020
1 parent 9be5fa3 commit fddbc19
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/plugin-hooks/useResizeColumns.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
ensurePluginOrder,
} from '../publicUtils'

import { getFirstDefined } from '../utils'
import { getFirstDefined, passiveEventSupported } from '../utils'

// Default Column
defaultColumn.canResize = true
Expand Down Expand Up @@ -95,12 +95,19 @@ const defaultGetResizerProps = (props, { instance, header }) => {
const events = isTouchEvent
? handlersAndEvents.touch
: handlersAndEvents.mouse
document.addEventListener(events.moveEvent, events.moveHandler, {
passive: false,
})
document.addEventListener(events.upEvent, events.upHandler, {
passive: false,
})
const passiveIfSupported = passiveEventSupported()
? { passive: false }
: false
document.addEventListener(
events.moveEvent,
events.moveHandler,
passiveIfSupported
)
document.addEventListener(
events.upEvent,
events.upHandler,
passiveIfSupported
)

dispatch({
type: actions.columnStartResizing,
Expand Down
23 changes: 23 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,29 @@ export function unpreparedAccessWarning() {
)
}

let passiveSupported = null
export function passiveEventSupported() {
// memoize support to avoid adding multiple test events
if (typeof passiveSupported === 'boolean') return passiveSupported

let supported = false
try {
const options = {
get passive() {
supported = true
return false
},
}

window.addEventListener('test', null, options)
window.removeEventListener('test', null, options)
} catch (err) {
supported = false
}
passiveSupported = supported
return passiveSupported
}

//

const reOpenBracket = /\[/g
Expand Down

0 comments on commit fddbc19

Please sign in to comment.