Skip to content

Instantly share code, notes, and snippets.

@ivanoats
Created December 10, 2015 04:32
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ivanoats/9f7a3f136a93d965fb17 to your computer and use it in GitHub Desktop.
Save ivanoats/9f7a3f136a93d965fb17 to your computer and use it in GitHub Desktop.
Hyperscript for jQuery
// hyperscript for jQuery
// create nested HTML elements with a DSL
// used to create reusable, interactive HTML components
//
// based on the many implentations out there like
// https://github.com/dominictarr/hyperscript
// https://github.com/Matt-Esch/virtual-dom/tree/master/virtual-hyperscript
// and Elm https://github.com/evancz/elm-html
var $h = function(element, properties, content) {
var $component = $('<' + element + '>');
var props = props || properties || [];
props.forEach(function(prop) {
$component.attr(prop);
});
if (typeof content === 'object') {
$component.append(content);
} else {
$component.text(content);
}
return $component;
};
@ivanoats
Copy link
Author

e.g.

var state = { counter: 1 };
var $component = function(state) {
  return $h('div'
    , [{class: 'container'}]
    , $h('p', [], 'The counter is: ' + state.counter)
  );
}
$('#app').replaceWith($component);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment