Skip to content

Commit

Permalink
Merge pull request #14729 from storybookjs/feat/addon-docs/show_code
Browse files Browse the repository at this point in the history
Addon-docs: Add parameter to show code by default
  • Loading branch information
shilman committed Apr 29, 2021
2 parents 63ae907 + ecac906 commit f6f4c8f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
19 changes: 19 additions & 0 deletions addons/docs/docs/docspage.md
Expand Up @@ -13,6 +13,7 @@ When you install [Storybook Docs](../README.md), `DocsPage` is the zero-config d
- [Remixing DocsPage using doc blocks](#remixing-docspage-using-doc-blocks)
- [Story file names](#story-file-names)
- [Inline stories vs. Iframe stories](#inline-stories-vs-iframe-stories)
- [Show/Hide code](#showhide-code)
- [More resources](#more-resources)

## Motivation
Expand Down Expand Up @@ -183,6 +184,24 @@ addParameters({

With that function, anyone using the docs addon for `@storybook/vue` can make their stories render inline, either globally with the `inlineStories` docs parameter, or on a per-story-basis using the `inline` prop on the `<Story>` doc block. If you come up with an elegant and flexible implementation for the `prepareForInline` function for your own framework, let us know! We'd love to make it the default configuration, to make inline stories more accessible for a larger variety of frameworks!

## Show/Hide code

By default, the code block under the Preview is collapsed and you have to click on "Show code" to reveal it.

You can override this default behavior in `.storybook/preview.js` (or in any of your components/stories):

```js
export const parameters = {
docs: {
source: {
state: 'open',
},
},
};
```

With that flag, now the docs addon will show all code blocks open by default.

## More resources

- References: [README](../README.md) / [DocsPage](docspage.md) / [MDX](mdx.md) / [FAQ](faq.md) / [Recipes](recipes.md) / [Theming](theming.md) / [Props](props-tables.md)
Expand Down
14 changes: 5 additions & 9 deletions addons/docs/src/blocks/Canvas.tsx
Expand Up @@ -22,16 +22,13 @@ type CanvasProps = PurePreviewProps & {
};

const getPreviewProps = (
{
withSource = SourceState.CLOSED,
mdxSource,
children,
...props
}: CanvasProps & { children?: ReactNode },
{ withSource, mdxSource, children, ...props }: CanvasProps & { children?: ReactNode },
docsContext: DocsContextProps,
sourceContext: SourceContextProps
): PurePreviewProps => {
if (withSource === SourceState.NONE) {
const { mdxComponentMeta, mdxStoryNameToKey, parameters } = docsContext;
const sourceState = withSource || parameters?.docs?.source?.state || SourceState.CLOSED;
if (sourceState === SourceState.NONE) {
return props;
}
if (mdxSource) {
Expand All @@ -44,7 +41,6 @@ const getPreviewProps = (
const stories = childArray.filter(
(c: ReactElement) => c.props && (c.props.id || c.props.name)
) as ReactElement[];
const { mdxComponentMeta, mdxStoryNameToKey } = docsContext;
const targetIds = stories.map(
(s) =>
s.props.id ||
Expand All @@ -57,7 +53,7 @@ const getPreviewProps = (
return {
...props, // pass through columns etc.
withSource: sourceProps,
isExpanded: withSource === SourceState.OPEN,
isExpanded: sourceState === SourceState.OPEN,
};
};

Expand Down
Expand Up @@ -29,7 +29,14 @@ export default {
},
},
},
parameters: { chromatic: { disable: true } },
parameters: {
chromatic: { disable: true },
docs: {
source: {
state: 'open',
},
},
},
};

const DEFAULT_NESTED_OBJECT = { a: 4, b: { c: 'hello', d: [1, 2, 3] } };
Expand Down

0 comments on commit f6f4c8f

Please sign in to comment.