Create a Reusable Button Component

  • CSS variables
  • Buttons
  • Unit tests

Before we get too far, we're going to need buttons. Buttons are a simple component that we can easily make reusable, so rather than use a global style and use a class like button-primary as you would with a framework like Bootstrap, we're going to create a component that we can reuse. We're going to have at least three types of buttons and we will use props to tell the button what to display.

$ mkdir app/components/Button
$ touch app/components/Button/{README.md,spec.js,index.js,style.css}

Our buttons will take in two properties, type and onClick. The type will determine the style, and onClick will take in a function that tell the button what to do when it's clicked. We will also pass in the text of the button as props.children. We are going to create our button as an efficient stateless functional component.

import React from "react"
import cssModules from "react-css-modules"
import style from "./style.css"
export const Button = props => {
return (
<button style={props.style} onClick={props.onClick} className={style[props.type]}>
{props.children}
</button>
)
}
export default cssModules(Button, style)

The code above should all look familiar. We can style these components with the same technique we used previously, but since different button types will share a lot of styling, we should look into ways of composing these to keep our CSS as DRY as possible.

Style the buttons

Now back to our Button component, within our style.css file, add the following, which will compose our buttons without repeating code (explained below).

.button {
color: white;
border-radius: 5px;
border: 1px;
padding: 0.8rem 2rem;
cursor: pointer;
font-size: 1em;
align-self: center;
outline: none;
}
.primary {
composes: button;
background: rgb(239, 95, 78);
}
.primary:hover {
background: color(rgb(239, 95, 78) lightness(+7%));
}
.flat {
composes: button;
background: white;
color: rgb(239, 95, 78);
border: 1px solid rgb(239, 95, 78);
}
.flat:hover {
color: color(rgb(239, 95, 78) lightness(+15%));
}
.accentPrimary {
composes: button;
background: rgb(58, 155, 207);
color: white;
}
.accentPrimary:hover {
background: color(rgb(58, 155, 207) lightness(+10%));
}
.accentFlat {
composes: button;
background: white;
color: rgb(58, 155, 207);
border: 1px solid rgb(58, 155, 207);
}
.accentFlat:hover {
color: color(rgb(58, 155, 207) lightness(+15%));
}
.disabled {
composes: button;
cursor: not-allowed;
}

First, we're creating the button class, which will contain all of the styles that are shared between the other buttons.

Then we compose using composes, which we get from CSS Modules, a flat, a primary, and a disabled button from the shared characteristics of the button, as well as an alternative color ("accent").

Alternatively, we could use the next-version syntax for applying styles to multiple classes. You can do this by assigning a variable to :root, then using @apply to add the styles to other classes. For more on this, see the (docs). The downside is that this will repeat code in your stylesheet.

Because we're using CSSNext, we can use the new color function (docs), which replace a lot of the useful functions that we get with a preprocessor. In this case, we are taking a color and making it lighter by a certain percentage. You can also make it darker by changing the + to a -.

We should also define our PropTypes since we'll be using this button a lot and it will be good to know if we are accidentally causing an error.

...
Button.propTypes = {
style: React.PropTypes.object,
onClick: React.PropTypes.func,
type: React.PropTypes.string.isRequired,
children: React.PropTypes.node.isRequired
}

If you're not familiar with PropTypes, you can think of them as a way to warn you in advance if you are not sending a necessary piece of data to a particular component. In the case of our Button, if we do not send type or children to our Button, it won't render properly, so we require that both of those props are passed in.

We are setting onClick as an optional parameter since we may not necessarily want the button to do anything if we click on it--sometimes the action will be handled by the wrapper outside of the button.

Unit tests

Now we should write our unit tests for our Button component. We know that we want our button to 1. Render its children, 2. Take in primary, flat, and disabled as type, 3. Handle click events, and 4. Render as a button.

Go ahead and add all the following tests. These should all look very familiar as they are almost identical to the tests we wrote for our Modal component. The only difference here is that we are passing in the style as type and checking to make sure that component rendered with the right style.

import React from "react"
import expect from "expect"
import { shallow } from "enzyme"
import { Button } from "./"
const props = {
type: "primary",
onClick: () => {},
}
describe("<Button />", () => {
it("should render its children", () => {
const children = <p>foo</p>
const renderedComponent = shallow(<Button {...props}>{children}</Button>)
expect(renderedComponent.contains(children))
})
it("should render a <button> element", () => {
const renderedComponent = shallow(<Button {...props}>Test</Button>)
expect(renderedComponent.is("button")).toEqual(true)
})
it("should handle click events", () => {
const onClickSpy = expect.createSpy()
const renderedComponent = shallow(
<Button {...props} onClick={onClickSpy}>
Test
</Button>
)
renderedComponent.find("button").simulate("click")
expect(onClickSpy).toHaveBeenCalled()
})
})

And now to use your new Button component, all you have to do is import it and pass in the type and children at a minimum.

Previous: User Accounts and Signup: Part 2Next: Login and Signup Forms