-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Proposal: Customized built-in elements via elementInternals.type
#11061
Comments
elementInternals.type
elementInternals.type
elementInternals.type
Not sure about the "type" property proposed here but enhancing I think I'd prefer breaking apart features of these builtin elements and adding each capability to |
If we're after more compositional "mixin" style features I wonder if we might consult with Lit folks (/cc @justinfagnani, @sorvell to name a few) about what an API shape might look like, especially with their work around the "Reactive Controllers" pattern. At the risk of scope creeping, providing a mixin pattern that allows for applying built-in behaviours, but also mixing in userland behaviours could be a really interesting concept. import {CustomMixin} from './my-mixin.js'
class CustomButton extends HTMLElement {
constructor() {
super()
const internals = this.attachInternals();
// Add the built-in mixin for button activation behaviour, gives us `popovertarget`, `commandfor`, etc.
internals.addMixin(HTMLButtonActivationBehaviorMixin);
// Also adopt my custom mixin, which can do other things...?
internals.addMixin(CustomMixin);
}
} |
Glad to hear!
If we break up the features and support them individually on Using |
I think the proposal has merit, however I noticed that the API is somewhat unusual:
To me, the requirement that the The value of the |
Bitwise flags come to mind, where I think the thing we'll want to identify is whether there are legitimate use-cases to opt in or out of some of the built-in behaviors. If there are, the API should enable those use cases. That said, the API should make it easy to "fall into the pit of success" so that the common use cases (e.g. |
What problem are you trying to solve?
Web component authors often want to create custom elements that inherit the behaviors and properties of native HTML elements. These types of custom elements are referred to as "customized built-in elements" or just "customized built-ins". By customizing built-in elements, custom elements can leverage the built-in functionality of standard elements while extending their capabilities to meet specific needs. Some of the use cases enabled by customized built-ins are listed below.
for
attribute or nesting a labelable element inside the custom label.What solutions exist today?
A partial solution for this problem already exists today. Authors can specify the
extends
option when defining a custom element. Authors can then use theis
attribute to give a built-in element a custom name, thereby turning it into a customized built-in element.Both
extends
andis
are supported in Firefox and Chromium-based browsers . However, this solution has limitations, such as not being able to attach shadow trees to (most) customized built-in elements. Citing these limitations, Safari doesn't plan to support customized built-ins in this way and have shared their objections here: WebKit/standards-positions#97 (comment). As such,extends
andis
are not on a path to full interoperability today.How would you solve it?
The ElementInternals interface gives web developers a way to participate in HTML forms and integrate with the accessibility OM. This will be extended to support the creation of customized built-ins by adding a
type
property, which can be set to string values that represent native element types. The initial set of type values being proposed are listed below. Support for additional values may be added in the future.'' (empty string)
- this is the default value, indicating the custom element is not a customized built-inbutton
- for button like behaviorsubmit
- for submit button like behaviorreset
- for reset button like behaviorlabel
- for label like behaviorThis proposal was presented and discussed at TPAC, where it was resolved by the WHATWG to add
elementInternals.type = 'button'
to the HTML spec.Initial proposal discussed at TPAC: openui/open-ui#1088 (comment)
WHATWG resolution for
elementInternals.type = 'button'
: openui/open-ui#1088 (comment)This issue formally tracks the proposal against the @whatwg/html repo and also includes a few additional types beyond
button
that had support at TPAC.Full explainer here: https://github.com/MicrosoftEdge/MSEdgeExplainers/blob/main/ElementInternalsType/explainer.md
Examples
Below is an example showcasing a custom button being used as a popup invoker. When the custom button is activated, ex. via a click, div id="my-popover will be shown as a popover.
Like with native buttons, if the
disabled
attribute is set, a custom button cannot be activated and thus cannot invoke popovers.Below is an example showcasing a custom submit button being used to submit a form. When the custom button is activated, ex. via a click, the form will be submitted and the page will navigate.
If the
disabled
attribute is set on a custom submit button, it cannot be activated and thus cannot submit forms.Below is an example showcasing a custom label being used to label a checkbox. When the custom label is activated, ex. via a click, the checkbox is also activated, resulting in its state changing to checked.
cc: @alexkeng
The text was updated successfully, but these errors were encountered: