From 66755de530939e7b7820b14adcc841dc2337ec42 Mon Sep 17 00:00:00 2001 From: Julian Wielga Date: Wed, 14 Aug 2024 12:03:15 +0200 Subject: [PATCH] fix: support keyboard in header buttons --- src/components/window/header/HeaderButton.tsx | 32 ++++++++++++++++--- .../window/header/HeaderButtonClose.tsx | 2 +- .../window/header/HeaderButtonZoom.tsx | 8 ++--- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/components/window/header/HeaderButton.tsx b/src/components/window/header/HeaderButton.tsx index c6267fd8..c9aac6b5 100644 --- a/src/components/window/header/HeaderButton.tsx +++ b/src/components/window/header/HeaderButton.tsx @@ -1,9 +1,18 @@ import { css, cx } from "@emotion/css"; import { useTheme } from "@emotion/react"; -import React, { ButtonHTMLAttributes, DetailedHTMLProps, forwardRef } from "react"; +import React, { + ButtonHTMLAttributes, + DetailedHTMLProps, + forwardRef, + KeyboardEvent, + KeyboardEventHandler, + PointerEventHandler, +} from "react"; import { buttonReset } from "../footer/ButtonReset"; -export type HeaderButtonProps = DetailedHTMLProps, HTMLButtonElement>; +export type HeaderButtonProps = DetailedHTMLProps, HTMLButtonElement> & { + action?: PointerEventHandler & KeyboardEventHandler; +}; const useStyles = () => { const { colors } = useTheme(); @@ -25,10 +34,25 @@ const useStyles = () => { }); }; +function filterByKeys(action: KeyboardEventHandler, keys: KeyboardEvent["key"][]): KeyboardEventHandler { + return (e: KeyboardEvent) => { + if (!keys.includes(e.key)) return; + return action(e); + }; +} + export const HeaderButton = forwardRef(function HeaderButton( - { className, ...props }: HeaderButtonProps, + { className, action, ...props }: HeaderButtonProps, ref: React.ForwardedRef, ): JSX.Element { const headerButtonTheme = useStyles(); - return