Adjust the line height for each font size using a styled system for managers

One useful feature of Styled components is the compose utility, which allows for combining multiple Styled System functions within a single component. My goal is to merge the lineHeight and fontSize properties based on the default array.

Rather than coding it this way:

  <Heading type="h1"
     fontSize={[ 1, 2, 3 ]}
     lineHeight={[1 , 2, 3 ]}
     color={theme.colors.heading.tinted}
  >

I aim to consolidate these into a single font property that includes both fontSize and lineHeight values.

<Heading type="h1"
     font={[ 1, 2, 3 ]}
     color={theme.colors.heading.tinted}
  >

In Heading.jsx:

const Element = styled('div')(
    space,
    color,
    compose(
        fontSize,
        lineHeight
    )
);

Your theme.js file might look like this:

const theme = {
    fontSizes: [11, 13, 16, 23, 29, 37, 47, 76, 97],
    lineHeights: ['12px', '12px', '12px', '32px', '32px', '40px', '48px', '56px', '72px', '80px', '104px']
}


Answer №1

Check out my example on codesandbox that meets your specifications: You can view the entire file here: https://codesandbox.io/s/styled-component-basic-j3zpx

<Title font={[50, 45]}>
        This heading includes the first array index for font size and the second array index for line height.

      </Title>

Here is the styled CSS snippet:

export const Title = styled.h1`
  color: red;
  margin: 0;
  border: 1px solid black;
  ${props =>
    props.font &&
    css`
      font-size: ${props => (props.font[0] ? `${props.font[0]}px` : "20px")};
      line-height: ${props => (props.font[1] ? `${props.font[1]}px` : "30px")};
    `}
`;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Update the state within a forEach iteration

Is there a way to update state when clicking on buttons? I keep getting an error. Error: Uncaught TypeError: this.setState is not a function I understand that this.setState cannot be used here, but I'm unsure of where to bind it. class Popup extend ...

Retrieving server information using AJAX in React

I've been attempting to utilize an AJAX call in order to fetch server data for my React Components. However, I'm encountering difficulties when it comes to displaying it with React as I keep receiving this error: Uncaught TypeError: Cannot read ...

Tips for organizing and concealing images within a Div for seamless transitions (no need for floats)

Currently, I am working on a grid layout for my website. My goal is to have 9 images load quickly, and then once the page has loaded, I want to fetch additional images, insert them into the image containers, and animate between them. While I understand how ...

Adjust canvas size based on chosen option

While experimenting with canvas, I had an interesting idea about altering the dimensions of a cube. Using HTML5 Canvas, I created a representation of a cube consisting of two squares connected by lines to form a 3D shape. My goal is to have the ability to ...

JavaFX text label does not adjust its font according to the CSS file

In my FXML file, the structure is as follows: ... <BorderPane id="header"> <right> <Label styleClass="title" text="A Label" /> </right> </BorderPane> ... I also have a CSS file with the following content: #h ...

Invoke the function when you finish typing in React.js

After I finish typing, I want to execute some code. I attempted to use the following script but it didn't work as expected const stopTypingHandler=(e)=>{ let time; clearTimeout(time); time = setTimeout(() => { console.log("click& ...

"Angular Ionic combination for displaying lists with customized headers and footers on cards

My issue is fairly straightforward - I am looking to create a list card with item array from my controller. However, I am struggling with how to structure my data in the HTML. The list card should have a header, content, and footer. I have attempted variou ...

What steps are necessary to alter the background color of a jumbotron?

Can someone help me figure out how to change the background-color of the 'jumbotron' class in Bootstrap? The default color set in bootstrap.css is #eee. I've tried various methods like replacing it with none, none !important, or transparent ...

Filtering MUI Data Grid by array elements

I am in the process of developing a management system that utilizes three MUIDataGrids. Although only one grid is displayed at a time, users can switch between the three grids by clicking on tabs located above. The setup I have resembles the Facebook Ads ...

Transform Your HTML Into a Stunning Canvas

Help Needed: Despite searching through various questions on Stack Overflow, I have not been able to find a solution to my problem. I am attempting to transform HTML into Canvas using either JavaScript or jQuery. Check out the JsFiddle Demo here .conta ...

Accept forward slashes in route parameters on React

I have a similar router setup: <Route path="/blog/:date/:folder" render={(props: any) => <Home /> It functions well with URLs like this: http://localhost/blog/2017-05-20/test But, when :folder includes slashes (subdirectories), I want to ca ...

Troubleshooting Hover Problem with D3 Chord Example on Internet Explorer 11

I came across an interesting tutorial on how to create chord diagrams in D3, which I'm currently experimenting with. The tutorial can be found at One example that caught my attention is about hair color preferences. When you hover over a group on the ...

What is the best way to securely store my JWT token within the userInfo?

I have been trying multiple things but I seem to be stuck. When I log in to my account, I am able to access my userInfo with my userID and password, but I also need my token. I wrote an if statement to capture the token, but now I want to store it in the ...

What is the best way to horizontally align an element within a Material UI Grid item?

How do I align elements in the center of a Material UI Grid item? Check out this code snippet from my React project: <Grid container> <Grid item xs={4}> // Other content goes here </Grid> <Grid item xs={4}> // How can ...

Display a video within a designated grid section of Bootstrap 4

To create a simple grid structure, I utilized Bootstrap 4 version 4.4.1. The layout consists of two columns, each spanning 50% width. In the left column, there are two rows, both occupying 50% height. The challenge was to embed a video within the upper lef ...

Concealed image lurking beneath navigation bar

My navbar displays an image below it, but the top part of the image is cut off. The desired outcome is for the image to start at the bottom of the navbar to prevent any cropping issues. This becomes more noticeable as the screen size decreases. I attempted ...

Is it possible for me to deactivate the Material-UI SpeedDial hover function?

I need to override the default mouseover/hover behavior of Material-UI's SpeedDial component (https://material-ui.com/api/speed-dial/). Currently, when hovering over the primary icon, the SpeedDial opens. It also opens on click, causing confusion for ...

Troubleshooting npm start error: "Cannot find module: Error: Unable to resolve..." when relocating webpack/react files

Initially, my project was functioning perfectly and I could easily access my web page on localhost:8080 using Chrome. In an attempt to learn something new, I organized all my files into a folder named 'dir copy' instead of just 'dir'. O ...

Building dynamic URLs with React and Node.js

I've been diving into React and Node development One challenge I'm facing now is how to create a dynamic URL for displaying user profiles In Node with EJS, it's relatively straightforward like so: route.get("/:id", function(req, res) { ...

The issue of placeholder values not displaying correctly in a React project deployed on Vercel and Netlify

Currently utilizing the React hook-forms library to handle form values and input data validations. The Placeholder text is visible when testing on the development server (localhost), but it does not appear upon deployment, leading to confusion for users tr ...