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

Implement <svelte:fragment slot="name"> #4556

Merged
merged 1 commit into from
Feb 27, 2021

Conversation

tanhauhau
Copy link
Member

@tanhauhau tanhauhau commented Mar 14, 2020

worked on top of the #4509 branch

<Component>
  <svelte:fragment>
     <slot />
  </svelte:fragment>
</Component>
<Component>
    <Inner slot="foo" />
</Component>

as a sugar syntax for

<Component>
   <svelte:fragment slot="foo">
       <Inner />
   </svelte:fragment>
</Component>

Fixes #5626

@tanhauhau tanhauhau force-pushed the slot-template branch 3 times, most recently from 79c6d32 to 6fd6790 Compare March 14, 2020 10:24
@rixo
Copy link
Contributor

rixo commented Mar 14, 2020

Oooh, this looks exciting! Has there been previous discussion about it? I mean, is there somewhere we can read/learn more about this?

@tanhauhau
Copy link
Member Author

@rixo there's a rfc sveltejs/rfcs#18, but no one discussed about it.

and it is useful for supporting #1037

@tanhauhau
Copy link
Member Author

Related to #1037

@Rich-Harris
Copy link
Member

Just to reiterate the discussion on the RFC, there was a suggestion that we change <svelte:slot slot="foo"> to <svelte:fragment slot="foo">, since it's the counterpart to a <slot> rather than an equivalent to it

@tanhauhau tanhauhau changed the title Implement <svelte:slot> Implement <svelte:fragment> Jun 28, 2020
@tanhauhau tanhauhau changed the title Implement <svelte:fragment> Implement <svelte:fragment slot="name"> Jun 28, 2020
@Conduitry
Copy link
Member

It looks like the test failures here are just some parsing things, probably because svelte:template has more characters than svelte:slot.

@tanhauhau tanhauhau marked this pull request as draft June 29, 2020 15:42
@tanhauhau tanhauhau marked this pull request as ready for review July 24, 2020 16:39
@tanhauhau tanhauhau force-pushed the slot-template branch 2 times, most recently from c65a386 to 2d06e72 Compare July 25, 2020 00:26
@tanhauhau
Copy link
Member Author

Oops, I took quite some time on this PR.

I rewrote the PR, shifting bulk of the logic to src/compiler/compile/nodes/InlineComponent.ts, mainly try to treat any child elements / components with slot attribute as a sugar syntax of wrapping it with <svelte:fragment>.

That way, I can remove the slot attribute logic from both Elements and InlineComponents in render_dom and render_ssr.

@tanhauhau
Copy link
Member Author

@Conduitry probably can help review it again? thanks.

@PatrickG
Copy link
Member

May I ask what is holding this back?

@moreofmorris
Copy link

Would love to see this implemented! Was just running into an issue today that this would solve nicely! 😌

@ievoms
Copy link

ievoms commented Feb 1, 2021

When could we expect this to be implemented?

@ggchivalrous
Copy link

I also had this problem

 <MyInput placeholder="请输入内容">
      <MyButton slot="append" />
  </MyInput>
//  'slot' is reserved for future use in named slots

Hope to support

@Steakeye
Copy link

👍 from me in support of this being merged in ASAP!

@wagnerflo
Copy link

Yes please. I would like to have this, too!

add validation and test

replace svelte:slot -> svelte:fragment

slot as a sugar syntax

fix eslint
@Rich-Harris Rich-Harris merged commit 1d6e20f into sveltejs:master Feb 27, 2021
@Steakeye
Copy link

🥳 Thanks!

@paulovieira
Copy link

This is fantastic! Thank you so much one again @tanhauhau! However I'm a bit confused about the previous approach of using "forward slots" (#2079 ).

Is the new <svelte:fragment slot="..."> equivalent to <slot slot="..."> (forward slot)?

I created a quick example and it seems the answer is yes. But it would be good to have a confirmation. Any advantages / disadvantages?

https://svelte.dev/repl/1b2f3fe9bc934e20b72d8b403b860d59?version=3.35.0

Thanks.

@tanhauhau
Copy link
Member Author

tanhauhau commented Mar 4, 2021

@paulovieira no.

<slot> is creating a new default slot,
where the attribute slot="xxx" is to say whatever is inside this element will be placed inside the component's slot named "xxx"

thus,

<!-- B.svelte -->

<C>
   <slot name="xxx" slot="yyy" />
</C>

will place the content within the <slot name="xxx"> into Component C's yyy slot.

so what is the content within <slot name="xxx">?

that'll depend on the usage of Component B

<!-- A.svelte -->
<B>
   <div slot="xxx">this is the content for slot named xxx</div>
</B>

Lastly, the content within <slot name="xxx"> is the fallback content if the slot "xxx" is not provided.

Thus if you have

<!-- B.svelte -->
<C>
   <slot name="xxx" slot="yyy">fallback</slot>
</C>

<!-- A.svelte -->
<B />
<B>
   <div slot="xxx">some content</div>
</B>

you'll see fallback in the first <B /> and some content for the 2nd <B />

lastly, a closed github issues isn't somewhere to ask question

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