See a Styled TodoMVC Application

Story: As a user, I want my application to live in a styled environment.

TodoApp component

Before we start styling our top level component, let's wrap it up in StyleRoot and Style Radium Components.

StyleRoot Component:

Usually wrapped around your top-level App component. StyleRoot wraps its children in a plain div followed by the root style sheet. Radium plugins, like keyframes and media queries, use this style sheet to inject CSS at runtime. Because the style sheet appears after your rendered elements, it is populated correctly during a server render.

StyleRoot transfers all of its props to the rendered div, and is itself wrapped in Radium, so you can pass it inline styles or radiumConfig.

-- StyleRoot Component

Style Component:

The Style component renders an HTML style tag containing a set of CSS rules. Using it, you can define an optional scopeSelector that all selectors in the resulting style element will include. ... (rules prop of the Style component) An object of CSS rules to render. Each key of the rules object is a CSS selector and the value is an object of styles. If rules is empty, the component will render nothing.

-- Style Component

These components will allow us to pass styling to the tags that live outside our components (i.e. html, body...). So, let's make the needed changes:

diff --git a/src/todomvc/components/TodoApp.js b/src/todomvc/components/TodoApp.js
index 1507dbb..ae87fcc 100644
--- a/src/todomvc/components/TodoApp.js
+++ b/src/todomvc/components/TodoApp.js
@@ -4,11 +4,13 @@ import React, {Component} from 'react'
 import PropTypes from 'prop-types'
 import {bindActionCreators} from 'redux'
 import {connect} from 'react-redux'
+import Radium, {StyleRoot, Style} from 'radium'

 import Header from '../components/Header'
 import MainSection from '../components/MainSection'
 import * as actions from '../actions'

+@Radium
 class TodoApp extends Component {
   static propTypes = {
     todos: PropTypes.object.isRequired,
@@ -19,10 +21,13 @@ class TodoApp extends Component {
     const {todos, actions} = this.props;

     return (
-      <div>
-        <Header actions={actions}/>
-        <MainSection todos={todos} actions={actions}/>
-      </div>
+      <StyleRoot>
+        <Style/>
+        <div>
+          <Header actions={actions}/>
+          <MainSection todos={todos} actions={actions}/>
+        </div>
+      </StyleRoot>
     )
   }
 }
(END)

Run the tests and watch some of them fail.

It is clear that wrapping our TodoApp in Radium components has broken our tests. We are going to update the code to make the tests pass.

The tests have not essentially changed. We are just looking for our components a little bit deeper. Run the tests and watch them pass.

The next step is to apply the styling to the page our application will live in. We will use rules prop of Style component.

All of our tests are still passing.

Commit the changes:

Now we are ready to start styling our components

Last updated

Was this helpful?