ui_util: Extract listener for change in `preferred-color-scheme`.

This will further be used for recipient bar colors since they
have different colors in dark and light color scheme.
This commit is contained in:
Aman Agrawal 2023-04-05 17:42:00 +00:00 committed by Tim Abbott
parent 397ca97060
commit 17a4140773
2 changed files with 17 additions and 12 deletions

View File

@ -3,6 +3,7 @@ import $ from "jquery";
import * as channel from "./channel"; import * as channel from "./channel";
import {page_params} from "./page_params"; import {page_params} from "./page_params";
import * as settings_data from "./settings_data"; import * as settings_data from "./settings_data";
import * as ui_util from "./ui_util";
import * as upload_widget from "./upload_widget"; import * as upload_widget from "./upload_widget";
export function build_realm_logo_widget(upload_function, is_night) { export function build_realm_logo_widget(upload_function, is_night) {
@ -105,16 +106,5 @@ export function initialize() {
render(); render();
// Rerender the realm-logo when the browser detects color scheme changes. // Rerender the realm-logo when the browser detects color scheme changes.
const media_query_list = window.matchMedia("(prefers-color-scheme: dark)"); ui_util.listener_for_preferred_color_scheme_change(render);
// MediaQueryList.addEventListener is missing in Safari < 14
const listener = () => {
if ($(":root").hasClass("color-scheme-automatic")) {
render();
}
};
if ("addEventListener" in media_query_list) {
media_query_list.addEventListener("change", listener);
} else {
media_query_list.addListener(listener);
}
} }

View File

@ -94,3 +94,18 @@ export async function play_audio(elem: HTMLVideoElement): Promise<void> {
blueslip.debug(`Unable to play audio. ${error.name}: ${error.message}`); blueslip.debug(`Unable to play audio. ${error.name}: ${error.message}`);
} }
} }
export function listener_for_preferred_color_scheme_change(callback: () => void): void {
const media_query_list = window.matchMedia("(prefers-color-scheme: dark)");
// MediaQueryList.addEventListener is missing in Safari < 14
const listener: () => void = () => {
if ($(":root").hasClass("color-scheme-automatic")) {
callback();
}
};
if ("addEventListener" in media_query_list) {
media_query_list.addEventListener("change", listener);
} else {
(media_query_list as MediaQueryList).addListener(listener);
}
}