@accessible/use-keycode
Advanced tools
Comparing version 4.0.0 to 4.0.1
@@ -1,9 +0,9 @@ | ||
import {MutableRefObject} from 'react' | ||
import * as React from 'react' | ||
export declare const useKeycode: ( | ||
which: number, | ||
callback: (event?: KeyboardEvent | undefined) => any | ||
) => MutableRefObject<any> | ||
onKeyDown: (event?: KeyboardEvent | undefined) => any | ||
) => React.MutableRefObject<any> | ||
export declare const useKeycodes: ( | ||
handlers: Record<number, (event: KeyboardEvent) => any> | ||
) => MutableRefObject<any> | ||
listeners: Record<number, (event: KeyboardEvent) => any> | ||
) => React.MutableRefObject<any> | ||
export default useKeycode |
@@ -6,7 +6,5 @@ 'use strict' | ||
var _react = _interopRequireDefault(require('react')) | ||
var React = _interopRequireWildcard(require('react')) | ||
var _passiveLayoutEffect = _interopRequireDefault( | ||
require('@react-hook/passive-layout-effect') | ||
) | ||
var _event = _interopRequireDefault(require('@react-hook/event')) | ||
@@ -17,7 +15,47 @@ function _interopRequireDefault(obj) { | ||
const {useRef} = _react.default | ||
function _getRequireWildcardCache() { | ||
if (typeof WeakMap !== 'function') return null | ||
var cache = new WeakMap() | ||
_getRequireWildcardCache = function() { | ||
return cache | ||
} | ||
return cache | ||
} | ||
const useKeycode = (which, callback) => | ||
function _interopRequireWildcard(obj) { | ||
if (obj && obj.__esModule) { | ||
return obj | ||
} | ||
if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) { | ||
return {default: obj} | ||
} | ||
var cache = _getRequireWildcardCache() | ||
if (cache && cache.has(obj)) { | ||
return cache.get(obj) | ||
} | ||
var newObj = {} | ||
var hasPropertyDescriptor = | ||
Object.defineProperty && Object.getOwnPropertyDescriptor | ||
for (var key in obj) { | ||
if (Object.prototype.hasOwnProperty.call(obj, key)) { | ||
var desc = hasPropertyDescriptor | ||
? Object.getOwnPropertyDescriptor(obj, key) | ||
: null | ||
if (desc && (desc.get || desc.set)) { | ||
Object.defineProperty(newObj, key, desc) | ||
} else { | ||
newObj[key] = obj[key] | ||
} | ||
} | ||
} | ||
newObj.default = obj | ||
if (cache) { | ||
cache.set(obj, newObj) | ||
} | ||
return newObj | ||
} | ||
const useKeycode = (which, onKeyDown) => | ||
useKeycodes({ | ||
[which]: callback, | ||
[which]: onKeyDown, | ||
}) | ||
@@ -27,28 +65,12 @@ | ||
const useKeycodes = handlers => { | ||
const ref = useRef(null) | ||
const storedHandlers = useRef(handlers) | ||
storedHandlers.current = handlers | ||
const useKeycodes = listeners => { | ||
const ref = React.useRef(null) | ||
;(0, _event.default)(ref, 'keydown', event => { | ||
var _listeners$event$whic | ||
function _ref(event) { | ||
var _storedHandlers$curre, _storedHandlers$curre2 | ||
;(_storedHandlers$curre = (_storedHandlers$curre2 = storedHandlers.current)[ | ||
event.which | ||
]) === null || _storedHandlers$curre === void 0 | ||
;(_listeners$event$whic = listeners[event.which]) === null || | ||
_listeners$event$whic === void 0 | ||
? void 0 | ||
: _storedHandlers$curre.call(_storedHandlers$curre2, event) | ||
} | ||
;(0, _passiveLayoutEffect.default)(() => { | ||
const current = ref.current | ||
if (current) { | ||
const maybeCallback = _ref | ||
current.addEventListener('keydown', maybeCallback) | ||
return () => { | ||
current.removeEventListener('keydown', maybeCallback) | ||
} | ||
} | ||
}, [ref]) | ||
: _listeners$event$whic.call(listeners, event) | ||
}) | ||
return ref | ||
@@ -55,0 +77,0 @@ } |
@@ -1,9 +0,9 @@ | ||
import {MutableRefObject} from 'react' | ||
import * as React from 'react' | ||
export declare const useKeycode: ( | ||
which: number, | ||
callback: (event?: KeyboardEvent | undefined) => any | ||
) => MutableRefObject<any> | ||
onKeyDown: (event?: KeyboardEvent | undefined) => any | ||
) => React.MutableRefObject<any> | ||
export declare const useKeycodes: ( | ||
handlers: Record<number, (event: KeyboardEvent) => any> | ||
) => MutableRefObject<any> | ||
listeners: Record<number, (event: KeyboardEvent) => any> | ||
) => React.MutableRefObject<any> | ||
export default useKeycode |
@@ -1,36 +0,19 @@ | ||
import React from 'react' | ||
const {useRef} = React | ||
import useLayoutEffect from '@react-hook/passive-layout-effect' | ||
export const useKeycode = (which, callback) => | ||
import * as React from 'react' | ||
import useEvent from '@react-hook/event' | ||
export const useKeycode = (which, onKeyDown) => | ||
useKeycodes({ | ||
[which]: callback, | ||
[which]: onKeyDown, | ||
}) | ||
export const useKeycodes = handlers => { | ||
const ref = useRef(null) | ||
const storedHandlers = useRef(handlers) | ||
storedHandlers.current = handlers | ||
export const useKeycodes = listeners => { | ||
const ref = React.useRef(null) | ||
useEvent(ref, 'keydown', event => { | ||
var _listeners$event$whic | ||
function _ref(event) { | ||
var _storedHandlers$curre, _storedHandlers$curre2 | ||
;(_storedHandlers$curre = (_storedHandlers$curre2 = storedHandlers.current)[ | ||
event.which | ||
]) === null || _storedHandlers$curre === void 0 | ||
;(_listeners$event$whic = listeners[event.which]) === null || | ||
_listeners$event$whic === void 0 | ||
? void 0 | ||
: _storedHandlers$curre.call(_storedHandlers$curre2, event) | ||
} | ||
useLayoutEffect(() => { | ||
const current = ref.current | ||
if (current) { | ||
const maybeCallback = _ref | ||
current.addEventListener('keydown', maybeCallback) | ||
return () => { | ||
current.removeEventListener('keydown', maybeCallback) | ||
} | ||
} | ||
}, [ref]) | ||
: _listeners$event$whic.call(listeners, event) | ||
}) | ||
return ref | ||
} | ||
export default useKeycode |
{ | ||
"name": "@accessible/use-keycode", | ||
"version": "4.0.0", | ||
"version": "4.0.1", | ||
"homepage": "https://github.com/accessible-ui/use-keycode#readme", | ||
@@ -79,3 +79,3 @@ "repository": "github:accessible-ui/use-keycode", | ||
"dependencies": { | ||
"@react-hook/passive-layout-effect": "^1.0.2" | ||
"@react-hook/event": "^1.0.3" | ||
}, | ||
@@ -82,0 +82,0 @@ "peerDependencies": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10420
103
+ Added@react-hook/event@^1.0.3
+ Added@react-hook/event@1.2.6(transitive)
- Removed@react-hook/passive-layout-effect@1.2.1(transitive)