Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Fix dark font on dark background by default #103

Merged
merged 1 commit into from
Nov 27, 2024
Merged
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
14 changes: 12 additions & 2 deletions client/src/util/storage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { JsonWebToken, SavedOAuthToken, UserTheme } from "@fumix/fu-blog-common";
import { base64UrlToBuffer, isOAuthType } from "@fumix/fu-blog-common";
import { base64UrlToBuffer, isOAuthType, isUserTheme } from "@fumix/fu-blog-common";

type OAuthState = { key: string; redirect_uri?: string };
const idTokenKey = "id_token";
Expand Down Expand Up @@ -67,7 +67,17 @@ export function saveCssPreference(css: UserTheme) {
}

export function loadCssPreference(): UserTheme {
return loadFromStorageAsString(window.localStorage, "cssTheme", "lightTheme") as UserTheme;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The culprit was the default value for UserTheme, which was "lightTheme" instead of "light".

I rewrote this a bit, so Typescript will make sure through the type system that only "light" or "dark" can be returned (also covers cases where some different string is saved in localStorage).

return loadFromStorage(
window.localStorage,
"cssTheme",
(saved) => {
if (isUserTheme(saved)) {
return saved;
}
return null;
},
"light",
);
}

//
Expand Down
7 changes: 6 additions & 1 deletion common/src/entity/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ export type UserWithOAuthProviders = User & {
oauthProviders: OAuthProviderId[];
};

export type UserTheme = "light" | "dark";
const userThemes = ["light", "dark"] as const;
export type UserTheme = (typeof userThemes)[number];

export function isUserTheme(s: string): s is UserTheme {
return userThemes.some((it) => it === s);
}