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

add addon background to monorepo #2220

Merged
merged 64 commits into from Nov 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
df0c6ca
setup project
Aug 31, 2016
a2ece0c
add some info to the readme
Aug 31, 2016
f84c9c6
OPS-2135 Adding react dependency
edolyne Aug 31, 2016
673837d
OPS-2135 Setting a blank background state on unmount
edolyne Aug 31, 2016
9a312de
added tests for swatch component
JakeDawkins Aug 31, 2016
fc2a77b
added tests for swatch component
JakeDawkins Aug 31, 2016
25e352a
added skeleton of backgroundPanel tests
JakeDawkins Aug 31, 2016
f191fac
filled out tests for the backgroundPanel
JakeDawkins Aug 31, 2016
8c87516
removed ghooks from package
JakeDawkins Aug 31, 2016
f13baad
SYS-2135 Adding system font
edolyne Aug 31, 2016
89f0b16
fix code block usage
Aug 31, 2016
7c581fb
fixed a few lint errors, commented out failing test for now, setup tr…
Sep 1, 2016
9703944
added coverage reports
Sep 1, 2016
b43653a
update badges
Sep 1, 2016
04a8d2a
added docs
Sep 1, 2016
b5bd13d
fix docs build
Sep 1, 2016
6e161cb
updated docs
Sep 1, 2016
fbabdd9
short docs and add typscript definitions
Sep 1, 2016
6a3d41a
minor bump for ts fix
Sep 1, 2016
81c3d0e
better registration
Sep 1, 2016
3043590
setup url based rendering
Sep 1, 2016
6d0e21d
updated readme
Sep 1, 2016
af203b3
bump version
Sep 1, 2016
0d268bb
update docs
Sep 1, 2016
3d28569
SYS-2136 Adding test for setting and unsetting query sting
edolyne Sep 1, 2016
a309d38
added test for default background swatch showing
JakeDawkins Sep 1, 2016
7364bbb
added test for default background swatch showing
JakeDawkins Sep 1, 2016
50ef165
added tests for BackgroundPanel
JakeDawkins Sep 1, 2016
20b74fb
fixed failing tests
JakeDawkins Sep 1, 2016
09162b5
Fixes HMR when used as a global decorator
Coobaha Sep 10, 2016
7e84ed9
Tests for prop updates
Sep 11, 2016
15f3661
Tests pass
Sep 11, 2016
3f4fafe
update changelog and version bump
Sep 11, 2016
a71125d
update build
Sep 11, 2016
6ac4d3d
Fixed typo ;)
JakeDawkins Oct 16, 2016
a227715
added react-dom dependency and updated react dep
JakeDawkins Oct 16, 2016
d65656e
add overflow scroll to allow scrolling again (#17)
Oct 16, 2016
c8129e1
Support background-images (#15)
Oct 16, 2016
97ec219
0.0.5
Oct 16, 2016
b0d34a6
update changelog
Oct 16, 2016
b987342
allow default background (#21)
Oct 17, 2016
bfce94f
set default background color (#23)
Oct 17, 2016
802e1d6
Fixed layout when background text is too long.
Dec 6, 2016
281b32a
Bump Storybook dependencies to v3
chris-verclytte Jun 30, 2017
c713fdc
UPDATE dependencies && RENAME package name
ndelangen Jul 4, 2017
1dbfc36
REMOVE coverage reporting
ndelangen Jul 4, 2017
8c7b889
FIX example && linting
ndelangen Jul 4, 2017
cdbcb9d
Update Readme
ndelangen Jul 5, 2017
3b2d1e2
Linting
ndelangen Jul 5, 2017
0d22ce3
convert to js with babel
psimyn Jul 5, 2017
8698781
disable eslint, plan to get everything else working first
psimyn Jul 5, 2017
849b505
fix warnings from React import. remove object-assign
psimyn Jul 5, 2017
c1d77a0
Update package name references and repos
chris-verclytte Jul 6, 2017
7a5601c
Update changelog
chris-verclytte Jul 6, 2017
a5f4a5c
remove unused eslint packages for now
psimyn Jul 7, 2017
4e92209
REMOVE docs
ndelangen Jul 8, 2017
e83e864
WIP
ndelangen Nov 2, 2017
ef83a81
Merge branch 'master' into ndelangen/import-addon-background
Hypnosphi Nov 3, 2017
81420d2
FIX unittests
ndelangen Nov 3, 2017
3c6bff8
Merge branch 'master' into ndelangen/import-addon-background
Hypnosphi Nov 3, 2017
a119a99
FIX linting
ndelangen Nov 3, 2017
037542d
Merge branch 'ndelangen/import-addon-background' of github.com:storyb…
ndelangen Nov 4, 2017
9285aae
DELETE local licence
ndelangen Nov 4, 2017
30fb685
Fix linting and accessibility
Hypnosphi Nov 4, 2017
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
55 changes: 55 additions & 0 deletions addons/background/README.md
@@ -0,0 +1,55 @@
# addon-backgrounds

[![Build Status](https://travis-ci.org/storybooks/addon-backgrounds.svg?branch=travis)](https://travis-ci.org/storybooks/addon-backgrounds)

![React Storybook Screenshot](./.storybook/backgrounds.gif)

### Getting Started

```sh
npm i --save @storybook/addon-backgrounds
```

Then create a file called `addons.js` in your storybook config.

Add following content to it:

```js
import '@storybook/addon-backgrounds/register';
```

Then write your stories like this:

```js
import React from 'react';
import { storiesOf } from "@storybook/react";
import backgrounds from "@storybook/addon-backgrounds";

storiesOf("Button", module)
.addDecorator(backgrounds([
{ name: "twitter", value: "#00aced", default: true },
{ name: "facebook", value: "#3b5998" },
]))
.add("with text", () => <button>Click me</button>);
```

### Development

This project is built using typescript and is tested with jest. To get started, clone this repo and run the following command:
Copy link
Member

Choose a reason for hiding this comment

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

is it still typescript?

Copy link
Member Author

Choose a reason for hiding this comment

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

good point, will remove it. No we removed the typescript.


```bash
$ npm install # install node deps
```

To run the project locally, run:

```bash
$ npm run storybook # for storybook testing
# (coming soon) $ npm run test-watch # for testing
```

To test the project run:

```bash
$ npm test
```
32 changes: 32 additions & 0 deletions addons/background/package.json
@@ -0,0 +1,32 @@
{
"name": "@storybook/addon-backgrounds",
"version": "3.2.14",
"description": "A storybook addon to show different backgrounds for your preview",
"keywords": [
"addon",
"background",
"react",
"storybook"
],
"homepage": "https://storybook.js.org",
"bugs": {
"url": "https://github.com/storybooks/storybook/issues"
},
"license": "MIT",
"author": "jbaxleyiii",
"main": "dist/index.js",
"repository": {
"type": "git",
"url": "https://github.com/storybooks/storybook.git"
},
"scripts": {
"prepare": "node ../../scripts/prepare.js"
},
"dependencies": {
"prop-types": "^15.6.0"
},
"peerDependencies": {
"@storybook/addons": "^3.2.14",
"react": "*"
}
}
1 change: 1 addition & 0 deletions addons/background/register.js
@@ -0,0 +1 @@
require('./dist/register');
125 changes: 125 additions & 0 deletions addons/background/src/BackgroundPanel.js
@@ -0,0 +1,125 @@
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import addons from '@storybook/addons';
import EventEmitter from 'events';

import Swatch from './Swatch';

const style = {
font: {
fontFamily:
"-apple-system,'.SFNSText-Regular', 'San Francisco', Roboto, 'Segoe UI', 'Helvetica Neue', 'Lucida Grande', sans-serif",
fontSize: '14px',
},
};

const defaultBackground = {
name: 'default',
value: 'transparent',
};

const instructionsHtml = `
import { storiesOf } from "@storybook/react";
import backgrounds from "@storybook/addon-backgrounds";

storiesOf("First Component", module)
.addDecorator(backgrounds([
{ name: "twitter", value: "#00aced" },
{ name: "facebook", value: "#3b5998" },
]))
.add("First Button", () => <button>Click me</button>);
`.trim();

const Instructions = () => (
<div style={Object.assign({ padding: '20px' }, style.font)}>
<h5 style={{ fontSize: '16px' }}>Setup Instructions</h5>
<p>
Please add the background decorator definition to your story. The background decorate accepts
an array of items, which should include a name for your color (preferably the css class name)
and the corresponding color / image value.
</p>
<p>Below is an example of how to add the background decorator to your story definition.</p>
<pre
style={{
padding: '30px',
display: 'block',
background: 'rgba(19,19,19,0.9)',
color: 'rgba(255,255,255,0.95)',
marginTop: '15px',
lineHeight: '1.75em',
}}
>
<code>{instructionsHtml}</code>
</pre>
</div>
);

export default class BackgroundPanel extends Component {
constructor(props) {
super(props);

const { channel, api } = props;

// A channel is explicitly passed in for testing
if (channel) {
this.channel = channel;
} else {
this.channel = addons.getChannel();
}

this.state = { backgrounds: [] };

this.channel.on('background-set', backgrounds => {
this.setState({ backgrounds });
const currentBackground = api.getQueryParam('background');

if (currentBackground) {
this.setBackgroundInPreview(currentBackground);
} else if (backgrounds.filter(x => x.default).length) {
const defaultBgs = backgrounds.filter(x => x.default);
this.setBackgroundInPreview(defaultBgs[0].value);
}
});

this.channel.on('background-unset', () => {
this.setState({ backgrounds: [] });
api.setQueryParams({ background: null });
});
}

setBackgroundInPreview = background => this.channel.emit('background', background);

setBackgroundFromSwatch = background => {
this.setBackgroundInPreview(background);
this.props.api.setQueryParams({ background });
};

render() {
const backgrounds = [...this.state.backgrounds];

if (!backgrounds.length) return <Instructions />;

const hasDefault = backgrounds.filter(x => x.default).length;
if (!hasDefault) backgrounds.push(defaultBackground);

return (
<div style={{ display: 'inline-block', padding: '15px' }}>
{backgrounds.map(({ value, name }) => (
<div key={`${name} ${value}`} style={{ display: 'inline-block', padding: '5px' }}>
<Swatch value={value} name={name} setBackground={this.setBackgroundFromSwatch} />
</div>
))}
</div>
);
}
}
BackgroundPanel.propTypes = {
api: PropTypes.shape({
getQueryParam: PropTypes.func,
setQueryParams: PropTypes.func,
}).isRequired,
channel: PropTypes.instanceOf(EventEmitter),
Copy link
Member

Choose a reason for hiding this comment

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

This fails in cra-kitchen-sink (probably because of different events package version):

manager.bundle.js:6219 Warning: Failed prop type: Invalid prop `channel` of type `Channel` supplied to `BackgroundPanel`, expected instance of `EventEmitter`

Let's use PropTypes.shape, like we do in other plugins

Copy link
Member Author

Choose a reason for hiding this comment

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

ok sure

};
BackgroundPanel.defaultProps = {
channel: undefined,
};
65 changes: 65 additions & 0 deletions addons/background/src/Swatch.js
@@ -0,0 +1,65 @@
import React from 'react';
import PropTypes from 'prop-types';

const style = {
swatches: {
backgroundColor: '#fff',
textAlign: 'center',
padding: '0',
border: '1px solid rgba(0,0,0,0.1)',
borderRadius: '4px',
cursor: 'pointer',
display: 'inline-block',
width: '175px',
verticalAlign: 'top',
wordWrap: 'break-word',
},
swatch: {
height: '80px',
borderRadius: '4px 4px 0 0',
transition: 'opacity 0.25s ease-in-out',
borderBottom: '1px solid rgba(0,0,0,0.1)',
},
listStyle: { listStyle: 'none' },
pushBottom: { marginBottom: '10px' },
pushLeft: { marginLeft: '10px' },
soft: { paddingLeft: '10px', paddingRight: '10px' },
hard: { padding: '0' },
flush: { margin: '0' },
font: {
fontFamily:
"-apple-system, '.SFNSText-Regular', 'San Francisco', Roboto, 'Segoe UI', 'Helvetica Neue', 'Lucida Grande', sans-serif",
fontSize: '14px',
wordBreak: 'break-word',
},
};

const Swatch = ({ name, value, setBackground }) => (
<button
style={Object.assign({}, style.swatches, style.listStyle, style.hard)}
onClick={() => setBackground(value)}
// Prevent focusing on mousedown
onMouseDown={event => event.preventDefault()}
>
<div
style={Object.assign({}, style.swatch, {
background: value,
backgroundSize: 'cover',
backgroundPosition: 'center',
})}
/>
<div style={Object.assign({}, style.listStyle, style.soft)}>
<h4 style={Object.assign({ float: 'left', fontWeight: 'bold' }, style.font)}>{name}:</h4>
<h4 style={Object.assign({ float: 'right', fontWeight: 'normal' }, style.font)}>
<em>{value}</em>
</h4>
</div>
</button>
);
Swatch.propTypes = {
name: PropTypes.string.isRequired,
value: PropTypes.string.isRequired,
setBackground: PropTypes.func.isRequired,
};

export default Swatch;