fix: Add data-bs-theme attribute for user dark/light modes (#1782)

* fix: Add data-bs-theme attribute for user dark/light modes

* fix: Remove unnecessary optional chain

* fix: Oops -- add missing files
This commit is contained in:
Jay Sitter 2023-07-03 20:02:24 -04:00 committed by GitHub
parent 8730f157da
commit b1292b958a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 2 deletions

View file

@ -1,10 +1,11 @@
import { isAuthPath, setIsoData } from "@utils/app"; import { isAuthPath, setIsoData } from "@utils/app";
import { dataBsTheme } from "@utils/browser";
import { Component, RefObject, createRef, linkEvent } from "inferno"; import { Component, RefObject, createRef, linkEvent } from "inferno";
import { Provider } from "inferno-i18next-dess"; import { Provider } from "inferno-i18next-dess";
import { Route, Switch } from "inferno-router"; import { Route, Switch } from "inferno-router";
import { IsoDataOptionalSite } from "../../interfaces"; import { IsoDataOptionalSite } from "../../interfaces";
import { routes } from "../../routes"; import { routes } from "../../routes";
import { FirstLoadService, I18NextService } from "../../services"; import { FirstLoadService, I18NextService, UserService } from "../../services";
import AuthGuard from "../common/auth-guard"; import AuthGuard from "../common/auth-guard";
import ErrorGuard from "../common/error-guard"; import ErrorGuard from "../common/error-guard";
import { ErrorPage } from "./error-page"; import { ErrorPage } from "./error-page";
@ -25,6 +26,13 @@ export class App extends Component<any, any> {
event.preventDefault(); event.preventDefault();
this.mainContentRef.current?.focus(); this.mainContentRef.current?.focus();
} }
user = UserService.Instance.myUserInfo;
componentDidMount() {
this.setState({ bsTheme: dataBsTheme(this.user) });
}
render() { render() {
const siteRes = this.isoData.site_res; const siteRes = this.isoData.site_res;
const siteView = siteRes?.site_view; const siteView = siteRes?.site_view;
@ -32,7 +40,11 @@ export class App extends Component<any, any> {
return ( return (
<> <>
<Provider i18next={I18NextService.i18n}> <Provider i18next={I18NextService.i18n}>
<div id="app" className="lemmy-site"> <div
id="app"
className="lemmy-site"
data-bs-theme={this.state?.bsTheme}
>
<button <button
type="button" type="button"
className="btn skip-link bg-light position-absolute start-0 z-3" className="btn skip-link bg-light position-absolute start-0 z-3"

View file

@ -0,0 +1,11 @@
import isDark from "./is-dark";
export default function dataBsTheme(user) {
return (isDark() && user?.local_user_view.local_user.theme === "browser") ||
(user &&
["darkly", "darkly-red", "darkly-pureblack"].includes(
user.local_user_view.local_user.theme
))
? "dark"
: "light";
}

View file

@ -1,5 +1,7 @@
import canShare from "./can-share"; import canShare from "./can-share";
import dataBsTheme from "./data-bs-theme";
import isBrowser from "./is-browser"; import isBrowser from "./is-browser";
import isDark from "./is-dark";
import loadCss from "./load-css"; import loadCss from "./load-css";
import restoreScrollPosition from "./restore-scroll-position"; import restoreScrollPosition from "./restore-scroll-position";
import saveScrollPosition from "./save-scroll-position"; import saveScrollPosition from "./save-scroll-position";
@ -7,7 +9,9 @@ import share from "./share";
export { export {
canShare, canShare,
dataBsTheme,
isBrowser, isBrowser,
isDark,
loadCss, loadCss,
restoreScrollPosition, restoreScrollPosition,
saveScrollPosition, saveScrollPosition,

View file

@ -0,0 +1,7 @@
import isBrowser from "./is-browser";
export default function isDark() {
return (
isBrowser() && window.matchMedia("(prefers-color-scheme: dark)").matches
);
}