Modular HTML Components
If you open your information-foraging repository and view the files/index.html file you will notice that there is content that appears on the website that is not actually included in the HTML on the page. For example, if you open your site and look at your header and compare that code to what you see in the index.html file you will notice a big difference. In index.html instead of a header there is a special tag:
<ts-load data-name="header"></ts-load>
This course uses a very simple JS library that was built to facilitate data collection of users and a streamlined website building experience for students of varying experience levels. This library does a lot of things behind the scenes that you do not need to worry about, however if you would like to you can create reusable HTML components that will be dynamically loaded into all pages that they are included in. Note that this functionality is new and if you notice any bugs or lacking features reach out sooner than later. Here is how to use it:
- Create a new html file in the scripts/dynamic-dom/html folder. Name it something that is representative of the component, for example this has already been done with the header and footer components.
- Write whatever html code that you need for your element in this file, feel free to reference the header and footer components for inspiration. Only include the HTML that you need and do not include a whole html document with a body and head component as they may not load properly.
- Open this file: scripts/dynamic-dom/html-imports.ts.
- As you can see there are already a few entries in the array named 'itemsToCache';
- Add a new entry to this list, make sure that the name you choose is not that same as an already used name. Feel free to copy this code to use it as a template for your component:
{
name: 'footer',
content: require('./html/footer.html'),
},
Now that you have done all of that you are nearly done. You have built your component and included it in the global components list. The only left to do is to use it!
- In order to use your component open up any of the html files that you wish to use it in. Then paste this tag in the location that you would like to use it (name of your component is the 'exact' name that you assigned it in the 'itemsToCache' array:
<ts-load data-name="NAME-OF-YOUR-COMPONENT"></ts-load>
- Now rebuild your application and you should see your new component on the specified page(s). From this point on you can just edit the html in your component's html file and the rest will happen automatically after your site is rebuilt.
Now that you have built and used your first component here are some guidelines:
- Components can have other components nested inside of them, this could cause an infinitely recursive loop and the current implementation will stop nesting at 100 levels and throw a big error. However, it could be useful to nest components in certain circumstances which I will leave up to you to decide.
- There is no built in way to programmatically add event listeners to these components. I did not want to encourage this behavior so I instead recommend only using this functionality for static content that is cumbersome to rewrite/copy and paste over and over again.
- If you would like to avoid my warning about adding event listeners there are several ways to go about it. The easiest way that I could think of is to embed the listener into your html and expose it to the global namespace by attaching it to the window in your js code. If this sounds confusing then stop by office hours and we can talk more about it, but this functionality should be avoided.
- There is no meaningful way to pass data into your components. They are meant for static content that exists in multiple places. If you have the same component used over and over again with different content there is currently no way to simplify that using this basic component system. If you feel like you need that please let us know and we may be able to implement it for a future class.