We’re going to explore how to include JavaScript in our pattern library, specifically how to integrate the form validation helper.
But some of you may want to include more JavaScript in the future—so this will help you set it up.
Walk through the process of integrating JavaScript into your pattern library: both JavaScript you’ve written & 3rd-party JavaScript.
We’re going to explore how to include JavaScript in our pattern library, specifically how to integrate the form validation helper.
But some of you may want to include more JavaScript in the future—so this will help you set it up.
Remember the purpose of this lesson is to type the code out yourself—build up that muscle memory in your fingers!
Let’s start some JavaScript integration in our pattern library by including the form validation helper that we previously explored. It will be very helpful when completing our form patterns.
form-validation-helper
»Make sure to press “Download ZIP” instead of what we normally press.
Inside the download package there is a file named index.js
—that’s the file that we need.
But keeping it called index.js
isn’t helpful for us—it’s not descriptive enough.
So, let’s move it and rename it:
js
folder within your ecommerce-pattern-library
folder.index.js
into your new js
folder.index.js
to form-validation-helper.js
Integrating JavaScript into our pattern library follows the identical format to CSS: a main.js
file with a bunch of includes inside it.
Inside your code editor, make a new file and immediately save it to your js
folder named exactly main.js
—this will be the shared JavaScript code file, just like our main.css
Now add this code into your main.js
file:
---
---
{% include_relative form-validation-helper.js %}
{% pattern_js %}
We can add JavaScript files in our patterns the same way we add CSS files: just right inside the pattern’s folder.
This will look inside every pattern you have for .js
files and include them into this location.