Skip to content

Custom Elements Type Support #100

Open
@bivens-dev

Description

@bivens-dev

Given a web component like shown below

/**
 * my-project/my-element.js
 */
export class MyElement extends HTMLElement {
  static get observedAttributes() {
    return ['disabled'];
  }

  set disabled(val) {
    this.__disabled = val;
  }
  get disabled() {
    return this.__disabled;
  }

  fire() {
    this.dispatchEvent(new Event('disabled-changed'));
  }
}

customElements.define('my-element', MyElement);

I should ideally be able to use it like any other component like so:

import 'package:web/web.dart';

void main() {
  final customComponent = document.createElement('my-element') as MyElement;
  document.body!.appendChild(customComponent);
  customComponent.fire(); // The analyzer would ideally know the methods available on the element
}

In order to support use cases like this and other general tooling there is an emerging standard known as Custom Elements Manifest which is just a JSON file which follows a standard schema describing the component. For example the component above would be:

{
  "schemaVersion": "2.0.0",
  "readme": "README.md",
  "modules": [
    {
      "kind": "javascript-module",
      "path": "my-project/my-element.js",
      "declarations": [
        {
          "kind": "class",
          "customElement": true,
          "name": "MyElement",
          "tagName": "my-element",
          "description": "This is the description of the class",
          "members": [
            {
              "kind": "field",
              "name": "disabled"
            },
            {
              "kind": "method",
              "name": "fire"
            }
          ],
          "events": [
            {
              "name": "disabled-changed",
              "type": {
                "text": "Event"
              }
            }
          ],
          "attributes": [
            {
              "name": "disabled"
            }
          ],
          "superclass": {
            "name": "HTMLElement"
          }
        }
      ],
      "exports": [
        {
          "kind": "js",
          "name": "MyElement",
          "declaration": {
            "name": "MyElement"
          }
        },
        {
          "kind": "custom-element-definition",
          "name": "my-element",
          "declaration": {
            "name": "MyElement"
          }
        }
      ]
    }
  ]
}

Therefore, my request would be that this package provided a workflow that allowed the relevant code generation required to support native custom elements provided you were able to also supply a custom elements manifest file to describe it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions