Skip to content

Commit dfe3746

Browse files
committed
WIP Refactor Layout
1 parent fa091d1 commit dfe3746

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"dependencies": {
2121
"@reach/rect": "^0.2.1",
2222
"@storybook/addons": "^5.0.11",
23+
"@storybook/theming": "^5.0.11",
2324
"@storybook/core-events": "^5.0.11",
2425
"prop-types": "^15.6.2"
2526
},

src/Layout.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import React from 'react';
2+
import Rect from '@reach/rect';
3+
import { styled, css } from '@storybook/theming';
4+
5+
const layout = React.createContext({});
6+
7+
const panelDimensions = rect =>
8+
rect
9+
? {
10+
width: rect.width,
11+
height: rect.height,
12+
isLandscape: rect.width >= rect.height,
13+
}
14+
: {};
15+
16+
const AddonHolder = styled('div')`
17+
height: 100;
18+
label: addon-holder;
19+
`;
20+
21+
export const LayoutProvider = ({ children }) => (
22+
<Rect>
23+
{({ rect, ref }) => {
24+
const dimensions = panelDimensions(rect);
25+
return (
26+
<AddonHolder ref={ref}>
27+
<layout.Provider value={dimensions}>{children}</layout.Provider>
28+
</AddonHolder>
29+
);
30+
}}
31+
</Rect>
32+
);
33+
34+
const StyledLayout = styled('div')`
35+
display: flex;
36+
flex-direction: ${({ isLandscape }) => (isLandscape ? 'row' : 'column')};
37+
justify-content: space-between;
38+
align-items: stretch;
39+
height: 100%;
40+
label: addon-layout;
41+
`;
42+
43+
export const Layout = ({ children }) => (
44+
<layout.Consumer>
45+
{({ isLandscape }) => (
46+
<StyledLayout isLandscape={isLandscape}>{children}</StyledLayout>
47+
)}
48+
</layout.Consumer>
49+
);
50+
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+
}));
62+
63+
export const Block = ({ size, children }) => (
64+
<layout.Consumer>
65+
{({ isLandscape }) => (
66+
<StyledBlock size={size} isLandscape={isLandscape}>
67+
{children}
68+
</StyledBlock>
69+
)}
70+
</layout.Consumer>
71+
);

src/register.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
} from './config';
99
import withChannel from './withChannel';
1010

11+
// todo: remove
1112
const panelDimesions = rect =>
1213
rect
1314
? {

0 commit comments

Comments
 (0)