Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Styled-components organization [closed]

I want to use styled-components in my app and I am wondering how to organize it.

Basically styled-components are assigned to specific component for reusability.

But what about styled-components which I would like to use many times in other components (for example animations components)? Should I create a file where I will keep these 'global' styled-components and import it to many components?

Is it good practice?

like image 404
Gemmi Avatar asked Mar 23 '17 22:03

Gemmi


People also ask

Is styled-components deprecated?

As of styled-components v4 the withComponent API is now a candidate for deprecation. In all likelihood, you probably want to use the new "as" prop to simply switch what element/component being rendered since the withComponent API is destructive toward styles if the lowest-wrapped component is a StyledComponent.

How do you organize styled-components?

Organizing styled components You can define your styled components within the file for the component you are adding them to, but it's easier to collect them in a separate file. Within this separate file, you would export each component individually so that you can import and use each exactly where you need it.

Where do you store styled-components?

The answer is the same as for any other React component: Keep them in a more top-level folder where it can be imported by more than one component folder. If you change the style of the styled component, check all components which import it.

Do companies use styled-components?

Who uses styled-components? 330 companies reportedly use styled-components in their tech stacks, including Revolut, Graphy, and Shelf.


2 Answers

This is what most of my big production applications built with styled-components look like:

src ├── App │   ├── Header │   │   ├── Logo.js     │   │   ├── Title.js    │   │   ├── Subtitle.js │   │   └── index.js │   └── Footer │       ├── List.js │       ├── ListItem.js │       ├── Wrapper.js │       └── index.js ├── shared │   ├── Button.js │   ├── Card.js │   ├── InfiniteList.js │   ├── EmojiPicker │   └── Icons └── index.js 

The App folder contains all the specific components that make up your bigger application. (you might structure that by route in your real app) Each of those bigger components is a folder with an index.js file and a bunch of styled components in individual files.

While I'm building my application and I notice I need a styled component from one bigger component in another bigger component, I drag its file to the shared/ folder, update all the imports and that's it! Over time shared/ becomes an improptu pattern library with all of the components I want/need to reuse throughout my entire app.

Another way that's fairly common is to have style.js files in the component folders which contains all of the styled components of that component. The upside being that you have less files that get in your way, but with the downside that it's harder to notice duplication and moving components into the shared folder is more work.

I personally use the first version more often than not, but that's probably just a matter of taste—try them both and see which one you like more!

like image 200
mxstbr Avatar answered Oct 10 '22 10:10

mxstbr


You can also try Atomic Design to structure your app. This way you will be able to reuse your styled components. Based on Atomic Design methodology, you organize your components into atoms, molecules, organisms, pages and templates.

Atom

An atom is native html tag. For example, an Input element can be an Atom

const Input = props => <input {...props} /> 

Molecules

Group of atoms is a molecule. For example:

const Field = ({ label, ...inputProps }) => (     <Label>         {label}         <Input {...inputProps} />     </Label> ) 

Organisms

Organism is a group of atoms, molecules and/or other organisms. For example:

const Form = (props) => (     <form {...props}>         <Field label="Name" type="text" />         <Field label="Email" type="email" />     </form> ) 

Pages

A page is where you will put mostly organisms. For example:

const HomePage = () => (     <PageTemplate header={<Header />}>         <Form />     </PageTemplate> ) 

Templates

A template is a layout to be used on pages. For example:

const PageTemplate = ({ header, children }) => (     <main>         {header && <div>{header}</div>}         {children}     </main> ) 

Github example

There is a React starter project on Github that uses Atomic Design methodology with styled-components integration. Here is the Link.

like image 44
Peeyush Kumar Avatar answered Oct 10 '22 11:10

Peeyush Kumar