mirror of
https://github.com/block/buzz.git
synced 2026-08-18 06:50:31 +02:00
Signed-off-by: Wes <wesbillman@users.noreply.github.com> Co-authored-by: Pinky <44b8e82baa6e0e254e0208d68f335c283c94e7b78dd1fa10d5a49d3f13dd0435@sprout-oss.stage.blox.sqprod.co>
44 lines
1007 B
TypeScript
44 lines
1007 B
TypeScript
import * as React from "react";
|
|
|
|
import { hasPrimaryShortcutModifier } from "@/shared/lib/platform";
|
|
|
|
type UseSettingsShortcutsOptions = {
|
|
onClose: () => void;
|
|
onOpenSettings: () => void;
|
|
open: boolean;
|
|
};
|
|
|
|
export function useSettingsShortcuts({
|
|
onClose,
|
|
onOpenSettings,
|
|
open,
|
|
}: UseSettingsShortcutsOptions) {
|
|
React.useLayoutEffect(() => {
|
|
function handleKeyDown(event: KeyboardEvent) {
|
|
const isSettingsShortcut =
|
|
hasPrimaryShortcutModifier(event) &&
|
|
!event.altKey &&
|
|
!event.shiftKey &&
|
|
(event.key === "," || event.code === "Comma");
|
|
|
|
if (!isSettingsShortcut) {
|
|
return;
|
|
}
|
|
|
|
event.preventDefault();
|
|
event.stopImmediatePropagation();
|
|
if (open) {
|
|
onClose();
|
|
return;
|
|
}
|
|
|
|
onOpenSettings();
|
|
}
|
|
|
|
window.addEventListener("keydown", handleKeyDown, true);
|
|
return () => {
|
|
window.removeEventListener("keydown", handleKeyDown, true);
|
|
};
|
|
}, [onClose, onOpenSettings, open]);
|
|
}
|