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

fromId is not defined when using mdx format #6766

Closed
chrispcode opened this issue May 11, 2019 · 15 comments
Closed

fromId is not defined when using mdx format #6766

chrispcode opened this issue May 11, 2019 · 15 comments

Comments

@chrispcode
Copy link

chrispcode commented May 11, 2019

Describe the bug
Alright, so I have been trying to translate some of my Docz .mdx files to storybook, with version
v5.2.0-alpha.4. And so far so good, I get the canvas tab to work, but when I navigate to the docs tab it crashes saying Cannot read property 'fromId' of undefined.

To Reproduce
Steps to reproduce the behavior:

  1. Install @storybook/react and @storybook/addon-docs to version 5.2.0-alpha.4.

  2. Add the following configuration:

.storybook/config.js

import { load } from '@storybook/react';

// load(require.context('../stories', true, /\.stories\.js$/), module);
load(require.context('../stories', true, /\.stories\.mdx$/), module);

.storybook/preset.js

module.exports = ['@storybook/addon-docs/common/preset'];

.storybook/webpack.config.js

const path = require('path');
const createCompiler = require('@storybook/addon-docs/mdx-compiler-plugin');

const babelrc = path.resolve(__dirname, '.babelrc');

module.exports = async ({ config }) => {
  console.log(config.module.rules);
  config.module.rules.push({
    test: /\.mdx$/,
    use: [
      { 
        loader: 'babel-loader',
        options: {
          configFile: babelrc
        }
      },
      {
        loader: '@mdx-js/loader',
        options: {
          compilers: [createCompiler({})],
        },
      },
    ],
  });
  return config;
};

.storybook/addons.js

import '@storybook/addon-docs/register';

.storybook/.babelrc

{
  "presets": [
    "@babel/preset-react"
  ]
}
  1. Add a stories.mdx file, let's say /stories/button.stories.mdx with the following content:

stories/button.stories.mdx

import { Story } from '@storybook/addon-docs/blocks';
import { Button } from '@storybook/react/demo';

export const componentMeta = {
  title: 'MDX|Button',
}

# Hello Docs

Welcome to the future of Storybook!

<Story name="hello">
  <Button >Hello button!</Button>
</Story>
  1. Run storybook and navigate to that documentation

Expected behavior
The Docs tab open, and shows the documentation

Screenshots
image
image
image

Code snippets
You can find them in the reproduce section.

System:

  • OS: MacOS
  • Device: Macbook Pro 2018
  • Browser: chrome
  • Framework: react
  • Addons: @storybook/addon-docs
  • Version: v5.2.0-alpha.4

Additional context
I think I have added everything, If more information is needed, I will provide it!

@shilman
Copy link
Member

shilman commented May 11, 2019

Some comments:

  • You can use @storybook/addon-docs/react/preset since you're using @storybook/react
  • The following configurations are redundant with the preset and should be removed:
    • webpack.config.js
    • .babelrc
    • addons.js

Try getting rid of those and maybe it will solve the problem?

@chrispcode
Copy link
Author

chrispcode commented May 11, 2019

If I remove them, I get a transpilation error:

image

@chrispcode
Copy link
Author

If I add only the webpack configuration, and remove the link to the .babelrc I get a react syntax error:

image

@chrispcode
Copy link
Author

If I add the .babelrc as well the project compiles with no errors, but the docs tab does not show up.

image

@chrispcode
Copy link
Author

Also the preset change does not effect it, in my situation

@shilman
Copy link
Member

shilman commented May 12, 2019

Oh, rename your file to presets.js

@chrispcode
Copy link
Author

Yes, that was the solution! Sorry for losing your time with this guys.

@eden-lane
Copy link

eden-lane commented Feb 27, 2020

I have the same issue in storybook 5.3.14
My main.js file is

module.exports = {
  stories: ['../app/**/*.stories.mdx'],
  addons: [
    {
      name: '@storybook/addon-docs',
      options: {
        configureJSX: true,
      }
    },
    '@storybook/addon-knobs',
    '@storybook/addon-actions',
    '@storybook/addon-links',
  ],
  presets: [
    '@storybook/preset-typescript',
  ],
};

Solved:
I forgot to set up DocsPage in .storybook/preview.js

@shilman
Copy link
Member

shilman commented Feb 28, 2020

@eden-lane Hmm, the docs preset should set up DocsPage automatically? 🤔

@eden-lane
Copy link

This doc says nothing about preset so I didn't use it (I tried but it didn't work, so I just followed the steps from docs)

@shilman
Copy link
Member

shilman commented Feb 28, 2020

@eden-lane this line is the preset:

addons: ['@storybook/addon-docs'],

@ufollettu
Copy link

ufollettu commented Mar 2, 2020

SOLVED: : upgrading to 5.3.14 solved the problem!

I have a similar issue using web-components and knobs addon. The Canvas tab works fine, but the docs tab pointing to ?path=/docs/zbutton--primary raise the error

Cannot read property 'fromId' of undefined

image

I have this story file:

import { html } from "lit-html";
import { Meta, Story, Preview, Props } from "@storybook/addon-docs/blocks";
import { withKnobs, text, boolean } from "@storybook/addon-knobs";

export const zButtonProps = () => ({
  buttonid: text("buttonid", "id1"),
  label: text("label", "my button"),
  type: text("type", "primary"),
  isdisabled: boolean("isdisabled", false),
  issmall: boolean("issmall", false),
  icon: text("icon", "download")
});

<Meta title="ZButton" component="z-button" decorators={[withKnobs]} />

# ZButton

// With `MDX` we can define a story for `ZButton` right in the middle of our markdown documentation.

<Preview>
  <Story name="primary">
    {html`
      <z-button
        isdisabled="${zButtonProps().isdisabled}"
        label="${zButtonProps().label}"
        buttonid="${zButtonProps().buttonid}"
        type="${zButtonProps().type}"
        issmall="${zButtonProps().issmall}"
        icon="${zButtonProps().icon}"
      ></z-button>
    `}
  </Story>
</Preview>

<Props of="z-button" />

This is the .storybook/main.js file:

module.exports = {
    stories: [
        // files MUST end with .story.mdx or .stories.mdx (@see https://github.com/storybookjs/storybook/issues/9918)
        '../web-components-library/src/components/**/*.story.mdx',
        '../web-components-library/src/components/**/*.stories.mdx',
    ],
    addons: [
        '@storybook/addon-viewport/register',
        '@storybook/addon-knobs/register',
        {
            name: '@storybook/addon-docs',
            options: {
                configureJSX: true,
                babelOptions: {},
                sourceLoaderOptions: null,
            },
        },
    ],
};

And this is the .storybook/preview.js:

import { setCustomElements } from '@storybook/web-components';
import customElements from '../web-components-library/custom-elements.json';

// bound generated Stencil documentation with Props table (https://github.com/storybookjs/storybook/blob/next/addons/docs/web-components/README.md)
setCustomElements(customElements);

This is the relevant part of the custom-elements.json file:

{
  "version": 1.1,
  "tags": [
    {
      "name": "z-button",
      "description": {
        "kind": "markdown",
        "value": "<!-- readme-group=\"buttons\" -->\n\n```html\n<z-button label=\"button\" type=\"primary\"></z-button>\n<z-button label=\"button\" type=\"primary\" icon=\"download\"></z-button>\n<z-button label=\"button\" type=\"primary\" isdisabled icon=\"download\"></z-button>\n<z-button label=\"button\" type=\"secondary\"></z-button>\n<z-button label=\"button\" type=\"secondary\" isdisabled></z-button>\n<z-button label=\"button\" type=\"tertiary\"></z-button>\n<z-button label=\"button\" type=\"tertiary\" isdisabled></z-button>\n<z-button label=\"button\" type=\"primary\" issmall></z-button>\n<z-button label=\"button\" type=\"primary\" issmall icon=\"download\"></z-button>\n<z-button label=\"button\" type=\"primary\" issmall isdisabled icon=\"download\"></z-button>\n<z-button label=\"button\" type=\"secondary\" issmall></z-button>\n<z-button label=\"button\" type=\"secondary\" issmall isdisabled></z-button>\n<z-button label=\"button\" type=\"tertiary\" issmall></z-button>\n<z-button label=\"button\" type=\"tertiary\" issmall isdisabled></z-button>\n```"
      },
      "attributes": [
        {
          "name": "buttonid",
          "description": "id, should be unique"
        },
        {
          "name": "icon",
          "description": "add an icon to button (optional)"
        },
        {
          "name": "isdisabled",
          "description": "disable button"
        },
        {
          "name": "issmall",
          "description": "reduce button size (optional)"
        },
        {
          "name": "label",
          "description": "label content"
        },
        {
          "name": "type",
          "description": "graphic variant"
        }
      ]
    }
]

It seems that the problem is the sourceId:
image

@bcbrian
Copy link

bcbrian commented Jun 9, 2020

@shilman I get this OPs error when I use pnpm switching to yarn or pm the issue goes away. I love the work you all are doing and know this may not be an issue you want to or can address (might be pnpm's issue). If you want me to make an actual issue that addresses this issue and try to solve it, I will. Mostly just dropping this here, so hopefully, someone else can be saved a few days worth of debugging. 😄

@shilman
Copy link
Member

shilman commented Jun 9, 2020

@bcbrian if it's a PNPM issue it could be something to do with react versions. storyStore gets passed in by a react context, and gets consumed by a useContext hook, and I've observed that hooks fail if there are multiple versions of react.

@brandonpittman
Copy link

brandonpittman commented Sep 2, 2020

@shilman Storybook/Angular is using an older version of React compared to the addon-packages. Probably causing the issue for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants