Files
outline/app/hooks/useKeyDown.ts
T
Tom Moor d57db81c6b feat: Split view (#13044)
Adds the ability to open a second route alongside the current one:

- The secondary route is stored in a `split` query string parameter so
  that reloading the page hydrates both panes.
- The secondary pane runs in its own in-memory router kept in sync with
  the query parameter, so links followed inside the pane navigate the
  pane rather than the primary route.
- Navigation while a split is open preserves the split, is directed to
  the focused pane, and routes that cannot render in a pane (such as
  settings) close the split.
- The focused pane is shown with an accent outline, updated as panes are
  clicked or focused, and keyboard shortcuts only apply to the focused
  pane.
- "Open in split view" added to document and collection menus and the
  command bar.
2026-07-19 11:57:33 -04:00

103 lines
2.8 KiB
TypeScript

import { useContext, useEffect } from "react";
import { isModKey } from "@shared/utils/keyboard";
import { SplitViewContext } from "~/components/SplitView/context";
import isTextInput from "~/utils/isTextInput";
import { getFocusedSplitPane } from "~/utils/splitView";
type Callback = (event: KeyboardEvent) => void;
export type KeyFilter = ((event: KeyboardEvent) => boolean) | string;
export type Options = {
allowInInput?: boolean;
};
type RegisteredCallback = {
callback: Callback;
options?: Options;
};
// Registered keyboard event callbacks
let callbacks: RegisteredCallback[] = [];
// Track if IME input suggestions are open so we can ignore keydown shortcuts
// in this case, they should never be triggered from mobile keyboards.
let imeOpen = false;
// Based on implementation in react-use
// https://github.com/streamich/react-use/blob/master/src/useKey.ts#L15-L22
const createKeyPredicate = (keyFilter: KeyFilter) =>
typeof keyFilter === "function"
? keyFilter
: typeof keyFilter === "string"
? (event: KeyboardEvent) => event.key === keyFilter
: keyFilter
? (_event: KeyboardEvent) => true
: (_event: KeyboardEvent) => false;
export default function useKeyDown(
key: KeyFilter,
fn: Callback,
options?: Options
): void {
const { pane, isSplitView } = useContext(SplitViewContext);
const predicate = createKeyPredicate(key);
useEffect(() => {
const handler = (event: KeyboardEvent) => {
// Shortcuts registered within an unfocused split view pane are ignored
// so that only the focused pane responds to them.
if (isSplitView && pane !== getFocusedSplitPane()) {
return;
}
if (predicate(event)) {
fn(event);
}
};
callbacks.push({
callback: handler,
options,
});
return () => {
callbacks = callbacks.filter((cb) => cb.callback !== handler);
};
}, [fn, predicate, options, isSplitView, pane]);
}
window.addEventListener("keydown", (event) => {
if (imeOpen) {
return;
}
// Track whether defaultPrevented was already set by an external handler (e.g.
// Radix UI's DismissableLayer) so we only break on preventDefault calls made
// by our own callbacks.
const wasDefaultPrevented = event.defaultPrevented;
// reverse so that the last registered callbacks get executed first
for (const registered of [...callbacks].reverse()) {
if (!wasDefaultPrevented && event.defaultPrevented) {
break;
}
if (
!isTextInput(event.target as HTMLElement) ||
registered.options?.allowInInput ||
isModKey(event)
) {
registered.callback(event);
}
}
});
window.addEventListener("compositionstart", () => {
imeOpen = true;
});
window.addEventListener("compositionend", () => {
imeOpen = false;
});