{"componentChunkName":"component---src-templates-lesson-js","path":"/phoenix-chat/style-the-chat-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 the Chat Component\",\n  \"order\": 36,\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, \"Styling the Chat Component\"), mdx(\"ul\", null, mdx(\"li\", {\n    \"parentName\": \"ul\"\n  }, \"Refactor with flexbox\"), mdx(\"li\", {\n    \"parentName\": \"ul\"\n  }, \"Make it look fancy\")), mdx(\"p\", null, \"In this lesson, we just change our styling to use more of flexbox. This will set us up for our next lesson where we display the recent activity of our users.\"), mdx(\"h2\", null, \"Update styles\"), mdx(\"p\", null, \"Before we get much further, we should update the style of our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Chat\"), \" component and our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Sidebar\"), \" so we can display things in a way that looks normal to a user. For starters, we're going to change around our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Chat\"), \" component so that our elements are no longer absolutely positioned to demonstrate different ways of aligning items and we're going to take this opportunity to show a few more features of flexbox.\"), mdx(\"p\", null, \"After updating our styles, we're going to want a chatroom that looks something like the image below.\"), mdx(\"p\", null, mdx(\"img\", {\n    \"src\": \"/learnphoenix-static-assets/images/chat-component-restyle.png\",\n    \"alt\": \"chat restyle\",\n    \"parentName\": \"p\"\n  })), mdx(\"p\", null, \"Although you generally don't want to mess with your \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"App\"), \" component, we're going to add some background styling to it since we want our entire app to live within this background to give us that fancy, subtle background color.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-js\",\n    \"parentName\": \"pre\"\n  }, \"import React from \\\"react\\\"\\nimport cssModules from \\\"react-css-modules\\\"\\nimport { connect } from \\\"react-redux\\\"\\nimport style from \\\"./style.css\\\"\\nimport Actions from \\\"../../redux/actions\\\"\\n\\nexport class App extends React.Component {\\n  componentDidMount() {\\n    this.props.dispatch(Actions.userAuth())\\n  }\\n\\n  render() {\\n    return (\\n      <div className={style.background}>\\n        <div className={style.backgroundHeader} />\\n        <div className={style.backgroundFooter} />\\n        {this.props.children}\\n      </div>\\n    )\\n  }\\n}\\n\\nexport default connect()(cssModules(App, style))\\n\")), mdx(\"p\", null, \"So now we have three additional elements around our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Chat\"), \" component. The first one we'll just set to \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"fixed\"), \" and cover the entire app. Then we have a header, which we will color our theme red and a footer which we will color an off-white.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-css\",\n    \"parentName\": \"pre\"\n  }, \".background {\\n  position: fixed;\\n  top: 0;\\n  left: 0;\\n  height: 100vh;\\n  width: 100vw;\\n}\\n\\n.backgroundHeader {\\n  position: absolute;\\n  top: 0;\\n  left: 0;\\n  right: 0;\\n  height: 200px;\\n  background-color: rgb(239, 95, 78);\\n}\\n\\n.backgroundFooter {\\n  position: absolute;\\n  top: 200px;\\n  left: 0;\\n  right: 0;\\n  height: calc(100vh - 200px);\\n  background-color: rgb(245, 245, 250);\\n}\\n\")), mdx(\"p\", null, \"This will make our app look somewhat broken since our chat component does not have a background color.\"), mdx(\"h2\", null, \"Refactor Sidebar component\"), mdx(\"p\", null, \"We're going to add a header to this component, which will eventually be used to filter/search through messages and we're going to change the positioning from \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"absolute\"), \" to flexbox. On the React-side, all we're doing is adding a header. On the CSS-side, changing the layout to a flex column.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-js\",\n    \"parentName\": \"pre\"\n  }, \"...\\n\\nexport const Sidebar = props => {\\n  return (\\n    <div className={style.sidebar}>\\n      <div className={style.header}>\\n        Search (coming soon)\\n      </div>\\n      { renderList(props) }\\n    </div>\\n  )\\n}\\n...\\n\")), mdx(\"p\", null, \"And now let's change the sidebar to a flex column with a width of 30%. We're also going to use a pattern from the awesome site \", mdx(\"a\", {\n    \"href\": \"https://subtlepatterns.com\",\n    \"parentName\": \"p\"\n  }, \"Subtle Patterns\"), \" to give our sidebar a little flare.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-css\",\n    \"parentName\": \"pre\"\n  }, \"... .sidebar {\\n  display: flex;\\n  flex-flow: column nowrap;\\n  background: url(\\\"/learnphoenix-static-assets/images/swirl_pattern.png\\\");\\n  overflow-y: scroll;\\n  border-right: 1px solid rgb(213, 213, 213);\\n  width: 30%;\\n  position: relative;\\n}\\n\\n.header {\\n  display: flex;\\n  justify-content: center;\\n  align-items: center;\\n  height: 60px;\\n  background: rgb(238, 238, 239);\\n  border-bottom: 1px solid rgb(213, 213, 213);\\n}\\n\")), mdx(\"p\", null, \"And now our app looks even more broken! Next we need to wrap our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"ChatRoom\"), \" component and change the positioning of the items to use flexbox as well.\"), mdx(\"h2\", null, \"Refactor ChatRoom component\"), mdx(\"p\", null, \"Since the elements in our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"ChatRoom\"), \" component are mostly absolutely positioned, this is going to be a significant change. The first thing we need to do is wrap our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"input\"), \" so we can position it properly, then we're going to wrap our chat component and set up flex rows and columns.\"), mdx(\"p\", null, \"This app is basically just one row with two columns (one 30% width, and one 70% width). The first column is our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Sidebar\"), \", which we already handled, and the second contains the header, our messages, and the input box.\"), mdx(\"p\", null, \"First, let's wrap our whole \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Chat\"), \" component with some padding to show a little bit of the background, and then wrap our other components in a row.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-js\",\n    \"parentName\": \"pre\"\n  }, \"...\\n\\nexport class Chat extends React.Component {\\n  ...\\n\\n  render() {\\n    return (\\n      <div className={style.container}>\\n        <div className={style.row}>\\n        <Sidebar\\n          presences={this.state.presences}\\n          lobbyList={this.state.lobbyList}\\n          onRoomClick={this.changeChatroom} />\\n        <ChatRoom\\n          input={this.state.input}\\n          handleChange={this.handleChange}\\n          handleMessageSubmit={this.handleMessageSubmit}\\n          currentRoom={this.state.currentRoom}\\n          messages={this.state.messages} />\\n        </div>\\n        { this.props.children }\\n      </div>\\n    )\\n  }\\n}\\n\\n...\\n\")), mdx(\"p\", null, \"Now we're going to introduce our styles. These are pretty simple and bring to what is almost a functional app. The first wraps our chat component and pads it with \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"2rem\"), \" so we show a little bit of the background behind our chat interface. It also centers it.\"), mdx(\"p\", null, \"The \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \".row\"), \" is basically our entire app, so we're going to give it a white background and a border. The \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \".column\"), \" contains our header, our messages, and our input box.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-css\",\n    \"parentName\": \"pre\"\n  }, \".container {\\n  display: flex;\\n  justify-content: center;\\n  align-items: center;\\n  position: relative;\\n  height: 100vh;\\n  width: 100vw;\\n  z-index: 100;\\n  padding: 2rem;\\n}\\n\\n.row {\\n  display: flex;\\n  flex-flow: row nowrap;\\n  height: 100%;\\n  width: 100%;\\n  border: 1px solid rgb(213, 213, 213);\\n  background: white;\\n}\\n\")), mdx(\"p\", null, \"The next step is to style our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"ChatRoom\"), \" component. We're also going to add a header which we can fill in with content in a later lesson.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-js\",\n    \"parentName\": \"pre\"\n  }, \"...\\nexport class ChatRoom extends React.Component {\\n  ...\\n\\n  renderHeader() {\\n    return (\\n      <div className={style.header}>\\n        Header (coming soon)\\n      </div>\\n    )\\n  }\\n\\n  ...\\n\\n  render() {\\n    return (\\n      <div className={style.container}>\\n        { this.renderHeader() }\\n        ...\\n\\n      </div>\\n    )\\n  }\\n}\\n...\\n\")), mdx(\"p\", null, \"Then we should style the components in our chatroom.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-css\",\n    \"parentName\": \"pre\"\n  }, \".container {\\n  position: relative;\\n  display: flex;\\n  flex-flow: column nowrap;\\n  position: relative;\\n  width: 70%;\\n}\\n\\n.header {\\n  display: flex;\\n  flex-flow: row nowrap;\\n  width: 100%;\\n  background: rgb(238, 238, 239);\\n  border-bottom: 1px solid rgb(213, 213, 213);\\n  height: 60px;\\n  display: flex;\\n  flex-flow: row nowrap;\\n  justify-content: space-between;\\n  align-items: center;\\n  padding-left: 10px;\\n}\\n\\n.inputWrapper {\\n  padding: 0 20px 20px 20px;\\n}\\n\\n.input {\\n  width: 100%;\\n  line-height: 40px;\\n  font-size: 16px;\\n  outline: none;\\n  border: 1px solid #ccc;\\n  border-radius: 3px;\\n  padding-left: 10px;\\n  color: #333;\\n}\\n\")), mdx(\"p\", null, \"At this point, the app is starting to come together quite nicely! But if you start typing messages, you'll see that the messages flow over behind our input box and our scrolling is all messed up. So let's add some more styles to fix that.\"), mdx(\"p\", null, \"This is also where we introduce \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"flex-grow\"), \". You'll often see \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"flex: 1\"), \" used, which is shorthand for the following:\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-css\",\n    \"parentName\": \"pre\"\n  }, \".style {\\n  flex-grow: 1;\\n  flex-shrink: 1;\\n  flex-basis: auto;\\n}\\n\")), mdx(\"p\", null, \"Spend a couple minutes looking through \", mdx(\"a\", {\n    \"href\": \"https://css-tricks.com/boxes-fill-height-dont-squish/\",\n    \"parentName\": \"p\"\n  }, \"this\"), \" and \", mdx(\"a\", {\n    \"href\": \"https://css-tricks.com/almanac/properties/f/flex/\",\n    \"parentName\": \"p\"\n  }, \"this\"), \" from \", mdx(\"a\", {\n    \"href\": \"https://css-tricks.com\",\n    \"parentName\": \"p\"\n  }, \"CSS-Tricks\"), \" for a great intro to how the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"flex\"), \" property is used. Since our header and input are fixed height, we want to be able to tell our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"messageWrapper\"), \" to fill all the remaining vertical space within our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"row\"), \". Thank to flexbox, all we have to do is set \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"flex-grow: 1\"), \".\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-css\",\n    \"parentName\": \"pre\"\n  }, \"/* ... */\\n\\n.chatWrapper {\\n  padding-top: 5px;\\n  flex-grow: 1;\\n  position: relative;\\n  overflow-y: scroll;\\n}\\n\")), mdx(\"p\", null, \"So now that our app is styled and works properly, let's add some more detail to our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Sidebar\"), \".\"));\n}\n;\nMDXContent.isMDXComponent = true;","excerpt":"Styling the Chat Component Refactor with flexbox Make it look fancy In this lesson, we just change our styling to use more of flexbox. This…","frontmatter":{"title":"Style the Chat Component","order":36,"active":true,"free":false,"tech":["react"]}}},"pageContext":{"slug":"style-the-chat-component","prevNode":{"frontmatter":{"title":"List Active and Inactive Users","active":true,"free":false,"order":35,"tech":["react"]},"fields":{"slug":"list-active-and-inactive-users"}},"nextNode":{"frontmatter":{"title":"Transactional Email with Mailgun and Bamboo","active":true,"free":false,"order":37,"tech":["phoenix","elixir","mailgun"]},"fields":{"slug":"transactional-email-with-mailgun-and-bamboo"}}}},"staticQueryHashes":["1723017916"]}