Tips for utilizing identical properties across numerous styled elements:

Utilizing the styled-components library, I have enhanced the header components from the Blueprintjs library. The current template for the H6 component appears as follows:

export const DH6 = styled(H6)`
  color: ${(props) => (props.white ? "white" : null)};
  display: ${(props) => (props.flex ? "flex" : null)};
  opacity: ${(props) => (props.opaque ? 0.7 : null)};
  font-weight: ${(props) => (props.heavy ? 500 : 300)};
  text-align: ${(props) => (props.center ? "center" : null)};
  font-style: ${(props) => (props.italics ? "italic" : null)};
  text-decoration: ${(props) => (props.underline ? "underline" : null)};
`;

The above code functions correctly. Now, I aim to apply the same properties from D1 to D5. Instead of duplicating the props, is there a way to accomplish this more efficiently in a DRY manner? I attempted saving the properties to a variable but faced challenges:

const generalProps = {
  opacity: ${(props) => (props.opaque ? 0.7 : null)},
  color: ${(props) => (props.white ? "white" : null)},
  display: ${(props) => (props.flex ? "flex" : null)},
  font-weight: ${(props) => (props.heavy ? 500 : 300)},
  text-align: ${(props) => (props.center ? "center" : null)},
  font-style: ${(props) => (props.italics ? "italic" : null)},
  text-decoration: ${(props) => (props.underline ? "underline" : null)}
}

export const DH6 = styled(H6)`
  {...generalProps}
`;

Is it possible to assign props using variables within styled-components?

Answer №1

An easy way to demonstrate this is by using the following code snippet:

import styled, {css} from 'styled-components'

const GlobalStyles = css`
  color: ${(props) => (props.white ? "white" : null)};
  display: ${(props) => (props.flex ? "flex" : null)};
  opacity: ${(props) => (props.opaque ? 0.7 : null)};
  font-weight: ${(props) => (props.heavy ? 500 : 300)};
  text-align: ${(props) => (props.center ? "center" : null)};
  font-style: ${(props) => (props.italics ? "italic" : null)};
  text-decoration: ${(props) => (props.underline ? "underline" : null)};
`

const StyledDiv = styled.div`
 ${GlobalStyles}
 // Other Styles
`

To implement this, you'll need to import css from the styled-components library.
With this approach, you can create customized CSS styles and return them as a template literal - for more details, refer to the official documentation.
For improved reusability, consider utilizing ${value}, which serves as the Spread Operator in styled-components.

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

Exploring discrepancies between two tables with the power of Javascript

const firstTable = document.getElementById('table_1') const secondTable = document.getElementById('table_2') const rows1 = firstTable.rows const rows2 = secondTable.rows for (let i = 0; i < rows1.length; i++) { for (let x in rows ...

Modify the templateUrl in routeProvider according to the user's authorization

Is there a way to dynamically change the templateUrl in the router provider based on user permissions? Here's an example of what I'd like to achieve: $routeProvider .when('/',{ if(user) templateUrl:'app/views/pages/h ...

Adjust a Javascript script to choose the best font color for use in R Shiny applications

I am currently seeking to determine the font color of hover messages based on the background color. This means white if the background is dark, and black if it is light. However, I stumbled upon a Stack Overflow question with a Javascript solution that see ...

Validating numbers in React JS input fields component

I need assistance with implementing number validation for 3 Textfields in my application. Currently, the code displays an error message if a Textfield is empty, but I am stuck on validating whether the input is text or numbers. import React from 'rea ...

AngularJS is throwing an error because the term "grunt" has not

Today is the day I embark on my journey with Grunt for testing my JavaScript code. All the necessary grunt modules have been successfully installed and are documented in a json file called package.json. { "name": "LarissaCity", "private": true, ...

Seamless movements to the exact midpoint

I am trying to find a method to create a smooth transition when removing the center class from p.title.class, allowing it to smoothly move to its new position. JSfiddle Here is an excerpt of my HTML: <body> <div class="wrapper-top"> ...

What is the best way to incorporate vertical scrolling into a React material table?

I'm having trouble getting vertical scroll to work with my material table in React Typescript. Horizontal scroll is functioning properly for large data, but I'm stuck on implementing the vertical scroll. Here's my code: {isLoading ? ...

Guide to acquiring the webViewLink using Google Drive Api v3?

I'm having trouble locating the webViewLink. The documentation (https://developers.google.com/drive/api/v3/reference/files) states that I should receive this parameter when requesting gapi.client.drive.files.list(). However, I don't even have a c ...

Bootstrap card elements are failing to display properly, deviating from the expected

My Bootstrap cards are displaying strangely. Instead of having a border, white padding, and a white background like the examples I've seen, mine look like plain text without any styling. Here is an image for reference: https://i.stack.imgur.com/9MsP ...

Issue with Vue v-for not updating data model variable

I am attempting to render a page with dynamic properties using the following code: <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="root"> <div v-for="current in 2&quo ...

Creating a div that expands to occupy the entire width, even when hidden and requiring a scroll mechanism

I am facing a challenge with my webpage that has two panels. The second panel includes a large table that does not fit within the width of the window, even with wrapped content. To address this issue, I added a horizontal scroll, but the div still does not ...

The font-weight property appears to be ineffective across all browsers

I'm struggling with the font-weight property in my CSS. Despite trying different values like lighter, bold, and even numerical values like 300, it doesn't seem to have any effect on the font weight in any browser. I am using the following code to ...

Vue watchers capturing original value prior to any updates

When working with Vue.js, we can easily access the value after modification within watchers using the following syntax: watch: function(valueAfterModification){ // ...... } But what about getting the value before it's modified? PS: The official ...

Is there a way in ReactJs to automatically trigger the browser to fetch the latest version of the app when a certain version is set in the index

I recently delved into learning ReactJS and came across a topic I can't seem to find now. How can I ensure the browser automatically downloads updated app files after a new release? Could setting cache-control help with this? For example, if I build ...

Using React and TypeScript to pass member variables

My component Child has a member variable that can change dynamically. While I am aware of passing props and states, is there a more elegant solution than passing member variables through props or other parameters? class Child extends React.Component< ...

Safari iOS does not properly support responsive images when using srcset and sizes

I am facing an issue with the following code snippet: <img src="/img/footer/logo_white.png?v=2.0" srcset="/img/footer/logo_white.png?v=2.0 1x, /img/footer/logo_white2x.png?v=2.0 2x" > This ...

Looking for an Angular Material component that displays a list of attributes?

I am trying to create a dynamic list of properties in Angular using Material Design that adjusts for different screen sizes. For example, something similar to this design: https://i.stack.imgur.com/EUsmV.png If the screen size decreases, the labels shou ...

Having trouble looping through an array of objects containing images in Javascript?

I am currently facing challenges with iterating through an array of objects that contain images. The array appears empty when logged in the console, but upon inspecting it in the console, I can see all the objects along with their iteration numbers. I have ...

In need of assistance with Ember data! Struggling to deserialize JSON into model

Here is the technology stack I'm currently using: Ember 1.10.0 Ember Data 1.0.0-beta.15 Within my application, I have defined a model as shown below: //models//acceptedtask.js import DS from "ember-data"; export default DS.Model.extend({ userAg ...

Tips for setting up listeners across multiple modules using socket.io

Last year, I created a multiplayer game using node.js and socket.io. Now, as part of my efforts to enhance the game, I am working on breaking down the code into modules. Currently, I am utilizing expressjs 4.4 along with socket.io 1.0. One challenge I enco ...