Skip to content

Commit 6f26668

Browse files
Chore: Document theme toggle keybinding (grafana#59031)
* make theme toggle keybinding dev only * fix bug + add support for theme change keybinding
1 parent 42baad8 commit 6f26668

File tree

4 files changed

+20
-16
lines changed

4 files changed

+20
-16
lines changed

public/app/core/components/help/HelpModal.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const shortcuts = {
1212
{ keys: ['esc'], description: 'Exit edit/setting views' },
1313
{ keys: ['h'], description: 'Show all keyboard shortcuts' },
1414
{ keys: ['mod+k'], description: 'Open command palette' },
15+
{ keys: ['c', 't'], description: 'Change theme' },
1516
],
1617
Dashboard: [
1718
{ keys: ['mod+s'], description: 'Save dashboard' },

public/app/core/services/keybindingSrv.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ export class KeybindingSrv {
4545
this.bindGlobalEsc();
4646
}
4747

48-
this.bind('t t', () => toggleTheme(false));
49-
this.bind('t r', () => toggleTheme(true));
48+
this.bind('c t', () => toggleTheme(false));
49+
this.bind('c r', () => toggleTheme(true));
5050

5151
if (process.env.NODE_ENV === 'development') {
5252
this.bind('t n', () => this.toggleNav());

public/app/core/services/toggleTheme.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ import { contextSrv } from '../core';
88
import { PreferencesService } from './PreferencesService';
99

1010
export async function toggleTheme(runtimeOnly: boolean) {
11-
const currentTheme = config.theme;
11+
const currentTheme = config.theme2;
1212
const newTheme = createTheme({
1313
colors: {
1414
mode: currentTheme.isDark ? 'light' : 'dark',
1515
},
1616
});
1717

1818
appEvents.publish(new ThemeChangedEvent(newTheme));
19+
config.theme2.isDark = newTheme.isDark;
1920

2021
if (runtimeOnly) {
2122
return;
@@ -25,20 +26,21 @@ export async function toggleTheme(runtimeOnly: boolean) {
2526
const newCssLink = document.createElement('link');
2627
newCssLink.rel = 'stylesheet';
2728
newCssLink.href = config.bootData.themePaths[newTheme.colors.mode];
28-
document.body.appendChild(newCssLink);
29-
30-
// Remove old css file
31-
const bodyLinks = document.getElementsByTagName('link');
32-
for (let i = 0; i < bodyLinks.length; i++) {
33-
const link = bodyLinks[i];
34-
35-
if (link.href && link.href.indexOf(`build/grafana.${currentTheme.type}`) > 0) {
36-
// Remove existing link after a 500ms to allow new css to load to avoid flickering
37-
// If we add new css at the same time we remove current one the page will be rendered without css
38-
// As the new css file is loading
39-
setTimeout(() => link.remove(), 500);
29+
newCssLink.onload = () => {
30+
// Remove old css file
31+
const bodyLinks = document.getElementsByTagName('link');
32+
for (let i = 0; i < bodyLinks.length; i++) {
33+
const link = bodyLinks[i];
34+
35+
if (link.href && link.href.includes(`build/grafana.${!newTheme.isDark ? 'dark' : 'light'}`)) {
36+
// Remove existing link once the new css has loaded to avoid flickering
37+
// If we add new css at the same time we remove current one the page will be rendered without css
38+
// As the new css file is loading
39+
link.remove();
40+
}
4041
}
41-
}
42+
};
43+
document.body.appendChild(newCssLink);
4244

4345
if (!contextSrv.isSignedIn) {
4446
return;

public/app/features/commandPalette/actions/global.static.actions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export default (navBarTree: NavModelItem[]) => {
3333
name: 'Change theme...',
3434
keywords: 'interface color dark light',
3535
section: 'Preferences',
36+
shortcut: ['c', 't'],
3637
},
3738
{
3839
id: 'preferences/dark-theme',

0 commit comments

Comments
 (0)