-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Components Storybook: Show badges in sidebar (#58518)
Co-authored-by: andrewhayward <[email protected]> Co-authored-by: ciampo <[email protected]> Co-authored-by: mirka <[email protected]>
- Loading branch information
1 parent
26deca1
commit 0e0fd19
Showing
9 changed files
with
118 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* Provides badge configuration options. | ||
* See https://github.com/geometricpanda/storybook-addon-badges | ||
*/ | ||
|
||
export default { | ||
private: { | ||
icon: '🔒', | ||
title: '🔒 Private', | ||
tooltip: { | ||
title: 'Component is locked as a private API', | ||
desc: 'We do not yet recommend using this outside of the Gutenberg codebase.', | ||
links: [ | ||
{ | ||
title: 'About @wordpress/private-apis', | ||
href: 'https://developer.wordpress.org/block-editor/reference-guides/packages/packages-private-apis/', | ||
}, | ||
], | ||
}, | ||
}, | ||
wip: { | ||
icon: '🚧', | ||
title: '🚧 WIP', | ||
styles: { backgroundColor: '#FFF0BD' }, | ||
tooltip: { | ||
title: 'Component is a work in progress', | ||
desc: 'This component is not ready for use in production, including the Gutenberg codebase. DO NOT export outside of @wordpress/components.', | ||
}, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/** | ||
* Provides sidebar configuration options. | ||
* See https://storybook.js.org/docs/configure/features-and-behavior | ||
*/ | ||
|
||
/** | ||
* External dependencies | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-restricted-imports | ||
import { createElement, useMemo } from 'react'; | ||
import { useStorybookApi } from '@storybook/manager-api'; | ||
import { styled } from '@storybook/theming'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import badges from './badges'; | ||
|
||
const Wrapper = styled.span( { | ||
flexGrow: 1, | ||
display: 'flex', | ||
paddingRight: '20px', | ||
} ); | ||
|
||
const Title = styled.span( { | ||
flexGrow: 1, | ||
} ); | ||
|
||
const Icons = styled.span( {} ); | ||
|
||
const Icon = styled.span( {} ); | ||
|
||
/** | ||
* Fetches tags from the Storybook API, and returns Icon | ||
* elements for any that have matching badge data | ||
*/ | ||
function useIcons( item ) { | ||
const api = useStorybookApi(); | ||
const prefix = 'status-'; | ||
|
||
return useMemo( () => { | ||
let data = {}; | ||
|
||
if ( item.isComponent && item.children?.length ) { | ||
data = api.getData( item.children[ 0 ] ) ?? {}; | ||
} | ||
|
||
const { tags = [] } = data; | ||
|
||
return tags | ||
.filter( ( tag ) => tag.startsWith( prefix ) ) | ||
.map( ( tag ) => badges[ tag.substring( prefix.length ) ] ) | ||
.map( ( { icon, title, tooltip } ) => | ||
icon | ||
? createElement( | ||
Icon, | ||
{ title: tooltip?.title ?? title }, | ||
icon | ||
) | ||
: null | ||
); | ||
}, [ api, item.children, item.isComponent ] ); | ||
} | ||
|
||
/** | ||
* Renders the item name and any associated badge icons. | ||
*/ | ||
function Label( { item } ) { | ||
const iconSet = useIcons( item ); | ||
const title = createElement( Title, {}, item.name ); | ||
const icons = createElement( Icons, { 'aria-hidden': true }, ...iconSet ); | ||
|
||
return createElement( Wrapper, {}, title, icons ); | ||
} | ||
|
||
export default { | ||
// Renders status icons for items tagged with `status-*` | ||
renderLabel: ( item ) => createElement( Label, { item } ), | ||
}; |