anki/ts/sveltelib/theme.ts
Henrik Giesel 30bbbaf00b
Use eslint for sorting our imports (#1637)
* Make eslint sort our imports

* fix missing deps in eslint rule (dae)

Caught on Linux due to the stricter sandboxing

* Remove exports-last eslint rule (for now?)

* Adjust browserslist settings

- We use ResizeObserver which is not supported in browsers like KaiOS,
  Baidu or Android UC

* Raise minimum iOS version 13.4

- It's the first version that supports ResizeObserver

* Apply new eslint rules to sort imports
2022-02-04 18:36:34 +10:00

37 lines
1.1 KiB
TypeScript

// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import { get, readable } from "svelte/store";
import { registerPackage } from "../lib/runtime-require";
interface ThemeInfo {
isDark: boolean;
}
function getThemeFromRoot(): ThemeInfo {
return {
isDark: document.documentElement.classList.contains("night-mode"),
};
}
let setPageTheme: ((theme: ThemeInfo) => void) | null = null;
/// The current theme that applies to this document/shadow root. When
/// previewing cards in the card layout screen, this may not match the
/// theme Anki is using in its UI.
export const pageTheme = readable(getThemeFromRoot(), (set) => {
setPageTheme = set;
});
// ensure setPageTheme is set immediately
get(pageTheme);
// Update theme when root element's class changes.
const observer = new MutationObserver((_mutationsList, _observer) => {
setPageTheme!(getThemeFromRoot());
});
observer.observe(document.documentElement, { attributeFilter: ["class"] });
registerPackage("anki/theme", {
pageTheme,
});