forked from WordPress/gutenberg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsidebar.js
79 lines (65 loc) · 1.79 KB
/
sidebar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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 } ),
};