|
1 |
| -## [React in patterns](../../README.md) / Styling React components |
| 1 | +## [React in patterns](../../README.md) / Styling React components |
| 2 | + |
| 3 | +React is a view layer. As such it kind of controls the markup rendered in the browser. And we know that the styling with CSS is tightly connected to the markup on the page. There are couple of approach for styling React applications and in this section we will go through the most popular ones. |
| 4 | + |
| 5 | +## The good old CSS class |
| 6 | + |
| 7 | +JSX syntax is pretty close to HTML syntax. As such we have almost the same tag attributes and we may still style using CSS classes. Classes which are defined in external `.css` file. The only caveat is using `className` and not `class`. For example: |
| 8 | + |
| 9 | +``` |
| 10 | +<h1 className='title'>Styling</h1> |
| 11 | +``` |
| 12 | + |
| 13 | +## Inline styling |
| 14 | + |
| 15 | +The inline styling works just fine. Similarly to HTML we are free to pass styles directly via a `style` attribute. However, while in HTML the value is a string in JSX must be an object. |
| 16 | + |
| 17 | +```js |
| 18 | +const inlineStyles = { |
| 19 | + color: 'red', |
| 20 | + fontSize: '10px', |
| 21 | + marginTop: '2em', |
| 22 | + 'border-top': 'solid 1px #000' |
| 23 | +}; |
| 24 | + |
| 25 | +<h2 style={ inlineStyles }>Inline styling</h2> |
| 26 | +``` |
| 27 | + |
| 28 | +Because we write the styles in JavaScript we have some limitations from a syntax point of view. If we want to keep the original CSS property names we have to put them in quotes. If not then we have to follow the camel case convention. However, writing styles in JavaScript is quite interesting and may be a lot more flexible then the plain CSS. Like for example inheriting of styles happens almost naturally: |
| 29 | + |
| 30 | +```js |
| 31 | +const theme = { |
| 32 | + fontFamily: 'Georgia', |
| 33 | + color: 'blue' |
| 34 | +}; |
| 35 | +const paragraphText = { |
| 36 | + ...theme, |
| 37 | + fontSize: '20px' |
| 38 | +}; |
| 39 | +``` |
| 40 | + |
| 41 | +We have some basic styles in `theme` and with mix them with what is in `paragraphText`. Shortly, we are able to use the whole power of JavaScript to organize our CSS. What it matters at the end is that we generate an object that goes to the `style` attribute. |
| 42 | + |
| 43 | +## CSS modules |
| 44 | + |
| 45 | +[CSS modules](https://github.com/css-modules/css-modules/blob/master/docs/get-started.md) is building on top of what we said so far. If we don't like the JavaScript syntax then we may use CSS modules and we will be able to do write plain CSS. Usually this library kicks in while we build our bundle. It is possible to hook it as part of the transpilation step but normally is distributed as a build system plugin. |
| 46 | + |
| 47 | +Here is a quick example to get an idea how it works: |
| 48 | + |
| 49 | +```js |
| 50 | +/* style.css */ |
| 51 | +.title { |
| 52 | + color: green; |
| 53 | +} |
| 54 | + |
| 55 | +// App.jsx |
| 56 | +import styles from "./style.css"; |
| 57 | + |
| 58 | +function App() { |
| 59 | + return <h1 style={ styles.title }>Hello world</h1>; |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +That is not possible by default but with CSS modules we may import directly a plain CSS file and use the classes inside. |
| 64 | + |
| 65 | +And when we say *plain CSS* we don't mean that it is exactly like the normal CSS. It supports some really helpful composition techniques. For example: |
| 66 | + |
| 67 | +``` |
| 68 | +.title { |
| 69 | + composes: mainColor from "./brand-colors.css"; |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +## Styled-components |
| 74 | + |
| 75 | +[Styled-components](https://www.styled-components.com/) took another direction. Instead of inlining styles the library provides a React component. We then use this component to represent a specific look and feel. For example, we may create a link component that has certain styling and use that instead of the `<a>` tag. |
| 76 | + |
| 77 | +```js |
| 78 | +const Link = styled.a` |
| 79 | + text-decoration: none; |
| 80 | + padding: 4px; |
| 81 | + border: solid 1px #999; |
| 82 | + color: black; |
| 83 | +`; |
| 84 | + |
| 85 | +<Link href='http://google.com'>Google</Link> |
| 86 | +``` |
| 87 | + |
| 88 | +There is again a mechanism for extending classes. We may still use the `Link` component but change the text color like so: |
| 89 | + |
| 90 | +```js |
| 91 | +const AnotherLink = styled(Link)` |
| 92 | + color: blue; |
| 93 | +`; |
| 94 | + |
| 95 | +<AnotherLink href='http://facebook.com'>Facebook</AnotherLink> |
| 96 | +``` |
| 97 | + |
| 98 | +By far for me styled-components are probably the most interesting approach for styling in React. It is quite easy to create components for everything and forget about the styling. If your company likes creating a design system and building a product with it then this option is probably the most suitable one. |
| 99 | + |
| 100 | +## Final thoughts |
| 101 | + |
| 102 | +There are multiple ways to style your React application. I did experienced all of them in production and I would say that there is no right or wrong. Sometimes it is a matter of feeling and team work. |
0 commit comments