Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions didact.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,14 @@ function updateDom(dom, prevProps, nextProps) {
.filter(isProperty)
.filter(isNew(prevProps, nextProps))
.forEach(name => {
dom[name] = nextProps[name]
if (name === 'style') { // update style
transformDomStyle(dom, nextProps.style)
} else if (name === 'className') { // update className
prevProps.className && dom.classList.remove(...prevProps.className.split(/\s+/))
dom.classList.add(...nextProps.className.split(/\s+/))
} else {
dom[name] = nextProps[name]
}
})

// Add event listeners
Expand All @@ -89,6 +96,21 @@ function updateDom(dom, prevProps, nextProps) {
})
}

const reg = /[A-Z]/g
function transformDomStyle(dom, style) {
dom.style = Object.keys(style)
.reduce((acc, styleName) => {
const key = styleName.replace(
reg,
function(v) {
return '-' + v.toLowerCase()
}
)
acc += `${key}: ${style[styleName]};`
return acc
}, '')
}

function commitRoot() {
deletions.forEach(commitWork)
commitWork(wipRoot.child)
Expand Down Expand Up @@ -309,7 +331,7 @@ const Didact = {
function Counter() {
const [state, setState] = Didact.useState(1)
return (
<h1 onClick={() => setState(c => c + 1)}>
<h1 style={{marginTop: '100px', textAlign: 'center'}} className={'foo bar'} onClick={() => setState(c => c + 1)}>
Count: {state}
</h1>
)
Expand Down