Easy apps with hyperHTML — 10, 3rd party libraries
Tags: hyperhtml, javascript, webdev, tutorial
Published: 2019-06-07 14:57:43

Part 10 written by {% user pinguxx %} {% user paritho %}

1. [Introduction, wire/bind](https://dev.to/pinguxx/easy-apps-with-hyperhtml-1-31cc)
2. [Events and components](https://dev.to/pinguxx/easy-apps-with-hyperhtml-2-hbi)
3. [Moar about components and simple state management](https://dev.to/pinguxx/easy-apps-with-hyperhtml-3-1m3l)
4. [Wire types and custom definitions (intents)](https://dev.to/pinguxx/easy-apps-with-hyperhtml-4-38k3)
5. [Custom elements with hyper](https://dev.to/pinguxx/easy-apps-with-hyperhtml-5-5hdl-temp-slug-7285698)
6. [Customizing my custom elements](https://dev.to/pinguxx/easy-apps-with-hyperhtml-6-h5b-temp-slug-4579110)
7. [Testing!](https://dev.to/pinguxx/easy-apps-with-hyperhtml-7-12g4-temp-slug-5472234)
8. [Async loading, placeholder and a Typeahead with hyper](https://dev.to/pinguxx/easy-apps-with-hyperhtml-8-async-loading-5d96)
9. [Handling routes](https://dev.to/pinguxx/easy-apps-with-hyperhtml-9-routing-59k0)
10. **3rd party libraries**

![](https://thepracticaldev.s3.amazonaws.com/i/q1bejwaip8ahozvruzh6.jpeg)
<figcaption>Photo by <a href="https://unsplash.com/photos/C0koz3G1I4I?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Markus Spiske</a> on <a href="https://unsplash.com/search/photos/block?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption>

Combining third-party libraries with your framework can often be challenging. Because hyperHTML is built on JavaScript standards, using it with other libraries is generally pain-free and easy. This is one of the chief beauties of hyper: it plays well with others.

In part 4 we saw how to define and use intents. In part 2, we worked with components, and in part 5 we looked at custom elements.

> If you missed any of those parts, head back now and review them. We’ll build on what we learned there.

Today, we are going to use 3rd party libraries to spice up all three of these. Just load ‘n use.

### Time 📅 and intents 😉

To start, we are going to use [dayjs](https://github.com/iamkun/dayjs). Dayjs is a simpler and smaller alternative to [moment.js](https://momentjs.com/). If you’re looking for a datetime library with a smaller footprint, a familiar and logical API, dayjs is a great choice. Back in part 4 we had a date format intent that was printing a date in a specific format. It looked like this:

{% gist https://gist.github.com/pinguxx/12c8f2a3e8c8261b0b849744cb379389 %}

Now lets update it to use dayjs.

```
import dayjs from 'dayjs';
```

Here’s our first attempt to use the intent:

{% stackblitz advanced-3rd-01 ctl=1 %}

But we now have the full power of dayjs, so why stop here? We can add formatting options to our intent. Check this out:

{% stackblitz advanced-3rd-02 ctl=1 %}

If you were expecting another step or two, prepare to be disappointed! That’s all there is to using dayjs inside your intents.

### Emojis 😊 and Components ✔️

We’ve already covered the basics of including 3rd party libraries. To add some fun to our components, we are going to convert some text strings to emojis using the [emojify](https://github.com/joypixels/emojify.js) library. If you’re following along at home, go ahead and import the library into a new file.

> Emojify will detect things like :) and :smile: and convert it to an emoji image. See this [cheat sheet](https://www.webfx.com/tools/emoji-cheat-sheet/) for more information on what it can do.

Let’s start with a simple component using `hyper.Component`. This component is going to receive some text, and then `onclick` we are going to use emojify to parse and show emojis. The magic happens in this line

```
emojify.replace(this.text);
```

This component is very simple. We pass in text when we construct the component, and save in two places: `this.text` is what we will transform, and we’ll keep it around in `this._original` so we can revert if we need to.

That too, was easy! Nothing else to do. The full source code:

{% stackblitz advanced-3rd-03 ctl=1 %}

### Charts 📊 and Custom Elements 🎉

Lastly, we are going to create charts with the amazing library [chartist](https://gionkunz.github.io/chartist-js/index.html). Chartist allows us to easily create simple or advanced charts. Just give it data and options, start a new chart of the type you want, and voila! This chart element is not complicated, but it is more involved than the intent and component we made earlier. We’ll just hit the highlights of it here.

As always, first load the package. For chartist to work, we also need to load some CSS.

```
import "chartist/dist/chartist.css";
import Chartist from "chartist";
```

The only attribute we are going to observe is the type; everything else will be plain getters and setters. We’ll need a get/set for data and for options. We’ll also include a get/set for both called details to make it easy to update everything in one call.

{% gist https://gist.github.com/pinguxx/4a838908d7ed6613a92e1753f9055a43 %}

As you can see, we are calling update on every attribute change. This update function is going to either create a chart, or if we already have one, update it. Variables are created in the created() function and updated in render() .

{% gist https://gist.github.com/pinguxx/11b28f8463ac47e72174ef6f8d61cedf %}

All we’re doing here is creating and storing a `<div>` in `this._el`, and starting a chart instance.

To use this element, we will have a custom-chart element in the page with the type.

```
<custom-chart type="Line"></custom-chart>
<custom-chart type="Bar"></custom-chart>
```

Then we can pass data and options to make it render the actual chart

{% gist https://gist.github.com/pinguxx/cc0d892874785cc2e721695e91c5aa06 %}

Full code for the element and example of usage

{% stackblitz advanced-3rd-04 ctl=1 %}

What? We’re already done with this part? There’s no crazy hoops to jump through to add 3rd-party libraries to hyperHTML? There’s no magic code dance we have to do? No library-author to ping for an update/change/bugfix? Nope. HyperHTML does a fantastic job of incorporating other libraries. And why would we expect anything different — hyper is standards-based vanilla JavaScript.

Hopefully we’ve done a good job helping you see the power of combining hyperHTML with some of your favorite libraries. Let us know in the comments what amazing applications you create using hyperHTML.

This is the last part of our series on hyperHTML. We will keep writing about hyperHTML in other posts, so let us know if there are topics you want to see covered. We hope you enjoyed this journey as much as we enjoyed writing it. Thanks for reading.

* * *