movie-web/src/components/Transition.tsx

67 lines
1.8 KiB
TypeScript
Raw Normal View History

2023-02-07 20:03:01 +01:00
import { Fragment, ReactNode } from "react";
2023-02-07 18:19:31 +01:00
import {
Transition as HeadlessTransition,
TransitionClasses,
} from "@headlessui/react";
2023-02-04 01:01:54 +01:00
2023-02-07 18:19:31 +01:00
type TransitionAnimations = "slide-down" | "slide-up" | "fade";
2023-02-04 01:01:54 +01:00
interface Props {
show: boolean;
durationClass?: string;
2023-02-04 01:01:54 +01:00
animation: TransitionAnimations;
className?: string;
children?: ReactNode;
}
function getClasses(
animation: TransitionAnimations,
duration: string
2023-02-07 18:19:31 +01:00
): TransitionClasses {
2023-02-04 01:01:54 +01:00
if (animation === "slide-down") {
return {
2023-02-07 20:03:01 +01:00
leave: `transition-[transform,opacity] ${duration}`,
2023-02-07 18:19:31 +01:00
leaveFrom: "opacity-100 translate-y-0",
leaveTo: "-translate-y-4 opacity-0",
2023-02-07 20:03:01 +01:00
enter: `transition-[transform,opacity] ${duration}`,
2023-02-07 18:19:31 +01:00
enterFrom: "opacity-0 -translate-y-4",
enterTo: "translate-y-0 opacity-100",
2023-02-04 01:01:54 +01:00
};
}
if (animation === "slide-up") {
return {
2023-02-07 20:03:01 +01:00
leave: `transition-[transform,opacity] ${duration}`,
2023-02-07 18:19:31 +01:00
leaveFrom: "opacity-100 translate-y-0",
leaveTo: "translate-y-4 opacity-0",
2023-02-07 20:03:01 +01:00
enter: `transition-[transform,opacity] ${duration}`,
2023-02-07 18:19:31 +01:00
enterFrom: "opacity-0 translate-y-4",
enterTo: "translate-y-0 opacity-100",
2023-02-04 01:01:54 +01:00
};
}
if (animation === "fade") {
return {
2023-02-07 20:03:01 +01:00
leave: `transition-[transform,opacity] ${duration}`,
2023-02-07 18:19:31 +01:00
leaveFrom: "opacity-100",
leaveTo: "opacity-0",
2023-02-07 20:03:01 +01:00
enter: `transition-[transform,opacity] ${duration}`,
2023-02-07 18:19:31 +01:00
enterFrom: "opacity-0",
enterTo: "opacity-100",
};
}
2023-02-04 01:01:54 +01:00
return {};
}
export function Transition(props: Props) {
const duration = props.durationClass ?? "duration-200";
2023-02-07 20:03:01 +01:00
const classes = getClasses(props.animation, duration);
2023-02-04 01:01:54 +01:00
return (
2023-02-07 20:03:01 +01:00
<HeadlessTransition show={props.show} as={Fragment} {...classes}>
<div className={props.className}>{props.children}</div>
</HeadlessTransition>
2023-02-04 01:01:54 +01:00
);
}