How can I make multiple styling for only one prop in React Styled Components?

GusDev

I'm using Styled Components in react, and I want to wrap all the styles I want for only one prop (it's a conditional prop). Right now I have something like this:

<ContentSty
    flexDirection={abilityScores ? "column" : "row"}
    flexWrap={abilityScores ? "no-wrap" : "wrap"}
    cardDisplay={abilityScores ? "flex" : "block"}
    cardHeight={abilityScores ? "auto" : "5rem"}
    cardTextMargin={abilityScores ? "auto 0.5rem" : "0.2rem 0"}
    hoverShadow={abilityScores ? "none" : "0px 0px 14px gold"}
    hoverCursor={abilityScores ? "auto" : "pointer"}
>

I always do the same check: if the variable abilityScores === true then have this value, else have this other one. Then, in my styles, I have a lot of some-css-prop: ${(props) => props.someProp}; and it gets kind of messy

Is there a way to just wrap all the styles for abilityScores === true in one prop, then just pass a unique prop like abilityScore={abilityScore === true ? allTheseStylings : ''}, or a cleaner way, instead of just making a lot of props?

wlh

See the documentation on StyleObjects here: https://styled-components.com/docs/advanced#style-objects

You can use the props of abilityScore to conditionally style your component.

For example:

const ContentSty = styled.div(props => ({
  flexDirection: props.abilityScores ? "column" : "row",
  flexWrap: props.abilityScores ? "no-wrap" : "wrap",
  cardDisplay: props.abilityScores ? "flex" : "block",
  cardHeight: props.abilityScores ? "auto" : "5rem",
  cardTextMargin: props.abilityScores ? "auto 0.5rem" : "0.2rem 0",
  hoverShadow: props.abilityScores ? "none" : "0px 0px 14px gold",
  hoverCursor: props.abilityScores ? "auto" : "pointer",
});

...

<ContentSty abilityScores={true} />

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Conditional styling with Styled Components

React - How can I import all my components into one

How to make top to bottom animation with react styled components

How can I focus a button on click with styled-components for React?

Can I create class-based React Styled Components?

How can I make the following styling using only pure CSS (profile-image with edit icon)?

Styled-Components: Conditional styling with multiple props / states

Multiple evaluations in template literal... How can I improve my padding calculation for styled-components?

How to make accordion menu using react & styled components

How can i do multiple onClick animations in Styled components?

How can I make different React components display 'inline'?

React styled-components - How to render styles based on prop condition or pseudo class?

For styled components, how can I make all component styles cascade to .hover component styles?

How can I make these components navigate to another screen in react?

How can I use custom icon in react-styled components?

How can I pass multiple values in a single prop in react?

How do I add base styling for entire app with "Styled-components"?

On styling of class components using React styled-components

How can I make a button in one activity to modify components in other

How i can change a style of one component in Styled-Components without create a new component?

How can I use multiple props in a single callback defined in a template literal in React styled-components

Styled Components - How to style a component passed as a prop?

React Native - Styling buttons with styled-components

Typescript React - Styled component conditional styling using prop not working

Styling Components (styled-components) | Superimposing one on top of the other

How can I conditionally apply styling to one React property in a paragraph?

How can i make changes in imported react icon using styled components

styled-components — how to use :not() selector on a prop?

How can I make cards components all the same size in React?