Skip to content

[Custom Elements with Native Element Behaviors] Other considered alternatives #1135

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

anaskim
Copy link
Contributor

@anaskim anaskim commented Aug 20, 2025

Add two considered alternatives for custom elements with native element behaviors.

  • Compositional Mixins via elementInternals.addMixin() & Compositional Mixins via Subclass Factories
    • Description
    • Sample code
    • Explanation of sample code
    • Disadvantages of the alternative approach

@anaskim anaskim marked this pull request as ready for review August 20, 2025 22:10
Copy link
Contributor

@alexkeng alexkeng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

initial review

};
}

class CustomButton extends ButtonCustomMixin(HTMLButtonElement) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
class CustomButton extends ButtonCustomMixin(HTMLButtonElement) {
class CustomButton extends ButtonInternalMixin(HTMLElement) {

ButtonInternalMixin probably is a better name, and the base class should be HTMLElement, not HTMLButtonElement

this.setAttribute('command', value);
}

attributeChangedCallback(name, oldValue, newValue) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this subclass factory should contain all properties/methods from the built-in Button element, not attributeChangedCallback, connectedCallback, etc which are for custom elements

This alternative proposes a compositional API that allows web developers to opt into specific native behaviors using mixins. These can be added via a method like `elementInternals.addMixin()`, injecting native-like capabilities (e.g., form participation, activation behavior) into custom elements. The approach supports both built-in mixins (provided by the platform) and user-defined ones, enabling flexible combinations of behaviors.

```js
function MyCustomBehaviorMixin(Base) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a subclass factory, which is not this alternative is about. This alternative is more about an approach similar to ReactiveController

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants