-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,8 +21,8 @@ const meta: Meta< typeof CustomSelectControl > = { | |
onChange: { control: { type: null } }, | ||
value: { control: { type: null } }, | ||
}, | ||
tags: [ 'status-wip' ], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: { | ||
|
@@ -34,7 +34,8 @@ export default { | |
controls: { expanded: true }, | ||
docs: { canvas: { sourceState: 'shown' } }, | ||
}, | ||
} as Meta< typeof DimensionControl >; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 } /> | ||
|
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; | ||
}; |
There was a problem hiding this comment.
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.