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

add slots option to component constructor options object #4296

Closed
wants to merge 6 commits into from

Conversation

tanhauhau
Copy link
Member

@tanhauhau tanhauhau commented Jan 21, 2020

rebased #2684

fixes #2588

@Conduitry
Copy link
Member

I'm assuming this doesn't support any sort of interactivity between the component and the slots you pass to it?

I think the main thing I'm worried about here is forcing everyone who uses slots to ship this extra code, whether they want the functionality or not.

@tanhauhau
Copy link
Member Author

So I guess maybe we can add a compile option to turn this on?

@Conduitry
Copy link
Member

Maybe. I'd prefer in general not to add compiler options though - and it feels like most of this feature already exists, just as part of the private $$slots prop. Is there any way to re-use more of the existing code when implementing this, or a way to change the private API so that it's more usable?

@TehShrike
Copy link
Member

If the extra bytes are only added in the case of components that accept slot content, that seems reasonable to me.

I imagine that the general case by far for a component with <slot> is to pass in slotted content.

@TehShrike
Copy link
Member

Requiring a compilation option or special outside code to pass in slot content to the constructor function of a component that has a <slot> feels like the equivalent of requiring a special option/custom code in order to pass in props.

@tanhauhau
Copy link
Member Author

will it be too much if to ask the user to call createSlot({ slotName: slotElements }) before passing in as options.slot?

@tanhauhau tanhauhau added the slot label Mar 25, 2020
@wyozi
Copy link

wyozi commented May 16, 2020

How would a user create Svelte components to pass as slots? Since creating such a component requires a target: DOMElement, the component will either need an arbitrary document.createElement("div") wrapper or use some magic to not require target for the slot component. How does Svelte internally do this?

const wrapper = document.createElement("div");
const slot = new SlotComponent({target: wrapper});
const actual = new ActualComponent({target: ..., slots: {default: slot}});

Copy link
Member

@lukeed lukeed left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

slightly less runtime 😉

src/runtime/slot/index.ts Outdated Show resolved Hide resolved
src/runtime/slot/index.ts Outdated Show resolved Hide resolved
src/runtime/slot/index.ts Outdated Show resolved Hide resolved
@lukeed
Copy link
Member

lukeed commented Sep 30, 2020

I think this is missing the ability to mount a Component inside a slot?

new Parent({
  target: document.body,
  props: { ... },
  slots: {
    // Parent has all info to `new Child`
    default: Child
  }
});

tanhauhau and others added 3 commits October 1, 2020 08:57
Co-authored-by: Luke Edwards <luke.edwards05@gmail.com>
Co-authored-by: Luke Edwards <luke.edwards05@gmail.com>
Co-authored-by: Luke Edwards <luke.edwards05@gmail.com>
@tanhauhau
Copy link
Member Author

Is this how we are going to pass a component into a slot? What would be the props of the component?

@lukeed
Copy link
Member

lukeed commented Oct 1, 2020

This is what I ended up with, and is working for me already:

new Parent({
  target: document.body,
  props: {
    $$scope: {},
    $$slots: create({
      default: [ Child ],
      // Or
      default: [ new Child({ $$inline: true, props: {...} }) ]
    })
  }
});

This works in current svelte@latest

If this is the approach we want to take, we can work on making it nicer / have pieces hidden. For example, a top-level slots option can implicitly call this create method when passing to props.

@lukeed
Copy link
Member

lukeed commented Oct 1, 2020

I should also note that my create helper is based off the one in this PR and even with the additional Component support, it's still a small utility

@truongsinh
Copy link

FYI I continue this PR at #5687

@akauppi
Copy link

akauppi commented Dec 29, 2020

@tanhauhau @truongsinh Isn't it weird to have two PRs with the same title, both open?

@timonmasberg
Copy link

Will this be merged, it is highly needed for testing 👀

@CcSimple
Copy link

Looking forward to this, which is very necessary, especially for developing components.

@B-Esmaili
Copy link

Looks like svelte team work on what only matters for themselves. You'd better listen to what community want right? This is second PR that is awaiting merge related to slot. Slot is turning into a nightmare for me. You don't even bother making clear your plan about these PRs. These PRs are 2-3 years old. This is not something to postpone to later time. I wonder how you do component testing without being able to pass slot to theme? Please make clear you roadmap about slots. PLEASE!

@dummdidumm dummdidumm added this to the one day milestone Mar 20, 2023
@letzfets
Copy link

Is there any news on this? The slot option would be highly appreciated for component testing.

@amyipdev
Copy link

Bumping due to how amazingly useful this would be

@Rich-Harris
Copy link
Member

Closing Svelte 4 PRs as stale — thank you

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

Successfully merging this pull request may close these issues.

Option to set slots when create component instance