Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Styled Components - Conditionally render an entire css block based on props

I understand that styles can be conditionally rendered such as:

const HelloWorldLabel= styled("div")<{ centered?: boolean }>`
  display: ${({ centered }) => (centered ? "block" : "flex")};;
  margin: ${({ centered }) => (centered ? "auto 0" : "unset")};
  padding: ${({ centered }) => (centered ? "0 15px" : "unset")};
`;

This does not look DRY - How can I (is it possible) render an entire block of css styles based on props?

Something like:

const HelloWorldLabel= styled("div")<{ centered?: boolean }>`
   if (centered) {
    display: "block" ;
    margin: $"auto 0";
     padding: "0 15px" ;
   } else {
     ......
   }
`;
like image 951
Null isTrue Avatar asked Dec 05 '19 16:12

Null isTrue


People also ask

What is the use of CSS function in styled components?

With Styled Components’ css function, you can use props to conditionally render css, which meant that we no longer had to render conditional class names based on props. This reduces clutter in your components as well as maintains a separation of concerns between CSS and JavaScript.

What is the use of CSS function in props?

The CSS Function With Styled Components’ css function, you can use props to conditionally render css, which meant that we no longer had to render conditional class names based on props. This reduces clutter in your components as well as maintains a separation of concerns between CSS and JavaScript.

Is it possible to add CSS rules to styled components?

Essentially the styled component has access to props when building the CSS string and you could overwrite/add styles based on them. not quite. I need to be able to add css rules whenever I want and from a specific file. The example on the homapage is not modular at all.

What is styled-components?

color: palevioletred; margin: 0 1em; padding: 0.25em 1em; I'm a styled <Button /> As you can see, styled-components lets you write actual CSS in your JavaScript. This means you can use all the features of CSS you use and love, including (but by far not limited to) media queries, all pseudo-selectors, nesting, etc.


2 Answers

With styled-component, or any CSS-in-JS, you can conditionally render a css block:

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

const light = css`
  background-color: white;
  color: black;
`;

const dark = css`
  background-color: black;
  color: white;
`;

const Box = styled.div`
  ${({ isDark }) => (isDark ? light : dark)}
`;

Full Example:

import React, { useState } from 'react';
import ReactDOM from 'react-dom';
import styled, { css } from 'styled-components';

const light = css`
  background-color: white;
  border: 2px solid black;
  color: black;
`;

const dark = css`
  background-color: black;
  color: white;
`;

const FlexBox = styled.div`
  margin: 20px;
  padding: 20px;
  ${({ isDark }) => (isDark ? light : dark)}
`;

const App = () => {
  const [isDark, setIsDark] = useState(false);

  const toggle = () => setIsDark(b => !b);

  return (
    <FlexBox isDark={isDark}>
      <div>Some Text</div>
      <button onClick={toggle}>Change Block</button>
    </FlexBox>
  );
};

ReactDOM.render(<App />, document.getElementById('root'));

Edit zen-https-5bkm5

like image 199
Dennis Vash Avatar answered Sep 19 '22 16:09

Dennis Vash


You can use a function and return the css based on prop:

const HelloWorldLabel= styled("div")`
  ${({centered}) => {
    if (centered) {
      return `
       display: "block" ;
       margin: "auto 0";
       padding: "0 15px";
      `
    } else {
      return `// Other styles here`
    }
   }}
`;
like image 43
Clarity Avatar answered Sep 21 '22 16:09

Clarity