-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* overview bugs * dbl click + month picker * show full week button (+ refactoring) * view dropdown * fix new stay start date bug * new keyboard shortcuts * finish overview refactoring
- Loading branch information
Showing
20 changed files
with
597 additions
and
326 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
'use client'; | ||
|
||
import { useEffect } from 'react'; | ||
|
||
/** | ||
* pass a handler function to assign keyboard shortcuts, most likely | ||
* using `e.code` to match for actions. | ||
* | ||
* YOU SHOULD DEDUPE THE HANDLER FUNCTION to avoid reassigning on every render (see example). | ||
* | ||
* @example | ||
* const handleShortcut = useCallback<GlobalKeyboardHandler>( | ||
* (e, { withModifiers }) => { | ||
* switch (e.code) { | ||
* case 'KeyP': | ||
* if (withModifiers) break; | ||
* runMyAction(); | ||
* break; | ||
* } | ||
* }, | ||
* [runMyAction], | ||
* ); | ||
* useGlobalKeyboardShortcuts(handleShortcut); | ||
*/ | ||
export function useGlobalKeyboardShortcuts(handler: GlobalKeyboardHandler) { | ||
useEffect(() => { | ||
const dom = window.document; | ||
if (!dom) return; | ||
|
||
const cb = (e: KeyboardEvent) => { | ||
// make sure user isn't typing | ||
const target = e.target as HTMLElement; | ||
if ( | ||
target instanceof HTMLInputElement || | ||
target instanceof HTMLTextAreaElement || | ||
target?.isContentEditable | ||
) | ||
return; | ||
|
||
handler(e, { withModifiers: withModifiers(e) }); | ||
}; | ||
|
||
// attach event listener | ||
dom.addEventListener('keydown', cb); | ||
return () => dom.removeEventListener('keydown', cb); | ||
}, [handler]); | ||
} | ||
|
||
// --------------------------- | ||
|
||
export type GlobalKeyboardHandler = ( | ||
e: KeyboardEvent, | ||
opts: { withModifiers: boolean }, | ||
) => void; | ||
|
||
export function withModifiers(e: KeyboardEvent) { | ||
return e.ctrlKey || e.shiftKey || e.altKey || e.metaKey; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.