Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

refactor(desktop): generic composeEventHandlers for keyboard inputs

Richie McIlroy0e0d11ebf6a3
diff --git a/apps/desktop/src/routes/editor/TextInput.tsx b/apps/desktop/src/routes/editor/TextInput.tsx
index 8fe37b95c..84d6a18a7 100644
--- a/apps/desktop/src/routes/editor/TextInput.tsx
+++ b/apps/desktop/src/routes/editor/TextInput.tsx
@@ -1,13 +1,11 @@
import type { ComponentProps } from "solid-js";
import { composeEventHandlers } from "~/utils/composeEventHandlers";
-// It's important to use this instead of plain text inputs as we use global key listeners
-// for keybinds
export function TextInput(props: ComponentProps<"input">) {
return (
<input
{...props}
- onKeyDown={composeEventHandlers<HTMLInputElement>([
+ onKeyDown={composeEventHandlers<HTMLInputElement, KeyboardEvent>([
props.onKeyDown,
(e) => {
e.stopPropagation();
diff --git a/apps/desktop/src/routes/screenshot-editor/TextInput.tsx b/apps/desktop/src/routes/screenshot-editor/TextInput.tsx
index 8fe37b95c..84d6a18a7 100644
--- a/apps/desktop/src/routes/screenshot-editor/TextInput.tsx
+++ b/apps/desktop/src/routes/screenshot-editor/TextInput.tsx
@@ -1,13 +1,11 @@
import type { ComponentProps } from "solid-js";
import { composeEventHandlers } from "~/utils/composeEventHandlers";
-// It's important to use this instead of plain text inputs as we use global key listeners
-// for keybinds
export function TextInput(props: ComponentProps<"input">) {
return (
<input
{...props}
- onKeyDown={composeEventHandlers<HTMLInputElement>([
+ onKeyDown={composeEventHandlers<HTMLInputElement, KeyboardEvent>([
props.onKeyDown,
(e) => {
e.stopPropagation();
diff --git a/apps/desktop/src/utils/composeEventHandlers.ts b/apps/desktop/src/utils/composeEventHandlers.ts
index 9764915b7..6365b536c 100644
--- a/apps/desktop/src/utils/composeEventHandlers.ts
+++ b/apps/desktop/src/utils/composeEventHandlers.ts
@@ -1,6 +1,5 @@
import type { JSX } from "solid-js";
-/** Call a JSX.EventHandlerUnion with the event. */
export function callHandler<T, E extends Event>(
event: E & { currentTarget: T; target: Element },
handler: JSX.EventHandlerUnion<T, E> | undefined,
@@ -16,11 +15,10 @@ export function callHandler<T, E extends Event>(
return event?.defaultPrevented;
}
-/** Create a new event handler which calls all given handlers in the order they were chained with the same event. */
-export function composeEventHandlers<T>(
- handlers: Array<JSX.EventHandlerUnion<T, Event> | undefined>,
+export function composeEventHandlers<T, E extends Event = Event>(
+ handlers: Array<JSX.EventHandlerUnion<T, E> | undefined>,
) {
- return (event: Event & { currentTarget: T; target: Element }) => {
+ return (event: E & { currentTarget: T; target: Element }) => {
for (const handler of handlers) {
callHandler(event, handler);
}