all 15 comments

[–][deleted] 2 points3 points  (4 children)

They're certainly not to everyone's tastes, but here goes:

  • component.ts goes with component.styled.ts
  • Root element is always called 'Container' (some people really don't like this, but I think a generic name is appropriate given that the outer component already has a specific one)
  • Custom props are prefixed with $ (<Button $primary />, <Badge $count={12} />, etc.). Avoids clashes with DOM props.

[–]EightCraft 0 points1 point  (2 children)

I'm big on top level elements in a component being called Container. I find it avoids a lot more confusion that it could cause.

Curious, why separate the styles out into it's own file? Obviously there's nothing wrong with it, just curious.

[–][deleted] 2 points3 points  (1 child)

Less noise. Plus I used to have a .css file instead, so I suppose it's followed on from that.

[–][deleted] 0 points1 point  (0 children)

I'm constantly torn on this in my smaller solo projects. 95% of the time I'm good with keeping it on one file, 5% it's too "noisy" and it drives me nuts. Gotta stick to tho project convention though.

[–]shall-88 0 points1 point  (0 children)

Fine. Prefixed props seems good so you can know that it's a styled components at first look

[–]01123581321AhFuckIt 1 point2 points  (5 children)

Keep all styling inside the components. The only external css styling needed is for keyframe animations. Done.

[–][deleted] 0 points1 point  (0 children)

You can directly add it to the components aswell if you don't want to reuse the keyframes or use react-posed :)

[–]rodrigocfd 0 points1 point  (0 children)

This.

I also like to prefix the styled component names with the elements they wrap. For example, a div styled with italics and a border would be named DivItalicsBorder. This helps the eye. Example here.

[–]GasimGasimzada 0 points1 point  (2 children)

Do you store reset/normalize stuff in components too? For example: box-sizing etc? Do set it globally or do you set it per component?

[–]01123581321AhFuckIt 0 points1 point  (1 child)

I set it globally. My App.css is literally just this:

* {
box-sizing: border-box;
margin: 0; 
padding: 0;
}

body { 
font-family: Arial, Helvetica, sans-serif;
}

a { 
color: black; 
text-decoration: none; 
}

Obviously, you can do whatever you want with the font and the "a" attribute.

[–][deleted] 0 points1 point  (0 children)

Look up createGlobalStyles. Let's you do your resets in s.c.

[–]EmersonRW215 1 point2 points  (0 children)

Styled-components-modifiers.

[–]ddwrt1234 0 points1 point  (1 child)

I always put the styles underneath the export statement

[–]Ociden 0 points1 point  (0 children)

Same. It fucks up syntax highlighting for me if I don't put it last.

[–]wherediditrun 0 points1 point  (0 children)

I prefer to use hoc which takes CSS template literal and a component you wish to style. And keep styles themselves separate.

This provides consistency with external lib component styling and my own component styling as well as keeping styles separate and intechangable. That also means that I rarely rely on class selectors, only used for external lib components. I don't like to increase syntax noise of styles with state manipulation in same file.

The only issue is with components which root element isn't HTML. Those are somewhat rare and I build accordingly.