svelte icon indicating copy to clipboard operation
svelte copied to clipboard

add slots option to component constructor options object

Open truongsinh opened this issue 4 years ago • 10 comments

This is a continuation of https://github.com/sveltejs/svelte/pull/4296, which tackles https://github.com/sveltejs/svelte/issues/2588#issuecomment-488343541

This commits also ensures that no extra code is added into svelte/internal. Instead, if users want to use slots API, they will import directly from svelte:

// parent.svelte
import { createSlot, slot } from 'svelte';
import MyChild from './mychild.svelte';

new MyCmp({
  slots: createSlot({
    slot(MyChild)
  })
});
<!-- mychild.svelte -->
<div>this div is in nested.svelte</div>
<span>this span is in nested.svelte</span>

Discussions

  • createSlot, slot seems really confusing, what would be good names?
  • see https://github.com/sveltejs/svelte/pull/5687/files#discussion_r525860067
  • typescript and sucrase dep bump to support null coalesce

truongsinh avatar Nov 18 '20 07:11 truongsinh

createSlot, slot seems really confusing, what would be good names?

If it's not too late, here's my 2c:

createSlot may not be needed, at all. It's so simple I would think people are okay doing a .map when needed. For providing the default slot only, the case without createSlot is simpler than with it:

This could be done just with createSlots, as @lukeed does here.

I would prefer the plural name, since the object can carry information for multiple slots. If wanted, there can be a createSlot for only defining the default slot but that may be unnecessary overkill (and people can do such wraps easily, ourselves).

This would make your sample look like:

// parent.svelte
import { createSlots } from 'svelte';
import MyChild from './mychild.svelte';

new MyCmp({
  slots: createSlots({
    default: MyChild
  })
});

The values can be components, elements, or arrays thereof.


Would like to use this with SVG - and it can (I have a project doing that). The PR however currently contains divs, making it HTML specific.

akauppi avatar Dec 29 '20 23:12 akauppi

@truongsinh this PR has a merge conflict. You will need to rebase against master

benmccann avatar Dec 31 '20 22:12 benmccann

This would help a lot when we are trying to write unit tests and we need to programmatically assign something to a slot?

jmsunseri avatar Feb 14 '21 08:02 jmsunseri

Any update on the status of these tickets/pull requests? Would love to see this feature added.

zachstence avatar Jul 15 '21 13:07 zachstence

Any update on this? It has pretty big ramifications for testing svelte components?

PClmnt avatar Jan 28 '22 10:01 PClmnt

looks like this blocks testing slots with vitest and @testing-library/svelte, bummer...

SarcevicAntonio avatar Apr 12 '22 07:04 SarcevicAntonio

Are there any updates on this? This prevents us from testing our components well.

We need to create slot content to thoroughly test our components with unit tests.

vekunz avatar Jun 02 '22 14:06 vekunz

Would love for this to get worked on!

rousah avatar Aug 23 '22 10:08 rousah

@truongsinh just a reminder that this PR needs a rebase

benmccann avatar Feb 23 '23 00:02 benmccann

The lack of the ability to pass slots when creating a component is blocking a lot of people here: https://github.com/testing-library/svelte-testing-library/issues/48 (the two workarounds listed there don't seem to work currently, at least for me — I really don't want to have to use Playwright's experimental component tests to do this!)

Please could this make it to release? It's been over two years for this PR…

unikitty37 avatar Mar 01 '23 17:03 unikitty37