Project: Restaurant
Introduction
Let's use what we've learned and take a chance to continue practicing DOM manipulation by dynamically rendering a simple restaurant homepage! By the end, we are going to be using JavaScript alone to generate the entire contents of the website!
Note: DOM elements should be created using JavaScript but styling can be done in a separate CSS file.
Assignment
Start the project the same way you began the webpack tutorial project.
run
npm initin your project directory to generate apackage.jsonfile.run
npm install webpack webpack-cli --save-devto install webpack to the node_modules directory of your project.Quick tip: the
node_modulesfolder can get really big. It is customary to add a.gitignorefile to your project so that you don't have to sync the contents ofnode_modulesto github. The dependencies that are stored there can be installed from your package.json by runningnpm install, so you don't need to sync them.
Create a
srcanddistdirectory with the following contents:an
index.jsfile insrcan
index.htmlfile indist. Go ahead and link themain.jsfile in a script tag.main.jsis the file that will be generated by webpack.create a
webpack.config.jsfile that looks just like our file from the tutorial.
Set up an HTML skeleton inside of
dist/index.htmlwith single<div id="content">.Inside of
src/index.jswrite a simple console.log or alert statement and then runnpx webpack. Load updist/index.htmlin a browser to make sure everything is working correctly.Quick tip #2: if you run
npx webpack --watchyou will not have to rerun webpack every time you make a change.
Create a bare-bones homepage for a restaurant. Include an image, headline, and some copy about how wonderful the restaurant is. It’s okay to hard-code these into the HTML for now just to see how they look on the page.
Now remove those elements from the HTML (so leave only the
<html>,<body>, and<div id="content">tags) and instead create them by using JavaScript only, e.g. by appending each new element todiv#contentonce the page is first loaded. Since we're all set up to write our code in multiple files, let's write this initial page-load function inside of its own module and then import and call it inside ofindex.js.Next, set up your restaurant site to use tabbed browsing to access the Contact and Menu pages. Look at #7 on this hongkiat post for visual inspiration.
Put the contents of each 'tab' inside of its own module. Each module will export a function that creates a div element, adds the appropriate content and styles to that element and then appends it to the DOM.
Write the tab-switching logic inside of
index.js. You should have event listeners for each tab that wipes out the current contents and then runs the correct 'tab module' to populate it again.
If you are using GitHub pages to host your completed page you need to do a tiny bit more work to get it to show up. After running
webpackthe full bundled version of your site is available in thedistfolder, but GH pages is looking for an index.html in the root directory of your project.Simply follow the instructions on this gist. EZPZ!
Recall that the source branch for GitHub Pages is set in your repository's settings.
Last updated