Skip to content

Commit

Permalink
Add the render event dispatched on roots
Browse files Browse the repository at this point in the history
  • Loading branch information
stasm committed Sep 11, 2017
1 parent dc1521c commit 46f395e
Show file tree
Hide file tree
Showing 11 changed files with 1,490 additions and 0 deletions.
12 changes: 12 additions & 0 deletions example03/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import html from "../index";
import { connect } from "./store";

import TextInput from "./TextInput";
import CharCounter from "./CharCounter";

export default function App(tasks) {
return html`
${TextInput()}
${CharCounter()}
`;
}
10 changes: 10 additions & 0 deletions example03/CharCounter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import html from "../index";
import { connect } from "./store";

function CharCounter({value}) {
return html`
<div>Characters typed: ${value.length}</div>
`;
}

export default connect(CharCounter);
11 changes: 11 additions & 0 deletions example03/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# innerself example 03

An example illustrating how much hand-holding innerself needs to handle
form-based elements. Because the assignment to innerHTML completely re-creates
the entire DOM tree under the root, all local state of the descendant elements
is lost upop re-render. Focus and selection need to be restored manually.

To install the dependencies and serve the example locally run:

$ npm install
$ npm start
12 changes: 12 additions & 0 deletions example03/TextInput.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import html from "../index";
import { connect } from "./store";

function TextInput({value}) {
return html`
<textarea id="text-input"
placeholder="Type here…"
onkeyup="dispatch('CHANGE_VALUE', this)">${value}</textarea>
`;
}

export default connect(TextInput);
9 changes: 9 additions & 0 deletions example03/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!doctype html>
<meta charset="utf8">
<title>innerself example 03</title>
<script defer src="dist.js"></script>

<h1>innerself example 03</h1>
<div id="root">


17 changes: 17 additions & 0 deletions example03/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { attach } from "./store";
import App from "./App";

const root = document.querySelector("#root");
root.addEventListener("render", function(event) {
// event.detail is the state that was rendered.
const { id, selectionStart, selectionEnd } = event.detail;
if (id) {
const textarea = root.querySelector("#" + id);
textarea.focus();
textarea.setSelectionRange(selectionStart, selectionEnd);
}
});

attach(App, root);


Loading

0 comments on commit 46f395e

Please sign in to comment.