fix(shorcuts): add shortcut to toggle the theme (#602)

This commit is contained in:
Jonathan Fishner
2025-03-04 10:28:30 +02:00
committed by GitHub
parent 467ff697c9
commit a643852837
3 changed files with 42 additions and 3 deletions

View File

@@ -7,6 +7,7 @@ export enum KeyboardShortcutAction {
SAVE_DIAGRAM = 'save_diagram',
TOGGLE_SIDE_PANEL = 'toggle_side_panel',
SHOW_ALL = 'show_all',
TOGGLE_THEME = 'toggle_theme',
}
export interface KeyboardShortcut {
@@ -63,6 +64,13 @@ export const keyboardShortcuts: Record<
keyCombinationMac: 'meta+0',
keyCombinationWin: 'ctrl+0',
},
[KeyboardShortcutAction.TOGGLE_THEME]: {
action: KeyboardShortcutAction.TOGGLE_THEME,
keyCombinationLabelMac: '⌘M',
keyCombinationLabelWin: 'Ctrl+M',
keyCombinationMac: 'meta+m',
keyCombinationWin: 'ctrl+m',
},
};
export interface KeyboardShortcutForOS {

View File

@@ -1,8 +1,13 @@
import React, { useEffect, useState } from 'react';
import React, { useEffect, useState, useCallback } from 'react';
import type { EffectiveTheme } from './theme-context';
import { ThemeContext } from './theme-context';
import { useMediaQuery } from 'react-responsive';
import { useLocalConfig } from '@/hooks/use-local-config';
import { useHotkeys } from 'react-hotkeys-hook';
import {
KeyboardShortcutAction,
keyboardShortcutsForOS,
} from '../keyboard-shortcuts-context/keyboard-shortcuts';
export const ThemeProvider: React.FC<React.PropsWithChildren> = ({
children,
@@ -29,6 +34,24 @@ export const ThemeProvider: React.FC<React.PropsWithChildren> = ({
}
}, [effectiveTheme]);
const handleThemeToggle = useCallback(() => {
if (theme === 'system') {
setTheme(effectiveTheme === 'dark' ? 'light' : 'dark');
} else {
setTheme(theme === 'dark' ? 'light' : 'dark');
}
}, [theme, effectiveTheme, setTheme]);
useHotkeys(
keyboardShortcutsForOS[KeyboardShortcutAction.TOGGLE_THEME]
.keyCombination,
handleThemeToggle,
{
preventDefault: true,
},
[handleThemeToggle]
);
return (
<ThemeContext.Provider value={{ theme, setTheme, effectiveTheme }}>
{children}

View File

@@ -470,8 +470,16 @@ export const Menu: React.FC<MenuProps> = () => {
</MenubarSub>
<MenubarSeparator />
<MenubarSub>
<MenubarSubTrigger>
{t('menu.view.theme')}
<MenubarSubTrigger className="flex items-center gap-1">
<span>{t('menu.view.theme')}</span>
<div className="flex-1" />
<MenubarShortcut>
{
keyboardShortcutsForOS[
KeyboardShortcutAction.TOGGLE_THEME
].keyCombinationLabel
}
</MenubarShortcut>
</MenubarSubTrigger>
<MenubarSubContent>
<MenubarCheckboxItem