Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions addons/backgrounds/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,14 @@ storiesOf('Button', module)
backgrounds: { disable: true },
});
```

## Events

If you want to react to a background change—for instance to implement some custom logic in your Storybook—you can subscribe to the `storybook/background/update` event. It will be emitted when the user changes the background.

```js
addonAPI.getChannel().on('storybook/background/update', (bg) => {
console.log('Background color', bg.selected);
console.log('Background name', bg.name);
});
```
4 changes: 4 additions & 0 deletions addons/backgrounds/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
export const ADDON_ID = 'storybook/background';
export const PARAM_KEY = 'backgrounds';

export const EVENTS = {
UPDATE: `${ADDON_ID}/update`,
};
15 changes: 9 additions & 6 deletions addons/backgrounds/src/containers/BackgroundSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React, { Component, Fragment } from 'react';
import memoize from 'memoizerific';

import { Combo, Consumer } from '@storybook/api';
import { Combo, Consumer, API } from '@storybook/api';
import { Global, Theme } from '@storybook/theming';

import { Icons, IconButton, WithTooltip, TooltipLinkList } from '@storybook/components';

import { PARAM_KEY } from '../constants';
import { PARAM_KEY, EVENTS } from '../constants';
import { ColorIcon } from '../components/ColorIcon';

interface Item {
Expand All @@ -31,12 +31,12 @@ const createBackgroundSelectorItem = memoize(1000)(
name: string,
value: string,
hasSwatch: boolean,
change: (arg: { selected: string; expanded: boolean }) => void
change: (arg: { selected: string; name: string }) => void
): Item => ({
id: id || name,
title: name,
onClick: () => {
change({ selected: value, expanded: false });
change({ selected: value, name });
},
value,
right: hasSwatch ? <ColorIcon background={value} /> : undefined,
Expand Down Expand Up @@ -96,13 +96,16 @@ interface State {
expanded: boolean;
}

export class BackgroundSelector extends Component<{}, State> {
export class BackgroundSelector extends Component<{ api: API }, State> {
state: State = {
selected: null,
expanded: false,
};

change = (args: State) => this.setState(args);
change = ({ selected, name }: { selected: string; name: string }) => {
this.props.api.emit(EVENTS.UPDATE, { selected, name });
this.setState({ selected, expanded: false });
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The menu always closes when selecting a color, so I moved expanded: false into here.

};

onVisibilityChange = (s: boolean) => {
if (this.state.expanded !== s) {
Expand Down
4 changes: 2 additions & 2 deletions addons/backgrounds/src/register.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { addons, types } from '@storybook/addons';
import { ADDON_ID } from './constants';
import { BackgroundSelector } from './containers/BackgroundSelector';

addons.register(ADDON_ID, () => {
addons.register(ADDON_ID, api => {
addons.add(ADDON_ID, {
title: 'Backgrounds',
type: types.TOOL,
match: ({ viewMode }) => viewMode === 'story',
render: () => <BackgroundSelector />,
render: () => <BackgroundSelector api={api} />,
});
});