Skip to content

Database diagrams editor that allows you to visualize and design your DB with a single query.

fix(open-diagram): add arrow keys navigation in open diagram dialog (#537)

Jonathan Fishner14f11c27a7ad
diff --git a/src/dialogs/open-diagram-dialog/open-diagram-dialog.tsx b/src/dialogs/open-diagram-dialog/open-diagram-dialog.tsx
index 7cbfc9e1..0d0e9b4c 100644
--- a/src/dialogs/open-diagram-dialog/open-diagram-dialog.tsx
+++ b/src/dialogs/open-diagram-dialog/open-diagram-dialog.tsx
@@ -22,10 +22,11 @@ import { useConfig } from '@/hooks/use-config';
import { useDialog } from '@/hooks/use-dialog';
import { useStorage } from '@/hooks/use-storage';
import type { Diagram } from '@/lib/domain/diagram';
-import React, { useEffect, useState } from 'react';
+import React, { useCallback, useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { useNavigate } from 'react-router-dom';
import type { BaseDialogProps } from '../common/base-dialog-props';
+import { useDebounce } from '@/hooks/use-debounce';
export interface OpenDiagramDialogProps extends BaseDialogProps {}
@@ -58,12 +59,65 @@ export const OpenDiagramDialog: React.FC<OpenDiagramDialogProps> = ({
fetchDiagrams();
}, [listDiagrams, setDiagrams, dialog.open]);
- const openDiagram = (diagramId: string) => {
- if (diagramId) {
- updateConfig({ defaultDiagramId: diagramId });
- navigate(`/diagrams/${diagramId}`);
- }
- };
+ const openDiagram = useCallback(
+ (diagramId: string) => {
+ if (diagramId) {
+ updateConfig({ defaultDiagramId: diagramId });
+ navigate(`/diagrams/${diagramId}`);
+ }
+ },
+ [updateConfig, navigate]
+ );
+
+ const handleRowKeyDown = useCallback(
+ (e: React.KeyboardEvent<HTMLTableRowElement>) => {
+ const element = e.target as HTMLElement;
+ const diagramId = element.getAttribute('data-diagram-id');
+ const selectionIndexAttr = element.getAttribute(
+ 'data-selection-index'
+ );
+
+ if (!diagramId || !selectionIndexAttr) return;
+
+ const selectionIndex = parseInt(selectionIndexAttr, 10);
+
+ switch (e.key) {
+ case 'Enter':
+ case ' ':
+ e.preventDefault();
+ openDiagram(diagramId);
+ closeOpenDiagramDialog();
+ break;
+ case 'ArrowDown': {
+ e.preventDefault();
+
+ (
+ document.querySelector(
+ `[data-selection-index="${selectionIndex + 1}"]`
+ ) as HTMLElement
+ )?.focus();
+ break;
+ }
+ case 'ArrowUp': {
+ e.preventDefault();
+
+ (
+ document.querySelector(
+ `[data-selection-index="${selectionIndex - 1}"]`
+ ) as HTMLElement
+ )?.focus();
+ break;
+ }
+ }
+ },
+ [openDiagram, closeOpenDiagramDialog]
+ );
+
+ const onFocusHandler = useDebounce(
+ (diagramId: string) => setSelectedDiagramId(diagramId),
+ 50
+ );
+
return (
<Dialog
{...dialog}
@@ -112,10 +166,17 @@ export const OpenDiagramDialog: React.FC<OpenDiagramDialogProps> = ({
</TableRow>
</TableHeader>
<TableBody>
- {diagrams.map((diagram) => (
+ {diagrams.map((diagram, index) => (
<TableRow
key={diagram.id}
data-state={`${selectedDiagramId === diagram.id ? 'selected' : ''}`}
+ data-diagram-id={diagram.id}
+ data-selection-index={index}
+ tabIndex={0}
+ onFocus={() =>
+ onFocusHandler(diagram.id)
+ }
+ className="focus:bg-accent focus:outline-none"
onClick={(e) => {
switch (e.detail) {
case 1:
@@ -133,6 +194,7 @@ export const OpenDiagramDialog: React.FC<OpenDiagramDialogProps> = ({
);
}
}}
+ onKeyDown={handleRowKeyDown}
>
<TableCell className="table-cell">
<div className="flex justify-center">
diff --git a/src/hooks/use-debounce.ts b/src/hooks/use-debounce.ts
new file mode 100644
index 00000000..539d8ecc
--- /dev/null
+++ b/src/hooks/use-debounce.ts
@@ -0,0 +1,21 @@
+import { useCallback, useRef } from 'react';
+
+// eslint-disable-next-line @typescript-eslint/no-explicit-any
+type AnyFunction = (...args: any[]) => any;
+
+export const useDebounce = <T extends AnyFunction>(
+ func: T,
+ delay: number
+): ((...args: Parameters<T>) => void) => {
+ const inDebounce = useRef<NodeJS.Timeout>();
+
+ const debounce = useCallback(
+ (...args: Parameters<T>) => {
+ clearTimeout(inDebounce.current);
+ inDebounce.current = setTimeout(() => func(...args), delay);
+ },
+ [func, delay]
+ );
+
+ return debounce;
+};