Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: use an object for the style prop & add className #32

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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