Skip to content

[#481] If user hasn't explicitly set a dark mode pref in Silverbullet, fall back to browser pref #1294

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plugs/editor/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export async function openAllNavigator() {
}

export async function toggleDarkMode() {
let darkMode = await clientStore.get("darkMode");
let darkMode = await editor.getUiOption("darkMode");
darkMode = !darkMode;
await clientStore.set("darkMode", darkMode);
await editor.reloadUI();
Expand Down
2 changes: 1 addition & 1 deletion web/components/basic_modals.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function Prompt({
message: string;
defaultValue?: string;
vimMode: boolean;
darkMode: boolean;
darkMode: boolean | undefined;
completer: (context: CompletionContext) => Promise<CompletionResult | null>;
callback: (value?: string) => void;
}) {
Expand Down
2 changes: 1 addition & 1 deletion web/components/command_palette.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function CommandPalette({
commands: Map<string, AppCommand>;
recentCommands: Map<string, Date>;
vimMode: boolean;
darkMode: boolean;
darkMode: boolean | undefined;
completer: (context: CompletionContext) => Promise<CompletionResult | null>;
onTrigger: (command: AppCommand | undefined) => void;
config: Config;
Expand Down
2 changes: 1 addition & 1 deletion web/components/filter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function FilterList({
preFilter?: (options: FilterOption[], phrase: string) => FilterOption[];
phrasePreprocessor?: (phrase: string) => string;
vimMode: boolean;
darkMode: boolean;
darkMode: boolean | undefined;
completer: (context: CompletionContext) => Promise<CompletionResult | null>;
allowNew?: boolean;
completePrefix?: string;
Expand Down
2 changes: 1 addition & 1 deletion web/components/mini_editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function MiniEditor(
text: string;
placeholderText?: string;
vimMode: boolean;
darkMode: boolean;
darkMode: boolean | undefined;
vimStartInInsertMode?: boolean;
focus?: boolean;
completer?: (
Expand Down
2 changes: 1 addition & 1 deletion web/components/page_navigator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function PageNavigator({
allPages: PageMeta[];
extensions: Set<string>;
vimMode: boolean;
darkMode: boolean;
darkMode: boolean | undefined;
mode: "page" | "meta" | "document" | "all";
onNavigate: (page: string | undefined, type: "document" | "page") => void;
onModeSwitch: (mode: "page" | "meta" | "document" | "all") => void;
Expand Down
2 changes: 1 addition & 1 deletion web/components/top_bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function TopBar({
syncFailures: number;
isLoading: boolean;
notifications: Notification[];
darkMode: boolean;
darkMode: boolean | undefined;
vimMode: boolean;
progressPerc?: number;
onRename: (newName?: string) => Promise<void>;
Expand Down
24 changes: 18 additions & 6 deletions web/editor_ui.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type { Client } from "./client.ts";
import { Panel } from "./components/panel.tsx";
import { safeRun, sleep } from "../lib/async.ts";
import { parseCommand } from "$common/command.ts";
import { clientStoreSyscalls } from "./syscalls/clientStore.ts";
import { defaultActionButtons } from "@silverbulletmd/silverbullet/type/config";
import type { FilterOption } from "@silverbulletmd/silverbullet/type/client";

Expand Down Expand Up @@ -97,12 +98,23 @@ export class MainUI {
}, [viewState.uiOptions.vimMode]);

useEffect(() => {
document.documentElement.dataset.theme = viewState.uiOptions.darkMode
? "dark"
: "light";
if (this.client.isDocumentEditor()) {
this.client.documentEditor.updateTheme();
}
clientStoreSyscalls(client.stateDataStore)["clientStore.get"]({}, "darkMode").then((storedDarkModePreference: boolean | undefined) => {
let theme: "dark" | "light";
if (storedDarkModePreference === true) {
theme = "dark";
} else if (storedDarkModePreference === false) {
theme = "light";
} else {
theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
}

viewState.uiOptions.darkMode = theme === "dark";
document.documentElement.dataset.theme = theme

if (this.client.isDocumentEditor()) {
this.client.documentEditor.updateTheme();
}
});
}, [viewState.uiOptions.darkMode]);

useEffect(() => {
Expand Down
4 changes: 2 additions & 2 deletions web/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export type AppViewState = {

uiOptions: {
vimMode: boolean;
darkMode: boolean;
darkMode: boolean | undefined;
forcedROMode: boolean;
customStyles?: string;
};
Expand Down Expand Up @@ -82,7 +82,7 @@ export const initialViewState: AppViewState = {
syncFailures: 0,
uiOptions: {
vimMode: false,
darkMode: false,
darkMode: undefined,
forcedROMode: false,
},
isMobile: false,
Expand Down