Code Monkey home page Code Monkey logo

Comments (6)

smalluban avatar smalluban commented on May 14, 2024 2

The hybrids does not support extending built-in elements for two reasons:

  1. Safari won't implement it (I can't find URL with an article about it at the moment), so it is impossible to use them in Safari without polyfill.
  2. It requires using the proper base class, but hybrids definition is all about plain objects with properties. There is no place for setting for example HTMLDivElement rather than HTMLElement.

However, it doesn't mean that you are out of luck. Even though you can't create custom element using is attribute, you can use built-in element inside of your definition:

const MyButton = {
  render: () => html`
    <button>...</button>
  `,
};

A <button> inside of the template still works correctly in accessibility terms, including not required aria attributes.

from hybrids.

AndyOGo avatar AndyOGo commented on May 14, 2024

@smalluban
Thank for your quick answer.
Yeah I know Safari is the only browser who refuses to support extending built-ins.

Indeed, the correct constructor has to be specified.

Though we need and use it for custom tables, so that we can benefit from rowspan, colspan, etc.

from hybrids.

smalluban avatar smalluban commented on May 14, 2024

If context object’s local name is not a valid custom element name, article, aside, blockquote, body, div, footer, h1, h2, h3, h4, h5, h6, header, main, nav, p, section, or span, then throw a NotSupportedError DOMException.

Using extended built-ins is limited. For example, table elements don't support Shadow DOM, so building custom tables using them is rather hard to implement. You can't change styles, internal structure, etc... The only thing that you can is adding custom JavaScript logic (new methods, event listeners, behaviors). It is not a real use case for hybrids library.

Also, the whole "is" idea is quite complex and creates a lot of edge cases. That is one of the reasons why Safari might refuse to implement it.

Closing issue. Fill free to re-open if you have any additional questions about the subject.

from hybrids.

AndyOGo avatar AndyOGo commented on May 14, 2024

@smalluban

Thanks for your quick answer.

Indeed, extended build-ins and the is attribute has consequences for all DOM related API's like document.createElement.
Though it's a living standard despite the fact that Safari refuses to implement it.

IMHO it's a super important feature to extend existing elements, not only in regards to SEO, accessibility, semantics, but also developer effort.

May I ask you to consider this feature and reopen this issue.
Please have a read on this article about extending built-ins:
https://hackernoon.com/extending-built-in-elements-9dce404b75b4

from hybrids.

smalluban avatar smalluban commented on May 14, 2024

I went through the article you provided, and only what I can see there is a lot of hate. I disagree with most of the arguments. V1 version of the Shadow DOM and Custom Elements API is widely supported, and polyfill for Shadow DOM V1 is working (look at the tests results), unlike the V0 version, where it was hard to implement, as for example, you could have multiple shadowRoots. Styling is not so complicated as he claims, and shadow DOM finally introduced real encapsulation, not a fake one - for example using [is="..."] selector can be easily overwritten. Accessibility is still provided, by using built-in elements inside of the custom element. Of course, there are limitations, like building custom inputs, that will work with <form> element. However, with extending built-in elements, you still can't create input, which renders custom content (like calendar or color picker).

The other thing is a hybrids architecture. It was my well-thought-out decision about using plain objects for the definition. It disallows extending custom constructor for the class definition.

Let's take simple composition example. If you have ElementA definition, which you decide that extends <input>, and ElementB, which extends <div>. The first one depends on the input API, so your properties require the HTMLInputElement constructor. Now image, that you want to reuse one of the property from the first definition...

const ElementA = {
  property: ...
};

const ElementB = {
  ...ElementA,
  otherProperty: ...,
};

The composition is broken now - one of the principles of the hybrids. An object with property descriptors can't be related to the custom element base class. There is no place for that.

Custom Element API is very flexible, and if you stick to standards, you can create them with any library or even without them. They will still be able to work together. If you really need to extend built-in elements, you should find another tool for that.

from hybrids.

AndyOGo avatar AndyOGo commented on May 14, 2024

@smalluban
Thanks for reply.
I appreciate that you took the time to read above article.

I see your points of view.
And as I said, I really like your functional concept.

Though I'm convinced that extending built-ins is a valid concept too.
Nevertheless, it's your lib and so you rule it.

from hybrids.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.