Skip to content

Commit

Permalink
Move updateDomProperties to dom-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
pomber committed May 21, 2017
1 parent 8eb7ffd commit 6f5fdb7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
26 changes: 26 additions & 0 deletions src/dom-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export function updateDomProperties(dom, prevProps, nextProps) {
const isEvent = name => name.startsWith("on");
const isAttribute = name => !isEvent(name) && name != "children";

// Remove event listeners
Object.keys(prevProps).filter(isEvent).forEach(name => {
const eventType = name.toLowerCase().substring(2);
dom.removeEventListener(eventType, prevProps[name]);
});

// Remove attributes
Object.keys(prevProps).filter(isAttribute).forEach(name => {
dom[name] = null;
});

// Set attributes
Object.keys(nextProps).filter(isAttribute).forEach(name => {
dom[name] = nextProps[name];
});

// Add event listeners
Object.keys(nextProps).filter(isEvent).forEach(name => {
const eventType = name.toLowerCase().substring(2);
dom.addEventListener(eventType, nextProps[name]);
});
}
15 changes: 3 additions & 12 deletions src/reconciler.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { updateDomProperties } from "./dom-utils";

let rootInstance = null;

export function render(element, container) {
Expand Down Expand Up @@ -27,18 +29,7 @@ function instantiate(element) {
? document.createTextNode("")
: document.createElement(type);

// Add event listeners
const isListener = name => name.startsWith("on");
Object.keys(props).filter(isListener).forEach(name => {
const eventType = name.toLowerCase().substring(2);
dom.addEventListener(eventType, props[name]);
});

// Set properties
const isAttribute = name => !isListener(name) && name != "children";
Object.keys(props).filter(isAttribute).forEach(name => {
dom[name] = props[name];
});
updateDomProperties(dom, [], props);

// Instantiate and append children
const childElements = props.children || [];
Expand Down

0 comments on commit 6f5fdb7

Please sign in to comment.