Replies: 1 comment
-
|
Related: #10678 GitHub does not seem to link discussions back to issues |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Svelte Advanced Component+Snippet Proposal
This proposal outlines behavior and syntax for a combination of Components and Snippets in Svelte.
TL;DR
Motivation
Until now, Components have been the "backbone" of Svelte. Usually, nothing works without them.
Snippets add more flexibility and replace the old slots.
Snippets are great. They allow for a kind of composability, mainly the ability to pass "snippets" of UI to another Component.
It also works great for sharing code for UI-parts that are "not worth creating a component file".
This works great with Snippets
Sharing Code by exporting a Snippet
This is great for the end-user and also for library authors that implement a View/Component that should show some arbitrary UI in some place within.
This only works "flawlessly" inside the template. When using Snippets in (JS) code, then there are severe limitations!
Works great in Template
Limitations in Code
Authors can work around this, in places they accept a Snippet, by accepting not only a Snippet, but also its props/arguments.
But this is not only cumbersome, but also hard to set TypeScript types correctly.
Possible Workaround
This was first discussed in #10678.
This solves our problem, but now makes using Snippets without arguments worse.
But there is another problem: When you want to use a Component in a place only Snippets are allowed in, you have to use redirection.
Using Components as a Snippet
But now imagine if this Component had props! Just like Snippets can have props/arguments.
The Problem I See
As a library author I would like to support all "kinds of UI" a user wants to give to me.
The user should be able to provide any view they choose:
Internallsyyou have to treat a "Modal" similar to this:
Why do we have to do this?
Because to render it, you have to know what it is:
Rendering such a Modal
Some of the problem with the above are:
Why this is a Problem
Think about this, you want to render a list of Snippets inside buttons and highlight the last one clicked on:
Now a user of this code wants to add a Snippet, this case works fine:
But this turns into a bad DX really fast:
Because you can not fix/set/populate Snippet arguments (neither by parts, or all), you have to defined snippets for each entry seperately below.
How this could/should look
Having the ability to populate/bind Snippet arguments, while also keeping others free to "pass on" greatly improves Composability.
Let's go even further
But I don't think we should stop there.
When a user defined the above
genericIconnot as a Snippet, but as a Component, then they would have to "redirect using a Snippet" again!They should not be punished for using a Component if they prefer that. Therefore the below code should be available to them too:
Now this would solve all of the above issues/limitations/problems at once, plus it would improve/add composability.
Which, to my belief, is highly advisable.
With the above implemented, even the following code should be possible:
Conclusion
I believe in a future where authors are more powerful using Svelte than ever, having the ability to compose UI in a more advanced and flexible way.
I love the way Svelte looks, works and behaves, its simplicity is important to us all.
But being powerful, dynamic and flexible is important too.
That's why many reach for other frameworks, like React or now even Ripple. I think Svelte should provide equal "power" to the user to compose UI, and not "stand in the way" and require the user to use these cumbersome workarounds (see above).
Who is this?
I have used Svelte for 3+ years to make GroupTube. A website that is also packaged as a mobile app.
I've created multiple "View Controllers" for "Modals", "Tabs" and similar kinds of interfaces. I ran into the limitations discussed above many times during this.
(See comments in #10678 for some of it.)
Beta Was this translation helpful? Give feedback.
All reactions