I am implementing my own React SideBar
component. I am able to make it slide in but unable to make it slide out. fade-out
executed but slide-out
didn't. I really have no idea why. Please help! Thanks in advance.
This is the simplified AccountSideBar.tsx
code:
import React, {useState} from 'react';
import './AccountSideBar.css';
interface AccountSideBarProps {
onClose: () => void;
}
const AccountSideBar: React.FC<AccountSideBarProps> = ({ onClose }) => {
const [isClosing, setIsClosing] = useState(false);
const handleClose = () => {
setIsClosing(true);
setTimeout(onClose, 300);
};
return (
<div className={`sidebar-overlay ${isClosing ? 'fade-out' : ''}`} onClick={handleClose}>
<div className={`account-sidebar ${isClosing ? 'slide-out' : ''}`} onClick={(e) => e.stopPropagation()}>
<div>
<button className="close-button" onClick={handleClose}>×</button>
</div>
</div>
</div>
);
};
export default AccountSideBar;
This is the App.css
:
.sidebar-overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
z-index: 1000;
display: flex;
justify-content: flex-end;
animation: fadeIn 0.3s ease-in-out;
}
.fade-out {
animation: fadeOut 0.3s forwards;
}
.slide-out {
animation: slideOut 0.3s forwards;
}
@keyframes fadeIn {
from {
background-color: rgba(0, 0, 0, 0);
}
to {
background-color: rgba(0, 0, 0, 0.5);
}
}
@keyframes fadeOut {
from {
background-color: rgba(0, 0, 0, 0.5);
}
to {
background-color: rgba(0, 0, 0, 0);
}
}
@keyframes slideIn {
from {
transform: translateX(100%);
}
to {
transform: translateX(0);
}
}
@keyframes slideOut {
from {
transform: translateX(0);
}
to {
transform: translateX(100%);
}
}
This is the AccountSideBar.css
:
.account-sidebar {
width: 300px;
height: 100%;
color: #fff;
box-shadow: -2px 0 5px rgba(0, 0, 0, 0.5);
display: flex;
flex-direction: column;
padding: 20px;
animation: slideIn 0.3s ease-in-out;
}
.account-sidebar .close-button {
position: absolute;
top: 10px;
right: 10px;
background: none;
border: none;
color: #fff;
font-size: 36px;
cursor: pointer;
}
---Update---
I made a terrible mistake. To simplify question, I put the code from AccountSideBar.css
and App.css
together, not noticing that this would cause confusion.
The issue you are encountering is related to CSS specificity. Both .account-sidebar
and .slide-out
are of the same selector type, giving them equal specificity. In such cases, the style defined later will override the earlier one. To resolve this, simply move the .slide-out
class below the .account-sidebar
in your CSS, and it should work as expected. like this
.sidebar-overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
z-index: 1000;
display: flex;
justify-content: flex-end;
}
.fade-out {
animation: fadeOut 0.3s forwards;
}
@keyframes fadeIn {
from {
background-color: rgba(0, 0, 0, 0);
}
to {
background-color: rgba(0, 0, 0, 0.5);
}
}
@keyframes fadeOut {
from {
background-color: rgba(0, 0, 0, 0.5);
}
to {
background-color: rgba(0, 0, 0, 0);
}
}
@keyframes slideIn {
from {
transform: translateX(100%);
}
to {
transform: translateX(0);
}
}
@keyframes slideOut {
from {
transform: translateX(0);
}
to {
transform: translateX(100%);
}
}
.account-sidebar {
width: 300px;
height: 100%;
color: #fff;
box-shadow: -2px 0 5px rgba(0, 0, 0, 0.5);
display: flex;
flex-direction: column;
padding: 20px;
animation: slideIn 0.3s ease-in-out;
}
.slide-out {
animation: slideOut 0.3s forwards;
}
.account-sidebar .close-button {
position: absolute;
top: 10px;
right: 10px;
background: none;
border: none;
color: #fff;
font-size: 36px;
cursor: pointer;
}