Skip to content

Commit

Permalink
Storybook: Add badges based on tags (#61111)
Browse files Browse the repository at this point in the history
* Consolidate tags in stories

* DimensionControl: Standardize story meta format for easier parsing

* Storybook: Add badges based on `tags`

Co-authored-by: mirka <0mirka00@git.wordpress.org>
Co-authored-by: tyxla <tyxla@git.wordpress.org>
  • Loading branch information
3 people authored May 16, 2024
1 parent e357a62 commit 48422a4
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ const meta: Meta< typeof UseCompositeStorePlaceholder > = {
// @ts-expect-error - See https://github.com/storybookjs/storybook/issues/23170
CompositeItem,
},
tags: [ 'status-private' ],
parameters: {
badges: [ 'private' ],
docs: {
canvas: { sourceState: 'shown' },
source: { transform },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ const meta: Meta< typeof CustomSelectControlV2 > = {
},
tags: [ 'status-wip' ],
parameters: {
badges: [ 'wip' ],
actions: { argTypesRegex: '^on.*' },
controls: { expanded: true },
docs: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ const meta: Meta< typeof CustomSelectControl > = {
onChange: { control: { type: null } },
value: { control: { type: null } },
},
tags: [ 'status-wip' ],
parameters: {
badges: [ 'wip' ],
actions: { argTypesRegex: '^on.*' },
controls: { expanded: true },
docs: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import sizes from '../sizes';
*/
import { desktop, tablet, mobile } from '@wordpress/icons';

export default {
const meta: Meta< typeof DimensionControl > = {
component: DimensionControl,
title: 'Components (Experimental)/DimensionControl',
argTypes: {
Expand All @@ -34,7 +34,8 @@ export default {
controls: { expanded: true },
docs: { canvas: { sourceState: 'shown' } },
},
} as Meta< typeof DimensionControl >;
};
export default meta;

const Template: StoryFn< typeof DimensionControl > = ( args ) => (
<DimensionControl { ...args } />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ const meta: Meta< typeof DropdownMenu > = {
tags: [ 'status-private' ],
parameters: {
actions: { argTypesRegex: '^on.*' },
badges: [ 'private' ],
controls: { expanded: true },
docs: {
canvas: { sourceState: 'shown' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const meta: Meta< typeof ProgressBar > = {
},
tags: [ 'status-private' ],
parameters: {
badges: [ 'private' ],
controls: {
expanded: true,
},
Expand Down
1 change: 0 additions & 1 deletion packages/components/src/tabs/stories/index.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ const meta: Meta< typeof Tabs > = {
tags: [ 'status-private' ],
parameters: {
actions: { argTypesRegex: '^on.*' },
badges: [ 'private' ],
controls: { expanded: true },
docs: { canvas: { sourceState: 'shown' } },
},
Expand Down
1 change: 0 additions & 1 deletion packages/components/src/theme/stories/index.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const meta: Meta< typeof Theme > = {
},
tags: [ 'status-private' ],
parameters: {
badges: [ 'private' ],
controls: { expanded: true },
docs: { canvas: { sourceState: 'shown' } },
},
Expand Down
8 changes: 7 additions & 1 deletion storybook/badges.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
/**
* Provides badge configuration options.
* See https://github.com/geometricpanda/storybook-addon-badges
*
* To apply a badge to a story, add a badge identifier prefixed by "status-" to
* the `tags` array in the story's metadata. For example, to apply the "private"
* badge, add "status-private" to the `tags` array.
*
* @see https://github.com/geometricpanda/storybook-addon-badges
* @see './webpack/copy-tags-to-badges.js' - Webpack loader that processes the tags
*/

export default {
Expand Down
21 changes: 16 additions & 5 deletions storybook/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,23 @@ module.exports = {
rules: [
...config.module.rules,
{
// Adds a `sourceLink` parameter to the story metadata, based on the file path
test: /\/stories\/.+\.story\.(j|t)sx?$/,
loader: path.resolve(
__dirname,
'./webpack/source-link-loader.js'
),
use: [
{
// Adds a `sourceLink` parameter to the story metadata, based on the file path
loader: path.resolve(
__dirname,
'./webpack/source-link-loader.js'
),
},
{
// Reads `tags` from the story metadata and copies them to `badges`
loader: path.resolve(
__dirname,
'./webpack/copy-tags-to-badges.js'
),
},
],
enforce: 'post',
},
{
Expand Down
71 changes: 71 additions & 0 deletions storybook/webpack/copy-tags-to-badges.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* External dependencies
*/
const babel = require( '@babel/core' );

/**
* Reads `tags` from the story metadata and copies them to `badges`.
*
* @see https://github.com/geometricpanda/storybook-addon-badges
* @see '../badges.js' - Badges config
*/
function copyTagsToBadgePlugin() {
return {
visitor: {
ExportDefaultDeclaration( visitorPath ) {
const properties =
// When default export is anonymous, the declaration is an object expression
visitorPath.node?.declaration.properties ??
// When default export is named, the declaration is an identifier, usually the previous node
visitorPath.getPrevSibling().node.declarations[ 0 ].init
.properties;

alterParameters( properties );
},
},
};
}

function alterParameters( properties ) {
const STATUS_PREFIX = 'status-';

const tags =
properties
.find( ( op ) => op.key.name === 'tags' )
?.value.elements.map( ( tag ) => tag.value ) ?? [];
const statusTags = tags.filter( ( tag ) =>
tag.startsWith( STATUS_PREFIX )
);

const badges = babel.types.objectProperty(
babel.types.identifier( 'badges' ),
babel.types.arrayExpression(
statusTags.map( ( tag ) =>
babel.types.stringLiteral(
tag.substring( STATUS_PREFIX.length )
)
)
)
);

let parameters = properties.find( ( op ) => op.key.name === 'parameters' );

if ( ! parameters ) {
parameters = babel.types.objectProperty(
babel.types.identifier( 'parameters' ),
babel.types.objectExpression( [] )
);
properties.push( parameters );
}

parameters.value.properties = [ badges, ...parameters.value.properties ];
}

module.exports = function ( source ) {
const output = babel.transform( source, {
plugins: [ copyTagsToBadgePlugin ],
filename: this.resourcePath,
sourceType: 'module',
} );
return output.code;
};

0 comments on commit 48422a4

Please sign in to comment.