Skip to content

Commit

Permalink
fix max pointer size
Browse files Browse the repository at this point in the history
  • Loading branch information
idomusha committed Mar 3, 2020
1 parent aa246af commit 6761416
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 20 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
### Removed
-->

## [2.0.0-beta.3] - 2020-03-03

### Fixed

- Max pointer size

## [2.0.0-beta.2] - 2020-02-26

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "use-interaction",
"version": "2.0.0-beta.2",
"version": "2.0.0-beta.3",
"private": false,
"description": "React hook for getting and following user interaction type",
"keywords": [
Expand Down
33 changes: 19 additions & 14 deletions src/useInteraction.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { useState, useEffect, useCallback } from 'react'
import PropTypes from 'prop-types'
import { round } from 'lodash'
import ulog from 'ulog'
const log = ulog('use-interaction')

const log = ulog('use-interaction')
let setMaxPointerSize = null
const getKey = event => (event.keyCode ? event.keyCode : event.which)
let handleInteractionPointer = null

const useInteraction = ({ initial = null } = {}) => {
const initialPointerType =
Expand Down Expand Up @@ -115,18 +115,19 @@ const useInteraction = ({ initial = null } = {}) => {
[keys, inputs, handleInteractionChange]
)

handleInteractionPointer = useCallback(
event => {
log.info(event, event.type)
setMaxPointerSize = event => {
log.info(event, event.type, event.pointerType)

const nextAccuracy = round(event.height, 1)
const nextAccuracy = round(event.height, 1)

if (nextAccuracy > pointerAccuracy) {
setPointerAccuracy(nextAccuracy)
}
},
[pointerAccuracy]
)
if (nextAccuracy > pointerAccuracy) {
setPointerAccuracy(nextAccuracy)
}
}

const handleInteractionPointer = useCallback(setMaxPointerSize, [
pointerAccuracy,
])

useEffect(() => {
window.addEventListener('touchstart', handleInteractionTouch, false)
Expand All @@ -138,7 +139,11 @@ const useInteraction = ({ initial = null } = {}) => {
window.removeEventListener('keydown', handleInteractionKeyboard, false)
window.removeEventListener('pointerdown', handleInteractionPointer, false)
}
}, [handleInteractionTouch, handleInteractionKeyboard])
}, [
handleInteractionTouch,
handleInteractionKeyboard,
handleInteractionPointer,
])

useEffect(() => {
if (firedEvent.mousemove === true || firedEvent.wheel === true) {
Expand Down Expand Up @@ -173,4 +178,4 @@ useInteraction.propTypes = {
}

export default useInteraction
export { handleInteractionPointer, log }
export { setMaxPointerSize, log }
10 changes: 5 additions & 5 deletions src/useInteraction.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react'
import { renderHook, cleanup, act } from '@testing-library/react-hooks'
import { render, fireEvent } from '@testing-library/react'

import useInteraction, { handleInteractionPointer, log } from './useInteraction'
import useInteraction, { setMaxPointerSize, log } from './useInteraction'

if (process.env.NODE_ENV === 'test') {
log.level = log.NONE
Expand Down Expand Up @@ -184,7 +184,7 @@ test('should set accuracy of the pointer', () => {
pointerType: 'touch',
type: 'pointerdown',
}
handleInteractionPointer(PointerEvent)
setMaxPointerSize(PointerEvent)
fireEvent.touchStart(getByTestId('test-div'), { type: 'touchstart' })
fireEvent.mouseMove(document.body, { type: 'mousemove' })
})
Expand All @@ -203,7 +203,7 @@ test('should set accuracy of the pointer', () => {
pointerType: 'touch',
type: 'pointerdown',
}
handleInteractionPointer(PointerEvent)
setMaxPointerSize(PointerEvent)
fireEvent.touchStart(getByTestId('test-div'), { type: 'touchstart' })
fireEvent.mouseMove(document.body, { type: 'mousemove' })
})
Expand All @@ -222,7 +222,7 @@ test('should set accuracy of the pointer', () => {
pointerType: 'touch',
type: 'pointerdown',
}
handleInteractionPointer(PointerEvent)
setMaxPointerSize(PointerEvent)
fireEvent.touchStart(getByTestId('test-div'), { type: 'touchstart' })
})

Expand All @@ -248,7 +248,7 @@ test('should set accuracy of the pointer', () => {
pointerType: 'mouse',
type: 'pointerdown',
}
handleInteractionPointer(PointerEvent)
setMaxPointerSize(PointerEvent)
fireEvent.mouseDown(getByTestId('test-div'), { type: 'mousedown' })
})

Expand Down

0 comments on commit 6761416

Please sign in to comment.