I want to be able to hover some component and display with special CSS characteristics. So, for example, if I hover on item A, i only want item A to be distinguished from the other items.
Right now I have this approach changing the element background image, this is my code:
const StatsIndicadores = () => {
const somearray = ['10%', '50%', '60%', '60%']
const [isHovering, setIsHovering] = useState(false)
const [hoverStyle, setHoverStyle] = useState({})
const [index, setIndex] = useState(0)
const mouseEntered = (index) => {
setIsHovering(true);
setIndex(index + 1);
}
const mouseLeft = () => setIsHovering(false)
useEffect(() => {
if (isHovering) {
setHoverStyle({
background: `center url("/images/stats/rounded-images/${index}.png") no-repeat`,
border: '1px solid #f5f5f5',
boxShadow: '0px 0px 10px #f5f5f5',
transition: 'all 0.3s ease-in-out'
});
} else {
setHoverStyle({});
}
}, [isHovering]);
return (
<>
{<Grid container className={style.test}>
{
somearray.map((item, index) => (
<Grid item xs={12} md={3} key={index}>
<div className={style.statsContainer}>
<div className={style.circle} style={hoverStyle} onMouseEnter={(e) => { mouseEntered(index) }} onMouseLeave={mouseLeft}>
<p className={style.circleStats}>Some value</p>
<p className={`${style.circleStats} ${style.circleStatsDescription}`}>Ver más</p>
</div>
<p className={style.indicador}>Some name</p>
</div>
</Grid>
))
}
</Grid>}
</>
)
}
When I hover over some item in the map, this is what I get:

I don't want to display the background image on the other circles, but I don't know how to display it only on the hovered one. I have the idea to compare the index of the hovered item and if that one is active, display the background... but I can't figure which way of doing this would be the best one.
As of now, you're applying the styles to all circles when one of them is hovered. You need to conditionally apply the styles based upon the active hovered circle (you can use any property that's unique -- in the example below, I used an id).
Demo Code:
Code:
import { useState } from "react";
import "./styles.css";
const data = [
{
id: 1,
url: "https://images.dog.ceo/breeds/terrier-american/n02093428_3305.jpg"
},
{
id: 2,
url: "https://images.dog.ceo/breeds/terrier-american/n02093428_10245.jpg"
},
{
id: 3,
url: "https://images.dog.ceo/breeds/terrier-american/n02093428_1345.jpg"
},
{
id: 4,
url: "https://images.dog.ceo/breeds/terrier-american/n02093428_10947.jpg"
},
{
id: 5,
url: "https://images.dog.ceo/breeds/terrier-american/n02093428_5331.jpg"
}
];
export default function App() {
const [activeId, setActiveId] = useState(0);
const setActiveElementOnHover = (id) => {
setActiveId(id);
};
const resetActiveElementOnLeave = () => {
setActiveId(0);
};
return (
<div className="app">
<h1 className="headline">Dog Gallery</h1>
<div className="gallery-container">
{data.map(({ id, url }) => (
<div
key={id}
role="presentation"
className="circle"
style={{
background:
activeId === id ? `center / cover no-repeat url(${url})` : ""
}}
onMouseEnter={() => setActiveElementOnHover(id)}
onMouseLeave={resetActiveElementOnLeave}
>
<p>Dog #{id}</p>
</div>
))}
</div>
</div>
);
}
styles:
.app {
font-family: sans-serif;
text-align: center;
}
.gallery-container {
display: flex;
align-items: center;
justify-content: center;
width: 800px;
margin: 0 auto;
}
.circle {
padding: 75px;
border-radius: 50%;
background: navy;
color: white;
margin: 0 10px;
}
.headline {
font-size: 10rem;
color: navy;
}
p {
padding: 0;
margin: 0;
}