뒤로
현우
·
안녕하세요 코드 질문 합니다 !
import React, { useState } from "react";
import "../styles/Category.css";
import logo from "../assets/logo.png";
const Category = () => {
const [isSidebarOpen, setSidebarOpen] = useState(false);
const toggleSidebar = () => {
setSidebarOpen((prevState) => !prevState);
};
return (
<>
<div className="header">
<img src={logo} alt="Logo" className="logo" />
<button className="menu-button" onClick={toggleSidebar}>
=
</button>
</div>
<div className={`sidebar ${isSidebarOpen ? "open" : ""}`}>
<button className="close-button" onClick={toggleSidebar}>
X
</button>
<div className="sidebar-content">
<h1 className="category-item">회원가입</h1>
<h1 className="category-item">캘린더</h1>
<h1 className="category-item">커뮤니티</h1>
<h1 className="category-item">마이페이지</h1>
</div>
</div>
<div
className={`overlay ${isSidebarOpen ? "visible" : ""}`}
onClick={toggleSidebar}
></div>
</>
);
};
export default Category;.header {
display: flex;
align-items: center;
padding: 10px;
}
.logo {
width: 200px;
height: auto;
padding-top: 20px;
cursor: pointer;
}
.menu-button {
font-size: 30px;
background: none;
border: none;
cursor: pointer;
margin-left: auto;
padding-right: 20px;
}
.sidebar {
position: fixed;
top: 0;
right: -300px;
width: 300px;
height: 100%;
background-color: #fff;
box-shadow: -2px 0 5px rgba(0, 0, 0, 0.5);
transition: right 0.3s ease;
z-index: 1000;
}
.sidebar.open {
right: 0;
}
.close-button {
font-size: 30px;
background: none;
border: none;
cursor: pointer;
position: absolute;
top: 10px;
right: 10px;
}
.sidebar-content {
padding: 20px;
opacity: 0;
transform: translateX(20px);
transition: opacity 0.3s ease, transform 0.3s ease;
}
.sidebar.open .sidebar-content {
opacity: 1;
transform: translateX(0);
}
.category-item {
margin: 15px 0;
font-size: 18px;
color: #999;
font-family: "Malgun Gothic", sans-serif;
cursor: pointer;
transition: color 0.3s ease, font-weight 0.3s ease;
}
.category-item:hover {
color: black;
font-weight: bold;
}
.overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.5);
opacity: 0;
visibility: hidden;
transition: opacity 0.3s ease;
z-index: 999;
}
.overlay.visible {
opacity: 1;
visibility: visible;
}
리액트 공부하고 있는데 x 버튼을 누르면 사이드바가 닫혀야 되는데 코드 잘 작성한거 같은데 뭐가 문제인지 잘 모르겠어요,, 혹시 코드 한번 봐주실수 있나요 ?
1
댓글
로그인 후 댓글을 남길 수 있습니다.
안녕하세요 현우님! 해당 코드를 봤을 때, css의 z-index가 문제로 보입니다. React 코드는 크게 문제없어 보이며, close 버튼의 z-index를 높여주고, 보통 버튼 같은 경우에는 cursor: pointer를 주는게 더 좋을 것 같습니다. 그리고 한 가지 더 정보를 드리자면 해당 코드는 메뉴의 상태를 관리하는 것으로 보이는데, 보통 프론트엔드에서 메뉴의 상태는 제일 상단의 상태관리에서 관리하는 것이 좋습니다. recoil, redux, context api 라는 것을 검색 하시고 적용해보시길 권해드립니다. 화이팅 하십시요. 감사합니다.😎😎 .close-button { z-index: 1001; // 추가 cursor: pointer; // 추가 font-size: 30px; background: none; border: none; cursor: pointer; position: absolute; top: 10px; right: 10px; }