-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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 theming bugs #5787
FIX theming bugs #5787
Changes from 11 commits
362a339
239d9f3
2eed5f7
9a5ccca
1ecf313
ef1f79c
d9bb177
16ff723
7622f72
f03d7e9
81770cc
242e611
6489d05
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,64 @@ | ||
import pick from 'lodash.pick'; | ||
|
||
import deprecate from 'util-deprecate'; | ||
|
||
import { create, themes } from '@storybook/theming'; | ||
import merge from '../libs/merge'; | ||
|
||
const deprecatedThemeOptions = { | ||
name: 'brandTitle', | ||
url: 'brandUrl', | ||
}; | ||
const deprecatedLayoutOptions = { | ||
goFullScreen: 'isFullscreen', | ||
showStoriesPanel: 'showNav', | ||
showAddonPanel: 'showPanel', | ||
addonPanelInRight: 'panelPosition', | ||
}; | ||
|
||
const deprecationMessage = (optionsMap, prefix) => | ||
`The options { ${Object.keys(optionsMap).join(', ')} } are deprecated -- use ${ | ||
prefix ? `${prefix}'s` : '' | ||
} { ${Object.values(optionsMap).join(', ')} } instead.`; | ||
|
||
const applyDeprecatedThemeOptions = deprecate(({ name, url, theme }) => { | ||
const vars = { | ||
brandTitle: name, | ||
brandUrl: url, | ||
brandImage: null, | ||
}; | ||
|
||
return { theme: create(vars, theme) }; | ||
}, deprecationMessage(deprecatedThemeOptions)); | ||
|
||
const applyDeprecatedLayoutOptions = deprecate(options => { | ||
const layoutUpdate = {}; | ||
|
||
['goFullScreen', 'showStoriesPanel', 'showAddonPanel'].forEach(option => { | ||
if (typeof options[option] !== 'undefined') { | ||
layoutUpdate[deprecatedLayoutOptions[option]] = options[option]; | ||
} | ||
}); | ||
if (options.addonPanelInRight) { | ||
layoutUpdate.panelPosition = 'right'; | ||
} | ||
return layoutUpdate; | ||
}, deprecationMessage(deprecatedLayoutOptions)); | ||
|
||
const checkDeprecatedThemeOptions = options => { | ||
if (Object.keys(deprecatedThemeOptions).find(key => !!options[key])) { | ||
return applyDeprecatedThemeOptions(options); | ||
} | ||
return {}; | ||
}; | ||
|
||
const checkDeprecatedLayoutOptions = options => { | ||
if (Object.keys(deprecatedLayoutOptions).find(key => typeof options[key] !== 'undefined')) { | ||
return applyDeprecatedLayoutOptions(options); | ||
} | ||
return {}; | ||
}; | ||
|
||
export default function({ store }) { | ||
const api = { | ||
toggleFullscreen(toggled) { | ||
|
@@ -69,7 +130,54 @@ export default function({ store }) { | |
}; | ||
}); | ||
}, | ||
|
||
setOptions: options => { | ||
const { layout, ui, selectedPanel } = store.getState(); | ||
|
||
if (options) { | ||
const updatedLayout = { | ||
...layout, | ||
...pick(options, Object.keys(layout)), | ||
...checkDeprecatedLayoutOptions(options), | ||
}; | ||
|
||
const updatedUi = { | ||
...ui, | ||
...pick(options, Object.keys(ui)), | ||
...checkDeprecatedThemeOptions(options), | ||
}; | ||
|
||
store.setState( | ||
{ | ||
layout: updatedLayout, | ||
ui: updatedUi, | ||
selectedPanel: options.panel || options.selectedPanel || selectedPanel, | ||
}, | ||
{ persistence: 'permanent' } | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this mean we perma-persist all options, including the ones that were session-persisted above? I'm OK with this for now for simplicity, but we might want to do something more subtle in the future (which will probably require improvements to the persistence API) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I briefly experimented with persisting all the layout & ui options, but decided to not increase the size of this PR even more. But it was actually really nice, things like full-screen, nav, panelPosition being preserved over refreshes... On my todo list after V5 launch. |
||
} | ||
}, | ||
}; | ||
|
||
return { api }; | ||
const fromState = pick(store.getState(), 'layout', 'ui', 'selectedPanel'); | ||
|
||
const initial = { | ||
ui: { | ||
enableShortcuts: true, | ||
sortStoriesByKind: false, | ||
sidebarAnimations: true, | ||
theme: themes.normal, | ||
}, | ||
layout: { | ||
isToolshown: true, | ||
isFullscreen: false, | ||
showPanel: true, | ||
showNav: true, | ||
panelPosition: 'bottom', | ||
}, | ||
}; | ||
|
||
const state = merge(fromState, initial); | ||
|
||
return { api, state }; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bit weerd (grabbing the selected panel twice). Is it intentional?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, what's going on here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just followed the arguments of
ensurePanel
,I can remove 1 I think.