Skip to content
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

Replace hidden <input> with ElementInternals integration #1128

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

Commits on Feb 9, 2024

  1. Extract delegate for TrixEditorElement

    In preparation for [basecamp#1128][], this commit introduces a module-private
    `Delegate` class to serve as a representation of what form integration
    requires for the `<trix-editor>` custom element. The structure of the
    `Delegate` class mirrors that of the `TrixEditorElement` from which its
    contents are extracted.
    
    First, there are the properties that mimic those of most form controls,
    including:
    
    * `labels`
    * `form`
    * `name`
    * `value`
    * `defaultValue`
    * `type`
    
    With the exception of `labels`, property access is mostly proxied
    through the associated `<input type="hidden">` element (accessed through
    its own `inputElement` property).
    
    Next, the `Delegate` defines methods that correspond to the Custom
    Element lifecycle events, including:
    
    * `connectedCallback`
    * `disconnectedCallback`
    * `setFormValue`
    
    The connected and disconnected callbacks mirror that of the
    `TrixEditorElement` itself. These callbacks attach and remove event
    listeners for `click` and `reset` events.
    
    The `setFormValue` is named to correspond with
    [ElementInternals.setFormValue][]. Along with introducing this callback
    method, this commit renames the `TrixEditorElement.setInputElementValue`
    method to `TrixEditorElement.setFormValue`.
    
    In addition to renaming `setInputElementValue`, this commit also defines
    `TrixEditorElement.formResetCallback` (along with other [empty
    form callbacks][]), then implements `TrixEditorElement.reset` as an alias. The
    name mirrors the [ElementInternals.formResetCallback][].
    
    [basecamp#1128]: basecamp#1128
    [ElementInternals.setFormValue]: https://developer.mozilla.org/en-US/docs/Web/API/ElementInternals/setFormValue
    [empty form callbacks]: https://webkit.org/blog/13711/elementinternals-and-form-associated-custom-elements/
    [ElementInternals.formResetCallback]: https://web.dev/articles/more-capable-form-controls#void_formresetcallback
    seanpdoyle committed Feb 9, 2024
    Configuration menu
    Copy the full SHA
    bc24ac5 View commit details
    Browse the repository at this point in the history
  2. Integrate with ElementInternals

    Closes [basecamp#1023][]
    
    Replace the requirement for an `<input type="hidden">` element with
    direct `<form>` integration through built-in support for
    [ElementInternals][].
    
    According to the [Form-associated custom elements][] section of [More
    capable form controls][], various behaviors that the `<trix-editor>`
    element was recreating are provided out of the box.
    
    For example, the `<input type="hidden">`-`[input]` attribute pairing can
    be achieved through [ElementInternals.setFormValue][]. Similarly, the
    `<label>` element support can be achieved through
    [ElementInternals.labels][].
    
    [basecamp#1023]: basecamp#1023
    [ElementInternals]: https://developer.mozilla.org/en-US/docs/Web/API/ElementInternals
    [Form-associated custom elements]: https://web.dev/articles/more-capable-form-controls#form-associated_custom_elements
    [More capable form controls]: https://web.dev/articles/more-capable-form-controls
    [ElementInternals.setFormValue]: https://developer.mozilla.org/en-US/docs/Web/API/ElementInternals/setFormValue
    [ElementInternals.labels]: https://developer.mozilla.org/en-US/docs/Web/API/ElementInternals/labels
    seanpdoyle committed Feb 9, 2024
    Configuration menu
    Copy the full SHA
    d6aefb9 View commit details
    Browse the repository at this point in the history