25
loading...
This website collects cookies to deliver better user experience
Automatic Critical CSS: Styled components automatically keep track of which component is rendered on the screen and inject only their style, when combined with code splitting you will be loading the least amount of codes which helps your project performance.
No ClassName bugs: it generates unique class names for every style, you will never have to worry about duplications or misspellings.
Easier deletion of CSS: If you are working on a large project codebase that makes use of clean CSS files, it mostly becomes tricky to keep track of unused classes, but with styled-components, every style is tied to a specific component. If a component is not used, it can easily be pointed out which can easily be deleted by deleting the component.
Dynamic Styling: Just like React, where props are passed into components, sometimes styles need to be adapted based on props, styled components make this easy and simple.
Painless Maintenance: it is easy to organize styles with styled components, and you don’t have to move across different files to find the file affecting your component.
Automatic Vendor Prefixing: for some of the new css features you might have to write the css properties for each browsers, but with styled components you can write your css to the current standard and the library will handle the rest.
THEMING: with styled-components, you are given full support to theming which gives you the ability to create a theme or lay-down structure for the project style, theming mostly contains colors, sizes, fonts and other common things which will be used throughout the project to avoid repetition.
To create a theme with styled-components, the Theme Provider wrapper needs to be imported.
import { ThemeProvider } from "styled-components"
const theme = {
color: {
primary: "#000f55",
secondary: "#04043f",
white: "#fff",
},
fontSize: {
large: "2.5rem",
medium: "1.5rem",
small: ".785rem"
}
}
const Button = styled.button`
padding: 4px .7rem;
color: ${props => props.theme.color.white};
background: ${props => props.theme.color.primary};
font-size: ${props => props.theme.fontSize.small};
border-radius: 8px;
`
const FlexWrapper = styled.div`
display: flex;
align-items: center;
justify-content: center;
`
<ThemeProvider theme={theme}>
<FlexWrapper>
<Button>Click Please</Button>
</FlexWrapper>
</ThemeProvider>
import { createGlobalStyle } from "styled-components/macro"
export default createGlobalStyle`
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
`
import GlobalStyles from "./globalStyles"
function App() {
return (
<GlobalStyles />
<AppContent />
)
}
import styled from "styled-components"
const Button = styled.button`
padding: 4px 12px;
border-radius: 4px;
color: #fff;
background: #000;
`
const TransparentButton = styled(Button)`
border: 1px solid #000;
background: none;
color: #000;
`
const Button = styled.button`
padding: 4px 12px;
border-radius: 4px;
color: #fff;
background: #000;
`
const Button = styled.button`
padding: 4px 12px;
border-radius: 4px;
color:${(props) => props.color ? props.color : '#fff'};
background: ${(props) => props.bg ? props.bg : '#000'};
`
<Button color="black" bg="orange">Clicked</Button>
<Button>Click Me</Button>
function Button({props}) {
return (
<button className={props.className}>Click Me</button>
)
}
Button = styled(Button)`
padding: 4px 8px;
border-radius: 4px;
border: 1px solid #000;
`
const Input = styled.input.attrs({
className: "form-control"
})`
background: #fff
`
const TailwindInput = styled.input.attrs({
className: "
mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md text-sm shadow-sm placeholder-gray-400
focus:outline-none focus:border-sky-500 focus:ring-1 focus:ring-sky-500
disabled:bg-gray-50 disabled:text-gray-500 disabled:border-gray-200 disabled:shadow-none
invalid:border-pink-500 invalid:text-pink-600
focus:invalid:border-pink-500 focus:invalid:ring-pink-500
"
})`
background: #fff
`
const Button = styled.button`
font-size: 0.75rem;
font-weight: 700;
padding: 8px 1.5rem;
border: 1px solid green;
color: green;
`
const Button = styled.button.attrs({
disabled: true
})`
font-size: 0.75rem;
font-weight: 700;
padding: 8px 1.5rem;
border: 1px solid green;
color: green;
`
const Button = styled.button`
padding: 2px 5px;
color: ${props => props.theme.color};
border-radius: 3px;
`
<Button as="a">Go Back Home</Button>
const Button = styled.button`
padding: 2px 5px;
color: ${props => props.theme.color};
border-radius: 3px;
`
const Link = Button.withComponent("a")