After configuring my code to display a component in the HTML only if a specific hook is set to true, I encountered an issue with a CSS transition. The problem arises because the 'open' class is triggered at the same time the element becomes true, causing the transition to not work as expected. I've explored timed delays as a solution but they also don't seem to solve the issue.
// Hook for setting state
const [mealOne_box, mealOne_boxSet] = useState(false);
const [box_transition, setbox_transition] = useState(false);
const [scroll, scrollSet] = useState(false);
// Prevent body scrolling behind popup
if (scroll) {
document.body.style.overflow = 'hidden';
} else {
document.body.style.overflow = 'unset';
}
// Handle clicks for popup
const mealOneClickHandler = (event) => {
mealOne_boxSet(!mealOne_box);
scrollSet(!scroll)
setbox_transition(!box_transition)
}
// Popup element
{mealOne_box && (
<div className='meal_popup'>
<div className={box_transition ? 'meal_popupElement open' : 'meal_popupElement'}>
<CancelIcon onClick={mealOneClickHandler} />
<img alt='' src={PancakeImage} />
<div className='text_scroll'>
<h2>Method:</h2>
<p>blablabla</p>
</div>
</div>
<div onClick={mealOneClickHandler} className='meal_popupBackground' />
</div>
)}
// CSS Styles
.meal_popupElement {
position: fixed;
margin: auto;
margin-top: 6%;
width: 95%;
left: 50%;
transform: translateX(-50%);
background: white;
border-radius: 15px;
height: 80%;
box-shadow: 0px 2px 1px -1px rgba(0, 0, 0, 0.2), 0px 1px 1px 0px rgba(0, 0, 0, 0.14),
0px 1px 3px 0px rgba(0, 0, 0, 0.12);
z-index: 2;
overflow: hidden;
p,
h2 {
padding-left: 1rem;
padding-right: 1rem;
font-family: "Open Sans", sans-serif;
}
p {
margin-top: 0;
}
h2 {
margin-bottom: 0.5rem;
font-size: larger;
}
svg {
position: absolute;
display: flex;
margin: 6px;
width: auto;
height: 35px;
color: white;
right: 0;
}
img {
width: 100%;
height: 30%;
object-fit: cover;
object-position: 0% 0%;
}
transition: opacity .25s ease-in-out;
opacity: 0;
}
.meal_popupElement.open {
opacity: 1;
}**