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

feat: add toggle shortcut option and support tinykey keybindings #126

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
"@reach/portal": "^0.16.0",
"fast-equals": "^2.0.3",
"match-sorter": "^6.3.0",
"react-virtual": "^2.8.2"
"react-virtual": "^2.8.2",
"tinykeys": "^1.2.0"
},
"peerDependencies": {
"react": "^16.0.0 || ^17.0.0",
Expand Down
105 changes: 49 additions & 56 deletions src/InternalEvents.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from "react";
import tinykeys from "tinykeys";
import { VisualState } from "./types";
import useKBar from "./useKBar";
import { getScrollbarWidth } from "./utils";
Expand All @@ -23,12 +24,12 @@ function useToggleHandler() {
}));

React.useEffect(() => {
function handleKeyDown(event: KeyboardEvent) {
if (
(event.metaKey || event.ctrlKey) &&
event.key === "k" &&
event.defaultPrevented === false
) {
const keybinding = Array.isArray(options.shortcut)
? options.shortcut.join(" ")
: options.shortcut || "$mod+k";

const unsubscribe = tinykeys(window, {
[keybinding]: (event: KeyboardEvent) => {
event.preventDefault();
query.toggle();

Expand All @@ -37,8 +38,8 @@ function useToggleHandler() {
} else {
options.callbacks?.onOpen?.();
}
}
if (event.key === "Escape") {
},
Escape: (event: KeyboardEvent) => {
if (showing) {
event.stopPropagation();
options.callbacks?.onClose?.();
Expand All @@ -50,12 +51,12 @@ function useToggleHandler() {
}
return VisualState.animatingOut;
});
}
}

window.addEventListener("keydown", handleKeyDown, true);
return () => window.removeEventListener("keydown", handleKeyDown, true);
}, [options.callbacks, query, showing]);
},
});
return () => {
unsubscribe();
};
}, [options.callbacks, options.shortcut, query, showing]);

const timeoutRef = React.useRef<Timeout>();
const runAnimateTimer = React.useCallback(
Expand Down Expand Up @@ -153,55 +154,47 @@ function useShortcuts() {

const inputs = ["input", "select", "button", "textarea"];

let buffer: string[] = [];
let lastKeyStrokeTime = Date.now();

function handleKeyDown(event: KeyboardEvent) {
const key = event.key?.toLowerCase();

const activeElement = document.activeElement;
const ignoreStrokes =
activeElement &&
(inputs.indexOf(activeElement.tagName.toLowerCase()) !== -1 ||
activeElement.attributes.getNamedItem("role")?.value === "textbox" ||
activeElement.attributes.getNamedItem("contenteditable")?.value ===
"true");

if (ignoreStrokes || event.metaKey || key === "shift") {
return;
const keyBindingMap = {};
for (let action of actionsList) {
if (!action.shortcut) {
continue;
}

const currentTime = Date.now();

if (currentTime - lastKeyStrokeTime > 400) {
buffer = [];
}

buffer.push(key);
lastKeyStrokeTime = currentTime;
const bufferString = buffer.join("");

for (let action of actionsList) {
if (!action.shortcut) {
continue;
const keybinding = Array.isArray(action.shortcut)
? action.shortcut.join(" ")
: action.shortcut;

keyBindingMap[keybinding] = (event: KeyboardEvent) => {
const activeElement = document.activeElement;
const ignoreStrokes =
activeElement &&
(inputs.indexOf(activeElement.tagName.toLowerCase()) !== -1 ||
activeElement.attributes.getNamedItem("role")?.value ===
"textbox" ||
activeElement.attributes.getNamedItem("contenteditable")?.value ===
"true");

if (ignoreStrokes) {
return;
}
if (action.shortcut.join("") === bufferString) {
event.preventDefault();
if (action.children?.length) {
query.setCurrentRootAction(action.id);
query.toggle();
} else {
action.perform?.();
}

buffer = [];
break;
event.preventDefault();
if (action.children?.length) {
query.setCurrentRootAction(action.id);
query.toggle();
} else {
action.perform?.();
}
}
};
}

window.addEventListener("keydown", handleKeyDown);
return () => window.removeEventListener("keydown", handleKeyDown);
const unsubscribe = tinykeys(window, keyBindingMap, {
timeout: 400,
});

return () => {
unsubscribe();
};
}, [actions, query]);
}

Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export type ActionId = string;
export interface BaseAction {
id: ActionId;
name: string;
shortcut?: string[];
shortcut?: string | string[];
keywords?: string;
section?: string;
icon?: string | React.ReactElement | React.ReactNode;
Expand All @@ -28,6 +28,7 @@ export interface ActionGroup {
}

export interface KBarOptions {
shortcut?: string | string[];
animations?: {
enterMs?: number;
exitMs?: number;
Expand Down
1 change: 1 addition & 0 deletions src/useStore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export default function useStore(props: useStoreProps) {
enterMs: 200,
exitMs: 100,
},
shortcut: "$mod+k",
...props.options,
} as KBarOptions);

Expand Down