-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Feature Suggestion: Improve versatility of <svelte:component>
and <svelte:element>
by treating them as a <svelte:fragment>
when the this
prop value is present but is a falsy or nullish value
#7437
Comments
I second this, as I suggested something similar: #6898 (comment) |
Rendering the slot by itself when For |
@dimfeld I believe part of the purpose of I am curious why you can't use an |
Contrived example and you could definitely make a component to make it easier to manage, but basically this:
|
I may be mistaken here, but wouldn't it constitute as a breaking change if this change were to break projects that are currently working? I believe this change would do the opposite; it would not break any working codebases, but it would cause Svelte to see (Again, I may very we'll be mistaken here ☝🏼) Yes, I also really like @lukaszpolowczyk's idea of being able to use |
The current (and officially documented) behavior of svelte:component is to render nothing if |
Great call-out, @dimfeld. I was mistaken here and thought I had seen a similar example throw an error. My suggestion would then be to support Seeing as those were closed and this current suggestion would be a breaking change, I will close this ticket. Thanks, all. 👋🏼 |
I agree there should be a way of conditionally rendering some content in a component or element with a single tag. I'm currently using the svelte component below to handle this, but a built-in solution would be preferable.
|
Curious if anyone has an argument against
|
We definitely don't want to do this, it would be a confusing change in behaviour. In Svelte 5, we have snippets which provide much more control over this sort of thing. |
Describe the problem
In some cases, I would like to conditionally render some content in a component or element.
Describe the proposed solution
For these cases, it would be useful if we were able to conditionally omit the component or element by using a nullish or falsy value in the
this
prop. So this example (below) would simply renderSome text
since thethis
prop is a falsy value:In such cases, the
<svelte:component>
or<svelte:element>
would simply be treated as a<svelte:fragment>
.Alternatives considered
The main alternative is see is similar but essentially creating my own
Fragment
component which simply returns the slotted content in this format:In practice, it could be used like this:
The downside to this, aside from having to import another custom component is that it would not work at all for
<svelte:element>
. In those cases, I would actually have to create a Svelte component counterpart for every single element so I could use them against my newFragment
component like this:Importance
would make my life easier
The text was updated successfully, but these errors were encountered: