I have a question about both directory structure and the technique of sharing styles when using styled-components in react.
I have a Project and Task page. They both contain different information, but they are displayed in kinda the same way.
Example:
const Foo = styled.div`
margin: 30px !important;
border: 1px solid black;
border-radius: 30px;
overflow: hidden;
min-width: 700px;
min-height: 800px;
`;
This is a styled component that I would use as a parent div for both of them. So I can do create const Task = styled.div and const Project = styled.div and copy this style to each of them (they are in different folders of course). But then if I want to change something, this becomes painful, because I have to make changes in multiple places.
My directory structure is as follows:
srccomponentsNavbarindex.jsNavbar.tsxstyles.tsx
containersTaskProject
Task and Project also contain 3 files, like Navbar. So here are my questions:
- Where do I define a shared style like
Foo? Is itsrc/components/shared,src/components/shared/stylesor maybesrc/shared/stylees? - No matter where I put it, how should I define this shared style? Because I can think of two options:
Option 1:
Create a styled.div like I've shown above and just use it as it is, so for example:
const Project = () => {
return <Foo>{content}</Foo>
}
Option 2:
Create a css instance (?) like this:
const Foo = css`
margin: 30px !important;
border: 1px solid black;
border-radius: 30px;
overflow: hidden;
min-width: 700px;
min-height: 800px;
`
And then use it like this (so I would define a new styled component for Task and Project and include this style I defined):
const StyledProject = styled.div`
${Foo}
`
const Project = () => {
return <StyledProject>{content}</StyledProject>
}
styled-components docs show how to define styled components for a current component that I am working on. I also found some conventions about that and I chose the index.js/<component_name>.tsx/styles.tsx trio, but there are many more. However, I couldn't really find docs/article about what to do with shared styles like this.
Do you guys know the way, or maybe you can share your particular approach?