From 78cbe0e71339c254eca4e9e22e7f612a8b60605b Mon Sep 17 00:00:00 2001 From: spacejack Date: Mon, 3 Oct 2016 09:12:44 -0400 Subject: [PATCH] Added hyperscript module (#11661) --- hyperscript/hyperscript-tests.ts | 58 ++++++++++++++++++++++++++++++++ hyperscript/hyperscript.d.ts | 19 +++++++++++ 2 files changed, 77 insertions(+) create mode 100644 hyperscript/hyperscript-tests.ts create mode 100644 hyperscript/hyperscript.d.ts diff --git a/hyperscript/hyperscript-tests.ts b/hyperscript/hyperscript-tests.ts new file mode 100644 index 0000000000..b850bbf4cb --- /dev/null +++ b/hyperscript/hyperscript-tests.ts @@ -0,0 +1,58 @@ +/// +import * as h from 'hyperscript' + +// Test/example code adapted from https://github.com/dominictarr/hyperscript/blob/master/README.md + +// example +h('div#page', + h('div#header', + h('h1.classy', 'h', { style: {'background-color': '#22f'} })), + h('div#menu', { style: {'background-color': '#2f2'} }, + h('ul', + h('li', 'one'), + h('li', 'two'), + h('li', 'three'))), + h('h2', 'content title', { style: {'background-color': '#f22'} }), + h('p', + "so it's just like a templating engine,\n", + "but easy to use inline with javascript\n"), + h('p', + "the intension is for this to be used to create\n", + "reusable, interactive html widgets. ")) + +// event +h('a', {href: '#', + onclick: function (e: Event) { + alert('you are 1,000,000th visitor!') + e.preventDefault() + } +}, 'click here to win a prize') + +// array of children +const obj: {[id: string]: string} = { + a: 'Apple', + b: 'Banana', + c: 'Cherry', + d: 'Durian', + e: 'Elder Berry' +} +h('table', + h('tr', h('th', 'letter'), h('th', 'fruit')), + Object.keys(obj).map(function (k) { + return h('tr', + h('th', k), + h('td', obj[k]) + ) + }) +) + +// new context +const h2 = h.context() +h2('a', {href: '#', + onclick: function (e: Event) { + alert('you are 1,000,000th visitor!') + e.preventDefault() + } +}, "Click this") + +h2.cleanup() diff --git a/hyperscript/hyperscript.d.ts b/hyperscript/hyperscript.d.ts new file mode 100644 index 0000000000..4274365404 --- /dev/null +++ b/hyperscript/hyperscript.d.ts @@ -0,0 +1,19 @@ +// Type definitions for hyperscript +// Project: https://github.com/dominictarr/hyperscript +// Definitions by: Mike Linkovich +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +declare module 'hyperscript' { + + interface HyperScript { + /** Creates an HTML element */ + (tagName: string, ...args: any[]): HTMLElement; + /** Cleans up any event handlers created by this hyperscript context */ + cleanup(): void; + /** Creates a new hyperscript context */ + context(): HyperScript; + } + + const h: HyperScript; + export = h; +}