-
-
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] Add Portal
s to Svelte
#7082
Comments
If ignoring the portal server-side isn't desirable, some potential SSR approaches were mentioned. I'm not aware of Svelte's internals, but perhaps another solution is the following: While the HTML to render is being generated server-side, For instance If we're going with the approach of ignoring portals server-side, then we'd just exclude portals from the rendered HTML. |
Related (and potentially related) issues: #3088, #1133, #4237 (becomes obsolete if this feature is addressed), #4036 Outside |
I solved this issue, this is what I created https://github.com/YeungKC/svelte-portal No dom move But I still think svelte should provide |
No ability to pass I really hope this feature gets implemented. |
Here is my hack: <script>
function portal(node,{to}){
const target = document.querySelector(to);
target&&target.appendChild(node);
return {}
}
</script>
<div use:portal={{to:'body'}}></div> It's works. |
If #8067 was implemented, it would be easy to support slots as well. |
It's a bit unfortunate to say this given the additional interest shown by the other votes on the OP... But this feature request (and its brothers, such as #4036) may not be valid anymore. As I mentioned previously, I agree with someone else's statement that the large majority of the use cases for portals are probably Modals. However, now we have the standardized There's perhaps a smaller set of scenarios where portals are needed for other things like popovers. But now we have browsers rushing to support the Popover API. If this API gets adopted, Svelte may not need to bother with portals at all considering that: A) Popovers will be a part of the native spec and B) We already have an [albeit-undesirable] workaround in Svelte. Pinging @Conduitry because you commented on #4036 and @pngwn because you've managed the tags on that issue. I just want to make sure that this is a visible consideration. Not sure where Svelte stands on portals at the moment. I'm only leaving this open in case Svelte is interested in supporting portals; otherwise, I might close. If people are aware of other use cases that browsers aren't ultimately intending to support, that might change things... maybe. But for now, it seems like browsers are seeking to give us what we need. |
A Portal component is now really easy to achieve thanks to slots just being props in Svelte 5. |
Small note for people who stumble on this answer when searching for Portals in Svelte 5, |
Describe the problem
I'm aware that this discussion has been split across a few issues. However, none of those issues have been explicit feature requests for portals. Some issues have been closed early, or been distracted from the original topic, or been too narrow (e.g.,
<:Body>
tags). Thus, I'm creating this guy to make a central, focused issue around portals (with new thoughts).The problem that this feature would address is the need for clean, well-supported
portal
s insvelte
. I believe @arggh caught most of the common use cases in their comment on another issue, such as modals and popovers. These use cases are pretty common and therefore can't really be ignored.I'm aware of other solutions that have spun off from other discussions, such as @ThomasJuster's solution, and the solution that gained inspiration from it:
svelte-portal
. While these solutions are clever, they are still flawed for multiple reasons:<div class="portal-clone">
empties its children, but the node still remains). Regardingsvelte-portal
, it temporarily pollutes the DOM unnecessarily. (More accurately, it temporarily renders to an "incorrect" location in the DOM, then moves the node to the correct location.)form
where people can add items which get rendered to atable
. Each row of the table is clickable, opening up a modal for editing (this isn't the place to debate table edit approaches). I can circumvent the need for creating state variables by using another form in the modal that "submits" (saves) the data. But nestingform
s is forbidden. I know the nestedform
would appear only temporarily with the existing approaches, but that still shouldn't be necessary.Arguments can be made for workarounds, workarounds, and more workarounds to defer responsibility to devs. ("Why are you using modal edits for tables?" "Is using a
form
to avoid state variables really that advantageous?" "Maybe you can try some tricks withposition: fixed
for modals?" etc.) But ultimately these workarounds ignore the real question, and they fail because:svelte
users in an unnecessarily inconvenient situation. (For example, I shouldn't have to create external, self-closingform
elements and tediously pollute my DOM elements withform
attributes.)Other well-established frameworks like
React
andVue
have supportedportal
features because this is a common, basic use case.Svelte
would benefit from this as well.Describe the proposed solution
A clear new feature like
<svelte:portal to={LOCATION}>CONTENT</svelte:portal>
would be sufficient. This solution should renderCONTENT
directly to the correctLOCATION
, instead of doing an awkward "DOM dance". If it's an unnecessary amount of effort to support selectors like Vue, it should be sufficient to require explicit DOM Nodes like React. After all, it's easy enough to saydocument.getElementById("ID")
, and this would give the user more granular control over what node is used. (I'd probably prefer selectors.)This solution would automatically resolve the request for
<:Body />
tags (#1133). And it has the added bonus of providing flexibility for where the portal sends content.I'm aware that @Conduitry has brought up SSR concerns across a few of these floating issues. I agree with @arggh that ignoring portals would be fine (and even desirable) in SSR.
Other well-respected frameworks like
Next.js
seem to avoid it altogether. For instance, timneutkens himself pointed to the official example for portals, which still only runs client-side. (If the component name wasn't obvious,useEffect
never runs server-side.)Nuxt.js 3
is still in beta, so I don't know what they'll do aboutteleport
. But I'm assuming it's possible they'll ignoreteleport
too. As has been said previously, most users are expecting to use portals client-side anyway.Alternatives considered
The alternatives I considered were already mentioned in the opening problem statement. However, none of these alternatives seem sufficient on their own.
Importance
would make my life easier
The text was updated successfully, but these errors were encountered: