ldc-bien-etre-animal/resources/ts/front/Components/Shared/ContentPanel.tsx

150 lines
4.2 KiB
TypeScript

import { ChevronDown } from "lucide-react";
import { type ReactNode, useState } from "react";
import useDeviceType from "@/hooks/use-device-type";
interface ContentPanelProps {
title?: string;
titleStyle?: string;
children: ReactNode;
bgColor?: string;
rotation?: number;
position?: "left" | "right";
align?: "start" | "end";
edgeOffset?: string;
roundedSize?: string;
maxWidth?: string;
height?: string;
type?: PanelType;
className?: string;
image?: string;
imageClassName?: string;
padding?: string;
noPadding?: boolean;
fullWidth?: boolean;
}
type PanelType = "accordion" | "panel" | "fullWidth" | "sliderImage";
export default function ContentPanel({
title,
titleStyle = "",
children,
bgColor = "bg-bleu",
rotation = -2.22,
position = "right",
align = "start",
edgeOffset,
roundedSize = "100px",
maxWidth = "max-w-[1000px]",
height = "",
type = "panel",
className = "",
image,
imageClassName = "w-[182px] h-[164px] -right-8 -top-16",
padding,
noPadding = false,
fullWidth = false,
}: Readonly<ContentPanelProps>) {
const { isMobile, isTablet } = useDeviceType();
const isCompact = isMobile || isTablet;
const [isOpen, setIsOpen] = useState(false);
const showAccordion = type === "accordion" && isCompact;
const isFullWidth = (type === "fullWidth" || fullWidth) && isCompact;
const isSliderImage = type === "sliderImage";
if (showAccordion) {
return (
<div
className={`mx-auto w-full overflow-hidden rounded-[20px] bg-bleu shadow-[0px_4px_4px_rgba(0,0,0,0.25)] ${
isMobile ? "min-w-auto" : "max-w-none"
} ${className}`}
>
<button
type="button"
onClick={() => {
setIsOpen((prev) => !prev);
}}
aria-expanded={isOpen}
className="flex w-full cursor-pointer items-center justify-between gap-4 p-6"
>
<span className="text-left font-lato-bold text-[24px] leading-7 text-white">
{title}
</span>
<ChevronDown
size={32}
className={`shrink-0 text-white transition-transform duration-300 ${isOpen ? "rotate-180" : ""}`}
/>
</button>
<div
className={`grid transition-all duration-300 ${isOpen ? "grid-rows-[1fr]" : "grid-rows-[0fr]"}`}
>
<div className="overflow-hidden">
<div className="px-6 pb-6 font-poppins-regular text-small text-white">
{children}
</div>
</div>
</div>
</div>
);
}
const roundedStyle =
position === "right"
? {
borderTopRightRadius: roundedSize,
borderBottomRightRadius: roundedSize,
}
: {
borderTopLeftRadius: roundedSize,
borderBottomLeftRadius: roundedSize,
};
const shouldRemovePadding =
isSliderImage || isFullWidth || (noPadding && isMobile);
const paddingClasses =
padding ??
(shouldRemovePadding
? ""
: position === "right"
? "py-12 pl-8 pr-4 md:py-16 md:pl-24 md:pr-20 lg:pl-32"
: "py-12 pl-16 pr-10 md:py-16 md:pl-20 md:pr-24 lg:pr-32");
const defaultOffset = align === "end" ? "-right-3" : "-left-3";
const offsetClass = isFullWidth ? "" : (edgeOffset ?? defaultOffset);
const effectiveRotation = isFullWidth ? 0 : rotation;
const widthClass =
isSliderImage || isFullWidth || fullWidth ? "w-full" : "w-fit";
return (
<div
className={`relative ${widthClass} ${isFullWidth ? "max-w-full" : maxWidth} ${height} ${offsetClass} ${
align === "end" ? "ml-auto" : "mr-auto"
}`}
>
<div
className={`drop-shadow-card absolute inset-0 ${bgColor}`}
style={{
...roundedStyle,
transform: `rotate(${String(effectiveRotation)}deg)`,
}}
/>
<div
className={`relative overflow-hidden ${className} ${paddingClasses} ${align === "end" ? "text-end" : "text-start"} `}
>
{title && <h3 className={`font-lato-bold ${titleStyle}`}>{title}</h3>}
{children}
</div>
{image && (
<img
src={image}
alt=""
className={`pointer-events-none absolute z-10 ${imageClassName}`}
/>
)}
</div>
);
}