From ede54f2a36aa5a946e7f4c9f9f6fc85bcfb49f4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=E2=80=A6?= Date: Tue, 1 Oct 2024 17:56:53 +0100 Subject: [PATCH] Refactor SideMenu component to update menu animation based on window width --- src/components/SIdeMenu.tsx | 52 +++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/src/components/SIdeMenu.tsx b/src/components/SIdeMenu.tsx index cb5661c..34c8feb 100644 --- a/src/components/SIdeMenu.tsx +++ b/src/components/SIdeMenu.tsx @@ -1,33 +1,45 @@ "use client"; -import React, { useState } from "react"; +import React, { useState, useEffect } from "react"; import Button from "./Button"; import { motion } from "framer-motion"; import Nav from "./Nav"; -const menu = { - open: { - width: "480px", - height: "650px", - top: "-25px", - right: "-25px", - transition: { duration: 0.75, type: "tween", ease: [0.76, 0, 0.24, 1] }, - }, - closed: { - width: "100px", - height: "40px", - top: "0px", - right: "0px", - transition: { duration: 0.75, type: "tween", ease: [0.76, 0, 0.24, 1] }, - }, -}; - const SideMenu = () => { const [isActive, setIsActive] = useState(false); + const [windowWidth, setWindowWidth] = useState(typeof window !== "undefined" ? window.innerWidth : 0); + + // Update windowWidth state on resize + useEffect(() => { + const handleResize = () => { + setWindowWidth(window.innerWidth); + }; + window.addEventListener("resize", handleResize); + return () => window.removeEventListener("resize", handleResize); + }, []); + + // Define the menu animation + const menu = { + open: { + width: windowWidth >= 500 ? "480px" : "90vw", // Use 480px for medium screens and above, 100vw for smaller screens + height: "650px", + top: "-25px", + right: "-25px", + transition: { duration: 0.75, type: "tween", ease: [0.76, 0, 0.24, 1] }, + }, + closed: { + width: "100px", + height: "40px", + top: "0px", + right: "0px", + transition: { duration: 0.75, type: "tween", ease: [0.76, 0, 0.24, 1] }, + }, + }; + return (
- - { isActive &&