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

[labs] Consider adding a SlotController to help with common slot patterns #2693

Closed
justinfagnani opened this issue Mar 31, 2022 · 3 comments
Closed
Assignees

Comments

@justinfagnani
Copy link
Collaborator

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.

We could make a SlotController(s) to help. Example that covers the first case in this gist: https://gist.github.com/justinfagnani/a90eefb8da6bbfa4250a46732a8e5fbc

@WickyNilliams
Copy link
Contributor

I've written something almost exactly like this in my component lib. I started with a single controller managing multiple slots, but in the end i settled on a 1:1 controller:slot relationship, because i preferred to write code like this:

class MyComponent {
  fooSlot = new SlotController("foo");

  render() {
    return html`
      <slot name=${this.fooSlot.name}></slot>
      <div ?hidden=${this.fooSlot.isEmpty}></div>
    `;
  }
}

it was nice to have the name defined in one place, the code felt more readable, and it avoided errors whenever i renamed/referenced slots

@justinfagnani
Copy link
Collaborator Author

A SlotController could also help with issues like: #2629

@justinfagnani
Copy link
Collaborator Author

Closing in favor of lit/rfcs#7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

No branches or pull requests

4 participants