LiteralJS
Is a simple view library for creating components with Template Literals, Strings, or Arrays and Objects.
Usage without NPM
<!DOCTYPE html>
<html>
<head>
<title>Literal JS Test</title>
<script src="http://unpkg.com/literaljs"></script>
</head>
<body>
<div id="app"></div>
<script>
var firstComponent = Literal.Component(
`<div>
This should render.
<p>Isn't this lovely?</p>
${secondComponent}
</div>`
);
var secondComponent = Literal.Component(
`<div>
Oh yeah!
</div>`
);
Literal.Render(firstComponent, 'app');
</script>
</body>
</html>
Example
Component([string, array])
With template literals, strings:
Literal.Component(
`<p class="corn">
<div>
This is a div!
</div>
</p>`
);
You can also use an array with multiple objects. Besides the example below, objects should accept any attributes and generate proper markup:
Literal.Component([
{
element: 'div',
class: 'layout',
children: [
{
element: 'h1',
id: 'child',
innerHTML: 'This is a child of layout',
'data-example': 'data-works'
},
{
element: 'p',
class: 'content',
innerHTML: 'Amazing!',
events: [
{
type: 'click',
action: function() {
console.log('This event listener is working!');
}
}
]
}
]
},
{
element: 'div',
id: 'notchild',
innerHTML: 'This is not a child of layout.',
events: {
type: 'click',
action: function() {
console.log('This also works!');
}
}
}
]);
Components just accept Template Literals, Strings, or an Array of Objects and generate markup.
You can combine template literal components with ${ }
, or just call another component directly inside of the component object:
var firstComponent = Literal.Component(
`<div>
This should render like React.
<p>Isn't this lovely?</p>
${secondComponent}
</div>`
);
var secondComponent = Literal.Component(
`<div>
Oh yeah!
</div>`
);
It is recommended to use either template literals, strings, or object arrays and not mix them within the same Render call.
Render
Literal.Render(RootComponent, 'app');
The Render method takes the main parent component of your app and id
of the DOM node you want to attach the markup to.