All Projects → atlassian-labs → babel-plugin-storybook-csf-title

atlassian-labs / babel-plugin-storybook-csf-title

Licence: Apache-2.0 license
A Babel plugin to generate titles for Storybook CSF stories at compile time, typically based on the story file's file name.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to babel-plugin-storybook-csf-title

Babel Plugin React Docgen
📝 Babel plugin to add react-docgen info into your code.
Stars: ✭ 156 (+817.65%)
Mutual labels:  babel-plugin, storybook
vue-authoring-template
Vue project template for authoring component and their use case
Stars: ✭ 14 (-17.65%)
Mutual labels:  storybook
storybook-addon-mock
This addon allows you to mock fetch or XMLHttpRequest in the storybook.
Stars: ✭ 67 (+294.12%)
Mutual labels:  storybook
babel-plugin-solid-undestructure
A Babel plugin for SolidJS that allows you to destructure component props without losing reactivity.
Stars: ✭ 45 (+164.71%)
Mutual labels:  babel-plugin
astraea
TypeScript + React Boilerplate
Stars: ✭ 15 (-11.76%)
Mutual labels:  storybook
storybook-webpack-federation-plugin
Exposes all the components in your Storybook as Webpack 5 federated components.
Stars: ✭ 54 (+217.65%)
Mutual labels:  storybook
react-library-starter
A library starter kit and bundler for your React projects, powered by Rollup. ⚡
Stars: ✭ 22 (+29.41%)
Mutual labels:  storybook
lofi
VHS music machine from the 80's
Stars: ✭ 24 (+41.18%)
Mutual labels:  storybook
storybook-graphql-kit
Write GraphQL queries and pass response data to your components
Stars: ✭ 19 (+11.76%)
Mutual labels:  storybook
storybook-talk
Storybook, the playground you need for your UI components! 🎨
Stars: ✭ 11 (-35.29%)
Mutual labels:  storybook
babel-plugin-solid-labels
Simple, reactive labels for SolidJS
Stars: ✭ 127 (+647.06%)
Mutual labels:  babel-plugin
babel-plugin-transform-for-of-as-array
Transform all for-of loops into the equivalent array for loop
Stars: ✭ 14 (-17.65%)
Mutual labels:  babel-plugin
create-material-ui-app
create-react-app + storybook + storybook-addon-material-ui
Stars: ✭ 55 (+223.53%)
Mutual labels:  storybook
twitter-web-react
twitter-web-react.now.sh
Stars: ✭ 104 (+511.76%)
Mutual labels:  storybook
cuida
A design system built by Sysvale, using storybook and Vue components
Stars: ✭ 16 (-5.88%)
Mutual labels:  storybook
gatsby-starter-bulma-storybook
A minimal Gatsby starter which includes Bulma and Storybook
Stars: ✭ 16 (-5.88%)
Mutual labels:  storybook
babel-plugin-feature-flags
A babel transform for managing feature flags
Stars: ✭ 57 (+235.29%)
Mutual labels:  babel-plugin
react-imageViewer
React component for image displaying in full screen
Stars: ✭ 61 (+258.82%)
Mutual labels:  storybook
babel-plugin-transform-rename-properties
A Babel plugin for renaming JavaScript properties
Stars: ✭ 19 (+11.76%)
Mutual labels:  babel-plugin
lerna-starter
Simple React UI Development environment boilerplate to develop, test and publish your React components.
Stars: ✭ 55 (+223.53%)
Mutual labels:  storybook

babel-plugin-storybook-csf-title

Atlassian license PRs Welcome

A Babel plugin to generate titles for Storybook CSF stories at compile time, typically based on the story file's file name.

Usage

The plugin adds a title property to all transformed files, based on the result of a toTitle function that is to be provided as an option to the plugin.

Assuming toTitle: () => 'foo', there are three general scenarios:

1️⃣ The file does not provide a default export

In this scenario, the plugin creates a default export { title: "foo" }.

E.g.,

import React from 'react';
import Component from './index';

export const Example = () => <Component />;

is transformed into

import React from 'react';
import Component from './index';

export const Example = () => <Component />;

export default { title: 'foo' };

2️⃣ The file provides an object as its default export

In this scenario, the plugin adds a title: foo property to the existing export.

E.g.,

import React from 'react';
import Component from './index';

export default { 
    something: 'something'
};

export const Example = () => <Component />;

is transformed into

import React from 'react';
import Component from './index';

export default { 
    title: 'foo'
    something: 'something'
};

export const Example = () => <Component />;

If the existing export already contains a title property, an error is thrown.

3️⃣ The file provides a non-object as its default export

If the renameDefaultExportsTo option is set, the plugin assumes that the default export is a component, and moves this component to a named export of the name ${renameDefaultExportsTo}. It then creates a default export { title: "foo" }.

E.g., assuming renameDefaultExportsTo is "Default",

import React from 'react';
import Component from './index';

export default () => <Component />;

is transformed into

import React from 'react';
import Component from './index';

export const Default = () => <Component />;

export default { 
    title: 'foo'
};

If a ${renameDefaultExportsTo} export already exists, an error is thrown.

Installation

Install the plugin e.g. via yarn;

yarn add --dev babel-plugin-storybook-csf-title

In your Babel configuration, add babel-plugin-storybook-csf-title as a plugin:

plugins: [
    ['babel-plugin-storybook-csf-title', { toTitle: require('./your-to-title-function') }],
]

Note that the plugin really only makes sense for story files. You will want to make sure it is only applied to exactly these files, e.g. like this:

/* add plugin to babel, however disable it by default */
plugins: [
    ['babel-plugin-storybook-csf-title', false], 
],
/* enable the plugin for all files that match your story name pattern */
overrides: [{ 
    include: /\/stories\.(ts|tsx)$/, 
    plugins: [
        ['babel-plugin-storybook-csf-title', { toTitle: require('./your-to-title-function') }]
    ]
}]

Options

The plugin takes three options, toTitle (required), ifTitleFound (optional), and renameDefaultExportsTo (optional):

  • toTitle is a function that, for every story file that is transformed, recieves Babel's state object, and must return the story file's title as a string. Most toTitle implementations will make decisions based on state.filename.

  • ifTitleFound is an optional string value that may either be set to:

    • 'skip' - skips adding a title if it has already been manually specified in the code
    • undefined (or any other value) - raise an error if processing a file that already defines a title
  • renameDefaultExportsTo is an optional string value that controls scenario 3 as described above. It is undefined by default.

Generating meaningful story names

In most cases, the story name will be generated based on the story file's file name. Here's a possible implementation of toTitle for a yarn workspaces-style monorepo setup:

const path = require('path');
const pkgUp = require('pkg-up');

module.exports = (state) => {

    // find the closest package.json
    const packageJsonPath = pkgUp.sync({ cwd: state.filename });

    // read the package.json
    const packageJson = require(packageJsonPath);

    // get the path of the story file relative to the package root
    const { dir: packageJsonDir } = path.parse(packageJsonPath);
    const { dir: fileDir, name: fileName } = path.parse(path.relative(packageJsonDir, state.filename));

    const storybookPath = [
        // package name; "/" has meaning to storybook, hence replace a possible "/" by "|"
        packageJson.name.replace('/', '|'),

        // file dir
        ...fileDir.split(path.sep),
    ];

    // handle file names
    if (fileName === 'examples' || fileName === 'stories') {
        // nothing to do
    } else if (fileName.endsWith('.stories')) {
        storybookPath.push(fileName.slice(0, '.stories'.length + 1));
    } else if (fileName.endsWith('.examples')) {
        storybookPath.push(fileName.slice(0, '.examples'.length + 1));
    }

    return storybookPath.join('/');
}

Contributions

Contributions to babel-plugin-storybook-csf-title are welcome! Please see CONTRIBUTING.md for details.

License

Copyright (c) 2020 Atlassian and others. Apache 2.0 licensed, see LICENSE file.


With ❤️ from Atlassian

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].