0.6.1 • Published 8 years ago

simpel v0.6.1

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
8 years ago

✨ SimpEl ✨

Build Status API Doc

A simple way to create and render HTML Elements.

Minimalistic, zero dependency, only 4KB, JavaScript UI library!

No but seriously, SimpEl provides a simple interface to create HTML Elements by defining Components. These can be created by either using JSX or vanilla JavaScript.

Only 2 methods are provided. One to create elements and one to render them.

Table of contents

Why?

Because why reinvent the wheel right? Well, I needed a very, very simple wheel and I really like React and JSX..

When to use

This library should only be used if you need to create almost static HTML pages with non complex UX.

If you need something more powerful, then I recommend you have a look at the amazing React library. The SimpEl API is heavily inspired by it, because React is awesome.

Requirements

You need Node version 4 or greater.

Install

npm i -S simpel

Syntax

import Simpel from 'simpel';

const div = Simpel.createElement('div', null, 'Hello World!');

Simpel.render(
	div,
	document.getElementById('root')
);

Interface

The following methods are provided:

createElement(type, props, children)

Creates an HTML Element.

Arguments

NAMETYPEDESCRIPTIONREQUIRED
typeString|Function|ElementThe Element to be created.Yes
propsObjectContains the properties of a SimpEl Component as key value pairs. This can be an Element attribute like class, an event handler like onClick, etc.No
childrenArray|Element|StringThe child Elements of a SimpEl Component. This can be an Array of Elements, a comma separated list of Elements (i.e. multiple arguments) or a plain String (i.e. to create a text node).No

Returns

HTML Element.

render(element, container)

Renders an HTML Element into the DOM in the supplied container.

Arguments

NAMETYPEDESCRIPTIONREQUIRED
elementElementThe HTML Element to be appended to the DOM.Yes
containerElementThe HTML container into which the HTML Element will be rendered.Yes

Returns

Void.

SimpEl ❤️ JSX

You can use JSX to create SimpEl components. But to do so you need to transform it to SimpEl function calls.

This means the following:

import Simpel from 'simpel';

const profile = <div>
	<img src="avatar.png" class="profile" />

	<h3>
		{
			[ user.firstName, user.lastName ].join(' ')
		}
	</h3>
</div>;

Needs to be transformed into:

import Simpel from 'simpel';

const profile = Simpel.createElement(
	'div',
	null,
	Simpel.createElement(
		'img',
		{ src: 'avatar.png', className: 'profile' }
	),
	Simpel.createElement(
		'h3',
		null,
		[ user.firstName, user.lastName ].join(' ')
	)
);

This can be done with the Babel React JSX Transform Plugin.

How to transform

First install the transform plugin:

npm i -D babel-plugin-transform-react-jsx

Then configure the plugin in your .babelrc file:

{
	"plugins": [
		[
			"transform-react-jsx",
			{
				"pragma": "Simpel.createElement"
			}
		]
	]
}

Note that you need to change the pragma to Simpel.createElement.

Caveats

Always make sure to have the SimpEl library in scope when defining JSX Components. If omitted, the transformed JSX will not be able to execute:

import Simpel from 'simpel';

export default Label = <span>Hi</span>;

Examples

Create and render a list

/**
 * We will render the following HTML:
 *
 * <div id="root">
 *     <ul class="list">
 *         <li class="list-item">milk</li>
 *         <li class="list-item">eggs</li>
 *         <li class="list-item">bread</li>
 *         <li class="list-item">cheese</li>
 *     </ul>
 * </div>
 */

'use strict';

const Simpel = require('simpel');

const List = Simpel.createElement(
	'ul',
	{ 'class': 'list' },
	Simpel.createElement(
		'li',
		{ 'class': 'list-item' },
		'milk'
	),
	Simpel.createElement(
		'li',
		{ 'class': 'list-item' },
		'eggs'
	),
	Simpel.createElement(
		'li',
		{ 'class': 'list-item' },
		'bread'
	),
	Simpel.createElement(
		'li',
		{ 'class': 'list-item' },
		'cheese'
	)
);

Simpel.render(
	List,
	document.getElementById('root')
);

Render Components stored in a variable

import Simpel from 'simpel';

const Story = (
	<div id="story">
		<p class="story-text">Once upon a time..</p>
	</div>
);

Simpel.render(
	<Story />, // You can also reference `Story`, i.e. non JSX.
	document.getElementById('root')
);

Render Functional Components

import Simpel from 'simpel';

const User = props => <span>My name is { props.name }</span>;

Simpel.render(
	<User name="Daniel Illouz" />,
	document.getElementById('root')
);

Render dynamically created Components

import Simpel from 'simpel';

const Groceries = ({ groceries }) => (
	<ul class="list">
		{
			groceries.map(
				item => <li class="list-item">{ item }</li>
			)
		}
	</ul>
);

Simpel.render(
	<Groceries
		groceries={ [ 'milk', 'eggs', 'bread', 'cheese' ] }
	/>,
	document.getElementById('root')
);

Bind an event listener to a Component

import Simpel from 'simpel';

function _showAlert() {
	alert('Hello!');
}

const Alert = <button onClick={ _showAlert }>alert</button>;

Simpel.render(
	<Alert />, // You can also use JSX, i.e. `<Alert />`.
	document.getElementById('root')
);

The following events are supported:

EVENT NAMEATTRIBUTE NAME
abortonAbort
animationstartonAnimationStart
animationiterationonAnimationIteration
animationendonAnimationEnd
bluronBlur
canplayonCanPlay
canplaythroughonCanPlayThrough
changeonChange
clickonClick
contextmenuonContextMenu
copyonCopy
cutonCut
dblclickonDoubleClick
dragonDrag
dragendonDragEnd
dragenteronDragEnter
dragexitonDragExit
dragleaveonDragLeave
dragoveronDragOver
dragstartonDragStart
droponDrop
durationchangeonDurationChange
emptiedonEmptied
encryptedonEncrypted
endedonEnded
erroronError
focusonFocus
inputonInput
invalidonInvalid
keydownonKeyDown
keypressonKeyPress
keyuponKeyUp
loadonLoad
loadeddataonLoadedData
loadedmetadataonLoadedMetadata
loadstartonLoadStart
pauseonPause
playonPlay
playingonPlaying
progressonProgress
mousedownonMouseDown
mouseenteronMouseEnter
mouseleaveonMouseLeave
mousemoveonMouseMove
mouseoutonMouseOut
mouseoveronMouseOver
mouseuponMouseUp
pasteonPaste
ratechangeonRateChange
resetonReset
scrollonScroll
seekedonSeeked
seekingonSeeking
submitonSubmit
stalledonStalled
suspendonSuspend
timeupdateonTimeUpdate
transitionendonTransitionEnd
touchcancelonTouchCancel
touchendonTouchEnd
touchmoveonTouchMove
touchstartonTouchStart
volumechangeonVolumeChange
waitingonWaiting
wheelonWheel

Use refs to reference other Components

import Simpel from 'simpel';

let n = 0;

function _addItem(e) {
	e.preventDefault();

	// `SimpEl` returns HTML Elements so you can use
	// all API methods like `appendChild`.
	this.refs.list.appendChild(
		<li>item { n++ }</li>
	);
}

const Widget = () => (
	<div>
		<ul ref="list" />

		<button onClick={ _addItem }>
			add
		</button>
	</div>
);

Simpel.render(
	<Widget />,
	document.getElementById('root')
);

License

Apache 2.0 © Daniël Illouz

0.6.1

8 years ago

0.5.0

8 years ago

0.4.0

8 years ago

0.3.0

8 years ago

0.2.0

8 years ago

0.1.0

8 years ago