|
| 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 | +); |
0 commit comments