Skip to content

Commit

Permalink
Merge pull request #8232 from storybookjs/core/setConfig-method
Browse files Browse the repository at this point in the history
ADD method to set config for manager
  • Loading branch information
ndelangen committed Oct 3, 2019
2 parents 43afd67 + f563459 commit d7495fb
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 4 deletions.
4 changes: 4 additions & 0 deletions app/react-native-server/src/client/manager/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ export default class ReactProvider extends Provider {
return addons.getElements(type);
}

getConfig() {
return this.addons.getConfig();
}

renderPreview() {
return (
<Consumer filter={mapper} pure>
Expand Down
7 changes: 7 additions & 0 deletions examples/dev-kits/addons.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
import '@storybook/addon-roundtrip/register';
import '@storybook/addon-parameter/register';

import { addons } from '@storybook/addons';
import { themes } from '@storybook/theming';

addons.setConfig({
theme: themes.dark,
});
12 changes: 12 additions & 0 deletions lib/addons/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ interface Elements {
[key: string]: Collection;
}

interface Config {
[key: string]: any;
}

export class AddonStore {
constructor() {
this.promise = new Promise(res => {
Expand All @@ -50,6 +54,8 @@ export class AddonStore {

private elements: Elements = {};

private config: Config = {};

private channel: Channel | undefined;

private promise: any;
Expand Down Expand Up @@ -96,6 +102,12 @@ export class AddonStore {
collection[name] = { id: name, ...addon };
};

setConfig = (value: Config) => {
Object.assign(this.config, value);
};

getConfig = () => this.config;

register = (name: string, registerCallback: (api: API) => void): void => {
if (this.loaders[name]) {
logger.warn(`${name} was loaded twice, this could have bad side-effects`);
Expand Down
18 changes: 15 additions & 3 deletions lib/api/src/modules/layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { themes, ThemeVars } from '@storybook/theming';
import merge from '../lib/merge';
import { State } from '../index';
import Store from '../store';
import { Provider } from '../init-provider-api';

export type PanelPositions = 'bottom' | 'right';

Expand Down Expand Up @@ -153,7 +154,7 @@ export const focusableUIElements = {
};

let hasSetOptions = false;
export default function({ store }: { store: Store }) {
export default function({ store, provider }: { store: Store; provider: Provider }) {
const api = {
toggleFullscreen(toggled?: boolean) {
return store.setState((state: State) => {
Expand Down Expand Up @@ -248,14 +249,25 @@ export default function({ store }: { store: Store }) {
}
},

getInitialOptions() {
const { theme } = provider.getConfig();

return {
...initial,
theme: theme || initial.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() : initial;
const { layout, ui, selectedPanel, theme } = hasSetOptions
? store.getState()
: api.getInitialOptions();

if (options) {
const updatedLayout = {
Expand Down Expand Up @@ -301,5 +313,5 @@ export default function({ store }: { store: Store }) {

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

return { api, state: merge(initial, persisted) };
return { api, state: merge(api.getInitialOptions(), persisted) };
}
2 changes: 1 addition & 1 deletion lib/api/src/tests/layout.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('layout API', () => {
getState: () => currentState,
setState: jest.fn(),
};
layoutApi = initLayout({ store }).api;
layoutApi = initLayout({ store, provider: { getConfig: jest.fn(() => ({})) } }).api;
});

it('should not change selectedPanel if it is undefined in the options', () => {
Expand Down
4 changes: 4 additions & 0 deletions lib/core/src/client/manager/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export default class ReactProvider extends Provider {
return this.addons.getElements(type);
}

getConfig() {
return this.addons.getConfig();
}

handleAPI(api) {
this.addons.loadAddons(api);
}
Expand Down
4 changes: 4 additions & 0 deletions lib/ui/src/app.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ class FakeProvider extends Provider {
handleAPI(api) {
addons.loadAddons(api);
}

getConfig() {
return {};
}
}

storiesOf('UI|Layout/App', module)
Expand Down
4 changes: 4 additions & 0 deletions lib/ui/src/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ export default class Provider {
handleAPI() {
throw new Error('Provider.handleAPI() is not implemented!');
}

getConfig() {
throw new Error('Provider.getConfig() is not implemented!');
}
}

1 comment on commit d7495fb

@vercel
Copy link

@vercel vercel bot commented on d7495fb Oct 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.