liveBook · Manning

Styled-components

As explained in React in Depth

styled-components is a JavaScript and React library for defining component-scoped CSS through styled components.

styled-components is a CSS-in-JS library for defining styled React elements in JavaScript files beside the components that use them. Instead of maintaining a CSS file and applying class names in JSX, define an element with CSS already attached and render it directly. This keeps small components’ structure, behavior, and styles together, though larger component files can become lengthy. section 7.5 section 7.5.3

Figure 7.5 The structure on the left is what CSS Modules give us. But what if we could skip some of that extra work and go to the version on the right, where we don't have to type the class names and can import the CSS directly as components?
figure

Compare the CSS Modules path—CSS file, local class name, and JSX element—with the styled-component path, which removes the class-name step from JSX. section 7.5

A styled definition can do more than attach a fixed rule:

  • Create an element with a tagged template literal, such as `styled.button` followed by backtick-delimited CSS. Interpolate JavaScript values or functions to derive rules from props. section 7.5.1
  • Use nested selectors for states such as `:hover` or `:disabled`, and interpolate another styled component to target it within a rule. section 7.5.1
  • Extend an existing definition with `styled(ExistingComponent)` to add or override rules. When wrapping a custom React component, pass its received `className` to the relevant JSX element so the generated styling applies. section 7.5.1 section 7.5.3

A prop-driven button

`const Submit = styled.button`background-color: ${({ color }) => color};`;` defines CSS from a prop; `<Submit color="red">Red submit!</Submit>` uses it. In a fuller button, styled definitions can sit beside JSX for normal, outline, disabled, wide, and extended custom variants. Install the package with `npm install --save styled-components` before importing `styled`. section 7.5.1 section 7.5.3 section 7.5.2

Colocation, short selectors, composition, and collision-free class naming make this a strong option for complex designs with many one-off components. It is not an automatic choice: unified dashboards and component libraries may suit another approach, and keeping CSS in JavaScript can reduce the overview benefit as components grow. Compare it with CSS files, CSS Modules, or other styling approaches against the needs of the project and team. section 7.5.4 section 7.5.6 section 7.7

How React in Depth covers it

How deep it goes
Works through tagged templates, interpolation, nested selectors, extension, installation, and a button implementation; leaves tagged-template mechanics largely unexplored.
Examples
Uses a prop-colored submit button and a button implementation with outline, disabled, width, animation, and custom-style variants.
What it assumes
Assumes React, JavaScript, JSX, and CSS familiarity while explaining the tagged-template syntax used to embed CSS.
What you can do after
Apply it via styled-components to build and extend styled React elements, including prop-driven rules and custom-component wrappers.
How it connects
Connects CSS-in-JS with CSS Modules, tagged template literals, component composition, pseudoclasses, and prop-driven CSS.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of React in Depth. The section links point to the parts of the book it draws on.