Skip to content

Feature request: polymorphic outlets #628

@sfnelson

Description

@sfnelson

Outlets require that the target controller name corresponds to the outlet name, e.g. if I write data-source-target-outlet then my target element must have data-controller="target".

This makes sense as a technical requirement, e.g. if there are multiple controllers registered on the target then the source must be able to discriminate between them to choose the controller to receive messages.

The technical constraint appears to us to be preventing object oriented polymorphism, i.e. we must statically know the name of the controller in order to send it messages, and it gets baked into the code for the source controller.

An example of where polymorphism would be useful is a generic "trigger" controller, ala aria-controls that triggers a change in state for a modal/menu controller.

Source:

class TriggerController extends Controller {
  static outlets = ["controlled"]
  toggle() {
    this.controlledOutlet.toggle();
  }
}

Targets:

// menu, or modal, or accordion, etc
class MenuController extends Controller {
  toggle() {
    ...
  }
}

We might use the source for a sliding menu, a modal+scrim, a menu, etc – anywhere that aria-controls might be appropriate.

We have previously solved this problem using events, but there's quite a bit of boilerplate to this approach and were hoping that outlets could simplify this approach.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions