-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathTrendArrow.tsx
More file actions
38 lines (34 loc) · 1.38 KB
/
TrendArrow.tsx
File metadata and controls
38 lines (34 loc) · 1.38 KB
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
import parse from 'html-react-parser'
import { TrendArrowType, TREND_ARROW_DOWN, TREND_ARROW_NO_CHANGE } from '../../helpers/trendIndicator'
import { SVG_REGISTRY, SvgRegistryId, getSvgRegistryLabel } from '../../helpers/svgRegistry'
import './trend-arrow.css'
type TrendArrowProps = {
arrowType: TrendArrowType
wrapperClassName?: string
label?: string
}
const TrendArrow = ({ arrowType, wrapperClassName = '', label }: TrendArrowProps) => {
const svgId: SvgRegistryId =
arrowType === TREND_ARROW_DOWN
? 'trend-arrow-down'
: arrowType === TREND_ARROW_NO_CHANGE
? 'trend-arrow-no-change'
: 'trend-arrow-up'
const rawSvg = SVG_REGISTRY[svgId].rawSvg
const trendArrowWrapClasses = ['cove-trend-arrow__wrap', wrapperClassName].filter(Boolean).join(' ')
const trimmedLabel = label?.trim()
const defaultAriaLabel = getSvgRegistryLabel(svgId) || `Trend ${arrowType}`
const iconAccessibilityAttributes = trimmedLabel
? 'aria-hidden="true" focusable="false"'
: `role="img" aria-label="${defaultAriaLabel}" focusable="false"`
const iconMarkup = rawSvg
.trim()
.replace('<svg', `<svg class="cove-trend-arrow" ${iconAccessibilityAttributes}`)
return (
<span className={trendArrowWrapClasses}>
{trimmedLabel && <span className='cove-trend-arrow__label'>{trimmedLabel}</span>}
{parse(iconMarkup)}
</span>
)
}
export default TrendArrow