cinco
HTML5 as ES6 objects - easy to manipulate and render. Handy for rapid prototyping. Is isomorphic.
Overview
Example on how to create a HTML5 document and then render it to string
import { Document, Element } from cinco;
let document = new Document()
.add(
new Element('title').text('Good morning')
);
if ( new Date().getHours > 12 ) {
document.find('title').get(0).text('Good evening');
}
document.render();
<!doctype html>
<meta charset="utf-8" />
<title>Good morning</title>
ES5 support
var cinco = require('cinco/dist'),
Element = cinco.Element,
Document = cinco.Document;
Element
new Element(
{ String | Function } selector,
{ Object | Function }? attributes,
{ [Element] | Elements | Function | [Function] }? children
)
let myElement = new Element('h1');
myElement.render()
You can declare attributes in the selector as well:
new Element('h1#foo.bar.barz');
Attributes
Attributes are passed as an object:
new Element('a', { 'href': '/' });
new Element('a').attr('href', '/');
You can also pass functions:
var props = {
signedIn: true,
user: {
id: 123
}
}
new Element('a', { href: () => '/' });
new Element('a', { href: async() => await async() });
Manipulate text
let p = new Element('p');
p.text('Hello world!')
p.text();
Conditional rendering
The conditions, if one evaluated to false, will skip the rendering of the element.
new Element('p').condition(true);
new Element('p').condition(false);
let element = new Element('p').condition(async() => await async());
element.satisfies();
Append children
new Element('foo').add(new Element('bar'));
Clearing all children
new Element('foo').add(new Element('bar')).empty();
Remove a child
let form = new Element('form');
let fieldset = new Element('fieldset');
form.add(fieldset);
form.render();
form.remove(child => child.is('fieldset'));
form.render();
Find
Utility to find and manipulate elements. Returns Elements
let form = new Element('form').add(
new Element('fieldset').add(
new Element('legend').text('Legend'),
new Element('section.form-group').add(
new Element('button')
)
)
);
form.find('button').each(button => button.text('Click me!'));
form.findByText('Click me!');
form.findByText(/click/i);
Classes
let elem = new Element('.c1', { className: 'c2 c3' });
let elem = new Element('.c1', { className: ['c2', 'c3'] });
let elem = new Element('.c1').addClass('c2', 'c3');
elem.removeClass('c3');
elem.hasClass('c3');