• No se han encontrado resultados

Inversiones contabilizadas utilizando el método de la participación

III. NOTAS EXPLICATIVAS

9.   Inversiones contabilizadas utilizando el método de la participación

There is a library which is very promising, because it takes into account all the problems the other libraries have encountered in styling components.

Different paths have been followed for writing CSS in JavaScript, and many solutions have been tried, so now the time is ripe for a library that takes all the learning and then builds something on top of it.

The library is conceived and maintained by two popular developers in the JavaScript community: Glenn Maddern and Max Stoiberg.

It represents a very modern approach to the problem, and it uses edge features of ES2015 and some advanced techniques applied to React to provide a complete solution for styling. Let's look at how it is possible to create the same button we saw in the previous sections, and check if all the CSS features we are interested in (for example, pseudo classes and Media queries) work with Styled Components.

First, we have to install the library by running the following command:

npm install --save styled-components

Once the library is installed, we have to import it inside our component's file:

import styled from 'styled-components'

At that point, we can use the styled function to create any element by doing

styled.elementName, where elementName can be a div, a button, or any other valid DOM element.

The second thing to do is define the style of the element we are creating, and to do so, we use a ES2015 feature called Tagged Template Literals, which is a way of passing template strings to a function without them being interpolated beforehand.

This means that the function receives the actual template with all the JavaScript expressions, and this makes the library able to use the full power of JavaScript to apply the styles to the elements.

Let's start by creating a simple button with a basic styling:

const Button = styled.button backgroundColor: #ff0000; width: 320px;

border: none; outline: none; `

This kind-of-weird syntax returns a proper React component called Button, which renders a button element and applies to it all the styles defined in the template. The way the styles are applied is first by creating a unique class name, adding it to the element, and then injecting the corresponding style in the head of the document.

The following is the component that gets rendered:

<button class="kYvFOg">Click me!</button>

The style that gets added to the page is as follows:

.kYvFOg { background-color: #ff0000; width: 320px; padding: 20px; border-radius: 5px; border: none; outline: none; }

The good thing about Styled Components is that it supports almost all the features of CSS, which makes it a good candidate to be used in a real-world application.

For example, it supports pseudo classes using an SASS-like syntax:

const Button = styled.button` background-color: #ff0000; width: 320px; padding: 20px; border-radius: 5px; border: none; outline: none; &:hover { color: #fff; } &:active { position: relative; top: 2px; }

It also supports Media queries:

const Button = styled.button` background-color: #ff0000; width: 320px; padding: 20px; border-radius: 5px; border: none; outline: none; &:hover { color: #fff; } &:active { position: relative; top: 2px; } @media (max-width: 480px) { width: 160px; } `

There are many other features that this library brings to your project.

For example, once you have created the button, you can easily override its styles and use it multiple times with different properties.

Inside the templates, it is also possible to use the props that the component received and change the style accordingly.

Another great feature is Theming. Wrapping your components into a ThemeProvider component, you can inject a theme property down to the three, which makes it extremely easy to create UIs where part of the style is shared between components and some other properties depend on the currently selected theme.

Summary

In this chapter, we have looked at a lot of interesting topics. We started by going through the problems of CSS at scale, specifically, the problems that they had at Facebook while dealing with CSS.

We learned how inline styles work in React and why it is good to co-locate the styles within components. We also looked at the limitations of inline styles.

Then, we moved to Radium, which solves the main problems of inline styles, giving us a clear interface to write our CSS in JavaScript. For those who think that inline styles are a bad solution, we moved into the world of CSS Modules, setting up a simple project from

scratch.

Importing the CSS files into our components makes the dependencies clear, and scoping the class names locally avoids clashes. We have looked at how CSS Module's composes is a great feature, and how we can use it in conjunction with Atomic CSS to create a framework for quick prototyping.

Finally, we had a quick look at Styled Components, which is a very promising library and is meant to completely change the way we approach the styling of components.

8

Server-Side Rendering for Fun