Skip to content

Commit a7bed09

Browse files
authored
perf: prevent running withoutTransition on initial load (#136)
1 parent 426c30f commit a7bed09

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

.changeset/eighty-snakes-relax.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"mode-watcher": patch
3+
---
4+
5+
perf: prevent running `withoutTransition` on initial load as there is nothing to transition from

packages/mode-watcher/src/lib/without-transition.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,23 @@
22

33
let timeoutAction: number;
44
let timeoutEnable: number;
5+
/**
6+
* Whether this is the first time the function has been
7+
* called, which will be true for the initial load, where
8+
* we shouldn't need to disable any transitions, as there
9+
* is nothing to transition from.
10+
*/
11+
let hasLoaded = false;
512

613
// Perform a task without any css transitions
714
// eslint-disable-next-line @typescript-eslint/no-explicit-any
815
export function withoutTransition(action: () => any) {
916
if (typeof document === "undefined") return;
17+
if (!hasLoaded) {
18+
hasLoaded = true;
19+
action();
20+
return;
21+
}
1022
// Clear fallback timeouts
1123
clearTimeout(timeoutAction);
1224
clearTimeout(timeoutEnable);

packages/mode-watcher/src/routes/+layout.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
let { children } = $props();
66
</script>
77

8-
<ModeWatcher themeColors={{ dark: "black", light: "white" }} />
8+
<ModeWatcher themeColors={{ dark: "black", light: "white" }} disableTransitions />
99
{@render children()}

0 commit comments

Comments
 (0)