Skip to content

Commit b01a8a8

Browse files
committed
Export Layout & Block as separate styled Components
1 parent dfe3746 commit b01a8a8

File tree

4 files changed

+79
-56
lines changed

4 files changed

+79
-56
lines changed

dev/register.js

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,36 @@
11
import React from 'react';
2+
import { styled } from '@storybook/theming';
23
import { register } from '../src/register';
4+
import { Layout, Block } from '../src/Layout';
35
import './config';
46

5-
const blockStyle = {
6-
margin: 2,
7-
padding: 4,
8-
border: '2px solid gray',
9-
};
7+
8+
const LayoutBlock = styled(Layout)`
9+
padding: 0px;
10+
border: red 1px solid;
11+
label: layout-with-styles;
12+
`
13+
14+
const AddonBlock = styled(Block)`
15+
margin: 2px;
16+
padding: 4px;
17+
border: 2px solid gray;
18+
font-size: 14px;
19+
background-color: pink;
20+
label: block-with-styles;
21+
`
1022

1123
const AddonPanel = ({
1224
api,
1325
data,
14-
setData,
1526
kind,
16-
story,
17-
rect,
18-
Layout,
19-
Block,
2027
indInc,
2128
indDec,
2229
update,
2330
}) => {
2431
return (
25-
<Layout style={{ padding: 0 }}>
26-
<Block style={{ ...blockStyle, minWidth: 50 }} size={200}>
32+
<LayoutBlock style={{ padding: 0 }}>
33+
<AddonBlock size={200}>
2734
kind: {kind}
2835
<br />
2936
<button onClick={() => indInc()}> + </button>
@@ -32,15 +39,15 @@ const AddonPanel = ({
3239
<button onClick={() => update({ themes: ['T1', 'T2', 'T3'] })}>
3340
Update
3441
</button>
35-
</Block>
42+
</AddonBlock>
3643
{/* <Block style={blockStyle}>
3744
<small>{JSON.stringify(api.getCurrentStoryData())}</small>
3845
</Block> */}
39-
<Block style={blockStyle}>
46+
<AddonBlock>
4047
channel store data: <br /> ({JSON.stringify(data)})
41-
</Block>
48+
</AddonBlock>
4249
{/* <Block style={blockStyle}>data ({JSON.stringify(rect, null, 2)})</Block> */}
43-
</Layout>
50+
</LayoutBlock>
4451
);
4552
};
4653

src/Layout.js

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22
import Rect from '@reach/rect';
3-
import { styled, css } from '@storybook/theming';
3+
import { styled, cx, css } from '@storybook/theming';
44

55
const layout = React.createContext({});
66

@@ -14,7 +14,7 @@ const panelDimensions = rect =>
1414
: {};
1515

1616
const AddonHolder = styled('div')`
17-
height: 100;
17+
height: 100%;
1818
label: addon-holder;
1919
`;
2020

@@ -31,7 +31,21 @@ export const LayoutProvider = ({ children }) => (
3131
</Rect>
3232
);
3333

34-
const StyledLayout = styled('div')`
34+
35+
const StyledOverridden = ({
36+
className,
37+
overrides,
38+
children,
39+
isLandscape,
40+
size,
41+
...props
42+
}) => (
43+
<div className={`${className} ${overrides}`} {...props}>
44+
{children}
45+
</div>
46+
);
47+
48+
const StyledLayout = styled(StyledOverridden)`
3549
display: flex;
3650
flex-direction: ${({ isLandscape }) => (isLandscape ? 'row' : 'column')};
3751
justify-content: space-between;
@@ -40,30 +54,28 @@ const StyledLayout = styled('div')`
4054
label: addon-layout;
4155
`;
4256

43-
export const Layout = ({ children }) => (
57+
export const Layout = ({ className, children }) => (
4458
<layout.Consumer>
4559
{({ isLandscape }) => (
46-
<StyledLayout isLandscape={isLandscape}>{children}</StyledLayout>
60+
<StyledLayout isLandscape={isLandscape} overrides={className}>{children}</StyledLayout>
4761
)}
4862
</layout.Consumer>
4963
);
5064

51-
const StyledBlock = styled.div(({ isLandscape, size }) => ({
52-
flexGrow: 1,
53-
...(size
54-
? {
55-
...(isLandscape ? { width: size } : { height: size }),
56-
flexGrow: undefined,
57-
}
58-
: {
59-
...(isLandscape ? { width: 2 } : { height: 2 }),
60-
}),
61-
}));
6265

63-
export const Block = ({ size, children }) => (
66+
const px = v => `${v}px`;
67+
68+
const StyledBlock = styled(StyledOverridden)`
69+
${({ isLandscape }) => (isLandscape ? 'width' : 'height')}: ${({ size }) =>
70+
size ? px(size) : '2px'};
71+
${({ size }) => (size ? '' : 'flex-grow: 1;')}
72+
label: addon-block;
73+
`;
74+
75+
export const Block = ({ size, children, className }) => (
6476
<layout.Consumer>
6577
{({ isLandscape }) => (
66-
<StyledBlock size={size} isLandscape={isLandscape}>
78+
<StyledBlock size={size} isLandscape={isLandscape} overrides={className}>
6779
{children}
6880
</StyledBlock>
6981
)}

src/register.js

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ import addons, { types as addonTypes } from '@storybook/addons';
33
import { STORY_CHANGED } from '@storybook/core-events';
44
import Rect from '@reach/rect';
55

6-
import {
7-
getConfig,
8-
} from './config';
6+
import { getConfig } from './config';
97
import withChannel from './withChannel';
8+
import { LayoutProvider } from './Layout';
109

1110
// todo: remove
1211
const panelDimesions = rect =>
@@ -76,7 +75,7 @@ class PanelHOC extends React.Component {
7675
render() {
7776
const Panel = this.props.component;
7877
const { api, active, data, setData, config } = this.props;
79-
const {ADDON_ID, PANEL_ID, PANEL_Title} = config;
78+
const { ADDON_ID, PANEL_ID, PANEL_Title } = config;
8079
const { kind, story } = this.state;
8180

8281
if (!active) return null;
@@ -89,22 +88,24 @@ class PanelHOC extends React.Component {
8988
const Block = addonBlock(dim.isLandscape);
9089
return (
9190
<div ref={ref} name="addon-holder" style={{ height: '100%' }}>
92-
<Panel
93-
{...this.props.actions}
94-
{...this.props.selectors}
95-
api={api}
96-
active={active}
97-
data={data}
98-
setData={setData}
99-
kind={kind}
100-
story={story}
101-
ADDON_ID={ADDON_ID}
102-
PANEL_ID={PANEL_ID}
103-
PANEL_Title={PANEL_Title}
104-
rect={dim}
105-
Layout={Layout}
106-
Block={Block}
107-
/>
91+
<LayoutProvider>
92+
<Panel
93+
{...this.props.actions}
94+
{...this.props.selectors}
95+
api={api}
96+
active={active}
97+
data={data}
98+
setData={setData}
99+
kind={kind}
100+
story={story}
101+
ADDON_ID={ADDON_ID}
102+
PANEL_ID={PANEL_ID}
103+
PANEL_Title={PANEL_Title}
104+
rect={dim}
105+
Layout={Layout}
106+
Block={Block}
107+
/>
108+
</LayoutProvider>
108109
</div>
109110
);
110111
}}
@@ -113,7 +114,10 @@ class PanelHOC extends React.Component {
113114
}
114115
}
115116

116-
export const register = (storeSelectors, createActions) => (Panel, { type = addonTypes.PANEL, initData } = {}) => {
117+
export const register = (storeSelectors, createActions) => (
118+
Panel,
119+
{ type = addonTypes.PANEL, initData } = {}
120+
) => {
117121
const config = getConfig();
118122
const {
119123
EVENT_ID_INIT,

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1678,7 +1678,7 @@
16781678
memoizerific "^1.11.3"
16791679
qs "^6.5.2"
16801680

1681-
"@storybook/[email protected]":
1681+
"@storybook/[email protected]", "@storybook/theming@^5.0.11":
16821682
version "5.0.11"
16831683
resolved "https://registry.yarnpkg.com/@storybook/theming/-/theming-5.0.11.tgz#46e243c36324676c36393c01695bdbd6369e79b4"
16841684
integrity sha512-zZoMFirkdGYd4M6HupNYn713GesA/kx0nbhPfyatwnwRV9/LrG7KwkMjVDMrSHRdZei7z1KF7yFhleAOKb8RaQ==

0 commit comments

Comments
 (0)