From 31cfda50f540710d6102d9a797c880ec943b57ce Mon Sep 17 00:00:00 2001 From: Miel Truyen Date: Tue, 22 Oct 2019 19:21:01 +0200 Subject: [PATCH] Multiple test-projects --- .../src/custom-element/custom-element.js | 8 ++ .../src/custom-element/define-element.js | 18 ++++ .../src/custom-element/index.js | 3 +- .../csx-custom-elements/src/vdom/render.js | 4 +- rollup.config.js | 87 ++++++++++++++----- test/basic/index.html | 10 +++ test/basic/index.jsx | 30 +++++++ test/{ => basic}/index.scss | 0 test/basic/page.js | 12 +++ test/index.html | 21 ++++- test/index.jsx | 28 +----- test/todos-mvc/index.html | 10 +++ test/todos-mvc/index.jsx | 9 ++ test/todos-mvc/index.scss | 19 ++++ 14 files changed, 205 insertions(+), 54 deletions(-) create mode 100644 packages/csx-custom-elements/src/custom-element/define-element.js create mode 100644 test/basic/index.html create mode 100644 test/basic/index.jsx rename test/{ => basic}/index.scss (100%) create mode 100644 test/basic/page.js create mode 100644 test/todos-mvc/index.html create mode 100644 test/todos-mvc/index.jsx create mode 100644 test/todos-mvc/index.scss diff --git a/packages/csx-custom-elements/src/custom-element/custom-element.js b/packages/csx-custom-elements/src/custom-element/custom-element.js index e69de29..68f89ad 100644 --- a/packages/csx-custom-elements/src/custom-element/custom-element.js +++ b/packages/csx-custom-elements/src/custom-element/custom-element.js @@ -0,0 +1,8 @@ +/** + * This CustomElement class is to avoid having to do an ugly workaround in every custom-element: + * Which would be replacing 'HTMLElement' with '(class extends HTMLElement{})' + * + * Also, it is a good starting point for implementing render() functionality, listening to props, state changes, events and whatnot (use decorators) + */ +export class CustomElement extends HTMLElement {} + diff --git a/packages/csx-custom-elements/src/custom-element/define-element.js b/packages/csx-custom-elements/src/custom-element/define-element.js new file mode 100644 index 0000000..b4932f6 --- /dev/null +++ b/packages/csx-custom-elements/src/custom-element/define-element.js @@ -0,0 +1,18 @@ + +/** + * The decorators proposal has changed since @babel implemented it. This code will need to change at some point... + */ +export function defineElement(tagName, options) { + return function decorator(target){ + // Register the tagName as a custom-element with the browser + window.customElements.define(tagName, target, options); + + // Define the chosen tagName on the class itself so our vdom.render-function knows what DOM-Element to create + Object.defineProperty(target, 'tagName', { + value: tagName, + writable: false, + enumerable: false, + configurable: false + }); + } +} \ No newline at end of file diff --git a/packages/csx-custom-elements/src/custom-element/index.js b/packages/csx-custom-elements/src/custom-element/index.js index 0e635f0..fd7a65e 100644 --- a/packages/csx-custom-elements/src/custom-element/index.js +++ b/packages/csx-custom-elements/src/custom-element/index.js @@ -1 +1,2 @@ -export * from './custom-element' \ No newline at end of file +export * from './define-element'; +export * from './custom-element'; \ No newline at end of file diff --git a/packages/csx-custom-elements/src/vdom/render.js b/packages/csx-custom-elements/src/vdom/render.js index b42538a..aad01cf 100644 --- a/packages/csx-custom-elements/src/vdom/render.js +++ b/packages/csx-custom-elements/src/vdom/render.js @@ -30,12 +30,12 @@ export function render(vnode, opts = {}) { } = opts; if(VNODE_EXCLUDE[vnode]) return undefined; - console.log(vnode); if(vnode instanceof Object){ // Type - if(!host) host = document.createElement(vnode.type); + let tagName = vnode.type instanceof Object? vnode.type.tagName : vnode.type; + if(!host) host = document.createElement(tagName); // Props if (vnode.props) { diff --git a/rollup.config.js b/rollup.config.js index d80734a..801eec3 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -9,24 +9,71 @@ import sass from "rollup-plugin-sass"; // `npm run dev` -> `production` is false const production = !process.env.ROLLUP_WATCH; -export default { - input: 'test/index.jsx', - output: { - file: 'public/index.js', - format: 'iife', // immediately-invoked function expression — suitable for + + \ No newline at end of file diff --git a/test/basic/index.jsx b/test/basic/index.jsx new file mode 100644 index 0000000..e82cc17 --- /dev/null +++ b/test/basic/index.jsx @@ -0,0 +1,30 @@ +import {render} from "../../packages/csx-custom-elements"; +import style from "./index.scss"; +import {ExamplePage} from "./page"; + +document.body.appendChild(render()); +document.body.appendChild(render(
+

I am a title!

+
)); + +//document.body.appendChild(render()); +document.body.appendChild(render()); + +/** + * Findings: + * - JSX does not allow dot-notation in attributes: language error + * - Current code lower-cases attributes that result: this a limitation of setAttribute + * - React uses on to capture events and the IDE auto-suggests using this (can we generalize this approach for customEvents?) + */ + +/** + * Continuation suggestionss: + * - ref={...} does not work yet + * - style-attribute untested + * - Want a way to toggle classes: could do + * - Need to support update an existing DOM-tree to a VNode-tree + * - Need to support the key-attribute for lists (linking with previous to have an idea how to update DOM-tree efficiently, are we going atomico/react/prect style diffing with a Virtual-DOM?) + * - and special handlers + * - Supporting fragments <>...? + * - Try working towards a simple ToDo-MVC application + */ \ No newline at end of file diff --git a/test/index.scss b/test/basic/index.scss similarity index 100% rename from test/index.scss rename to test/basic/index.scss diff --git a/test/basic/page.js b/test/basic/page.js new file mode 100644 index 0000000..da36748 --- /dev/null +++ b/test/basic/page.js @@ -0,0 +1,12 @@ +import {defineElement, render, CustomElement} from "../../packages/csx-custom-elements/lib"; + +@defineElement('example-page') +export class ExamplePage extends CustomElement{ + connectedCallback() { + this.appendChild(render( +
+

This page is an example!

+
+ )); + } +} \ No newline at end of file diff --git a/test/index.html b/test/index.html index 1e2a2c0..190bf35 100644 --- a/test/index.html +++ b/test/index.html @@ -2,9 +2,22 @@ - Cerxes - CustomElements + CSX - CE Tests + - - - + + + \ No newline at end of file diff --git a/test/index.jsx b/test/index.jsx index dd0008d..4d843ca 100644 --- a/test/index.jsx +++ b/test/index.jsx @@ -1,27 +1 @@ -import {render} from "../packages/csx-custom-elements/lib"; -import style from "./index.scss"; - -document.body.appendChild(render()); -document.body.appendChild(render(
-

I am a title!

-
)); - - -/** - * Findings: - * - JSX does not allow dot-notation in attributes: language error - * - Current code lower-cases attributes that result: this a limitation of setAttribute - * - React uses on to capture events and the IDE auto-suggests using this (can we generalize this approach for customEvents?) - */ - -/** - * Continuation suggestionss: - * - ref={...} does not work yet - * - style-attribute untested - * - Want a way to toggle classes: could do - * - Need to support update an existing DOM-tree to a VNode-tree - * - Need to support the key-attribute for lists (linking with previous to have an idea how to update DOM-tree efficiently, are we going atomico/react/prect style diffing with a Virtual-DOM?) - * - and special handlers - * - Supporting fragments <>...? - * - Try working towards a simple ToDo-MVC application - */ \ No newline at end of file +/** I don't do nothing! (but it might later...) **/ \ No newline at end of file diff --git a/test/todos-mvc/index.html b/test/todos-mvc/index.html new file mode 100644 index 0000000..db77583 --- /dev/null +++ b/test/todos-mvc/index.html @@ -0,0 +1,10 @@ + + + + + Todos MVC + + + + + \ No newline at end of file diff --git a/test/todos-mvc/index.jsx b/test/todos-mvc/index.jsx new file mode 100644 index 0000000..ab0856b --- /dev/null +++ b/test/todos-mvc/index.jsx @@ -0,0 +1,9 @@ +import {render} from "../../packages/csx-custom-elements"; +import style from "./index.scss"; + +// Replace this with an example implementation of the Todos-MVC app +// look for inspiration here: https://github.com/shprink/web-components-todo +document.body.appendChild(render()); +document.body.appendChild(render(
+

Todos MVC

+
)); diff --git a/test/todos-mvc/index.scss b/test/todos-mvc/index.scss new file mode 100644 index 0000000..1229903 --- /dev/null +++ b/test/todos-mvc/index.scss @@ -0,0 +1,19 @@ +html{ + width: 100%; + height: 100%; +} + +body{ + display: flex; + flex-direction: column; + overflow: auto; + + width: 100%; + height: 100%; + padding: 0; + margin: 0; +} + +.center-me{ + align-self: center; +} \ No newline at end of file