Skip to content

Commit c8a236b

Browse files
authored
Merge pull request #50 from DataFlowAnalysis/ligh-dark-switch
Theme switch button
2 parents 2fd414a + 64da69e commit c8a236b

File tree

4 files changed

+82
-14
lines changed

4 files changed

+82
-14
lines changed

src/common/di.config.ts

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { FitToScreenKeyListener as CenterDiagramKeyListener } from "./fitToScree
1919
import { DiagramModificationCommandStack } from "./customCommandStack";
2020

2121
import "./commonStyling.css";
22+
import { LightDarkSwitch } from "./lightDarkSwitch";
2223

2324
export const commonModule = new ContainerModule((bind, unbind, isBound, rebind) => {
2425
bind(ServerCommandPaletteActionProvider).toSelf().inSingletonScope();
@@ -33,6 +34,10 @@ export const commonModule = new ContainerModule((bind, unbind, isBound, rebind)
3334
bind(TYPES.IUIExtension).toService(HelpUI);
3435
bind(EDITOR_TYPES.DefaultUIElement).toService(HelpUI);
3536

37+
bind(LightDarkSwitch).toSelf().inSingletonScope();
38+
bind(TYPES.IUIExtension).toService(LightDarkSwitch);
39+
bind(EDITOR_TYPES.DefaultUIElement).toService(LightDarkSwitch);
40+
3641
bind(DynamicChildrenProcessor).toSelf().inSingletonScope();
3742

3843
unbind(TYPES.ICommandStack);

src/common/lightDarkSwitch.css

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
div.light-dark-switch {
2+
left: 20px;
3+
bottom: 70px;
4+
padding: 10px 10px;
5+
}
6+
7+
#light-dark-label #light-dark-button::before {
8+
content: "";
9+
background-image: url("@fortawesome/fontawesome-free/svgs/regular/moon.svg");
10+
display: inline-block;
11+
filter: invert(var(--dark-mode));
12+
height: 16px;
13+
width: 16px;
14+
background-size: 16px 16px;
15+
vertical-align: text-top;
16+
}
17+
18+
#light-dark-switch:checked ~ label #light-dark-button::before {
19+
background-image: url("@fortawesome/fontawesome-free/svgs/regular/sun.svg");
20+
}

src/common/lightDarkSwitch.ts

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { injectable } from "inversify";
2+
import "./lightDarkSwitch.css";
3+
import { AbstractUIExtension } from "sprotty";
4+
5+
@injectable()
6+
export class LightDarkSwitch extends AbstractUIExtension {
7+
static readonly ID = "light-dark-switch";
8+
static useDarkMode = false;
9+
10+
id(): string {
11+
return LightDarkSwitch.ID;
12+
}
13+
containerClass(): string {
14+
return LightDarkSwitch.ID;
15+
}
16+
protected initializeContents(containerElement: HTMLElement): void {
17+
containerElement.classList.add("ui-float");
18+
containerElement.innerHTML = `
19+
<input type="checkbox" id="light-dark-switch" hidden />
20+
<label id="light-dark-label" for="light-dark-switch">
21+
<div id="light-dark-button"></div>
22+
</label>
23+
`;
24+
25+
const checkbox = containerElement.querySelector("#light-dark-switch") as HTMLInputElement;
26+
checkbox.addEventListener("change", () => {
27+
this.changeDarkMode(checkbox.checked);
28+
});
29+
30+
// use the default browser theme
31+
if (window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches) {
32+
checkbox.checked = true;
33+
this.changeDarkMode(true);
34+
}
35+
}
36+
37+
private changeDarkMode(useDark: boolean) {
38+
const rootElement = document.querySelector(":root") as HTMLElement;
39+
const sprottyElement = document.querySelector("#sprotty") as HTMLElement;
40+
41+
const value = useDark ? "dark" : "light";
42+
rootElement.setAttribute("data-theme", value);
43+
sprottyElement.setAttribute("data-theme", value);
44+
}
45+
}

src/theme.css

+12-14
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,17 @@
99
--dark-mode: 0;
1010
}
1111

12-
@media (prefers-color-scheme: dark) {
13-
:root {
14-
--color-foreground: #fff;
15-
--color-background: #000;
16-
--color-primary: #222;
17-
--color-valid: #0f0;
18-
--color-tool-palette-hover: #444;
19-
--color-tool-palette-selected: #555;
20-
--dark-mode: 1;
21-
}
12+
:root[data-theme="dark"] {
13+
--color-foreground: #fff;
14+
--color-background: #000;
15+
--color-primary: #222;
16+
--color-valid: #0f0;
17+
--color-tool-palette-hover: #444;
18+
--color-tool-palette-selected: #555;
19+
--dark-mode: 1;
20+
}
2221

23-
#sprotty div {
24-
/* Use default dark theme browser styles for scrollbars etc. inside all UI extensions */
25-
color-scheme: dark;
26-
}
22+
#sprotty[data-theme="dark"] div {
23+
/* Use default dark theme browser styles for scrollbars etc. inside all UI extensions */
24+
color-scheme: dark;
2725
}

0 commit comments

Comments
 (0)