Files
tabler/shared/ui/Trending.astro

28 lines
870 B
Plaintext

---
import Icon from './Icon.astro'
interface Props {
/** a number, or a signed string such as "+9.5" that is rendered as written */
value: number | string
unit?: string | undefined
class?: string
}
const { value, unit = '%', class: className } = Astro.props
const amount = Number(value)
if (Number.isNaN(amount)) {
throw new Error(`Trending: value "${value}" is not a number`)
}
const trendingColor = amount > 0 ? 'green' : amount === 0 ? 'muted' : 'red'
const trendingIcon = amount > 0 ? 'arrow-up' : amount === 0 ? 'minus' : 'arrow-down'
const classes = [`text-${trendingColor} d-inline-flex align-items-center lh-1`, className]
---
<span class:list={classes}>
<span class="visually-hidden">{amount > 0 ? 'Increased by' : amount < 0 ? 'Decreased by' : 'No change,'} </span>{value}{unit}
<Icon name={trendingIcon} class="ms-0" size="sm" />
</span>