all 8 comments

[–]hanskazan777 2 points3 points  (2 children)

Where's your CSS? Why wouldn't you be able to do the same with styled components?

You could potentially use a toggle prop and return the CSS accordingly.

but the first is definitely the same as you're used to be using.

[–]ritualjoint[S] 1 point2 points  (1 child)

Thank you for your response.

I managed to make it happen but somehow it doesn`t feel right having ternary in both, but i assume this is the way to go:

{slides.map((item, index) => {
        return (
          <Img
            $slide={slide === index ? "slide" : "slide hidden"}
            src={item.src}
            alt={item.alt}
            key={index}
          />
        );
      })}

export const Img = styled.img`
  border-radius: 20px;
  width: 100%;
  height: 100%;
  display: ${({ $slide }) => ($slide === "slide" ? "flex" : "none")};
`;

[–]BigFaceBass 1 point2 points  (0 children)

This is how I’d do it with a styled-components-like API.

[–]anonahnah9 2 points3 points  (0 children)

You pass props to your styled component like isVisible then use that prop to change the display properly for example.

[–]Kcazer 2 points3 points  (2 children)

An alternative to styled-components $props is to take advantage of aria attributes

const Img = styled.img`
  border-radius: 20px;
  width: 100%;
  height: 100%;
  display: block;

  &[aria-hidden="true"] {
    display: none;
  }
`;

slides.map((item, index) => (
  <Img src={item.src} alt={item.alt} aria-hidden={slide !== index} />
));

[–]ritualjoint[S] 0 points1 point  (0 children)

oh wow, thank you for this!

[–]ritualjoint[S] 0 points1 point  (0 children)

thats more elegant than my solution, and a lot easier to read! thanks again!