{"componentChunkName":"component---src-templates-lesson-js","path":"/phoenix-chat/npm-package-for-a-react-component-part-1","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\": \"NPM Package for a React Component: Part 1\",\n  \"order\": 23,\n  \"free\": false,\n  \"active\": true,\n  \"tech\": [\"react\", \"npm\"]\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, \"NPM Package for a React Component: Part 1\"), mdx(\"ul\", null, mdx(\"li\", {\n    \"parentName\": \"ul\"\n  }, \"Create the chat component\"), mdx(\"li\", {\n    \"parentName\": \"ul\"\n  }, \"Publish to NPM\")), mdx(\"p\", null, \"So now that we have the ability to sign up and login, as well as a basic interface for chat, we need to create the interface for the anonymous customer.\"), mdx(\"p\", null, \"The primary use-case of this app is as an NPM installation on an existing project to add chat functionality. Fortunately, this is really easy for us to do because of React's modular nature.\"), mdx(\"p\", null, \"This piece of the app is gives us the chat functionality that you can easily install on any client and it's the medium through which our potential customers will contact the company representatives. When it's finished, it will look like the images below.\"), mdx(\"h2\", null, \"Installing dependencies\"), mdx(\"p\", null, \"The first thing we need to is create a new directory and initialize a new NPM package. This is the same as the last time we initialized NPM. \", mdx(\"em\", {\n    \"parentName\": \"p\"\n  }, \"Do not create this directory within the existing phoenix-chat-frontend directory; this is a separate project\"), \". So the directory structure will look roughly like the following, with three separate directories:\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-bash\",\n    \"parentName\": \"pre\"\n  }, \"phoenix-chat\\n  |-- node_modules\\n  |-- dist\\n  |-- src\\n\\nphoenix-chat-frontend\\n\\nphoenix-chat-api\\n\")), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-bash\",\n    \"parentName\": \"pre\"\n  }, \"$ mkdir phoenix-chat\\n$ cd phoenix-chat && npm init\\n\")), mdx(\"p\", null, \"Then we're going install \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"babel\"), \" which will allow us to use React with \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"jsx\"), \" and \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"ES2015\"), \" syntax.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-bash\",\n    \"parentName\": \"pre\"\n  }, \"$ npm install --save-dev babel-cli babel-preset-react \\\\\\n  babel-preset-es2015 babel-preset-stage-0\\n\")), mdx(\"p\", null, \"Then, just as we did with our frontend app, we need to add Babel presets to package.json.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-js\",\n    \"parentName\": \"pre\"\n  }, \"...\\n  \\\"devDependencies\\\": {\\n    ...\\n  },\\n  \\\"babel\\\": {\\n    \\\"presets\\\": [\\n      \\\"es2015\\\",\\n      \\\"react\\\",\\n      \\\"stage-0\\\"\\n    ]\\n  }\\n...\\n\")), mdx(\"h2\", null, \"Creating the PhoenixChat component\"), mdx(\"p\", null, \"Now we need to create our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"PhoenixChat\"), \" component. We will start with the fixed-position chat bubble in the bottom right, then build out the chat interface.\"), mdx(\"p\", null, \"To start, we need a \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"jsx\"), \" file and a \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"style.js\"), \" file that will contain out \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"style\"), \" object. You may recall from an earlier section that there are many ways to import styles in React. For this project, we are going to use inline-styling.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-bash\",\n    \"parentName\": \"pre\"\n  }, \"$ mkdir src dist\\n$ touch src/{PhoenixChat.jsx,style.js,README.md} README.md\\n\")), mdx(\"p\", null, \"And since placeholder content is boring, lets add a chat image. At some point, you should upload your own image to S3 and use that URL for static asset hosting, but for now, we'll just use Github and a filter to turn the image white.\"), mdx(\"p\", null, \"Then let's create the chat bubble component.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-js\",\n    \"parentName\": \"pre\"\n  }, \"import React from \\\"react\\\"\\nimport style from \\\"./style.js\\\"\\n\\nexport class PhoenixChat extends React.Component {\\n  constructor(props) {\\n    super(props)\\n  }\\n\\n  render() {\\n    return (\\n      <div style={style.chatButton}>\\n        <img\\n          src=\\\"https://github.com/LearnPhoenix/graphics/blob/master/phoenix-chat-icon.png?raw=true\\\"\\n          style={style.chatImage}\\n        />\\n      </div>\\n    )\\n  }\\n}\\n\\nexport default PhoenixChat\\n\")), mdx(\"p\", null, \"Then we need to add styling to this component within our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"style.js\"), \" file. We're just going to use pure JavaScript styling, both because it's worth knowing how to do and because it's quick and easy.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-js\",\n    \"parentName\": \"pre\"\n  }, \"export const style = {\\n  chatButton: {\\n    position: \\\"fixed\\\",\\n    right: \\\"20px\\\",\\n    bottom: \\\"20px\\\",\\n    background: \\\"rgb(58, 155, 207)\\\",\\n    color: \\\"white\\\",\\n    zIndex: \\\"1000\\\",\\n    height: \\\"60px\\\",\\n    width: \\\"60px\\\",\\n    border: \\\"1px solid #ccc\\\",\\n    borderRadius: \\\"50%\\\",\\n    display: \\\"flex\\\",\\n    justifyContent: \\\"center\\\",\\n    alignItems: \\\"center\\\",\\n    cursor: \\\"pointer\\\",\\n    boxShadow: \\\"2px 4px 2px -2px rgba(0,0,0,0.3)\\\",\\n  },\\n  chatImage: {\\n    display: \\\"block\\\",\\n    height: \\\"40px\\\",\\n    width: \\\"40px\\\",\\n    filter: \\\"invert(100%)\\\",\\n    WebkitFilter: \\\"invert(100%)\\\",\\n    MozFilter: \\\"invert(100%)\\\",\\n  },\\n}\\n\\nexport default style\\n\")), mdx(\"h2\", null, \"Connecting in development\"), mdx(\"p\", null, \"Before we can check to make sure this works properly, we have to change around a few things in our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"package.json\"), \" file and link this package to our existing frontend (at least temporarily).\"), mdx(\"p\", null, \"We're going to add two scripts to \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"package.json\"), \", and change the main import file. The first, \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"watch\"), \", will watch for changes and automatically compile your code as you go. This is what you'll have running while in development. \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"build\"), \" will prepare the app for production, and \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"version\"), \" prepares the next version for deployment.\"), mdx(\"p\", null, \"We are also changing the import file to \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"dist/phoenix-chat.js\"), \", which is where our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"babel\"), \" command line tool will output the resulting file.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-js\",\n    \"parentName\": \"pre\"\n  }, \"...\\n  \\\"main\\\": \\\"./dist/PhoenixChat.js\\\",\\n  \\\"scripts\\\": {\\n    \\\"test\\\": \\\"echo \\\\\\\"Error: no test specified\\\\\\\" && exit 1\\\",\\n    \\\"watch\\\": \\\"babel src --watch --out-dir dist\\\",\\n    \\\"build\\\": \\\"babel src --out-dir dist\\\"\\n  },\\n\")), mdx(\"p\", null, \"Now that we have our package ready, it's time to link it to our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"phoenix-chat-frontend\"), \". We can use \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"npm link\"), \" to create a globally-installed symbolic link that you can use to locally install the package in a different app. First let's build the package.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-bash\",\n    \"parentName\": \"pre\"\n  }, \"$ npm run build\\n\")), mdx(\"p\", null, \"Then we're going to create an \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"npm link\"), \" between our package and our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"node_modules\"), \" for our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"phoenix-chat-frontend\"), \". Do this by running \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"npm link\"), \" within the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"phoenix-chat\"), \" directory to create the global module, then running \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"npm link phoenix-chat\"), \" from within \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"phoenix-chat-frontend\"), \" to link the module.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-bash\",\n    \"parentName\": \"pre\"\n  }, \"$ npm link\\n$ cd ../phoenix-chat-frontend\\n$ npm link phoenix-chat\\n\")), mdx(\"p\", null, \"Unfortunately with the current version of npm this will not work, because two copies of React will end up being installed and you won't be able to use \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"refs\"), \" \", mdx(\"a\", {\n    \"href\": \"https://gist.github.com/jimfb/4faa6cbfb1ef476bd105\",\n    \"parentName\": \"p\"\n  }, \"(see this specific issue if you are interested)\"), \". According to the \", mdx(\"a\", {\n    \"href\": \"http://webpack.github.io/docs/troubleshooting.html#npm-linked-modules-doesn-t-find-their-dependencies\",\n    \"parentName\": \"p\"\n  }, \"docs\"), \", this is intentional, but it's an easy fix with webpack. Within \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"phoenix-chat-frontend\"), \", add the following to your webpack configuration:\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-js\",\n    \"parentName\": \"pre\"\n  }, \"...\\n  resolve: {\\n    extensions: [ '', '.js' ],\\n    fallback: path.join(__dirname, \\\"node_modules\\\")\\n  },\\n  resolveLoader: {\\n    fallback: path.join(__dirname, \\\"node_modules\\\")\\n  },\\n...\\n\")), mdx(\"p\", null, \"Now if you look in your \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"node_modules\"), \" directory within your frontend app, you should see a \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"phoenix-chat\"), \" module installed. Keep in mind that if you \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"npm prune\"), \" or re-download this repo, you will have to re-link this package since it wont be downloaded from an \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"npm install\"), \".\"), mdx(\"p\", null, \"Now we're going to run the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"phoenix-chat-frontend\"), \" and add this component to our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Home\"), \" component.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-js\",\n    \"parentName\": \"pre\"\n  }, \"import PhoenixChat from \\\"phoenix-chat\\\"\\n\\nexport class Home extends React.Component {\\n  ...\\n\\n  render() {\\n    if (this.props.user.email) {\\n      return (\\n        <Chat>\\n          <PhoenixChat />\\n        </Chat>\\n      )\\n    }\\n    return (\\n      <div className={style.leader}>\\n        <h1 className={style.title}>Phoenix Chat</h1>\\n        { this.state.formState === \\\"signup\\\" ? <Signup /> : null }\\n        { this.state.formState === \\\"login\\\" ? <Login /> : null }\\n        { this.renderToggleContent() }\\n        <img\\n          role=\\\"presentation\\\"\\n          className={style.circles}\\n          src=\\\"/learnphoenix-static-assets/images/circles-full.png\\\" />\\n        <PhoenixChat />\\n      </div>\\n    )\\n  }\\n  ...\\n}\\n\")), mdx(\"p\", null, \"Obviously, in the long-run, there is no reason to have an anonymous chat window on the same screen as the administrator, since he would be anonymously messaging himself, but this makes testing a lot easier.\"), mdx(\"p\", null, \"Now you should see the chat bubble appear in the bottom right.\"), mdx(\"p\", null, \"From here, we can continue working on our npm module while watching our changes take effect in realtime. Within the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"phoenix-chat\"), \" directory, run the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"watch\"), \" command to keep your project updated as you make changes.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-bash\",\n    \"parentName\": \"pre\"\n  }, \"$ npm run watch\\n\")), mdx(\"p\", null, \"But this component isn't really all that useful, since it's just a button that doesn't do anything. In the next lesson, we'll add some more functionality.\"));\n}\n;\nMDXContent.isMDXComponent = true;","excerpt":"NPM Package for a React Component: Part 1 Create the chat component Publish to NPM So now that we have the ability to sign up and login, as…","frontmatter":{"title":"NPM Package for a React Component: Part 1","order":23,"active":true,"free":false,"tech":["react","npm"]}}},"pageContext":{"slug":"npm-package-for-a-react-component-part-1","prevNode":{"frontmatter":{"title":"Check Login Status","active":true,"free":false,"order":22,"tech":["react","redux"]},"fields":{"slug":"check-login-status"}},"nextNode":{"frontmatter":{"title":"NPM Package for a React Component: Part 2","active":true,"free":false,"order":24,"tech":["react","npm"]},"fields":{"slug":"npm-package-for-a-react-component-part-2"}}}},"staticQueryHashes":["1723017916"]}