2022-04-20 19:37:44 +00:00
|
|
|
import React from 'react';
|
|
|
|
import { usePrevious } from './use-previous';
|
|
|
|
import type { BigNumber } from '../lib/bignumber';
|
2022-05-09 15:19:19 +00:00
|
|
|
import { theme as tailwindcss } from '@vegaprotocol/tailwindcss-config';
|
|
|
|
const Colors = tailwindcss.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-06-10 15:07:44 +00:00
|
|
|
{ backgroundColor: Colors.vega.red, color: Colors.white.DEFAULT },
|
2022-05-09 15:19:19 +00:00
|
|
|
{
|
2022-06-10 15:07:44 +00:00
|
|
|
backgroundColor: Colors.vega.red,
|
2022-05-09 15:19:19 +00:00
|
|
|
color: Colors.white.DEFAULT,
|
|
|
|
offset: 0.8,
|
|
|
|
},
|
|
|
|
{
|
2022-07-27 14:30:29 +00:00
|
|
|
backgroundColor: Colors.white[60],
|
2022-05-09 15:19:19 +00:00
|
|
|
color: Colors.white.DEFAULT,
|
|
|
|
},
|
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-06-10 15:07:44 +00:00
|
|
|
backgroundColor: Colors.vega.green,
|
2022-05-09 15:19:19 +00:00
|
|
|
color: Colors.white.DEFAULT,
|
|
|
|
},
|
|
|
|
{
|
2022-06-10 15:07:44 +00:00
|
|
|
backgroundColor: Colors.vega.green,
|
2022-05-09 15:19:19 +00:00
|
|
|
color: Colors.white.DEFAULT,
|
2022-04-20 19:37:44 +00:00
|
|
|
offset: 0.8,
|
|
|
|
},
|
2022-05-09 15:19:19 +00:00
|
|
|
{
|
2022-07-27 14:30:29 +00:00
|
|
|
backgroundColor: Colors.white[60],
|
2022-05-09 15:19:19 +00:00
|
|
|
color: Colors.white.DEFAULT,
|
|
|
|
},
|
2022-04-20 19:37:44 +00:00
|
|
|
],
|
|
|
|
FLASH_DURATION
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|