Use view transition API to smoothly transition between light/dark theme

This commit is contained in:
Tom Moor committed 2024-10-20 20:02:46 -04:00
1 parent c5278a71de
commit 2ee7e0f832
2 files changed
+20 -1

No files matched your search

+7 -1
View File
@@ -1,8 +1,10 @@
import { action, autorun, computed, observable } from "mobx";
import { flushSync } from "react-dom";
import { light as defaultTheme } from "@shared/styles/theme";
import Storage from "@shared/utils/Storage";
import Document from "~/models/Document";
import type { ConnectionStatus } from "~/scenes/Document/components/MultiplayerEditor";
import { startViewTransition } from "~/utils/viewTransition";
import type RootStore from "./RootStore";
const UI_STORE = "UI_STORE";
@@ -140,7 +142,11 @@ class UiStore {
@action
setTheme = (theme: Theme) => {
this.theme = theme;
startViewTransition(() => {
flushSync(() => {
this.theme = theme;
});
});
Storage.set("theme", this.theme);
};
+13
View File
@@ -0,0 +1,13 @@
/**
* A simple wrapper around the startViewTransition API, if it exists. Otherwise
* it will just call the callback immediately.
*
* @param callback The callback to call inside the view transition.
*/
export const startViewTransition = (callback: UpdateCallback) => {
if (self.document.startViewTransition) {
self.document.startViewTransition(callback);
} else {
callback();
}
};