question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Docs page doesn't show code preview when default export is a function call

See original GitHub issue

Describe the bug

I have been writing stories using CSF, and the auto generated docs show the source code of the stories, as expected.

I’m using Storybook to build a sort of living design reference, so I wanted to add a status badge to each docspage indicating the readiness of each component. After much searching, I settled on the best way to achieve this being configuring parameters.docs.page and adding the badge component at the top. To save having to write this in every file, I made a helper function:

export const ComponentStory = (options: Options) => ({
  title: options.title,
  component: options.component,
  parameters: {
    docs: {
      page: () => (
        <>
          <StatusBadge status={options.componentStatus} />
          <DocBlocks.Title />
          <DocBlocks.Subtitle />
          <DocBlocks.Description />
          <DocBlocks.Primary />
          <DocBlocks.Props />
          <DocBlocks.Stories />
        </>
      ),
    },
  },
})

which is used like this:

export default ComponentStory({
  title: "Components/Brand/CircleIcon",
  component: CircleIcon,
  componentStatus: "review",
});

However, this causes the story previews to show “no code available”.

image

To debug a bit I tried writing it without the helper function:

export default {
  title: "Components/Brand/CircleIcon",
  component: CircleIcon,
  parameters: {
    docs: {
      page: () => (
        <>
          <StatusBadge status="review" />
          <DocBlocks.Title />
          <DocBlocks.Subtitle />
          <DocBlocks.Description />
          <DocBlocks.Primary />
          <DocBlocks.Props />
          <DocBlocks.Stories />
        </>
      ),
    },
  },
};

The code preview now works as expected.

image

I suspect this is because something in the chain doesn’t recognise the function call version as being CSF.

To Reproduce Steps to reproduce the behavior:

  1. use a function to generate the default export object for a CSF story

Expected behavior The code preview on the generated docs page shows the source code for the story.

Screenshots/ Code See above

System: Please paste the results of npx -p @storybook/cli@next sb info here.

Environment Info:

  System:
    OS: Linux 4.19 Ubuntu 18.04.2 LTS (Bionic Beaver)
    CPU: (4) x64 Intel(R) Core(TM) i5-4570 CPU @ 3.20GHz
  Binaries:
    Node: 12.16.3 - ~/.nodenv/versions/12.16.3/bin/node
    Yarn: 1.22.0 - /mnt/c/Program Files (x86)/Yarn/bin/yarn
    npm: 6.14.4 - ~/.nodenv/versions/12.16.3/bin/npm

Additional context

If there’s an alternative more standard way to achieve what I want to do then I’m all ears. It seems a bit of a hassle to override the layout, when what I want feels more like a decorator for a story. I’m not sure that docs supports decorators, though.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
domyencommented, Aug 7, 2020

I support a feature that allows you to set componentStatus and get a badge. Most professional design systems have this pattern.

Loop me in 😃

1reaction
shilmancommented, Aug 11, 2020

Yes there’s a wrapper for the docs page: https://github.com/storybookjs/storybook/blob/next/addons/docs/docs/recipes.md#overwriting-docs-container

I would probably use story parameters:

export default { 
  title: 'blah',
  parameters: {
    componentStatus: 'deprecated'
  }
}
...

Then define a new DocBlock and add it globally in .storybook/main.js (untested):

import React, { FC, useContext } from 'react';
import { DocsContext } from '@storybook/addon-docs/blocks';

const StatusBadge: FC<{}> = () => {
  const { parameters: { componentStatus = 'unknown' } } = useContext(DocsContext);
  return <>{componentStatus}</>;
};

import { StatusBadge }  from '
export const  parameters = {
    docs: {
      page: () => (
        <>
          <StatusBadge />
          <DocBlocks.Title />
          <DocBlocks.Subtitle />
          <DocBlocks.Description />
          <DocBlocks.Primary />
          <DocBlocks.Props />
          <DocBlocks.Stories />
        </>
      ),
    },
  },
};

cc @domyen WDYT about building something like this in?

Read more comments on GitHub >

github_iconTop Results From Across the Web

What does "export default" do in JSX? - Stack Overflow
A module is a self contained unit that can expose assets to other modules using export , and acquire assets from other modules...
Read more >
`export default thing` is different to `export { thing as default }`
This means when a different value is assigned to thing in module.js , that change is reflected in the import in main.js ....
Read more >
export - JavaScript - MDN Web Docs
The export declaration is used to export values from a JavaScript module. Exported values can then be imported into other programs with the ......
Read more >
Understanding Modules and Import and Export Statements in ...
As the importance of JavaScript in web development grows, there is a bigger need to use third-party code for common tasks, to break...
Read more >
Troubleshooting Cloud Functions - Google Cloud
By default this account is assigned the Cloud Functions cloudfunctions. ... to your code, that is, the exported function name, is not specified...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found