Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge two styled-components which are in different base components but have similar styles?

I am trying to merge these two styled-components into one.

const CustomInput = styled.input`
    width: 150px;
    height: 100%; 
`;
const CustomTextarea = styled.textarea`
    width: 150px;
    height: 100%; 
`;
like image 592
Diamond Avatar asked May 15 '19 16:05

Diamond


People also ask

Can I use mixin in styled-components?

Mixins with Styled Components Essentially, a mixin is a block of styles that can be seen like a component in the sense that: A set of styles are written as a standalone block. Those styles can be imported and used many times. The set can support props to dynamically alter values throughout lifecycles.

Does styled-components support SSR?

styled-components support server-side rendering with stylesheet rehydration which allows you to use styled-components with React DOM's every time you render an app on the server. In order to do that in Next. js, you need to follow some setups in its docs: Install Babel plugins.


1 Answers

Use string interpolation:

const sharedCSS = css`
  width: 150px;
  height: 100%; 
`
const CustomInput = styled.Input`
  ${sharedCSS}
`
const CustomTextarea = styled.textarea`
  ${sharedCSS}
`
like image 74
Akrion Avatar answered Oct 17 '22 04:10

Akrion