Skip to content

Instantly share code, notes, and snippets.

@developit
Last active October 10, 2023 03:03
Show Gist options
  • Save developit/2038b141b31287faa663f410b6649a87 to your computer and use it in GitHub Desktop.
Save developit/2038b141b31287faa663f410b6649a87 to your computer and use it in GitHub Desktop.
export const h=(t,p,...c)=>({t,p,c,k:p&&p.key})
export const render=(e,d,t=d.t||(d.t={}),p,r,c,m,y)=>
// arrays
e.map?e.map((e,p)=>render(e,d,t.o&&t.o[p])):
// components
e.t.call?(e.i=render((render.c=e).t(Object.assign({children:e.c},e.p),e.s=t.s||{},t=>
render(Object.assign(e.s,t)&&e,d,e)),t.i||d,t&&t.i||{}),d.t=t=e):(
// create notes
m=t.d||(e.t?document.createElement(e.t):new Text(e.p)),
// diff props
(e.t?Object.keys(Object.assign({},t.p,e.p)).map(d=>
(c=e.p&&e.p[d])!=(t.p&&t.p[d])&&(d in m?(m[d]=c):m[(c?'set':'remove')+'Attribute'](d, c))):m.data=e.p),
// copy children for storage
y=(t.o||[]).slice(),
// diff children (typed/keyed)
e.o=e.c.concat.apply([],e.c).map((d,p)=>render(d=d&&d.c?d:h('',d),m,
y.find((e,c)=>e&&e.t==d.t&&e.k==d.k&&(c==p&&(p=r),y[c]=0,e))||{},p)),
// insert at position
t.d&&p==r||d.insertBefore(e.d=m,d.childNodes[p+1]),
// remove stragglers
y.map(e=>(y=e.i&&e.i.d||e.d)&&y.remove()),
Object.assign(t,e)
)
h=(t,p,...c)=>({t,p,c,k:p&&p.key}),render=(e,d,t=d.t||(d.t={}),p,r,c,m,y)=>e.map?e.map((e,p)=>render(e,d,t.o&&t.o[p])):e.t.call?(e.i=render((render.c=e).t(Object.assign({children:e.c},e.p),e.s=t.s||{},t=>render(Object.assign(e.s,t)&&e,d,e)),t.i||d,t&&t.i||{}),d.t=t=e):(m=t.d||(e.t?document.createElement(e.t):new Text(e.p)),e.t?Object.keys(Object.assign({},t.p,e.p)).map(d=>(c=e.p&&e.p[d])!=(t.p&&t.p[d])&&(d in m?m[d]=c:m[(c?'set':'remove')+'Attribute'](d,c))):m.data=e.p,y=(t.o||[]).slice(),e.o=e.c.concat.apply([],e.c).map((d,p)=>render(d=d&&d.c?d:h('',d),m,y.find((e,c)=>e&&e.t==d.t&&e.k==d.k&&(c==p&&(p=r),y[c]=0,e))||{},p)),t.d&&p==r||d.insertBefore(e.d=m,d.childNodes[p+1]),y.map(e=>(y=e.i&&e.i.d||e.d)&&y.remove()),Object.assign(t,e))
little={h,render};typeof module<'u'&&(module.exports=little)
{"name":"little-vdom","module":"little-vdom.js","main":"little-vdom.min.js","version":"0.2.0"}
@jimfranke
Copy link

Can you please check why returning an array (fragment) as the root from a component doesnt work? Thanks

@evatrium
Copy link

@jimfranke
No fragments here either, but here's a spin on this using display:contents as a fragment and a few other bug fixes and add-ons. Not tweet size, no SVG but works pretty well. https://gist.github.com/iosio/8e76d782570b51ddbca64809d0bf1669

@luwes
Copy link

luwes commented Jan 14, 2022

this is really tight..tight, tight!
what does render.c=e do?

also ,t.i||d, shouldn't that be ,t.i&&t.i.d||d because you need a dom node there

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment