Add itch.io scrolling protection

This commit is contained in:
Gwilym Inzani 2023-07-05 12:07:47 +01:00 committed by Corwin
parent 91dfb63fbd
commit f944f35b61
No known key found for this signature in database
2 changed files with 20 additions and 0 deletions

View file

@ -4,6 +4,7 @@ import { BindingsControl, DefaultBindingsSet, Bindings } from "./bindings";
import { styled } from "styled-components"; import { styled } from "styled-components";
import { useOnKeyUp } from "./useOnKeyUp.hook"; import { useOnKeyUp } from "./useOnKeyUp.hook";
import { useLocalStorage } from "./useLocalStorage.hook"; import { useLocalStorage } from "./useLocalStorage.hook";
import { useAvoidItchIoScrolling } from "./useAvoidItchIoScrolling";
const BindingsDialog = styled.dialog` const BindingsDialog = styled.dialog`
border-radius: 5px; border-radius: 5px;
@ -47,6 +48,8 @@ function App() {
setShowBindings(!showBindings); setShowBindings(!showBindings);
}); });
useAvoidItchIoScrolling();
return ( return (
<AppContainer> <AppContainer>
{showBindings && ( {showBindings && (

View file

@ -0,0 +1,17 @@
import { useEffect } from "react";
export const useAvoidItchIoScrolling = () => {
useEffect(() => {
const eventHandler = (e: KeyboardEvent) => {
if ([32, 37, 38, 39, 40].indexOf(e.keyCode) > -1) {
e.preventDefault();
}
};
window.addEventListener("keydown", eventHandler, false);
return () => {
window.removeEventListener("keydown", eventHandler, false);
};
}, []);
};