Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using Css separately at styled-components

I made a code using styled-components.

Used map function for making repeated components and it worked well.

But I want to use ‘clr’ in the ‘cardProperty’ array for color. So I need to deliever ‘clr’ to styled-components. But don’t know how to deliver it and which way would be effective.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Question:
How can I use ‘clr’ in the ‘h2’ tag separately using styled-components?

code:

import styled from "styled-components";

const cardProperty = [
  {
    clr: "#009688",
    name: "Sea",
    img: "img1.jpg",
  },
  {
    clr: "#03a9f4",
    name: "Sky",
    img: "img2.jpg",
  },
  {
    clr: "#ff3e7f",
    name: "Mountain",
    img: "img3.jpg",
  },
];

export default function App() {
  return (
    <Wrap>
      <div className="container">
        {cardProperty.map((idx) => {
          return (
            <div className="card">
              <div className="imgBx">
                <img src={idx.img} />
              </div>
              <div className="content">
                <h2>{idx.name}</h2>
                <p>
                  Lorem, ipsum dolor sit amet consectetur adipisicing elit. Ab
                  commodi officia aspernatur error. Veritatis quisquam rem quis
                  ut, necessitatibus aut aperiam adipisci odio voluptatem quos
                  totam esse sit dolorem incidunt?
                </p>
                <a href="#">Read More</a>
              </div>
            </div>
          );
        })}
      </div>
    </Wrap>
  );
}

const Wrap = styled.div`
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: "Poppins", sans-serif;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(45deg, #00bcd4, #ffeb3b);

  .container {
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-wrap: wrap;
    gap: 100px 50px;
    padding: 100px 50px;
  }

  .container .card {
    position: relative;
    display: flex;
    justify-content: center;
    align-items: flex-start;
    width: 350px;
    height: 300px;
    background: #fff;
    border-radius: 20px;
    box-shadow: 0 35px 80px rgba(0, 0, 0, 0.15);
    transition: 0.5s;
  }

  .container .card:hover {
    height: 400px;
  }

  .container .card .imgBx {
    position: absolute;
    top: 20px;
    width: 300px;
    height: 220px;
    background-color: #333;
    border-radius: 12px;
    overflow: hidden;
    transition: 0.5s;
  }

  .container .card:hover .imgBx {
    top: -100px;
    scale: 0.75;
    box-shadow: 0 15px 45px rgba(0, 0, 0, 0.2);
  }

  .container .card .imgBx img {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
  }

  .container .card .content {
    position: absolute;
    width: 100%;
    padding: 0 30px;
    text-align: center;
    top: 252px;
    height: 40px;
    overflow: hidden;
    transition: 0.5s;
  }

  .container .card:hover .content {
    top: 130px;
    height: 250px;
  }

  .container .card .content h2 {
    font-size: 2rem;
    font-weight: bold;
    margin-bottom: 1rem;
    text-transform: capitalize;
    color: // want to use it here
  }
`;

CodeSandBox
CodeSandBox

>Solution :

If I understand your question you can try passing color props to styled-component.
Something like this:

const Typography = styled.h2`
  font-size: 1em;
  line-height: 24px;
  color: ${(props) => props.color}; // dynamic color prop
`;

Typography.defaultProps = {
  color: "#000000"; // default color if nothing is passed
};


const cardProperty = [
  {
    clr: "#009688",
    name: "Sea",
    img: "img1.jpg",
  },
  {
    clr: "#03a9f4",
    name: "Sky",
    img: "img2.jpg",
  },
  {
    clr: "#ff3e7f",
    name: "Mountain",
    img: "img3.jpg",
  },
];

export default function App() {
  return (
    <>
        {cardProperty.map((idx) => {
          return (
            <div className="card">
              <div className="imgBx">
                <img src={idx.img} />
              </div>
              <div className="content">
                <Typography color={idx.clr}>{idx.name}</Typography>

                <p>
                  Lorem, ipsum dolor sit amet consectetur adipisicing elit. Ab commodi officia aspernatur error.
                  Veritatis quisquam rem quis ut, necessitatibus aut aperiam adipisci odio voluptatem quos totam esse
                  sit dolorem incidunt?
                </p>
                <a href="#">Read More</a>
              </div>
            </div>
          );
        })}
      </>
  );
}

Also if you wish to use this component as global dynamic typography you can do this:

// typography.js
const Typography = styled(props => <props.tag {...props}>{props.children}</props.tag>)`
  font-size: ${(props) => props.size};
  color: ${(props) => props.color};
`;

Typography.defaultProps = {
  color: "red",
size:'20px'
};

// Usage in component
<Typography tag="h1" color="#000">Typography test color</Typography>
<Typography tag="p" color="red">Typography test color</Typography>

Hope this helps 🙂

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading