2024-10-16 17:18:06 +03:00
|
|
|
import { createContext, useContext, useEffect, useState } from "react";
|
|
|
|
|
import { createPortal } from "react-dom";
|
|
|
|
|
import { motion, AnimatePresence } from "framer-motion";
|
|
|
|
|
import { IoMdArrowRoundBack } from "react-icons/io";
|
2024-09-12 22:39:35 +03:00
|
|
|
import { cn } from "@/lib/utils";
|
|
|
|
|
import { isPWA } from "@/utils/isPWA";
|
2024-10-16 17:18:06 +03:00
|
|
|
import { Button } from "@/components/ui/button";
|
2025-03-16 18:36:20 +03:00
|
|
|
import { useTranslation } from "react-i18next";
|
2024-09-12 22:39:35 +03:00
|
|
|
|
2024-10-16 17:18:06 +03:00
|
|
|
const MobilePageContext = createContext<{
|
2024-09-12 22:39:35 +03:00
|
|
|
open: boolean;
|
|
|
|
|
onOpenChange: (open: boolean) => void;
|
2024-10-16 17:18:06 +03:00
|
|
|
} | null>(null);
|
|
|
|
|
|
|
|
|
|
type MobilePageProps = {
|
|
|
|
|
children: React.ReactNode;
|
|
|
|
|
open?: boolean;
|
|
|
|
|
onOpenChange?: (open: boolean) => void;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export function MobilePage({
|
|
|
|
|
children,
|
|
|
|
|
open: controlledOpen,
|
|
|
|
|
onOpenChange,
|
|
|
|
|
}: MobilePageProps) {
|
|
|
|
|
const [uncontrolledOpen, setUncontrolledOpen] = useState(false);
|
|
|
|
|
|
|
|
|
|
const open = controlledOpen ?? uncontrolledOpen;
|
2024-10-25 15:24:04 +03:00
|
|
|
const setOpen = (value: boolean) => {
|
|
|
|
|
if (onOpenChange) {
|
|
|
|
|
onOpenChange(value);
|
|
|
|
|
} else {
|
|
|
|
|
setUncontrolledOpen(value);
|
|
|
|
|
}
|
|
|
|
|
};
|
2024-10-16 17:18:06 +03:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<MobilePageContext.Provider value={{ open, onOpenChange: setOpen }}>
|
|
|
|
|
{children}
|
|
|
|
|
</MobilePageContext.Provider>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type MobilePageTriggerProps = React.HTMLAttributes<HTMLDivElement>;
|
|
|
|
|
|
|
|
|
|
export function MobilePageTrigger({
|
|
|
|
|
children,
|
|
|
|
|
...props
|
|
|
|
|
}: MobilePageTriggerProps) {
|
|
|
|
|
const context = useContext(MobilePageContext);
|
|
|
|
|
if (!context)
|
|
|
|
|
throw new Error("MobilePageTrigger must be used within MobilePage");
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div onClick={() => context.onOpenChange(true)} {...props}>
|
|
|
|
|
{children}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type MobilePagePortalProps = {
|
|
|
|
|
children: React.ReactNode;
|
|
|
|
|
container?: HTMLElement;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export function MobilePagePortal({
|
|
|
|
|
children,
|
|
|
|
|
container,
|
|
|
|
|
}: MobilePagePortalProps) {
|
|
|
|
|
const [mounted, setMounted] = useState(false);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
setMounted(true);
|
|
|
|
|
return () => setMounted(false);
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
if (!mounted) return null;
|
|
|
|
|
|
|
|
|
|
return createPortal(children, container || document.body);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type MobilePageContentProps = {
|
|
|
|
|
children: React.ReactNode;
|
|
|
|
|
className?: string;
|
2024-09-12 22:39:35 +03:00
|
|
|
};
|
|
|
|
|
|
2024-10-16 17:18:06 +03:00
|
|
|
export function MobilePageContent({
|
|
|
|
|
children,
|
|
|
|
|
className,
|
|
|
|
|
}: MobilePageContentProps) {
|
|
|
|
|
const context = useContext(MobilePageContext);
|
|
|
|
|
if (!context)
|
|
|
|
|
throw new Error("MobilePageContent must be used within MobilePage");
|
|
|
|
|
|
|
|
|
|
const [isVisible, setIsVisible] = useState(context.open);
|
2024-09-12 22:39:35 +03:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2024-10-16 17:18:06 +03:00
|
|
|
if (context.open) {
|
2024-09-12 22:39:35 +03:00
|
|
|
setIsVisible(true);
|
|
|
|
|
}
|
2024-10-16 17:18:06 +03:00
|
|
|
}, [context.open]);
|
2024-09-12 22:39:35 +03:00
|
|
|
|
|
|
|
|
const handleAnimationComplete = () => {
|
2024-10-16 17:18:06 +03:00
|
|
|
if (!context.open) {
|
2024-09-12 22:39:35 +03:00
|
|
|
setIsVisible(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<AnimatePresence>
|
|
|
|
|
{isVisible && (
|
|
|
|
|
<motion.div
|
|
|
|
|
className={cn(
|
2024-09-13 00:06:56 +03:00
|
|
|
"fixed inset-0 z-50 mb-12 bg-background",
|
2024-09-12 22:39:35 +03:00
|
|
|
isPWA && "mb-16",
|
|
|
|
|
"landscape:mb-14 landscape:md:mb-16",
|
2024-10-16 17:18:06 +03:00
|
|
|
className,
|
2024-09-12 22:39:35 +03:00
|
|
|
)}
|
|
|
|
|
initial={{ x: "100%" }}
|
2024-10-16 17:18:06 +03:00
|
|
|
animate={{ x: context.open ? 0 : "100%" }}
|
2024-09-12 22:39:35 +03:00
|
|
|
exit={{ x: "100%" }}
|
|
|
|
|
transition={{ type: "spring", damping: 25, stiffness: 200 }}
|
|
|
|
|
onAnimationComplete={handleAnimationComplete}
|
|
|
|
|
>
|
|
|
|
|
{children}
|
|
|
|
|
</motion.div>
|
|
|
|
|
)}
|
|
|
|
|
</AnimatePresence>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface MobilePageHeaderProps extends React.HTMLAttributes<HTMLDivElement> {
|
2024-10-16 17:18:06 +03:00
|
|
|
onClose?: () => void;
|
2024-09-12 22:39:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function MobilePageHeader({
|
|
|
|
|
children,
|
|
|
|
|
className,
|
|
|
|
|
onClose,
|
|
|
|
|
...props
|
|
|
|
|
}: MobilePageHeaderProps) {
|
2025-03-16 18:36:20 +03:00
|
|
|
const { t } = useTranslation(["common"]);
|
2024-10-16 17:18:06 +03:00
|
|
|
const context = useContext(MobilePageContext);
|
|
|
|
|
if (!context)
|
|
|
|
|
throw new Error("MobilePageHeader must be used within MobilePage");
|
|
|
|
|
|
|
|
|
|
const handleClose = () => {
|
|
|
|
|
if (onClose) {
|
|
|
|
|
onClose();
|
|
|
|
|
} else {
|
|
|
|
|
context.onOpenChange(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-09-12 22:39:35 +03:00
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
className={cn(
|
2024-10-07 16:18:09 +03:00
|
|
|
"sticky -top-2 z-50 mb-2 flex items-center justify-center bg-background p-4",
|
2024-09-12 22:39:35 +03:00
|
|
|
className,
|
|
|
|
|
)}
|
|
|
|
|
{...props}
|
|
|
|
|
>
|
|
|
|
|
<Button
|
|
|
|
|
className="absolute left-0 rounded-lg"
|
2025-03-16 18:36:20 +03:00
|
|
|
aria-label={t("label.back", { ns: "common" })}
|
2024-09-12 22:39:35 +03:00
|
|
|
size="sm"
|
2024-10-16 17:18:06 +03:00
|
|
|
onClick={handleClose}
|
2024-09-12 22:39:35 +03:00
|
|
|
>
|
|
|
|
|
<IoMdArrowRoundBack className="size-5 text-secondary-foreground" />
|
|
|
|
|
</Button>
|
|
|
|
|
<div className="flex flex-row text-center">{children}</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-16 17:18:06 +03:00
|
|
|
type MobilePageTitleProps = React.HTMLAttributes<HTMLHeadingElement>;
|
|
|
|
|
|
|
|
|
|
export function MobilePageTitle({ className, ...props }: MobilePageTitleProps) {
|
|
|
|
|
return <h2 className={cn("text-lg font-semibold", className)} {...props} />;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type MobilePageDescriptionProps = React.HTMLAttributes<HTMLParagraphElement>;
|
|
|
|
|
|
|
|
|
|
export function MobilePageDescription({
|
2024-09-12 22:39:35 +03:00
|
|
|
className,
|
|
|
|
|
...props
|
2024-10-16 17:18:06 +03:00
|
|
|
}: MobilePageDescriptionProps) {
|
2024-09-12 22:39:35 +03:00
|
|
|
return (
|
2024-10-16 17:18:06 +03:00
|
|
|
<p className={cn("text-sm text-muted-foreground", className)} {...props} />
|
2024-09-12 22:39:35 +03:00
|
|
|
);
|
|
|
|
|
}
|