nanohtml icon indicating copy to clipboard operation
nanohtml copied to clipboard

Support for custom elements

Open diffcunha opened this issue 6 years ago • 13 comments

This PR adds the support for custom components (web components)

~Blocked on: https://github.com/choojs/hyperx/pull/68~

// MyComponent.js

class MyComponent extends HTMLElement {
  connectedCallback () {
    this._shadow = this.attachShadow({ mode: 'open' })
    this._shadow.appendChild(html`<span>Hello ${this.getAttribute('foo')}</span>`)
  }
}

MyComponent.tagName = 'my-component'

// MyParagraph.js

class MyParagraph extends HTMLParagraphElement {
  connectedCallback () {
    this._shadow = this.attachShadow({ mode: 'open' })
    this._shadow.appendChild(html`<strong>${this.children[0]}</strong>`)
  }
}

MyParagraph.tagName = 'my-paragraph'
MyParagraph.options = { extends: 'p' }

// index.js

var tree = html`
  <div>
    <${MyComponent} foo="bar"></${MyComponent}>
    <p is=${MyParagraph}><span>Lorem ipsum</span></p>
  </div>
`

diffcunha avatar Mar 21 '18 20:03 diffcunha

Very cool!

bcomnes avatar Mar 22 '18 02:03 bcomnes

I'm open to landing this, but in order to do so it would need to include tests for client, server & transforms too. It's a pretty big patch, and I suspect there might be a few rough edges.

yoshuawuyts avatar Mar 22 '18 05:03 yoshuawuyts

@yoshuawuyts PR updated, only babel transform is missing. @goto-bus-stop and @AndyOGo pushed a few commits replacing bel with nanohtml on tests. I was doing the same so there might be some weird things to be fixed

diffcunha avatar Mar 23 '18 12:03 diffcunha

@diffcunha Correct I refactored all tests, which where using bel instead of nanohtml. I would suggest you pull and rebase your branch onto master to get the updated stuff.

AndyOGo avatar Mar 23 '18 13:03 AndyOGo

@AndyOGo I rebased already. I have a question: how did you made the require('nanohtml') work in tests? Previously bel was available to be required because it’s a dep of choo and choo is a dep of this project. I had to use pathmodify plugin to provide nanohtml as a relative path.

diffcunha avatar Mar 23 '18 14:03 diffcunha

@diffcunha That's weird. Actually I did nothing... just run npm run test to check if it works, and it worked 🤔

AndyOGo avatar Mar 23 '18 14:03 AndyOGo

@diffcunha the transform output didn't use the main nanohtml import previously, so it didn't matter whether the import path could be resolved.

goto-bus-stop avatar Mar 23 '18 14:03 goto-bus-stop

@goto-bus-stop that’s it! Thanks

diffcunha avatar Mar 23 '18 14:03 diffcunha

@yoshuawuyts PR updated. The major change here is that transformed code is generated around our createElement only instead of document.createElement and other auxiliary functions. One single implementation rather than 3 separate ones (browserify transform, babel plugin and template string runtime eval)

diffcunha avatar Mar 26 '18 04:03 diffcunha

Had to take some time to get used to this massive refactor, but I like the createElement change for sure :+1: appreciate your work on this!

goto-bus-stop avatar Mar 26 '18 15:03 goto-bus-stop

@goto-bus-stop @diffcunha: Kudos on the team work! Where did we land on this?

jmealo avatar Jul 31 '18 20:07 jmealo

@diffcunha @goto-bus-stop @bcomnes @jmealo I just submitted a relatively smaller PR #137 , which just adds support for is attribute (extended build-in elements as defined by custom elements V1 spec), please see #136

I think that should be all which is needed. May I ask you to review it?

AndyOGo avatar Nov 06 '18 13:11 AndyOGo

PS: I also provided tests 💪

AndyOGo avatar Nov 06 '18 16:11 AndyOGo