Skip to content

Commit

Permalink
Added debouncing around setThemeMode to avoid sync events from other …
Browse files Browse the repository at this point in the history
…tabs firing into too often.
  • Loading branch information
lancegliser committed May 5, 2024
1 parent f25ebf9 commit 1c2f364
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/ui/src/hooks/use-theme-mode.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use client";

import debounce from "debounce";
import { useEffect, useState } from "react";
import { isClient } from "../helpers/is-client";
import { useWatchLocalStorageValue } from "../hooks/use-watch-localstorage-value";
Expand Down Expand Up @@ -40,12 +41,13 @@ export const useThemeMode = () => {
/**
* Sets `mode` to a given value: `light | dark` | `auto`
*/
const handleSetMode = (mode: ThemeMode) => {
const _handleSetMode = (mode: ThemeMode) => {
setMode(mode);
setModeInLS(mode);
setModeInDOM(mode);
document.dispatchEvent(new CustomEvent(SYNC_THEME_MODE, { detail: mode }));
};
const handleSetMode = debounce(_handleSetMode, 25, { immediate: true });

/**
* Toggles between: `light | dark`
Expand Down

0 comments on commit 1c2f364

Please sign in to comment.