-
Notifications
You must be signed in to change notification settings - Fork 22
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
Mayflower v10: display rendered styles #1224
Changes from 7 commits
06da44b
cf88b36
e73d29a
12737e6
b97c83e
050ebcb
b356aac
83c1110
e549d12
e0dcc0c
98832dc
9fb73ad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,12 +15,16 @@ import { | |
Canvas, Story | ||
} from '@storybook/addon-docs/blocks'; | ||
import { ActionBar, Source } from '@storybook/components'; | ||
|
||
import prettier from 'prettier/standalone'; | ||
import parserHtml from 'prettier/parser-html'; | ||
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter'; | ||
import { okaidia } from 'react-syntax-highlighter/dist/esm/styles/prism'; | ||
import parserCss from 'prettier/parser-postcss'; | ||
import SyntaxHighlighter, { Renderer, Wrapper } from './syntax-highlighter'; | ||
|
||
import '../src/index.scss'; | ||
|
||
import logo from '!url-loader!@massds/mayflower-assets/static/images/stateseal.png'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should use the UNPKG CDN for linking the assets (pending on the mayflower-assets release) |
||
|
||
const storyKindOrder = [ | ||
'about', // storyKindOrder.indexOf -1 follow alphabetical order | ||
'brand', // storyKindOrder.indexOf -1 follow alphabetical order | ||
|
@@ -33,46 +37,60 @@ const storyKindOrder = [ | |
'others/pages' | ||
]; | ||
|
||
export const StoryPage = ({ StoryComponent = null, showStories = false, Description }) => { | ||
export const StoryPage = ({ StoryComponent = null, showStories = false, Description, styles = null }) => { | ||
const docsContext = React.useContext(DocsContext); | ||
const [showHTML, setShowHTML] = React.useState(true); | ||
const [showCSS, setShowCSS] = React.useState(true); | ||
|
||
const css = React.useMemo(() => showCSS && styles ? styles.toString() : null, [showCSS, styles]); | ||
|
||
const { id, name, parameters = {}, args } = docsContext; | ||
const { component } = parameters; | ||
const HtmlComponent = StoryComponent || component; | ||
let html = null; | ||
if (HtmlComponent) { | ||
html = prettier.format(ReactDOMServer | ||
.renderToStaticMarkup( | ||
( | ||
<HtmlComponent {...args} /> | ||
)), | ||
{ | ||
htmlWhitespaceSensitivity: 'ignore', | ||
endOfLine: 'auto', | ||
parser: 'html', | ||
plugins: [parserHtml] | ||
} | ||
); | ||
} | ||
|
||
let html = ReactDOMServer | ||
.renderToStaticMarkup( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we use |
||
( | ||
<HtmlComponent {...args} /> | ||
)); | ||
const actionItem = { | ||
title: showHTML ? 'Hide HTML' : 'Show HTML?', | ||
onClick: () => setShowHTML((prev) => !prev) | ||
}; | ||
const cssActionItem = { | ||
title: showCSS ? 'Hide Styles' : 'Show Styles?', | ||
onClick: () => setShowCSS((prev) => !prev) | ||
}; | ||
|
||
html = prettier.format(html, | ||
{ | ||
htmlWhitespaceSensitivity: 'ignore', | ||
endOfLine: 'auto', | ||
parser: 'html', | ||
plugins: [parserHtml] | ||
}); | ||
// Replaces the path to the state seal with a base64 image. | ||
html = html.replace(/static\/media\/stateseal\.(.*)\.png/, logo); | ||
|
||
|
||
return( | ||
<> | ||
<Title>{component.displayName}</Title> | ||
<Subtitle /> | ||
{ Description && <Description />} | ||
<Primary name={name} /> | ||
<ArgsTable story={CURRENT_SELECTION}/> | ||
{html && ( | ||
<Heading> | ||
HTML | ||
<ActionBar actionItems={[actionItem]} /> | ||
</Heading> | ||
)} | ||
{!showHTML && <Source storyId={id} error="Click Show HTML above to view markup source." />} | ||
{html && showHTML && <Source storyId={id} language="html" code={html} dark />} | ||
{html && ( | ||
<Heading> | ||
HTML | ||
<ActionBar actionItems={[actionItem]} /> | ||
</Heading> | ||
)} | ||
{!showHTML && <Source storyId={id} error="Click Show HTML above to view markup source." />} | ||
{html && showHTML && <SyntaxHighlighter format={false} renderer={Renderer} language="html" code={html} dark />} | ||
<Heading>Styles<ActionBar actionItems={[cssActionItem]} /></Heading> | ||
{!showCSS && <Source storyId={id} error="Click Show Styles above to view styles source." />} | ||
{css && showCSS && <SyntaxHighlighter format={false} renderer={Renderer} language="css" code={css} dark />} | ||
{ showStories && <Stories />} | ||
</> | ||
); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is no longer needed right?