2022-04-20 19:37:44 +00:00
|
|
|
import React from 'react';
|
|
|
|
import { usePrevious } from './use-previous';
|
|
|
|
import type { BigNumber } from '../lib/bignumber';
|
2022-08-31 04:35:46 +00:00
|
|
|
import { theme } from '@vegaprotocol/tailwindcss-config';
|
|
|
|
import colors from 'tailwindcss/colors';
|
|
|
|
const customColors = theme.colors;
|
2022-04-20 19:37:44 +00:00
|
|
|
|
|
|
|
const FLASH_DURATION = 1200; // Duration of flash animation in milliseconds
|
|
|
|
|
|
|
|
export function useAnimateValue(
|
|
|
|
elRef: React.MutableRefObject<HTMLElement | null>,
|
|
|
|
value?: BigNumber | null
|
|
|
|
) {
|
|
|
|
const shouldAnimate = React.useRef(false);
|
|
|
|
const previous = usePrevious(value);
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
const timeout = setTimeout(() => {
|
|
|
|
shouldAnimate.current = true;
|
|
|
|
}, 800);
|
|
|
|
return () => clearTimeout(timeout);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
if (
|
|
|
|
shouldAnimate.current &&
|
|
|
|
value &&
|
|
|
|
previous &&
|
|
|
|
!value.isEqualTo(previous) &&
|
|
|
|
value.isLessThan(previous)
|
|
|
|
) {
|
|
|
|
elRef.current?.animate(
|
|
|
|
[
|
2022-08-31 04:35:46 +00:00
|
|
|
{ backgroundColor: customColors.vega.red, color: colors.white },
|
2022-05-09 15:19:19 +00:00
|
|
|
{
|
2022-08-31 04:35:46 +00:00
|
|
|
backgroundColor: customColors.vega.red,
|
|
|
|
color: colors.white,
|
2022-05-09 15:19:19 +00:00
|
|
|
offset: 0.8,
|
|
|
|
},
|
|
|
|
{
|
2022-08-31 04:35:46 +00:00
|
|
|
backgroundColor: colors.neutral[500],
|
|
|
|
color: colors.white,
|
2022-05-09 15:19:19 +00:00
|
|
|
},
|
2022-04-20 19:37:44 +00:00
|
|
|
],
|
|
|
|
FLASH_DURATION
|
|
|
|
);
|
|
|
|
} else if (
|
|
|
|
shouldAnimate.current &&
|
|
|
|
value &&
|
|
|
|
previous &&
|
|
|
|
!value.isEqualTo(previous) &&
|
|
|
|
value.isGreaterThan(previous)
|
|
|
|
) {
|
|
|
|
elRef.current?.animate(
|
|
|
|
[
|
|
|
|
{
|
2022-08-31 04:35:46 +00:00
|
|
|
backgroundColor: customColors.vega.green,
|
|
|
|
color: colors.white,
|
2022-05-09 15:19:19 +00:00
|
|
|
},
|
|
|
|
{
|
2022-08-31 04:35:46 +00:00
|
|
|
backgroundColor: customColors.vega.green,
|
|
|
|
color: colors.white,
|
2022-04-20 19:37:44 +00:00
|
|
|
offset: 0.8,
|
|
|
|
},
|
2022-05-09 15:19:19 +00:00
|
|
|
{
|
2022-08-31 04:35:46 +00:00
|
|
|
backgroundColor: colors.neutral[500],
|
|
|
|
color: colors.white,
|
2022-05-09 15:19:19 +00:00
|
|
|
},
|
2022-04-20 19:37:44 +00:00
|
|
|
],
|
|
|
|
FLASH_DURATION
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|