Add support for slot arguments #2540
Open
+214
−10
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What are you trying to accomplish?
This PR adds support for slot arguments.
Returning:
What approach did you choose and why?
Slot arguments provide 2 major benefits to a component approach:
Right now ViewComponent only supports basic slots without arguments. This means you have to follow patterns like this in order to get the data you need for rendering:
This breaks one of the key component principles: Encapsulation. If the parent component knows about internal state of the child component they become coupled. That leads to unreliable refactorings and degraded component's reusability.
A common pattern of customizing a slot would be to use lambda slots:
Unfortunately this approach does not allow us to pass data to the block directly, meaning it's actually the inverse of what we want: it passes data to a child component, not the child component passing data to the parent.
Another problem with this approach is that we can't customize what exactly we render inside the lambda.
Slot arguments allow us to solve these problems with elegance. Instead of component instance we access slot arguments passed to the block:
This means we are in full control of what's rendered inside the slot and also never touch component internals, allowing for safer refactorings.
With slot arguments we can now support more advanced cases of using slots:
Anything you want to highlight for special attention from reviewers?
This feature has been previously discussed but there still is no working alternative to it.
This approach is not new and is inspired by Scoped Slots in Vue and Child Component as Function pattern in React.