Lit-element: Create an element derived from a native one -- inheritance? Composition?

Created on 20 Apr 2019  路  10Comments  路  Source: Polymer/lit-element

I realise this is not a support forum; I am trying to sneak this in as a documentation request.

Short question: what's the recommended way of creating elements that behave exactly the same as native ones, in all respects? (offering extras, yes, but being 100% compatible drop-in replacement of native ones)

Long version:

I need to extend the native

docs

All 10 comments

this article outlines how to extend standard elements -- we have to provide an awkward option "extends" to the "customElements.define" call:

class ConfirmLink extends HTMLAnchorElement {
    ...
}
customElements.define("confirm-link", ConfirmLink, { 
    extends: "a" 
});

i think this is what you are looking to do

in the above example, i don't know if you need to extends HTMLAnchorElement given the awkward "extends" option

if so, this might be a problem with lit-element, in which case we'd need a lit-element mixin or decorator we can use to class extends lit(HTMLAnchorElement) {}

Do note that this is not, and will not be supported by Safari

I would vote for option 2. First and foremost, because as @thepassle pointed out, option 1 is not supported by Safari. Also I think this gives you a bit more finegrained control over what you want the user of your component actually be able to customize. Most of the time, I just need/want a small subset of what builtin controls can do.

Also, delegatesFocus helps a lot with gluing your control and the builtin control together as a button inside the shadowRoot will still be focused: https://developer.mozilla.org/en-US/docs/Web/API/ShadowRoot/delegatesFocus

In terms of best practices I also like to look at what the material web components team does: https://github.com/material-components/material-components-web-components/blob/master/packages/button/src/mwc-button.ts

Hello, I'll have to bring this problem up in our next meeting on this, but a way that we made "lighter" elements in polymer 2+ after the removal of is=..., because of Safari support, was to project your light Dom.

e.g.

<my-input-lite>
  <input slot="maybeYouWontUseASlotHere">
</my-input-lite>

Downside is listening to slotchange event and initializing after the fact.

I just opened a comment thread on this topic at https://dev.to/lkraav/comment/ad06

AFAICS https://github.com/ungap/custom-elements-builtin should be safe to use to get Safari compatibility, and you're on your way.

I was thinking of the same and google sent me here. In react, inheritance is possible to extend non-native components, but composition is always preferred (docs), even with composition, spread operator can handle this perfectly, and we can extend functionalities by composition or adjusting individual props:

render() {
  const { props } = this;
  return (
    <button prop1={val1} {...props}></button>
  );
}

Really hope lit-element docs shed some light on this topic 馃槂

Hi ,
I'm fan a of Polymer since Polymer 2 , and trying to change my mind from P2 to P3 , lit-element & lit-html, and I was facing same sort of problem.

I needed a lit-element to extend HTMLSelectElement

It seems that i found a solution with the "named slot" functionnality.

in the parent, I got that

  <solid-folders
  url="${constraint.reference}"
  @change=${this.selectorChange}
  @select-event="${(e) => { this.changeValue(e) }}" >

  <select slot="mySelect"
  @change=${this.selectorChange}>
  </select>


  </solid-folders>

and in the child element, i got this

    <select class="teal lighten-4"
    title="${this.url}"
    @change=${this.selectorChange}>

    <slot name="mySelect">
    <option value="" disabled selected>${this.url}</option>
    <option value="" ></option>
    ${this.folder.files.map(i => html`
      <option value="${i.url}"  >${i.label || i.name}</option>
      `)}
      </slot>

      </select>

Ok, there is lot of @change=${this.selectorChange} but I can catch the event & fire a custom event

image

It's just a try but hope it can help.

can be tested here : https://scenaristeur.github.io/spoggy-simple/shexy/shexy-lit/index.html

full code : https://github.com/scenaristeur/spoggy-simple/commit/95823cd93f864e95c9d42db348564f31df312c87

Safari isn't going to implement customized built-ins. I suggest using an alternate approach like @e111077 pointed out rather than forcing all Safari users to use a polyfill indefinitely.

We would need to make LitElement a mixin to support this. That's not in the plans at the moment. If that changes we'll take a look at this again.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zzzgit picture zzzgit  路  4Comments

ghost picture ghost  路  3Comments

kurukururuu picture kurukururuu  路  3Comments

antonioaltamura picture antonioaltamura  路  4Comments

chwzr picture chwzr  路  3Comments