import { useEffect } from 'react'; import { useLayout } from '../../contexts/LayoutContext'; const SIDEBAR_KEYBOARD_SHORTCUT = 'b'; /** * `useLayoutShortcuts` — Registra atalhos de teclado globais para controle de layout. * * Deve ser usado uma única vez no componente raiz da aplicação (ex: `App.tsx` ou `Layout.tsx`). * Extrai a responsabilidade de atalhos de teclado do `LayoutContext`, seguindo o * princípio de responsabilidade única. * * **Atalhos registrados:** * - `Ctrl+B` / `Cmd+B` — Alterna a sidebar * * @example * ```tsx * // Em App.tsx ou no componente de layout raiz: * import { useLayoutShortcuts } from 'xertica-ui/hooks'; * * function AppLayout({ children }: { children: React.ReactNode }) { * useLayoutShortcuts(); // registra Ctrl+B para toggle da sidebar * return
{children}
; * } * ``` */ export function useLayoutShortcuts() { const { toggleSidebar } = useLayout(); useEffect(() => { if (typeof window === 'undefined') return; const handleKeyDown = (event: KeyboardEvent) => { if (event.key === SIDEBAR_KEYBOARD_SHORTCUT && (event.metaKey || event.ctrlKey)) { event.preventDefault(); toggleSidebar(); } }; window.addEventListener('keydown', handleKeyDown); return () => window.removeEventListener('keydown', handleKeyDown); }, [toggleSidebar]); }