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

Storybook: Add badges based on tags #61111

Merged
merged 3 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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' ],
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already had a slip-up here, where the tags weren't present in the metadata so the icon wasn't being displayed in the sidebar.

The automation in this PR will prevent omissions like this.

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' ],
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another slip-up here.

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 >;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Converting this back to our standard format, because the type casting overly complicates the AST parsing we'd have to do. Not worth it right now.

};
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;
};
Loading