Skip to content

Extensions (JS)

Mattia Schiano edited this page Jul 27, 2020 · 1 revision

Extending Haipa in JavaScript is as simple as modifying the HaipaNode class' prototype, exporting the modified HaipaNode, then importing the file where ever you'd like to use the extension.

For example, on my personal website, I made a component for navigation bar links.

// haipaExt.js
const { h } = require('haipa');
const { HaipaNode } = require('haipa/main/node');

HaipaNode.prototype.navIcon = function(name) {
	return this.li(h()
		.a(h().href(`/${name}.html`)
			.class(`navLink`)
			.i(h().class(`icon ...`))
			.span(h(name))
		)
	);
}
...
module.exports = HaipaNode;

// template.js
const { h } = require('haipa');
require('./haipaExt');
...

Clone this wiki locally