Created
July 6, 2022 23:21
-
-
Save yaboynoem/cfbc17a2939f24da8cc30b863ff0f677 to your computer and use it in GitHub Desktop.
animated sidebar on clicking hamburger using react and framer motion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { ReactElement } from "react" | |
import { motion } from "framer-motion" | |
import { useSelector } from "react-redux" | |
//Components | |
import SideBar from "../SideBar/SideBar" | |
//Constants | |
import { EColor } from "Constants/Colors" | |
//Stores | |
import { store } from "Stores" | |
import { setIsSideBarOpen } from "Stores/App/Creators" | |
//Styles | |
import { SHamburgerBox, SHamburgerContainer, SHamburgerPatty } from "./Hamburger.styles" | |
//Types | |
import { TRootState } from "Types/GlobalTypes" | |
const Hamburger: React.FC = (): ReactElement => { | |
const { darkMode, isSideBarOpen } = useSelector((state: TRootState) => state.app) | |
return ( | |
<SHamburgerContainer> | |
<SHamburgerBox | |
as={motion.div} | |
animate={{ | |
backgroundColor: darkMode ? EColor.CONTAINER_DARK : EColor.CONTAINER, | |
border: `2px solid ${darkMode ? EColor.CONTAINER : EColor.CONTAINER_DARK}`, | |
}} | |
onClick={() => store.dispatch(setIsSideBarOpen(!isSideBarOpen))} | |
> | |
<SHamburgerPatty | |
as={motion.div} | |
style={{ | |
originX: "right", | |
originY: "center", | |
}} | |
animate={{ | |
backgroundColor: darkMode ? EColor.CONTAINER : EColor.CONTAINER_DARK, | |
y: isSideBarOpen ? 8 : 0, | |
width: isSideBarOpen ? 12 : 22.5, | |
marginLeft: isSideBarOpen ? 11.5 : 0, | |
rotate: isSideBarOpen ? 45 : 0, | |
transition: { | |
y: { | |
duration: 0.25, | |
type: "spring", | |
delay: isSideBarOpen ? 0 : 0.25, | |
}, | |
width: { | |
duration: 0.25, | |
type: "spring", | |
delay: isSideBarOpen ? 0.25 : 0, | |
}, | |
marginLeft: { | |
duration: 0.25, | |
type: "spring", | |
delay: isSideBarOpen ? 0.25 : 0, | |
}, | |
rotate: { | |
duration: 0.25, | |
type: "spring", | |
delay: isSideBarOpen ? 0.25 : 0, | |
}, | |
}, | |
}} | |
/> | |
<SHamburgerPatty | |
as={motion.div} | |
animate={{ | |
backgroundColor: darkMode ? EColor.CONTAINER : EColor.CONTAINER_DARK, | |
}} | |
/> | |
<SHamburgerPatty | |
as={motion.div} | |
style={{ | |
originX: "right", | |
originY: "center", | |
}} | |
animate={{ | |
backgroundColor: darkMode ? EColor.CONTAINER : EColor.CONTAINER_DARK, | |
y: isSideBarOpen ? -8 : 0, | |
width: isSideBarOpen ? 12 : 22.5, | |
marginLeft: isSideBarOpen ? 11.5 : 0, | |
rotate: isSideBarOpen ? -45 : 0, | |
transition: { | |
y: { | |
duration: 0.25, | |
type: "spring", | |
delay: isSideBarOpen ? 0 : 0.25, | |
}, | |
width: { | |
duration: 0.25, | |
type: "spring", | |
delay: isSideBarOpen ? 0.25 : 0, | |
}, | |
marginLeft: { | |
duration: 0.25, | |
type: "spring", | |
delay: isSideBarOpen ? 0.25 : 0, | |
}, | |
rotate: { | |
duration: 0.25, | |
type: "spring", | |
delay: isSideBarOpen ? 0.25 : 0, | |
}, | |
}, | |
}} | |
/> | |
</SHamburgerBox> | |
//Add this SideBar in the SHamburgerContainer | |
<SideBar /> | |
</SHamburgerContainer> | |
) | |
} | |
export default Hamburger |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment