Skip to content

Commit

Permalink
Merge pull request #9076 from storybookjs/core/no-persist-theme
Browse files Browse the repository at this point in the history
Core: Don't persist theme to localStorage
  • Loading branch information
ndelangen authored Mar 18, 2020
2 parents bfe27f8 + 6b0453d commit 88f2fca
Showing 1 changed file with 13 additions and 24 deletions.
37 changes: 13 additions & 24 deletions lib/api/src/modules/layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ const checkDeprecatedLayoutOptions = (options: Partial<UIOptions>) => {
return {};
};

const initial: SubState = {
const defaultState: SubState = {
ui: {
enableShortcuts: true,
sidebarAnimations: true,
Expand All @@ -163,7 +163,6 @@ export const focusableUIElements = {
storyPanelRoot: 'storybook-panel-root',
};

let hasSetOptions = false;
export default function({ store, provider }: { store: Store; provider: Provider }) {
const api = {
toggleFullscreen(toggled?: boolean) {
Expand Down Expand Up @@ -284,31 +283,23 @@ export default function({ store, provider }: { store: Store; provider: Provider
const { theme, selectedPanel, ...options } = provider.getConfig();

return {
...initial,
...defaultState,
layout: {
...initial.layout,
...pick(options, Object.keys(initial.layout)),
...defaultState.layout,
...pick(options, Object.keys(defaultState.layout)),
...checkDeprecatedLayoutOptions(options),
},
ui: {
...initial.ui,
...pick(options, Object.keys(initial.ui)),
...defaultState.ui,
...pick(options, Object.keys(defaultState.ui)),
},
selectedPanel: selectedPanel || initial.selectedPanel,
theme: theme || initial.theme,
selectedPanel: selectedPanel || defaultState.selectedPanel,
theme: theme || defaultState.theme,
};
},

setOptions: (options: any) => {
// The very first time the user sets their options, we don't consider what is in the store.
// At this point in time, what is in the store is what we *persisted*. We did that in order
// to avoid a FOUC (e.g. initial rendering the wrong theme while we waited for the stories to load)
// However, we don't want to have a memory about these things, otherwise we see bugs like the
// user setting a name for their storybook, persisting it, then never being able to unset it
// without clearing localstorage. See https://github.com/storybookjs/storybook/issues/5857
const { layout, ui, selectedPanel, theme } = hasSetOptions
? store.getState()
: api.getInitialOptions();
const { layout, ui, selectedPanel, theme } = store.getState();

if (options) {
const updatedLayout = {
Expand Down Expand Up @@ -336,23 +327,21 @@ export default function({ store, provider }: { store: Store; provider: Provider
if (!deepEqual(layout, updatedLayout)) {
modification.layout = updatedLayout;
}
if (!deepEqual(theme, updatedTheme)) {
modification.theme = updatedTheme;
}
if (options.selectedPanel && !deepEqual(selectedPanel, options.selectedPanel)) {
modification.selectedPanel = options.selectedPanel;
}

if (Object.keys(modification).length) {
store.setState(modification, { persistence: 'permanent' });
}

hasSetOptions = true;
if (!deepEqual(theme, updatedTheme)) {
store.setState({ theme: updatedTheme });
}
}
},
};

const persisted = pick(store.getState(), 'layout', 'ui', 'selectedPanel', 'theme');
const persisted = pick(store.getState(), 'layout', 'ui', 'selectedPanel');

return { api, state: merge(api.getInitialOptions(), persisted) };
}

0 comments on commit 88f2fca

Please sign in to comment.