Skip to content

Commit

Permalink
Storybook: Addon to wrap stories in max-width div (#45134)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronrobertshaw authored Oct 24, 2022
1 parent eded26a commit cdbbcb0
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
40 changes: 40 additions & 0 deletions storybook/decorators/with-max-width-wrapper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* External dependencies
*/
import styled from '@emotion/styled';

/**
* A Storybook decorator to wrap a story in a div applying a max width and
* padding. This can be used to simulate real world constraints on components
* such as being located within the WordPress editor sidebars.
*/

const Wrapper = styled.div`
max-width: 248px;
`;

const Indicator = styled.div`
display: flex;
justify-content: center;
align-items: center;
height: 32px;
background: #e0e0e0;
text-transform: uppercase;
font-size: 11px;
font-weight: 500;
color: #757575;
margin-top: 24px;
`;

export const WithMaxWidthWrapper = ( Story, context ) => {
if ( context.globals.maxWidthWrapper === 'none' ) {
return <Story { ...context } />;
}

return (
<Wrapper>
<Story { ...context } />
<Indicator>Max-Width Wrapper - 248px</Indicator>
</Wrapper>
);
};
14 changes: 14 additions & 0 deletions storybook/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import { WithGlobalCSS } from './decorators/with-global-css';
import { WithMarginChecker } from './decorators/with-margin-checker';
import { WithMaxWidthWrapper } from './decorators/with-max-width-wrapper';
import { WithRTL } from './decorators/with-rtl';
import { WithTheme } from './decorators/with-theme';
import './style.scss';
Expand Down Expand Up @@ -63,13 +64,26 @@ export const globalTypes = {
],
},
},
maxWidthWrapper: {
name: 'Max-Width Wrapper',
description: 'Wrap the component in a div with a max-width.',
defaultValue: 'none',
toolbar: {
icon: 'outline',
items: [
{ value: 'none', title: 'None' },
{ value: 'wordpress-sidebar', title: 'WP Sidebar' },
],
},
},
};

export const decorators = [
WithTheme,
WithGlobalCSS,
WithMarginChecker,
WithRTL,
WithMaxWidthWrapper,
];

export const parameters = {
Expand Down

0 comments on commit cdbbcb0

Please sign in to comment.