{"componentChunkName":"component---src-templates-lesson-js","path":"/phoenix-chat/style-a-react-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\": \"Style a React Component\",\n  \"order\": 7,\n  \"free\": true,\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, \"Style a React Component\"), mdx(\"ul\", null, mdx(\"li\", {\n    \"parentName\": \"ul\"\n  }, \"Creating and styling the Sidebar\"), mdx(\"li\", {\n    \"parentName\": \"ul\"\n  }, \"CSS best practices\")), mdx(\"h2\", null, \"Creating a Sidebar component\"), mdx(\"p\", null, \"Now that we finally a basic Webpack configuration ready, let's create a new \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Sidebar\"), \" component and use our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"css-modules\"), \" to style it.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-bash\",\n    \"parentName\": \"pre\"\n  }, \"$ mkdir app/components/Sidebar\\n$ touch app/components/Sidebar/{index.js,style.css}\\n\")), mdx(\"p\", null, \"Within this new component, we need to define our dependencies, create a placeholder component, then export that component wrapped in our styles.\"), 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 Sidebar = () => {\\n  return (\\n    <div>\\n      This is the sidebar\\n    </div>\\n  )\\n}\\n\\nexport default cssModules(Sidebar, style)\\n\")), mdx(\"p\", null, \"The third and fourth lines import our styles from our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"styles.css\"), \" file and the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"cssModules\"), \" function which allows us to export this component with its local styles. You'll see at the bottom of the file that, rather than simply exporting the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Sidebar\"), \" component, we are wrapping it in the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"cssModules\"), \" function and pairing it with the styles that we imported above.\"), mdx(\"p\", null, mdx(\"em\", {\n    \"parentName\": \"p\"\n  }, \"A note on relative vs. absolute paths:\"), \" For the most part, we want to use absolute paths. Unfortunately, the coverage package we're going to use (Istanbul/nyc) does not sufficiently support the use of \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"jsdom\"), \" in favor of Karma (to be explained in a future lesson), so we will not be able to run webpack when running our tests, so for now, we will have to use relative paths.\"), mdx(\"p\", null, \"Maintaining this consistency makes it much easier to move files around since if everything is absolute, you can merely find and replace. If on the other hand, everything is relative, you have to change the relative path all over your app, which can be a nightmare. Unfortunately, we do not have a choice at the moment, so we will use relative paths so we don't have to run our tests through webpack.\"), mdx(\"p\", null, \"We want our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Sidebar\"), \" to look roughly like the image below:\"), mdx(\"p\", null, mdx(\"em\", {\n    \"parentName\": \"p\"\n  }, \"image coming soon\")), mdx(\"p\", null, \"We will use \", mdx(\"a\", {\n    \"href\": \"https://css-tricks.com/snippets/css/a-guide-to-flexbox/\",\n    \"parentName\": \"p\"\n  }, \"Flexbox\"), \" for most of our alignment because it is now widely supported and much, much better than hacking away with tables. It's also the standard for React Native. If you break this \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Sidebar\"), \" component down, you can see that our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Sidebar\"), \" is really a single column of fixed width which contains a list of contact cards.\"), mdx(\"p\", null, mdx(\"em\", {\n    \"parentName\": \"p\"\n  }, \"image coming soon\")), mdx(\"p\", null, \"We will eventually split these individual components into smaller pieces, but for now, we can just put them all under the single \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Sidebar\"), \" component.\"), mdx(\"p\", null, \"Let's start with something small:\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-js\",\n    \"parentName\": \"pre\"\n  }, \"...\\nexport const Sidebar = () => {\\n  return (\\n    <div>\\n      <h3>John Smith</h3>\\n      <p>Last active: {Math.floor((Math.random() * 10) + 1)} minutes ago.</p>\\n    </div>\\n  )\\n}\\n...\\n\")), mdx(\"p\", null, \"In order to get this to render, we have to import this component into another component. Since we're going to want to render the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Sidebar\"), \" on on the root route, we should import it into our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Home\"), \" component.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-js\",\n    \"parentName\": \"pre\"\n  }, \"...\\nimport { default as Sidebar } from \\\"../Sidebar\\\"\\n\\nexport class Home extends React.Component {\\n  render() {\\n    return (\\n      <div>\\n        <Sidebar />\\n        Home component\\n      </div>\\n    )\\n  }\\n}\\n...\\n\")), mdx(\"p\", null, \"Now when you refresh your page, you'll see your \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Sidebar\"), \".\"), mdx(\"p\", null, mdx(\"em\", {\n    \"parentName\": \"p\"\n  }, \"image coming soon\")), mdx(\"p\", null, \"But it hardly looks like a sidebar... So now we need to style it.\"), mdx(\"h2\", null, \"Optional formatting for styles\"), mdx(\"p\", null, \"There are several options when it comes to styling with \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"react-css-modules\"), \". One is to import your styles and reference each style as a property of the style object.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-css\",\n    \"parentName\": \"pre\"\n  }, \".container {\\n  background: red;\\n}\\n\")), 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\\nconst HelloWorld = () => {\\n  return (\\n    <div className={style.container}>\\n      Hello World!\\n    </div>\\n  )\\n}\\n\")), mdx(\"p\", null, \"Another option is the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"styleName\"), \" reference that \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"react-css-modules\"), \" gives us. This allows us to use a string rather than an object reference. Because of the way components are wrapped using \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"cssModules\"), \", you'll get better errors (compilation instead of runtime) when you mess up or forget to add a style.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-js\",\n    \"parentName\": \"pre\"\n  }, \"const HelloWorld = () => {\\n  return (\\n    <div styleName=\\\"container\\\">\\n      Hello World!\\n    </div>\\n  )\\n}\\n\")), mdx(\"p\", null, \"That said, we're going to use the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"className\"), \" with the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"style\"), \" object because it's currently the most widely supported.\"), mdx(\"p\", null, mdx(\"em\", {\n    \"parentName\": \"p\"\n  }, \"A note on using multiple classes for a single element:\"), \" Don't do it. But if you must, you can \", mdx(\"a\", {\n    \"href\": \"https://github.com/gajus/react-css-modules#multiple-css-modules\",\n    \"parentName\": \"p\"\n  }, \"set an option\"), \" within \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"react-css-modules\"), \" to allow you to include multiple modules for a single element. Just be aware, once you start down the road of multiple class names, you start accumulating dead code that is time consuming and tedious to refactor out of your codebase.\"), mdx(\"h2\", null, \"Styling the Sidebar component\"), mdx(\"p\", null, \"The first thing we should do is give our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Sidebar\"), \" a few \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"className\"), \" attributes so we can reference them in our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"style.css\"), \" file:\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-js\",\n    \"parentName\": \"pre\"\n  }, \"...\\nexport const Sidebar = () => {\\n  return (\\n    <div className={style.sidebar}>\\n      <h3>John Smith</h3>\\n      <p>Last active: {Math.floor((Math.random() * 10) + 1)} minutes ago.</p>\\n    </div>\\n  )\\n}\\n...\\n\")), mdx(\"p\", null, \"Then within our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"style.css\"), \" file, add the classes that we will use to style our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Sidebar\"), \":\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-css\",\n    \"parentName\": \"pre\"\n  }, \".sidebar {\\n  position: absolute;\\n  left: 0;\\n  top: 0;\\n  height: 100vh;\\n  width: 300px;\\n  background: #eeeeee;\\n  overflow-y: scroll;\\n  border-right: 1px solid #ccc;\\n}\\n\")), mdx(\"p\", null, \"Most of this styling is straightforward. If you are not comfortable with Flexbox, I recommend checking out \", mdx(\"a\", {\n    \"href\": \"http://flexboxfroggy.com/\",\n    \"parentName\": \"p\"\n  }, \"Flexbox Froggy\"), \", which is a game that will get you up to speed in about 10 minutes.\"), mdx(\"p\", null, \"You may have noticed that our \\\"Home component\\\" text disappeared. That's because it's hidden behind the absolutely positioned \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Sidebar\"), \". Let's create a wrapper in our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Home\"), \" component that will eventually contain all of our chat messages.\"), 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\\nimport { default as Sidebar } from \\\"../Sidebar\\\"\\n\\nexport class Home extends React.Component {\\n  render() {\\n    return (\\n      <div>\\n        <Sidebar />\\n        <div className={style.chatWrapper}>\\n          Home component\\n        </div>\\n      </div>\\n    )\\n  }\\n}\\n\\nexport default cssModules(Home, style)\\n\")), mdx(\"p\", null, \"Then let's put 300px of margin on the left so that all of our content displays on the page.\"), mdx(\"p\", null, \"Also note that you cannot use dashes (\", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"-\"), \") in the names of your styles when using object notation because JavaScript will interpret them as a mathematical expression. As it turns out, camelCase is already supported by CSS, so this shouldn't cause any problems over the long-run.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-bash\",\n    \"parentName\": \"pre\"\n  }, \"$ touch app/components/Home/style.css\\n\")), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-css\",\n    \"parentName\": \"pre\"\n  }, \".chatWrapper {\\n  margin-left: 300px;\\n}\\n\")), mdx(\"h2\", null, \"Additional\"), mdx(\"p\", null, mdx(\"em\", {\n    \"parentName\": \"p\"\n  }, \"A note on nesting:\"), \" Nesting your styles is generally considered bad practice. It makes refactoring harder and can lead to a lot of confusion. There are some exceptions.\"), mdx(\"p\", null, \"Some people prefer to nest things like \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \":hover\"), \" or other pseudo-classes, as they could make it easier to understand the full functionality of a particular selector. This is also the case with nested \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"@media\"), \" queries, since it can be useful to quickly see which element is being affected by a particular media query. It's mostly a matter of preference, but the larger style guides tend to avoid nesting, so we will also avoid it.\"));\n}\n;\nMDXContent.isMDXComponent = true;","excerpt":"Style a React Component Creating and styling the Sidebar CSS best practices Creating a Sidebar component Now that we finally a basic Webpack…","frontmatter":{"title":"Style a React Component","order":7,"active":true,"free":true,"tech":["react"]}}},"pageContext":{"slug":"style-a-react-component","prevNode":{"frontmatter":{"title":"Set Up Styles in Webpack","active":true,"free":true,"order":6,"tech":["react","webpack"]},"fields":{"slug":"set-up-styles-in-webpack"}},"nextNode":{"frontmatter":{"title":"Snippets and Aliases","active":true,"free":true,"order":8,"tech":["atom","vim"]},"fields":{"slug":"snippets-and-aliases"}}}},"staticQueryHashes":["1723017916"]}