@atomico/use-keyboard
Advanced tools
Comparing version 1.0.1 to 1.1.0
import { useCurrentValue } from "@atomico/use-current-value"; | ||
import { useRefValues } from "@atomico/use-ref-values"; | ||
import { addListener } from "@atomico/use-listener"; | ||
import { useRefEffect } from "atomico"; | ||
export function useKeyboard(ref, keys, callback) { | ||
const value = useCurrentValue(callback); | ||
useRefValues(([current]) => { | ||
useRefEffect(() => { | ||
const { current } = ref; | ||
if (!current) | ||
return; | ||
const history = new Set(); | ||
@@ -8,0 +11,0 @@ const check = () => { |
{ | ||
"name": "@atomico/use-keyboard", | ||
"description": "Series of utilities in hooks format to extend the operation of Atomico", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"type": "module", | ||
@@ -30,7 +30,6 @@ "license": "MIT", | ||
"@atomico/use-current-value": "*", | ||
"@atomico/use-listener": "*", | ||
"@atomico/use-ref-values": "*" | ||
"@atomico/use-listener": "*" | ||
}, | ||
"peerDependencies": { | ||
"atomico": "^1.75.1" | ||
"atomico": "^1.76.4" | ||
}, | ||
@@ -37,0 +36,0 @@ "types": "dist/index.d.ts", |
@@ -1,46 +0,40 @@ | ||
import { it, expect } from "vitest"; | ||
import { createHooks } from "atomico/test-hooks"; | ||
import { useKeyboard } from "../src/"; | ||
import { asyncEventListener } from "atomico/test-dom"; | ||
import { it } from "vitest"; | ||
import { Element } from "./element"; | ||
it("useKeyboard", () => { | ||
const ref = { current: document }; | ||
const hooks = createHooks(); | ||
const capture = []; | ||
hooks.load(() => { | ||
useKeyboard(ref, ["KeyA", "KeyQ"], (event) => { | ||
capture.push(event); | ||
}); | ||
}); | ||
it("useKeyboard", async () => { | ||
const element = new Element(); | ||
hooks.cleanEffects()()(); | ||
document.body.append(element); | ||
ref.current.dispatchEvent( | ||
new KeyboardEvent("keydown", { code: "KeyQ", key: "1" }), | ||
); | ||
await element.updated; | ||
ref.current.dispatchEvent( | ||
new KeyboardEvent("keydown", { code: "KeyA", key: "1" }), | ||
); | ||
const input = element.querySelector("input"); | ||
ref.current.dispatchEvent( | ||
new KeyboardEvent("keyup", { code: "KeyQ", key: "1" }), | ||
); | ||
setTimeout(() => { | ||
input.dispatchEvent( | ||
new KeyboardEvent("keydown", { code: "KeyQ", key: "1" }), | ||
); | ||
ref.current.dispatchEvent( | ||
new KeyboardEvent("keyup", { code: "KeyA", key: "1" }), | ||
); | ||
input.dispatchEvent( | ||
new KeyboardEvent("keydown", { code: "KeyA", key: "1" }), | ||
); | ||
ref.current.dispatchEvent( | ||
new KeyboardEvent("keydown", { code: "KeyA", key: "2" }), | ||
); | ||
ref.current.dispatchEvent( | ||
new KeyboardEvent("keydown", { code: "KeyQ", key: "2" }), | ||
); | ||
input.dispatchEvent( | ||
new KeyboardEvent("keyup", { code: "KeyQ", key: "1" }), | ||
); | ||
expect(capture.length).to.equal(1); | ||
input.dispatchEvent( | ||
new KeyboardEvent("keyup", { code: "KeyA", key: "1" }), | ||
); | ||
const [event] = capture; | ||
input.dispatchEvent( | ||
new KeyboardEvent("keydown", { code: "KeyA", key: "2" }), | ||
); | ||
input.dispatchEvent( | ||
new KeyboardEvent("keydown", { code: "KeyQ", key: "2" }), | ||
); | ||
}, 100); | ||
// The captured event defines key as 2, if it is 1 the hook ignores the convination | ||
expect(event.key).to.equal("2"); | ||
await asyncEventListener(element, "matchKeys"); | ||
}); |
Wildcard dependency
QualityPackage has a dependency with a floating version range. This can cause issues if the dependency publishes a new major version.
Found 1 instance in 1 package
4309
3
8
120
3
- Removed@atomico/use-ref-values@*