Skip to content

Commit

Permalink
Implement dark/white theme controls. Fix #56
Browse files Browse the repository at this point in the history
  • Loading branch information
OctoNezd committed Jan 3, 2024
1 parent 614a297 commit 207b678
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/features/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import Sidebar from "./sidebar";
import RESCompatibility from "./RESCompatibility";
import PostsEnhancements from "./posts";
import { OLFeature } from "./base";
import WhiteTheme from "./themeSwitch";
type Constructor = new (...args: any[]) => OLFeature;
const features: Array<Constructor> = [Expandos, RESButtons, Sidebar, RESCompatibility, RedditMarquee];
const features: Array<Constructor> = [Expandos, RESButtons, Sidebar, RESCompatibility, RedditMarquee, WhiteTheme];
features.push(...PostsEnhancements)
export default features;
40 changes: 40 additions & 0 deletions src/features/themeSwitch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { store } from "../extensionPreferences";
import { waitForElement } from "../utility/waitForElement";
import { OLFeature, SettingToggle } from "./base";

const systemThemeKey = "systemTheme"
const whiteThemeKey = "whiteTheme"

export default class WhiteTheme extends OLFeature {
moduleId: string = "whiteTheme";
moduleName: string = "Theme controls";
async init () {
this.settingOptions.push(
new SettingToggle("White theme", "Enables white theme instead of dark.", whiteThemeKey, async (theme) => {
const { systemTheme } = await store.get(systemThemeKey)
if (systemTheme !== undefined && systemTheme) {
console.log("Systemtheme key is set, not enabling white theme")
return
}
waitForElement("body", () => {
document.body.classList.toggle("whiteTheme", theme)
})
})
)
this.settingOptions.push(
new SettingToggle("System theme", "Makes oldlander follow system theme. Overrides white theme option", systemThemeKey, (followSystemTheme) => {
if (followSystemTheme) {
this.followSystemTheme()
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', this.followSystemTheme)
}
})
)
}
followSystemTheme() {
const darkTheme = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches
console.log("Dark theme?", darkTheme)
waitForElement("body", () => {
document.body.classList.toggle("whiteTheme", !darkTheme)
})
}
}
4 changes: 2 additions & 2 deletions src/material/theme.css
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
@import url(tokens.css);
@import url(colors.css);
@import url(typography.css);
@import url(theme.light.css) (prefers-color-scheme: light);
@import url(theme.dark.css) (prefers-color-scheme: dark);
@import url(theme.light.css);
@import url(theme.dark.css);
:root {
--md-sys-shape-corner-extra-small: (4px 4px 0 0)
}
Expand Down
2 changes: 1 addition & 1 deletion src/material/theme.light.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
:root {
.whiteTheme {
--md-sys-color-primary: var(--md-sys-color-primary-light);
--md-sys-color-on-primary: var(--md-sys-color-on-primary-light);
--md-sys-color-primary-container: var(
Expand Down

0 comments on commit 207b678

Please sign in to comment.