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

Merge pull request #9229 from storybookjs/core/preview-layouts #9229

Merged
merged 14 commits into from
Jan 20, 2020
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
33 changes: 33 additions & 0 deletions examples/official-storybook/stories/core/layout.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React from 'react';

// eslint-disable-next-line react/prop-types
const Box = ({ children, display = 'block' }) => (
<div style={{ display, border: '2px solid #FF4785', padding: 10 }}>{children}</div>
);

export default {
title: 'Core/Layout',
};

export const Default = () => <Box>padded by default</Box>;

export const PaddedBlock = () => <Box>padded</Box>;
PaddedBlock.story = { parameters: { layout: 'padded' } };

export const PaddedInline = () => <Box display="inline-block">padded</Box>;
PaddedInline.story = { parameters: { layout: 'padded' } };

export const FullscreenBlock = () => <Box>fullscreen</Box>;
FullscreenBlock.story = { parameters: { layout: 'fullscreen' } };

export const FullscreenInline = () => <Box display="inline-block">fullscreen</Box>;
FullscreenInline.story = { parameters: { layout: 'fullscreen' } };

export const CenteredBlock = () => <Box>centered</Box>;
CenteredBlock.story = { parameters: { layout: 'centered' } };

export const CenteredInline = () => <Box display="inline-block">centered</Box>;
CenteredInline.story = { parameters: { layout: 'centered' } };

export const Invalid = () => <Box>invalid layout value</Box>;
CenteredInline.story = { parameters: { layout: '!invalid!' } };
40 changes: 37 additions & 3 deletions lib/core/src/client/preview/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,37 @@ function focusInInput(event) {
);
}

const applyLayout = (() => {
let previousStyles;

return layout => {
const layouts = {
centered: `
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
margin: 0;
padding: 1rem;
box-sizing: border-box;
`,
fullscreen: `
margin: 0;
`,
padded: `
margin: 0;
padding: 1rem;
`,
};
const styles = layouts[layout] || layouts.padded;

if (styles !== previousStyles) {
document.body.style = styles;
previousStyles = styles;
}
};
})();

export default function start(render, { decorateStory } = {}) {
const context = getContext(decorateStory);

Expand All @@ -140,9 +171,12 @@ export default function start(render, { decorateStory } = {}) {

const data = storyStore.fromId(storyId);

const { kind, name, getDecorated, id, parameters, error } = data || {};
const { kind, name, getDecorated, id, parameters = {}, error } = data || {};

const { docsOnly, layout } = parameters;
applyLayout(layout);

const viewMode = parameters && parameters.docsOnly ? 'docs' : urlViewMode;
const viewMode = docsOnly ? 'docs' : urlViewMode;

const renderContext = {
...context,
Expand Down Expand Up @@ -220,7 +254,7 @@ export default function start(render, { decorateStory } = {}) {
// Given a cleaned up state, render the appropriate view mode
switch (viewMode) {
case 'docs': {
const docs = (parameters && parameters.docs) || {};
const docs = parameters.docs || {};
const DocsContainer = docs.container || (({ children }) => <>{children}</>);
const Page = docs.page || NoDocs;
ReactDOM.render(
Expand Down