umami/components/metrics/MetricCard.js

42 lines
1.2 KiB
JavaScript
Raw Normal View History

2020-07-30 05:09:41 +02:00
import { useSpring, animated } from 'react-spring';
import { formatNumber } from 'lib/format';
import styles from './MetricCard.module.css';
const MetricCard = ({
value = 0,
change = 0,
label,
reverseColors = false,
format = formatNumber,
hideComparison = false,
}) => {
2020-09-01 06:11:53 +02:00
const props = useSpring({ x: Number(value) || 0, from: { x: 0 } });
const changeProps = useSpring({ x: Number(change) || 0, from: { x: 0 } });
2020-07-30 05:09:41 +02:00
return (
<div className={styles.card}>
<animated.div className={styles.value}>{props.x.interpolate(x => format(x))}</animated.div>
<div className={styles.label}>
{label}
{~~change !== 0 && !hideComparison && (
<animated.span
className={`${styles.change} ${
change >= 0
? !reverseColors
? styles.positive
: styles.negative
: !reverseColors
? styles.negative
: styles.positive
} ${change >= 0 ? styles.plusSign : ''}`}
>
{changeProps.x.interpolate(x => format(x))}
</animated.span>
)}
</div>
2020-07-30 05:09:41 +02:00
</div>
);
};
export default MetricCard;