Hotkeys and Shortcuts
Relevant source files
The following files were used as context for generating this wiki page:
The Analog web application implements a comprehensive system of hotkeys and keyboard shortcuts to enhance user productivity and provide a faster navigation experience. This system allows users to perform common actions like changing calendar views, navigating between time periods, and managing events without using a mouse.
The core of this functionality is built upon the react-hotkeys-hook library, which provides a declarative way to bind keyboard inputs to specific actions. The hotkeys are organized into different "scopes" (e.g., calendar, event) to ensure they are only active in the appropriate context, preventing conflicts and unintended behavior. This contextual activation is managed through React components and hooks that are integrated into the application's provider tree.
Architecture Overview
The hotkey system is composed of dedicated components for different contexts, which are loaded within the main application layout. The react-hotkeys-hook library underpins this system, managing the event listeners and action dispatching.
This diagram shows the high-level architecture of the hotkey system.
Core Implementation
The hotkey functionality relies on the react-hotkeys-hook package, which is listed as a dependency in the web application's package.json.
Sources: apps/web/package.json:63
This library provides the useHotkeys hook, which is the primary mechanism for defining shortcuts. A key feature used in the Analog codebase is scoping. Hotkeys are registered within specific scopes, such as calendar or event, which allows the application to control which shortcuts are active at any given time.
// Example of a scoped hotkey
useHotkeys(
KEYBOARD_SHORTCUTS.MONTH,
() => setView("month"),
{ scopes: ["calendar"] }
);
Sources: apps/web/src/lib/hotkeys/calendar-hotkeys.ts:22-24
Calendar Hotkeys
The CalendarHotkeys component defines shortcuts related to calendar navigation and view switching. These hotkeys are active within the calendar scope.
Sources: apps/web/src/lib/hotkeys/calendar-hotkeys.ts
Available Shortcuts
| Key | Action |
|---|---|
m | Switch to "month" view. |
w | Switch to "week" view. |
d | Switch to "day" view. |
a | Switch to "agenda" view. |
t | Navigate to the current date ("today"). |
n | Navigate to the next period (day, week, or month). |
p | Navigate to the previous period (day, week, or month). |
Sources: apps/web/src/lib/hotkeys/calendar-hotkeys.ts:18-47
Navigation Flow (n key)
The following diagram illustrates the flow when a user presses the n key to navigate to the next time period.
Event Hotkeys
The EventHotkeys component manages shortcuts for actions related to calendar events, such as creating, deleting, or joining a meeting. These hotkeys are active within the event scope.
Sources: apps/web/src/lib/hotkeys/event-hotkeys.tsx
Available Shortcuts
| Key | Action |
|---|---|
c | Create a new draft event. Opens the right sidebar if closed. |
j | Join the meeting of the currently selected event, if a join URL exists. |
backspace | Opens a confirmation dialog to delete the selected event. |
esc | Unselects all currently selected events. |
Sources: apps/web/src/lib/hotkeys/event-hotkeys.tsx:18-23, 30-88
Create Event Flow (c key)
Pressing c initiates the creation of a new draft event. It uses the default time zone and event duration from settings to pre-populate the event.
Delete Event Flow (backspace key)
The backspace key triggers the event deletion flow, which includes a confirmation step to prevent accidental data loss.
Input-Specific Shortcuts
Beyond the globally scoped hotkeys, certain components implement their own keyboard handling for specific interactions.
Create Event Input
The CreateEventInput component uses the Enter key as a shortcut to submit the new event form.
Sources: apps/web/src/components/create-event-input.tsx
The handleKeyDown logic for the editor checks if the Enter key is pressed without the Shift key. It also ensures that a suggestion dropdown is not active before proceeding to submit the form. This prevents the Enter key from submitting the form when the user intends to select a suggestion.
// apps/web/src/components/create-event-input.tsx:48-59
handleKeyDown: (view, event) => {
if (event.key === "Enter" && !event.shiftKey) {
// @ts-expect-error this property exists as a custom extension
const isSuggestionActive = view.state.mention$.active;
if (isSuggestionActive) {
return false;
}
// Otherwise handle the enter key
event.preventDefault();
handleSubmit();
return true;
}
return false;
},
Summary
The hotkey system in Analog is a powerful feature that significantly improves user workflow. By leveraging react-hotkeys-hook, the application provides two main sets of shortcuts: calendar hotkeys for navigation and event hotkeys for event management. The use of scopes ensures these shortcuts are only active in relevant contexts. Additionally, specific components like the CreateEventInput implement custom keyboard handling for fine-tuned user interactions, contributing to a fluid and efficient user experience.