{"componentChunkName":"component---src-templates-lesson-js","path":"/phoenix-chat/create-a-reusable-button-component","result":{"data":{"mdx":{"body":"function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }\n\nfunction _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }\n\nfunction _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }\n\n/* @jsx mdx */\nvar _frontmatter = {\n  \"title\": \"Create a Reusable Button Component\",\n  \"order\": 14,\n  \"free\": false,\n  \"active\": true,\n  \"tech\": [\"react\"]\n};\n\nvar makeShortcode = function makeShortcode(name) {\n  return function MDXDefaultShortcode(props) {\n    console.warn(\"Component \" + name + \" was not imported, exported, or provided by MDXProvider as global scope\");\n    return mdx(\"div\", props);\n  };\n};\n\nvar layoutProps = {\n  _frontmatter: _frontmatter\n};\nvar MDXLayout = \"wrapper\";\nreturn function MDXContent(_ref) {\n  var components = _ref.components,\n      props = _objectWithoutProperties(_ref, [\"components\"]);\n\n  return mdx(MDXLayout, _extends({}, layoutProps, props, {\n    components: components,\n    mdxType: \"MDXLayout\"\n  }), mdx(\"h1\", null, \"Create a Reusable Button Component\"), mdx(\"ul\", null, mdx(\"li\", {\n    \"parentName\": \"ul\"\n  }, \"CSS variables\"), mdx(\"li\", {\n    \"parentName\": \"ul\"\n  }, \"Buttons\"), mdx(\"li\", {\n    \"parentName\": \"ul\"\n  }, \"Unit tests\")), mdx(\"p\", null, \"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 \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"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 \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"props\"), \" to tell the button what to display.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-bash\",\n    \"parentName\": \"pre\"\n  }, \"$ mkdir app/components/Button\\n$ touch app/components/Button/{README.md,spec.js,index.js,style.css}\\n\")), mdx(\"p\", null, \"Our buttons will take in two properties, \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"type\"), \" and \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"onClick\"), \". The \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"type\"), \" will determine the style, and \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"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 \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"props.children\"), \". We are going to create our button as an efficient \", mdx(\"a\", {\n    \"href\": \"https://facebook.github.io/react/blog/2015/10/07/react-v0.14.html#stateless-functional-components\",\n    \"parentName\": \"p\"\n  }, \"stateless functional component\"), \".\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-js\",\n    \"parentName\": \"pre\"\n  }, \"import React from \\\"react\\\"\\nimport cssModules from \\\"react-css-modules\\\"\\nimport style from \\\"./style.css\\\"\\n\\nexport const Button = props => {\\n  return (\\n    <button style={props.style} onClick={props.onClick} className={style[props.type]}>\\n      {props.children}\\n    </button>\\n  )\\n}\\n\\nexport default cssModules(Button, style)\\n\")), mdx(\"p\", null, \"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 \", mdx(\"a\", {\n    \"href\": \"https://en.wikipedia.org/wiki/Don%27t_repeat_yourself\",\n    \"parentName\": \"p\"\n  }, \"DRY\"), \" as possible.\"), mdx(\"h2\", null, \"Style the buttons\"), mdx(\"p\", null, \"Now back to our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Button\"), \" component, within our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"style.css\"), \" file, add the following, which will compose our buttons without repeating code (explained below).\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-css\",\n    \"parentName\": \"pre\"\n  }, \".button {\\n  color: white;\\n  border-radius: 5px;\\n  border: 1px;\\n  padding: 0.8rem 2rem;\\n  cursor: pointer;\\n  font-size: 1em;\\n  align-self: center;\\n  outline: none;\\n}\\n\\n.primary {\\n  composes: button;\\n  background: rgb(239, 95, 78);\\n}\\n.primary:hover {\\n  background: color(rgb(239, 95, 78) lightness(+7%));\\n}\\n\\n.flat {\\n  composes: button;\\n  background: white;\\n  color: rgb(239, 95, 78);\\n  border: 1px solid rgb(239, 95, 78);\\n}\\n.flat:hover {\\n  color: color(rgb(239, 95, 78) lightness(+15%));\\n}\\n\\n.accentPrimary {\\n  composes: button;\\n  background: rgb(58, 155, 207);\\n  color: white;\\n}\\n.accentPrimary:hover {\\n  background: color(rgb(58, 155, 207) lightness(+10%));\\n}\\n\\n.accentFlat {\\n  composes: button;\\n  background: white;\\n  color: rgb(58, 155, 207);\\n  border: 1px solid rgb(58, 155, 207);\\n}\\n.accentFlat:hover {\\n  color: color(rgb(58, 155, 207) lightness(+15%));\\n}\\n\\n.disabled {\\n  composes: button;\\n  cursor: not-allowed;\\n}\\n\")), mdx(\"p\", null, \"First, we're creating the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"button\"), \" class, which will contain all of the styles that are shared between the other buttons.\"), mdx(\"p\", null, \"Then we compose using \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"composes\"), \", which we get from CSS Modules, a \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"flat\"), \", a \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"primary\"), \", and a \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"disabled\"), \" button from the shared characteristics of the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"button\"), \", as well as an alternative color (\\\"accent\\\").\"), mdx(\"p\", null, \"Alternatively, we could use the next-version syntax for applying styles to multiple classes. You can do this by assigning a variable to \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \":root\"), \", then using \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"@apply\"), \" to add the styles to other classes. For more on this, see the \", mdx(\"a\", {\n    \"href\": \"http://cssnext.io/features/#custom-properties-set-apply\",\n    \"parentName\": \"p\"\n  }, \"(docs)\"), \". The downside is that this will repeat code in your stylesheet.\"), mdx(\"p\", null, \"Because we're using CSSNext, we can use the new \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"color\"), \" function \", mdx(\"a\", {\n    \"href\": \"https://github.com/postcss/postcss-color-function\",\n    \"parentName\": \"p\"\n  }, \"(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 \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"+\"), \" to a \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"-\"), \".\"), mdx(\"p\", null, \"We should also define our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"PropTypes\"), \" since we'll be using this button a lot and it will be good to know if we are accidentally causing an error.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-js\",\n    \"parentName\": \"pre\"\n  }, \"...\\n\\nButton.propTypes = {\\n  style: React.PropTypes.object,\\n  onClick: React.PropTypes.func,\\n  type: React.PropTypes.string.isRequired,\\n  children: React.PropTypes.node.isRequired\\n}\\n\")), mdx(\"p\", null, \"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 \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Button\"), \", if we do not send \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"type\"), \" or \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"children\"), \" to our Button, it won't render properly, so we require that both of those props are passed in.\"), mdx(\"p\", null, \"We are setting \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"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.\"), mdx(\"h2\", null, \"Unit tests\"), mdx(\"p\", null, \"Now we should write our unit tests for our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Button\"), \" component. We know that we want our button to 1. Render its children, 2. Take in \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"primary\"), \", \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"flat\"), \", and \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"disabled\"), \" as \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"type\"), \", 3. Handle click events, and 4. Render as a \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"button\"), \".\"), mdx(\"p\", null, \"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 \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Modal\"), \" component. The only difference here is that we are passing in the style as \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"type\"), \" and checking to make sure that component rendered with the right style.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-js\",\n    \"parentName\": \"pre\"\n  }, \"import React from \\\"react\\\"\\nimport expect from \\\"expect\\\"\\nimport { shallow } from \\\"enzyme\\\"\\n\\nimport { Button } from \\\"./\\\"\\n\\nconst props = {\\n  type: \\\"primary\\\",\\n  onClick: () => {},\\n}\\n\\ndescribe(\\\"<Button />\\\", () => {\\n  it(\\\"should render its children\\\", () => {\\n    const children = <p>foo</p>\\n    const renderedComponent = shallow(<Button {...props}>{children}</Button>)\\n    expect(renderedComponent.contains(children))\\n  })\\n  it(\\\"should render a <button> element\\\", () => {\\n    const renderedComponent = shallow(<Button {...props}>Test</Button>)\\n    expect(renderedComponent.is(\\\"button\\\")).toEqual(true)\\n  })\\n  it(\\\"should handle click events\\\", () => {\\n    const onClickSpy = expect.createSpy()\\n    const renderedComponent = shallow(\\n      <Button {...props} onClick={onClickSpy}>\\n        Test\\n      </Button>\\n    )\\n    renderedComponent.find(\\\"button\\\").simulate(\\\"click\\\")\\n    expect(onClickSpy).toHaveBeenCalled()\\n  })\\n})\\n\")), mdx(\"p\", null, \"And now to use your new \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Button\"), \" component, all you have to do is import it and pass in the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"type\"), \" and \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"children\"), \" at a minimum.\"));\n}\n;\nMDXContent.isMDXComponent = true;","excerpt":"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…","frontmatter":{"title":"Create a Reusable Button Component","order":14,"active":true,"free":false,"tech":["react"]}}},"pageContext":{"slug":"create-a-reusable-button-component","prevNode":{"frontmatter":{"title":"User Accounts and Signup: Part 2","active":true,"free":true,"order":13,"tech":["phoenix"]},"fields":{"slug":"user-accounts-and-signup-part-2"}},"nextNode":{"frontmatter":{"title":"Login and Signup Forms","active":true,"free":false,"order":15,"tech":["react"]},"fields":{"slug":"login-and-signup-forms"}}}},"staticQueryHashes":["1723017916"]}