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.

Story source code shows "MDXCreateElement" instead of actual source code

See original GitHub issue

Describe the bug The output of Show Code tabs mismatches the source

To Reproduce

  1. Create a new .mdx story:
# Alert 

<Preview>
  <Story name="Alert with one option">
    {() => {
      const [visible, toggleVisible] = useState(false);
      return (
        <div>
          <button onClick={() => toggleVisible(!visible)}>Open Alert with one action</button>
          <Alert
            visible={visible}
            toggleVisible={() => toggleVisible(false)}
            title="Title"
            description="Description"
          />
        </div>
      );
    }}
  </Story>
</Preview>
  1. Click on ‘Show Code’
  2. Scroll down to screenshot of output

Expected behavior Output match contents of the <Story>

Screenshots image

Code snippets Full Story

import { Story, Preview, Props } from '@storybook/addon-docs/blocks';
import { withA11y } from '@storybook/addon-a11y';
import { useState } from 'react';
import OverlayAlert from './OverlayAlert';

<Meta title="Components/Overlay - Alert" component={OverlayAlert} decorators={[withA11y]} />

# Overlay - Alert

<Preview>
  <Story name="Alert with one option">
    {() => {
      const [visible, toggleVisible] = useState(false);
      return (
        <div>
          <button onClick={() => toggleVisible(!visible)}>Open Alert with one action</button>
          <Alert
            visible={visible}
            toggleVisible={() => toggleVisible(false)}
            title="Title"
            description="Description"
          />
        </div>
      );
    }}
  </Story>
</Preview>

Main.js

module.exports = {
  stories: ['../src/docs/*.mdx', '../src/**/*.stories.(js|mdx)'],
  addons: [
    '@storybook/preset-create-react-app',
    '@storybook/addon-controls',
    '@storybook/addon-actions',
    '@storybook/addon-links',
    '@storybook/addon-a11y/register',
    '@storybook/addon-viewport/register',
    {
      name: '@storybook/addon-docs',
      options: { configureJSX: true },
    },
  ],
};

Show code output

<MDXCreateElement
  mdxType="div"
  originalType="div"
>
  <MDXCreateElement
    mdxType="button"
    onClick={() => {}}
    originalType="button"
  >
    Open Alert with one action
  </MDXCreateElement>
  <MDXDefaultShortcode
    description="Description"
    title="Title"
    toggleVisible={function noRefCheck() {}}
  />
</MDXCreateElement>

System: Environment Info:

System: OS: macOS 10.15.5 CPU: (12) x64 Intel® Core™ i7-8850H CPU @ 2.60GHz Binaries: Node: 10.15.2 - /usr/local/bin/node Yarn: 1.17.3 - /usr/local/bin/yarn npm: 6.4.1 - /usr/local/bin/npm Browsers: Chrome: 83.0.4103.116 Firefox: 74.0 Safari: 13.1.1 npmPackages: @storybook/addon-a11y: ^6.0.0-rc.3 => 6.0.0-rc.3 @storybook/addon-actions: ^6.0.0-rc.3 => 6.0.0-rc.3 @storybook/addon-controls: ^6.0.0-beta.15 => 6.0.0-beta.15 @storybook/addon-docs: ^6.0.0-rc.3 => 6.0.0-rc.3 @storybook/addon-links: ^6.0.0-rc.3 => 6.0.0-rc.3 @storybook/addon-viewport: ^6.0.0-rc.3 => 6.0.0-rc.3 @storybook/addons: ^6.0.0-rc.3 => 6.0.0-rc.3 @storybook/preset-create-react-app: ^3.1.2 => 3.1.3 @storybook/react: ^6.0.0-rc.3 => 6.0.0-rc.3 @storybook/theming: ^6.0.0-rc.3 => 6.0.0-rc.3

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:29
  • Comments:44 (11 by maintainers)

github_iconTop GitHub Comments

43reactions
robbertklcommented, Jul 28, 2020

It seems to only occur when using an intermediate variable (including the template/bind pattern):

export const test = args => <Button {...args} />;

<Preview>
  <Story name="button">
    {test}
  </Story>
</Preview>

☝️ Does not work (shows MDXCreateElement)

<Preview>
  <Story name="button">
    {args => <Button {...args} />}
  </Story>
</Preview>

☝️ Does work (shows Button)

21reactions
git-arbitrarysystemscommented, Sep 4, 2020

@shilman Thanks for the work around. But seems not working for me. After I set docs.source.type to “code”. It shows Template.bind({})

This is still an issue in version 6.1.0-alpha.3. However if i use {args => <TestComponent {...args} />} instead of Template.bind({}) it works correctly.

TestComponent.stories.mdx

import { Meta, Story, Canvas, ArgsTable } from "@storybook/addon-docs/blocks";
import { TestComponent } from "./TestComponent";

<Meta title="Generated/TestComponent" component={TestComponent} />

# TestComponent

This is the default description for a component in mdx format

<Canvas>
    <Story name="Primary" args={{ value: "primary" }}>
        {args => <TestComponent {...args} />}
    </Story>
</Canvas>

## Properties

<ArgsTable of={TestComponent} />

Read more comments on GitHub >

github_iconTop Results From Across the Web

Story source code shows "MDXCreateElement" instead of ...
I'm experiencing this issue using Template.bind({}) with or without decorators. It seems any component that is defined in the MDX file is being...
Read more >
storybook/addon-docs - npm
DocsPage pulls information from your stories, components, source code, and story metadata to construct a sensible, zero-config default.
Read more >
when MDXCreateElement shows up in storybook
... method suggested in the docs ends up putting MDXCreateElement rather than the actual JSX component names behind the "Show code" button.
Read more >
Storybook for everyone: CSF vs. MDX - DEV Community ‍ ‍
propTypes = { /** The display src of the avatar */ src: ... have multiple stories from a single component since it reduces...
Read more >
how to see full code in storybook by using ... - Stack Overflow
Storybook 6.0 introduced an unintentional change to source-loader, in which only the source of the selected story is shown in the addon.
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