Skip to content

Consider exporting base class HTMLDialogElement for subclassing #229

Description

@mitranim

First off, thanks for the polyfill. It seems to work.

Motive: my app is organized around custom elements, many of which are instantiated directly with new. I'm not concerned with tag name compatibility such as <dialog is=some-dialog> vs <some-dialog>. What's important is simply having the base class available.

I hacked together the following and it seems to work, but having it provided by the library would be better. Hopefully without increasing the size of the polyfill.

import dp from 'https://cdn.jsdelivr.net/npm/dialog-polyfill@0.5.6/dist/dialog-polyfill.esm.min.js'

const HTMLDialogElement = window.HTMLDialogElement || class HTMLDialogElement extends HTMLElement {
  constructor() {
    super()

    // Hack for the polyfill.
    this.showModal = undefined

    // Actual polyfill.
    dp.forceRegisterDialog(this)

    this.showPoly = this.show
    this.showModalPoly = this.showModal
    this.closePoly = this.close

    delete this.show
    delete this.showModal
    delete this.close
  }

  // Allows `super.X` in subclasses.
  show(...args) {return this.showPoly(...args)}
  showModal(...args) {return this.showModalPoly(...args)}
  close(...args) {return this.closePoly(...args)}

  // Hack for the polyfill.
  get localName() {return `dialog`}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions