Skip to content

[RRFC] SlotController #7

@justinfagnani

Description

@justinfagnani
  • I searched for an existing RRFC which might be relevant to my RRFC

Motivation

Moved from lit/lit#2693

There are a few common patterns for using slots that could be nicely wrapped up in a reactive controller:

  • Determining when a slot has assigned content so you can style the component differently (ex: add padding when there's content).
  • Tracking slotted children to call APIs (set properties, add/remove event listeners, call methods) when child enter and exit the slot.

Example

We could make a SlotController(s) to help.

class MyElement extends LitElement {
  private _slotController = new SlotController(this);
    
  render() {
    const slotStyles = {
      padding: this._slotController.hasAssignedElements() ? '4px' : 0,
    };
    return html`<slot style=${styleMap(slotStyles)}></slot>`;
  }  
}

How

See https://gist.github.com/justinfagnani/a90eefb8da6bbfa4250a46732a8e5fbc

Current Behavior

Libraries write their own slot controller, like https://github.com/shoelace-style/shoelace/blob/next/src/internal/slot.ts or users use slotchange.

Desired Behavior

It's easier.

References

  • n/a

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