♻️(react) prevent animate usage on some envs

This animate function is not supported by some testing environments,
to prevent any crashes on consumers side we need to first make sure
this function is defined.

Fixes #281
This commit is contained in:
Nathan Vasse
2024-02-27 18:17:15 +01:00
committed by NathanVss
parent ea39ea09fd
commit 6d1da169e7

View File

@@ -85,27 +85,33 @@ export const ToastProvider = ({ children }: PropsWithChildren) => {
);
const animateContainer = async () => {
if (!container.current) {
return;
}
// FLIP pattern: https://aerotwist.com/blog/flip-your-animations/
// FIRST
const first = previousContainerHeight.current;
// LAST
const last = container.current!.offsetHeight;
const last = container.current.offsetHeight;
// INVERT
const invert = last - first;
// PLAY
container.current!.animate(
[
{ transform: `translateY(${invert}px)` },
{ transform: "translateY(0)" },
],
{
duration: getSlideInDuration(),
easing: "ease",
},
);
// We need to check if the animate function exists because it's not available in some testing environments ( like jest-dom )
if (typeof container.current.animate === "function") {
container.current.animate(
[
{ transform: `translateY(${invert}px)` },
{ transform: "translateY(0)" },
],
{
duration: getSlideInDuration(),
easing: "ease",
},
);
}
};
// We only want this to be triggered when an item gets ADDED to the list, not when