#React Styled Components

78 messages · Page 1 of 1 (latest)

lilac garden
#

You can use css function from styled components and define your styles with it. This will create a CSS class (as far as I remember) which can be be used anywhere in your application.

const sharedBtnStyles = css`
  // css definiton
`

const Button1 = styled.button`
  ${sharedBtnStyles}
`

const Button2 = styled.button`
  ${sharedBtnStyles}
`

Reference if needed: https://styled-components.com/docs/api#css

You can also define a common button for your applications, then you could also target that button's css in your main container:

const Button = styled.button`
  color: red;
  background: blue;
`

const MainContainer = styled.div`
  & ${Button} {
    color: purple;
    background: yellow;
  }
`
lilac garden
#

me pleasure 🙂

rotund lake
#

Hi guys supposedly you have a button component created in react styled components. You style it using various styles eg give it a padding and border radius and it also takes in a prop. Let's call this button1

And then later on you want to place a button(let's call this button2) which has different styles entirely from the button1 which you created before, what do you do? Do you create another button component or you can reuse the one created earlier, if it's the latter how do you go about it? Thanks in advance.

lament pilot
#

I generally wouldn't advise styled-components be used

#

There's a lot of better options

#

Tailwind, Vanilla extract, Unocss, Postcss

rotund lake
lament pilot
#

variants

rotund lake
lament pilot
# rotund lake How??
import styled, { css, DefaultTheme } from 'styled-components';

const variantStyles = (theme: DefaultTheme, variant = 'primary') =>
  ({
    primary: css`
      color: ${theme.colors.light};
      background: ${theme.colors.primary};
      border: 1px solid ${theme.colors.primary};
    `,
  }[variant]);

const Button = styled.button<{ variant: string }>`
  padding: 1rem;
  font-size: 0.875rem;
  transition: all 0.3s;
  cursor: pointer;

  ${({ theme, variant }) => variantStyles(theme, variant)}

  &:active {
    transform: translateY(1.5px);
  }
`;

export default Button;

This is an example

#

Then you can do this

<Button variant="primary">
  Start Learning
</Button>
#
const variantStyles = (theme: DefaultTheme, variant = 'primary') =>
  ({
    primary: css`
      color: ${theme.colors.light};
      background: ${theme.colors.primary};
      border: 1px solid ${theme.colors.primary};
    `,
    secondary: css`
      ...
    `,
    etcetera: css``,
    ...
  }[variant]);
rotund lake
#

Why would you use tailwind css instead of styled components?

lament pilot
#

with tailwind i just write what it looks like

#

with styled-components i have to name something

#

then you end up with Button1 Button2

#

another thing is, tailwind is just fast to write once you get it, its like the vim of css

#

and the default styles of tailwind are amazing, really some good color palettes and sizing units, its just tailored with presets by a genius

rotund lake
# lament pilot and the default styles of tailwind are amazing, really some good color palettes ...

You forget that it makes the code look bloated. If you have a div that has 10 css rules it becomes unreadable.

In terms of naming with styled components it's easy. That button I have is called "styled button". I just have to put "styled" before the actual element.

Another thing I hate with tailwind is that there are several steps of configuration before you can use it. With styled components you just import and you good to go.

lament pilot
#

it becomes unreadable.
for me, no, it's a hump to get over for sure but i dont find it obstructing, also theres extensions to hide the classname when not focusing on the element

several steps of configuration before you can use it
meh, install, init, copy paste config, good to go

rotund lake
lament pilot
#

string is a type lol

#

if you're not a chad and not using typescript just remove the type

#

so just normal style.button``

rotund lake
#

What about the default Theme in const variantStyles. It's underlined in red still

rotund lake
#

So in short i should just import css and styled from styled components??

lament pilot
#

yh

rotund lake
#

Take a look @lament pilot it's not working. What am I missing??

lament pilot
rotund lake
lament pilot
#

it is working

#

you just havent passed variant

rotund lake
lament pilot
#

oh wait no you have

#

confus

#

never use images for code lol

rotund lake
#

So why is it not working?

lament pilot
#

oh i see why lol

#
(theme, variant = "primary")
#

Set the params of variants to that

#

It's taking your theme as the first one, and theme isn't "primary" so it doesn't know wtf to do

#

This would've been avoided with typescript

rotund lake
#
import React from "react";
import styled, { css } from "styled-components";


// below is your suggestion
const variantStyles = (variant = "primary") =>
  ({
    primary: css`
      color: white;
      background: black;
    `,
  }[variant]);

export const TestButton = styled.button`
  padding: 11rem;
  
  font-size: 2rem;
  cursor: pointer;
  ${({ theme, variant }) => variantStyles(theme, variant)}
  &:active {
    transform: translateY(1.5px);
  }
`;

// below is my original code

const Button = (props) => {
  return (
    <>
      <StyledButton color={props.color}>{props.title}</StyledButton>
    </>
  );
};

const StyledButton = styled.button`
  border-radius: 30px;
  padding: 15px 30px;
  color: ${({ theme }) => theme.colors.gray};
  background-color: ${({ theme }) => theme.colors.brandColor};
  cursor: pointer;
  border: none;
  font-size: 15px;
  font-weight: 500;
  font-family: "Merriweather";

  &:hover {
    color: ${({ theme }) => theme.colors.brandColor};
    background-color: ${({ theme }) => theme.colors.white};
    transition: all 0.5s ease-in-out;
  }

  @media screen and (max-width: ${({ theme }) => theme.mobile}) {
    display: none;
  }
`;
export default Button;


#
Header js

import React from "react";
import styled from "styled-components";
import Image1 from "../images/iLander-Logo.png";
import Button from "./Button";
import TestButton from "./Button";

const Header = () => {
  return (
    <StyledHeader>
      <StyledLogo src={Image1} alt="logo" />
      <Button title="Get Started" />
      <TestButton title="test" variant="primary" />
    </StyledHeader>
  );
};

const StyledHeader = styled.header`
  padding: 20px 15px;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
  z-index: 5;
  background-color: red;
`;

const StyledLogo = styled.img`
  font-size: 2rem;
`;


export default Header;



lament pilot
#

just add theme

#

or remove it if you wont ever need it

#

Either

const variantStyles = (theme, variant = "primary") =>
  ({
    primary: css`
      color: white;
      background: black;
    `,
  }[variant]);

Or

${({ variant }) => variantStyles(variant)}
rotund lake
#

On your end you say it's working??

#

None of this is working

lament pilot
#

Works for me

#

What does it mean when you say it isn’t working lol

#

Like what happens

#

@rotund lake

rotund lake
#

Copy the code please.

#

I have been coding from morning. My mind might as well be tired.

rotund lake
lament pilot
# rotund lake Copy the code please.
import "./styles.css";
import styled, { css } from "styled-components";

const variants = (theme, variant = "primary") =>
  ({
    primary: css`
      font-size: 2rem;
    `,
    secondary: css`
      font-size: 1rem;
    `
  }[variant]);

const Title = styled.h1`
  ${({ theme, variant }) => variants(theme, variant)}
`;

export default function App() {
  return (
    <div className="App">
      <Title variant="primary">Hello</Title>
      <Title variant="secondary">Hello again</Title>
    </div>
  );
}
#

That's an example.

rotund lake
#

Where is this theme coming from in const variants??

#

Is the const variants supposed to be exported??

lament pilot
rotund lake
# lament pilot styled components

You know in my app.js there is a theme provider that takes in theme. And the theme is an object that has colors and mobile size width. So I am wondering is this the same theme ?

rotund lake
#

Greetings @lament pilot what you showed me works perfectly with text. I would like it to work with buttons too. First of all I would like you to look at my app.js file. It seems there is some confusion about where "theme " is coming from. Below is App.js ```jsx
import { ThemeProvider } from "styled-components";
import { GlobalStyles } from "./styles/Global";
import GlobalFonts from './fonts/fonts'
import Header from "./components/Header";
import HeroSection from "./components/HeroSection";

const theme = {
colors: {
blackMain: '#0E1133',
blackLight: '#505056',
gray: '#E7EAEF',
brandColor: '#FF3E66',
white: '#EEF1F6',
},
mobile: '800px'
}

function App() {
return (
<ThemeProvider theme={theme}>
<>
<GlobalStyles />
<GlobalFonts />
<Header />
<HeroSection/>
</>
</ThemeProvider>
);
}
export default App;

#

I tried what worked with texts but with buttons but it isn't working. Below is my button component. I have imported the button to two different components. Header and Hero section```jsx
import React from "react";
import styled, { css } from "styled-components";

const variants = (theme, variant = "primary") =>
({
primary: css border-radius: 30px; padding: 15px 30px; color: green; background-color: black; cursor: pointer; border: none; font-size: 15px; font-weight: 500; font-family: "Merriweather"; ,
secondary: css border-radius: 30px; padding: 15px 30px; color: orange; background-color: black; cursor: pointer; border: none; font-size: 30px; font-weight: 500; font-family: "Oswald"; ,
}[variant]);

// export const Title = styled.h1// ${({ theme, variant }) => variants(theme, variant)} //;

const Button = (props) => {
return (
<>
<StyledButton color={props.color}>{props.title}</StyledButton>

</>

);
};

const StyledButton = styled.button`
${({theme, variant }) => variants(theme, variant)}
&:hover {
color: ${({ theme }) => theme.colors.brandColor};
background-color: ${({ theme }) => theme.colors.white};
transition: all 0.5s ease-in-out;
}

@media screen and (max-width: ${({ theme }) => theme.mobile}) {
display: none;
}
`;
export default Button;

#

Here is what the button looks like in Hero section ```jsx
import React from "react";
import styled from "styled-components";
import Image1 from "../images/hero-thumb-3.png";
import Button from "./Button";
const HeroSection = () => {
return (
<>
<StyledHeroSection>
<StyledHeroHeading1>14 Day Free</StyledHeroHeading1>
<HeroImage src={Image1} />
<StyledHeroHeading2>
Ready to launch <br /> your app in one <br /> Application.
<br />
<Button variant="secondary" title="Testing" />
</StyledHeroHeading2>
<StyledHeroParagraph>
Find the best application in the iLander.
</StyledHeroParagraph>
</StyledHeroSection>
</>
);
};
const StyledHeroSection = styled.div`
background-color: ${({ theme }) => theme.colors.white};

padding: 10rem;
`;

const StyledHeroHeading1 = styled.h4 color: ${({ theme }) => theme.colors.brandColor}; font-size: 20px;;
const StyledHeroHeading2 = styled.h2 color: ${({ theme }) => theme.colors.blackMain}; line-height: 1.1em; font-size: 60px;;
const StyledHeroParagraph = styled.p color: ${({ theme }) => theme.colors.blackMain}; z-index: 1;;

const HeroImage = styled.img position: absolute; background-position: bottom right; width: 48%; right: 0; bottom: 0;;

export default HeroSection;

#

And here is what the button looks like in header section. ```jsx
import React from "react";
import styled from "styled-components";
import Image1 from "../images/iLander-Logo.png";
import Button from "./Button";

const Header = () => {
return (
<StyledHeader>
<StyledLogo src={Image1} alt="logo" />
<Button variant="primary" title="Get Started" />
</StyledHeader>
);
};

const StyledHeader = styled.header padding: 20px 15px; display: flex; flex-direction: row; justify-content: space-between; align-items: center; z-index: 5; background-color: red;;

const StyledLogo = styled.img font-size: 2rem;;

export default Header;

#

Secondary button should have a font size of 30px and color of Orange, which is different from primary which a color of green and it's 15px has . But when I change the primary css styles it changes for secondary too. Please assist.

#

This is how it looks in the browser. Like I said they should vary, both buttons should have different fonts and colors but they don't

lament pilot
#

Idk at this point I hate styled components and you’re proving my case 😂