mirror of
https://github.com/outline/outline.git
synced 2026-11-30 09:00:18 +03:00
Use view transition API to smoothly transition between light/dark theme
This commit is contained in:
1 parent
c5278a71de
commit
2ee7e0f832
2 files changed
+20
-1
No files matched your search
@@ -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);
|
||||
};
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
};
|
||||
Reference in new issue
Block a user