Search code examples
reactjsstorybook

Storybook MDX: Dynamic Meta Title


How can I set a dynamic title property on a Storybook Meta component in a Markdown Extended *.stories.mdx file?

import { Meta } from '@storybook/addon-docs/blocks';

// displays title as 'undefined' in sidebar nav
<Meta title={conditionalValue ? 'foo' : 'bar'} />

I also tried wrapping Meta in a higher-order component, which triggers an error: Unexpected default export without title

import { Meta } from '@storybook/addon-docs/blocks';

export const MetaCustom = (props) => {
  const title = conditionalValue ? props.foo : props.bar;
  return <Meta title={title} />;
};

// mdx stories file
<MetaCustom foo="foo" bar="bar" />

Solution

  • I tried creating a util function and import into the mdx and seems like it is working.

    utils.js

    export function generateTitle (condition) {
       return condition ? 'foo' : 'bar'
    }
    

    Component.stories.mdx

    import { generateTitle } from './utils.js';
    
        <Meta
          title={generateTitle(true)}
        />
    

    Hope that helps!