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

Storybook: Addon to wrap stories in max-width div #45134

Merged
merged 2 commits into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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',
aaronrobertshaw marked this conversation as resolved.
Show resolved Hide resolved
items: [
{ value: 'none', title: 'None' },
{ value: 'wordpress-sidebar', title: 'WP Sidebar' },
],
},
},
};

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

export const parameters = {
Expand Down