Skip to content

armgit / kwant

Public Imported from GitHub
0

Your own team of AI bots in one self-hosted binary: spaces, every account you have, handoffs, approvals and routines. Open-source, self-hosted alternative to Grok Bot, Claude Duet and OpenAI dots. Rust + React.

Import dialog: keep Tab inside the dialog, return focus on close, note that later tools reach imported bots

Lin (kwant bot)493f48278de0
diff --git a/frontend/src/components/ImportDialog.tsx b/frontend/src/components/ImportDialog.tsx
index 420e8ec3..bdc83c71 100644
--- a/frontend/src/components/ImportDialog.tsx
+++ b/frontend/src/components/ImportDialog.tsx
@@ -15,6 +15,7 @@ import {
previewRows,
sourceName,
textTooLong,
+ trapFocusTarget,
} from '../imports';
const btn =
@@ -288,6 +289,7 @@ export const DoneView: React.FC<{ applied: Applied; undo: UndoState; onUndo: (fo
</div>
<Notes items={applied.warnings.map((n) => n.message)} tone="warn" label="Worth a look" />
<p className="text-[11px] text-zinc-400">Give them an account and tools in Space settings when you’re ready. Skills and routines are there to review.</p>
+ <p className="text-[11px] text-zinc-400">Tools you add to this space later will be on for these bots too.</p>
{undo.kind === 'confirm_edited' && (
<div role="alert" className="rounded-lg border border-amber-500/40 bg-amber-500/10 p-3 space-y-2">
<p className="text-xs text-amber-100">
@@ -320,6 +322,8 @@ interface Props {
onChanged: () => void;
}
+const FOCUSABLE = 'button:not([disabled]), textarea:not([disabled]), input:not([disabled]), select:not([disabled]), a[href], [tabindex]:not([tabindex="-1"])';
+
/** "Import bots": pick a source, paste a reply or upload a file, check the preview, import, undo. */
export const ImportDialog: React.FC<Props> = ({ spaceName, onClose, onChanged }) => {
const [step, setStep] = useState<Step>({ kind: 'choose' });
@@ -336,6 +340,7 @@ export const ImportDialog: React.FC<Props> = ({ spaceName, onClose, onChanged })
const [undo, setUndo] = useState<UndoState>({ kind: 'idle' });
const fileRef = useRef<HTMLInputElement>(null);
const bodyRef = useRef<HTMLDivElement>(null);
+ const dialogRef = useRef<HTMLDivElement>(null);
const closeRef = useRef(onClose);
closeRef.current = onClose;
@@ -352,6 +357,20 @@ export const ImportDialog: React.FC<Props> = ({ spaceName, onClose, onChanged })
window.addEventListener('keydown', onKey);
return () => window.removeEventListener('keydown', onKey);
}, []);
+ // Focus goes back to whatever opened the dialog when it closes.
+ useEffect(() => {
+ const opener = document.activeElement as HTMLElement | null;
+ return () => opener?.focus?.();
+ }, []);
+ // Tab and Shift+Tab stay inside the dialog.
+ const onTrapKey = (e: React.KeyboardEvent) => {
+ if (e.key !== 'Tab' || !dialogRef.current) return;
+ const items = [...dialogRef.current.querySelectorAll<HTMLElement>(FOCUSABLE)].filter((el) => el.offsetParent !== null || el === document.activeElement);
+ const next = trapFocusTarget(items.length, items.indexOf(document.activeElement as HTMLElement), e.shiftKey);
+ if (next === null) return;
+ e.preventDefault();
+ items[next].focus();
+ };
// Each step starts at the top, with focus inside the dialog.
useEffect(() => {
bodyRef.current?.scrollTo?.(0, 0);
@@ -470,6 +489,8 @@ export const ImportDialog: React.FC<Props> = ({ spaceName, onClose, onChanged })
return (
<div className="fixed inset-0 z-50 bg-black/75 backdrop-blur-sm flex sm:items-center justify-center sm:p-4" onMouseDown={(e) => e.target === e.currentTarget && onClose()}>
<div
+ ref={dialogRef}
+ onKeyDown={onTrapKey}
role="dialog"
aria-modal="true"
aria-labelledby="import-title"
diff --git a/frontend/src/imports.ts b/frontend/src/imports.ts
index 2450cb5d..e75c5b04 100644
--- a/frontend/src/imports.ts
+++ b/frontend/src/imports.ts
@@ -314,3 +314,15 @@ export async function downloadExport(kwantId?: string): Promise<string> {
setTimeout(() => URL.revokeObjectURL(href), 1000);
return name;
}
+
+/**
+ * Where Tab should land in a focus trap over `count` focusable items, given the focused one's index (-1 if
+ * focus is outside them). Returns null when the browser's own Tab order already stays inside.
+ */
+export function trapFocusTarget(count: number, index: number, shift: boolean): number | null {
+ if (count === 0) return null;
+ if (index < 0) return shift ? count - 1 : 0;
+ if (shift && index === 0) return count - 1;
+ if (!shift && index === count - 1) return 0;
+ return null;
+}
diff --git a/frontend/tests/importDialog.test.mjs b/frontend/tests/importDialog.test.mjs
index 17ace9b6..f1c24d4b 100644
--- a/frontend/tests/importDialog.test.mjs
+++ b/frontend/tests/importDialog.test.mjs
@@ -164,3 +164,19 @@ test('the download name comes from Content-Disposition', () => {
assert.equal(m.downloadName("attachment; filename*=UTF-8''Caf%C3%A9.kwant.json", 'x'), 'Café.kwant.json');
assert.equal(m.downloadName(null, 'export.kwant.json'), 'export.kwant.json');
});
+
+test('focus trap: Tab wraps from the last item to the first, Shift+Tab the other way', () => {
+ assert.equal(m.trapFocusTarget(3, 2, false), 0);
+ assert.equal(m.trapFocusTarget(3, 0, true), 2);
+ assert.equal(m.trapFocusTarget(3, 1, false), null);
+ assert.equal(m.trapFocusTarget(3, 1, true), null);
+ assert.equal(m.trapFocusTarget(3, -1, false), 0);
+ assert.equal(m.trapFocusTarget(3, -1, true), 2);
+ assert.equal(m.trapFocusTarget(0, -1, false), null);
+});
+
+test('success state says tools added later reach the imported bots', () => {
+ const applied = { import: { id: 'imp_1', created: { kwants: [{ id: 'k1', name: 'Recipe-Pal' }], teams: [], skills: [], routines: [], memories: 0 } }, warnings: [] };
+ const out = text(html(m.DoneView, { applied, undo: { kind: 'idle' }, onUndo: () => {}, onKeep: () => {} }));
+ assert.match(out, /Tools you add to this space later will be on for these bots too\./);
+});