From cd8928ab54f725a9db06fdbf7e1ff5885a03c437 Mon Sep 17 00:00:00 2001 From: ner Date: Fri, 22 Mar 2024 14:28:49 +0200 Subject: [PATCH] fix: mobile menu --- app/(site)/container.tsx | 47 ++++++++++++------- .../Header/components/Navigation/index.tsx | 8 +++- components/Header/index.tsx | 1 + .../Movie/Preview/components/Actions.tsx | 4 +- components/Movie/Preview/index.tsx | 6 ++- hooks/useMounted.ts | 11 +++++ 6 files changed, 54 insertions(+), 23 deletions(-) create mode 100644 hooks/useMounted.ts diff --git a/app/(site)/container.tsx b/app/(site)/container.tsx index d12edf3..ad6ed6d 100644 --- a/app/(site)/container.tsx +++ b/app/(site)/container.tsx @@ -1,12 +1,14 @@ 'use client'; import { useEffect, useState } from 'react'; +import { createPortal } from 'react-dom'; import { useAtom } from 'jotai'; import { atomWithStorage } from 'jotai/utils'; import { ArrowLeft, ArrowRight, SlidersHorizontal } from 'lucide-react'; import Movie from '@/components/Movie/Preview'; import { Button } from '@/components/ui/button'; +import useMounted from '@/hooks/useMounted'; import { genres } from '@/lib/api'; import getQueryClient from '@/lib/queryClient'; @@ -19,6 +21,7 @@ export default function Container() { const [isShowFilter, setIsShowFilter] = useAtom(isShowFilterAtom); const { movie, next, previous, hasPrevious } = useRandomMovie(); const [direction, setDirection] = useState(0); + const isMounted = useMounted(); useEffect(() => { const queryClient = getQueryClient(); @@ -34,23 +37,33 @@ export default function Container() { return ( <> {isShowFilter && } -
- - - -
- + +
+ + {isMounted && + createPortal( + , + document.getElementById('actions')! + )} + +
+
); diff --git a/components/Header/components/Navigation/index.tsx b/components/Header/components/Navigation/index.tsx index 9decc42..7445052 100644 --- a/components/Header/components/Navigation/index.tsx +++ b/components/Header/components/Navigation/index.tsx @@ -1,5 +1,6 @@ 'use client'; +import { useState } from 'react'; import { Menu } from 'lucide-react'; import Link from 'next/link'; import { usePathname } from 'next/navigation'; @@ -31,11 +32,12 @@ interface Props { } export function NavigationSmall({ className }: Props) { + const [isOpen, setIsOpen] = useState(false); const currentPath = usePathname(); return (
- +
+ {children &&
{children}
}
diff --git a/hooks/useMounted.ts b/hooks/useMounted.ts new file mode 100644 index 0000000..19c69eb --- /dev/null +++ b/hooks/useMounted.ts @@ -0,0 +1,11 @@ +import { useLayoutEffect, useState } from 'react'; + +export default function useMounted() { + const [isMounted, setIsMounted] = useState(false); + + useLayoutEffect(() => { + setIsMounted(true); + }, []); + + return isMounted; +}