Skip to content

Commit

Permalink
Add setForced states to useTheme
Browse files Browse the repository at this point in the history
  • Loading branch information
mayank1513 committed Jun 18, 2024
1 parent f7deaec commit ff2df36
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ interface UseThemeYield {
setThemeSet: (themeSet: { darkTheme: string; lightTheme: string }) => void;
setColorSchemePref: (colorSchemePref: ColorSchemeType) => void;
toggleColorScheme: (skipSystem?: boolean) => void;
setForcedTheme: (forcedTheme: string) => void;
setForcedColorScheme: (forcedColorScheme: ColorSchemeType) => void;
}
```

Expand Down
8 changes: 7 additions & 1 deletion lib/src/hooks/use-theme.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useEffect } from "react";
import { useThemeStore } from "../store";
import { useForcedStore, useThemeStore } from "../store";
import { ResolveFunc } from "../client/theme-switcher/no-fouc";
import { ColorSchemeType, ResolvedColorSchemeType } from "../types";
import { DARK, LIGHT, SYSTEM } from "../constants";
Expand All @@ -22,6 +22,8 @@ interface UseThemeYield {
setThemeSet: (themeSet: { darkTheme: string; lightTheme: string }) => void;
setColorSchemePref: (colorSchemePref: ColorSchemeType) => void;
toggleColorScheme: (skipSystem?: boolean) => void;
setForcedTheme: (forcedTheme: string) => void;
setForcedColorScheme: (forcedColorScheme: ColorSchemeType) => void;
}

/**
Expand All @@ -37,6 +39,7 @@ interface UseThemeYield {

export const useTheme = (targetSelector?: string): UseThemeYield => {
const [state, setState] = useThemeStore(targetSelector);
const [_, setForcedState] = useForcedStore(targetSelector);
useEffect(() => {
resolveTheme = window.r;
}, []);
Expand Down Expand Up @@ -66,6 +69,9 @@ export const useTheme = (targetSelector?: string): UseThemeYield => {
if (index === -1 || (skipSystem && index === len - 1)) index = 0;
setter("c")(colorSchemes[(index + 1) % len]);
},
setForcedColorScheme: forcedColorScheme =>
setForcedState(state => ({ ...state, fc: forcedColorScheme })),

Check warning on line 73 in lib/src/hooks/use-theme.ts

View check run for this annotation

Codecov / codecov/patch

lib/src/hooks/use-theme.ts#L73

Added line #L73 was not covered by tests
setForcedTheme: forcedTheme => setForcedState(state => ({ ...state, f: forcedTheme })),
};

if (resolveTheme) {
Expand Down

0 comments on commit ff2df36

Please sign in to comment.