Skip to content

Commit

Permalink
fix-deepsource
Browse files Browse the repository at this point in the history
  • Loading branch information
mayank1513 committed Dec 30, 2023
1 parent 3adbd37 commit ac19cd1
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions lib/nextjs-themes/src/client/theme-switcher/theme-switcher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ export interface UpdateProps {
th: string;
}

function updateDOM(
const updateDOM = (
{ resolvedTheme, resolvedColorScheme, resolvedColorSchemePref, th }: UpdateProps,
isSystemDark: boolean,
targetSelector?: string,
) {
) => {
[document.querySelector(targetSelector || "#nextjs-themes"), document.documentElement].forEach(target => {
/** ensuring that class 'dark' is always present when dark color scheme is applied to support Tailwind */
if (target)
Expand All @@ -49,9 +49,8 @@ function updateDOM(

/** store system preference for computing data-theme on server side */
document.cookie = `data-color-scheme-system=${isSystemDark ? "dark" : "light"}`;
}
};

// todo: customizable transition time
const disableAnimation = (themeTransition = "none") => {
const css = document.createElement("style");
const transition = `transition: ${themeTransition} !important;`;
Expand All @@ -70,6 +69,10 @@ const disableAnimation = (themeTransition = "none") => {
};
};

/**
* You can use this hook in place of `<ThemeSwitcher />` component.
* Please note that you need to add "use client" on top of the component in which you are using this hook.
*/
export function useThemeSwitcher(props: ThemeSwitcherProps) {
const [setStorage, ...depArray] = useTheme(state => [
state.setStorage,
Expand Down Expand Up @@ -106,6 +109,10 @@ export function useThemeSwitcher(props: ThemeSwitcherProps) {
}, [props.forcedColorScheme, props.forcedTheme, props.targetSelector, ...depArray]);
}

/**
* Use this component in your layout - `app/layout.tsx` or your custom layout or in `_app.tsx` file.
* @component
*/
export function ThemeSwitcher(props: ThemeSwitcherProps) {
useThemeSwitcher(props);
return null;
Expand Down

0 comments on commit ac19cd1

Please sign in to comment.