|
| 1 | +import React, { memo, useMemo } from 'react' |
| 2 | +import type { CSSProperties } from 'react' |
| 3 | +import { useParameter } from 'storybook/manager-api' |
| 4 | + |
| 5 | +import { resolveBadges } from './badges' |
| 6 | +import { BADGES_CONFIG_PARAMETER, BADGES_PARAMETER, TOOL_ID } from './constants' |
| 7 | + |
| 8 | +const containerStyle: CSSProperties = { |
| 9 | + alignItems: 'center', |
| 10 | + display: 'inline-flex', |
| 11 | + gap: '4px', |
| 12 | + marginInline: '6px', |
| 13 | +} |
| 14 | + |
| 15 | +const badgeStyle: CSSProperties = { |
| 16 | + alignItems: 'center', |
| 17 | + border: '1px solid currentColor', |
| 18 | + borderRadius: '3px', |
| 19 | + boxSizing: 'border-box', |
| 20 | + display: 'inline-flex', |
| 21 | + fontWeight: 600, |
| 22 | + minHeight: '20px', |
| 23 | + padding: '2px 6px', |
| 24 | + whiteSpace: 'nowrap', |
| 25 | +} |
| 26 | + |
| 27 | +const emptyBadges: unknown[] = [] |
| 28 | +const emptyBadgesConfig: Record<string, unknown> = {} |
| 29 | + |
| 30 | +export const BadgesTool = memo(function BadgesTool() { |
| 31 | + const badgesParameter = useParameter(BADGES_PARAMETER, emptyBadges) |
| 32 | + const badgesConfigParameter = useParameter(BADGES_CONFIG_PARAMETER, emptyBadgesConfig) |
| 33 | + |
| 34 | + const badges = useMemo( |
| 35 | + () => resolveBadges(badgesParameter, badgesConfigParameter), |
| 36 | + [badgesParameter, badgesConfigParameter], |
| 37 | + ) |
| 38 | + |
| 39 | + if (badges.length === 0) { |
| 40 | + return null |
| 41 | + } |
| 42 | + |
| 43 | + return ( |
| 44 | + <div |
| 45 | + key={TOOL_ID} |
| 46 | + aria-label="Story badges" |
| 47 | + style={containerStyle} |
| 48 | + title={badges.map(({ title }) => title).join(', ')} |
| 49 | + > |
| 50 | + {badges.map(({ id, title, styles }, index) => ( |
| 51 | + <span key={`${id}-${index}`} style={{ ...badgeStyle, ...styles }}> |
| 52 | + {title} |
| 53 | + </span> |
| 54 | + ))} |
| 55 | + </div> |
| 56 | + ) |
| 57 | +}) |
0 commit comments