Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: current param not being respected by the dark OS color mode #207

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 21 additions & 4 deletions src/Tool.tsx
Expand Up @@ -33,6 +33,8 @@ interface DarkModeStore {
lightClass: string;
/** Apply mode to iframe */
stylePreview: boolean;
/** Persist if the user has set the theme */
userHasExplicitlySetTheTheme: boolean;
}

const STORAGE_KEY = 'sb-addon-themes-3';
Expand All @@ -45,6 +47,7 @@ const defaultParams: Required<Omit<DarkModeStore, 'current'>> = {
light: themes.light,
lightClass: 'light',
stylePreview: false,
userHasExplicitlySetTheTheme: false,
};

/** Persist the dark mode settings in localStorage */
Expand Down Expand Up @@ -146,7 +149,10 @@ export function DarkMode({ api }: DarkModeProps) {
const { current: defaultMode, stylePreview, ...params } = darkModeParams;
const channel = api.getChannel();
// Save custom themes on init
const initialMode = React.useMemo(() => store(params).current, [params]);
const userHasExplicitlySetTheTheme = React.useMemo(
() => store(params).userHasExplicitlySetTheTheme,
[params]
);
/** Set the theme in storybook, update the local state, and emit an event */
const setMode = React.useCallback(
(mode: Mode) => {
Expand Down Expand Up @@ -176,6 +182,10 @@ export function DarkMode({ api }: DarkModeProps) {

/** Update the theme based on the color preference */
function prefersDarkUpdate(event: MediaQueryListEvent) {
if (userHasExplicitlySetTheTheme || defaultMode) {
return;
}

updateMode(event.matches ? 'dark' : 'light');
}

Expand All @@ -185,6 +195,13 @@ export function DarkMode({ api }: DarkModeProps) {
setMode(current);
}, [setMode]);

/** Handle the user event and side effects */
const handleIconClick = () => {
updateMode();
const currentStore = store();
updateStore({ ...currentStore, userHasExplicitlySetTheTheme: true });
};

/** When storybook params change update the stored themes */
React.useEffect(() => {
const currentStore = store();
Expand Down Expand Up @@ -219,7 +236,7 @@ export function DarkMode({ api }: DarkModeProps) {
// need the effect to run whenever defaultMode is updated
React.useEffect(() => {
// If a users has set the mode this is respected
if (initialMode) {
if (userHasExplicitlySetTheTheme) {
return;
}

Expand All @@ -228,14 +245,14 @@ export function DarkMode({ api }: DarkModeProps) {
} else if (prefersDark.matches) {
updateMode('dark');
}
}, [defaultMode, updateMode, initialMode]);
}, [defaultMode, updateMode, userHasExplicitlySetTheTheme]);
return (
<IconButton
key="dark-mode"
title={
isDark ? 'Change theme to light mode' : 'Change theme to dark mode'
}
onClick={() => updateMode()}
onClick={handleIconClick}
>
{isDark ? <Sun /> : <Moon />}
</IconButton>
Expand Down