Files
supabase/apps/studio/hooks/ui/useHotKey.ts
CharisandJoshen Lim 3eb7da7766 feat(preferences): allow disable hotkeys (#39573)
* feat(preferences): allow disable hotkeys

Add a section in /account/me for disabling hotkeys. Only added one
hotkey for now (Cmd + E for toggling editor side panel) but we can add
more with the same pattern.

* refactor: remove default export on ProjectLayout

* feat(hotkeys): allow toggling of command menu and ai assistant hotkeys

* Nit

* PRettier lint

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2025-10-17 08:09:38 -04:00

26 lines
604 B
TypeScript

import { useEffect } from 'react'
export function useHotKey(
callback: (e: KeyboardEvent) => void,
key: string,
dependencies: any[] = [],
options?: { enabled?: boolean }
): void {
const enabled = options?.enabled ?? true
useEffect(() => {
if (!enabled) return
function handler(e: KeyboardEvent) {
if ((e.metaKey || e.ctrlKey) && e.key === key && !e.altKey && !e.shiftKey) {
callback(e)
}
}
window.addEventListener('keydown', handler)
return () => {
window.removeEventListener('keydown', handler)
}
}, [key, enabled, ...dependencies])
}