Header 에 위치할 Navigation 을 만들어보자!
motion.svg
로 스타일 부여하기// -Header.tsx
<Logo
variants={logoVariants}
initial="normal"
whileHover="active"
xmlns="http://www.w3.org/2000/svg"
width="1024"
height="276.742"
viewBox="0 0 1024 276.742"
>
<motion.path d="M140....295" />
</Logo>
variants
를 설정하고,function Header() {
// NAV 애니메이션을 위한 스크롤 감지
const navAnimation = useAnimation();
const { scrollY } = useScroll();
useEffect(() => {
scrollY.onChange(() => {
if (scrollY.get() > 50) {
navAnimation.start("scroll");
} else {
navAnimation.start("top");
}
});
}, [scrollY, navAnimation]);
//---------Variants
const navVariants = {
top: {
backgroundColor: "rgba(0,0,0,0)",
},
scroll: {
backgroundColor: "rgba(20,20,20,1)",
},
};
useMatch()
함수를 사용했다.const ItemCircle = styled(motion.span)`
position: absolute;
width: 5px;
height: 5px;
background-color: ${props => props.theme.red};
border-radius: 5px;
bottom: -10px;
left: 0;
right: 0;
margin: 0 auto;
`;
function Header() {
// 현재 URL 위치 확인
const homeMatch = useMatch("/");
const tvMatch = useMatch("tv");
const searchMatch = useMatch("search");
...
return (
...
<Items>
<Item>
<Link to="/">
Home {homeMatch && <ItemCircle layoutId="circle" />}
</Link>
</Item>
<Item>
<Link to="tv">
Series {tvMatch && <ItemCircle layoutId="circle" />}
</Link>
</Item>
<Item>
<Link to="search">
Search {searchMatch && <ItemCircle layoutId="circle" />}
</Link>
</Item>
</Items>
...
function Header() {
const { register, handleSubmit, setValue } = useForm<IForm>();
const navigate: NavigateFunction = useNavigate();
const onValid = ({ keyword }: IForm) => {
navigate(`/search?keyword=${keyword}`);
setValue("keyword", "");
};
return (
...
<Search onSubmit={handleSubmit(onValid)}>
<motion.svg
onClick={toggleSearch}
animate={{ x: searchOpen ? -200 : 0 }}
transition={{ type: "linear" }}
fill="currentColor"
viewBox="0 0 20 20"
xmlns="http://www.w3.org/2000/svg"
>
<path fillRule="evenodd" ...></path>
<Input
animate={inputAnimation}
initial={{ scaleX: 0 }}
transition={{ type: "linear" }}
placeholder="Title, Person, Genre"
{...register("keyword", { required: true, minLength: 2 })}
/>
</Search>
input
입력창이 나오도록 할것이기에 애니메이션도 추가!// 검색창 오픈 여부에 따른 애니메이션
const inputAnimation = useAnimation();
const toggleSearch = () => {
if (searchOpen) {
inputAnimation.start({
scaleX: 0,
});
} else {
inputAnimation.start({
scaleX: 1,
});
}
setSearchOpen(prev => !prev);
};
framer motion을 사용한 네비게이션바 완성❤️