Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: keyboard shortcuts bug #59

Merged
merged 1 commit into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/components/Sidebar/internal/SidebarInner/SidebarInner.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { createRef, useCallback } from 'react';
import React, { useCallback, useRef } from 'react';
import { useKey } from '../../../../hooks/useKey';
import { useMergeRefs } from '../../../../hooks/useMergeRefs';
import { useEntities, useEntityFilter } from '../../../../states/entity';
import {
useSidebarEntities,
Expand Down Expand Up @@ -32,7 +33,7 @@ export const SidebarInner = ({ scrollerRef, inputRef, listRef }: Props) => {
const { toggle } = useSidebarMutators();
const sidebarEntity = useSidebarEntities();
const entity = useEntities();
const innerRef = scrollerRef || createRef();
const innerRef = useRef<HTMLDivElement>(null);

const handleChange = useCallback(
(e: React.ChangeEvent<HTMLInputElement>) => {
Expand All @@ -53,9 +54,11 @@ export const SidebarInner = ({ scrollerRef, inputRef, listRef }: Props) => {
tryNextFocus(innerRef.current);
});

const ref = useMergeRefs(innerRef, scrollerRef);

return (
<>
<div ref={innerRef} className={styles.inner} id="sidebar">
<div ref={ref} className={styles.inner} id="sidebar">
<SearchBox
inputRef={inputRef}
placeholder="Filter by file name"
Expand Down
21 changes: 19 additions & 2 deletions src/hooks/useKey.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { useCallback, useEffect, useRef } from 'react';
import { tinykeys } from 'tinykeys';

const ignore = new Set(['input', 'select', 'textarea']);

export type UseKeyCallback = (e: KeyboardEvent) => void;

export const useKey = (
target: React.RefObject<HTMLElement> | null,
target:
| React.RefObject<HTMLElement>
| React.MutableRefObject<HTMLElement>
| null,
keys: string[],
callback: UseKeyCallback,
) => {
Expand All @@ -20,7 +25,19 @@ export const useKey = (
return tinykeys(
target?.current ?? window,
Object.fromEntries(
keys.map((key) => [key, (e) => callbackRef.current(e)]),
keys.map((key) => [
key,
(e) => {
const el = e.target;
if (
el instanceof HTMLElement &&
ignore.has(el.tagName.toLowerCase())
) {
return;
}
callbackRef.current(e);
},
]),
),
);
}, [target, keys]);
Expand Down
37 changes: 37 additions & 0 deletions src/hooks/useMergeRefs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { useMemo } from 'react';

type ReactRef<T> = React.Ref<T> | React.MutableRefObject<T>;

const assignRef = <T = any>(ref: ReactRef<T> | undefined, value: T) => {
if (ref == null) {
return;
}

if (typeof ref === 'function') {
ref(value);
return;
}

try {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
ref.current = value;
} catch (error) {
throw new Error(`Cannot assign value '${value}' to ref '${ref}'`);
}
};

export const useMergeRefs = <T>(...refs: (ReactRef<T> | undefined)[]) => {
return useMemo(() => {
if (refs.every((ref) => ref == null)) {
return null;
}
return (node: T) => {
refs.forEach((ref) => {
if (ref) {
assignRef(ref, node);
}
});
};
}, refs); // eslint-disable-line react-hooks/exhaustive-deps
};