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

feat: conditional slots #8304

Closed
wants to merge 34 commits into from

Conversation

tanhauhau
Copy link
Member

@tanhauhau tanhauhau commented Feb 21, 2023

Implementing sveltejs/rfcs#64

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with npm test and lint the project with npm run lint

@vercel
Copy link

vercel bot commented Feb 21, 2023

@tanhauhau is attempting to deploy a commit to the Svelte Team on Vercel.

A member of the Team first needs to authorize it.

@tanhauhau tanhauhau marked this pull request as draft February 21, 2023 16:52
@tanhauhau tanhauhau force-pushed the feat/conditional-slots branch from 1497cf4 to 347774a Compare February 23, 2023 00:36
@tanhauhau tanhauhau changed the title feat: conditional slots csr feat: conditional slots Feb 23, 2023
@tanhauhau tanhauhau marked this pull request as ready for review February 23, 2023 00:39
@vercel
Copy link

vercel bot commented Feb 23, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated
svelte-dev-2 ❌ Failed (Inspect) Feb 23, 2023 at 5:57PM (UTC)

@tanhauhau tanhauhau force-pushed the feat/conditional-slots branch from af38d89 to 07d4350 Compare February 28, 2023 16:06
mpellerin42 added a commit to nuclia/frontend that referenced this pull request Mar 3, 2023
Seems like there is a bug in Svelte which make $$slots.default being true even if the slot is not there. The code should work once sveltejs/svelte#8304 is fixed.
mpellerin42 added a commit to nuclia/frontend that referenced this pull request Mar 3, 2023
* Add menu in expanded tile when provided by viewer or searchResults widget

* Setup tile menu in dashboard's search results

* Attempt to fix separator display when no result navigator.

Seems like there is a bug in Svelte which make $$slots.default being true even if the slot is not there. The code should work once sveltejs/svelte#8304 is fixed.

* Update our package versions

* Use ng14 branch of Pastanaga

the time for us to upgrade our apps to ng15

* Fix tests
@JReinhold
Copy link

Does this make it work with {#each} too, or only for {#if} conditions?

Eg. would this work:

<script>
  // an array of tuples, [['slotName', SlotContent],...];
  const someDynamicSlots = getSlots();
</script>

<ChildComponent>
	{#each someDynamicSlots as [name, Content]}
		<Content slot={name} />
	{/each}
</ChildComponent>

This is useful when at compile time you don't know which or how many slots you have, but that might be out of scope for this.

benmccann and others added 6 commits April 13, 2023 15:46
…s#7442)


---------

Co-authored-by: Ivan Hofer <[email protected]>
Co-authored-by: Simon H <[email protected]>
Co-authored-by: Ignatius Bagus <[email protected]>
Co-authored-by: Ben McCann <[email protected]>
Co-authored-by: Simon Holthausen <[email protected]>
Null and undefined `value` bindings should always be set to an empty string. This allows native browser validation of `required` fields to work as expected with placeholder options.

Placeholder options bound to null are necessary in forms where the field is conditionally required, and the bound value is posted to an API endpoint which requires it to be a nullable number or object rather than a string.

fixes sveltejs#8312
@dummdidumm dummdidumm added this to the 4.x milestone Apr 14, 2023
@dummdidumm dummdidumm changed the base branch from master to version-4 April 14, 2023 15:47
@dummdidumm
Copy link
Member

Svelte 5 will substantially rewrite how things works under the hood. Adding such a big enhancement prior to us knowing if it and how it would work in Svelte 5 feels dangerous, so we're holding off from merging until we have more confidence in how Svelte 5 shapes out. Sorry to make everyone wait a little longer, but we're very aware of people wanting such a feature and are thinking actively about it.

@opensas
Copy link
Contributor

opensas commented Mar 18, 2024

Will this make conditionally forwarding named slots in svelte 4 possible?

Right now I get this error if I try to conditionally forward (meaning, only forward it if it was specified) a named slot:

  <!-- 
    Error: Element with a slot='...' attribute must be 
    a child of a component or a descendant of a custom element 
  -->
  {#if $$slots.title}<slot name="title" slot="title" />{/if}

btw, the (clumsy) workaround I found so far is to add an optional slots prop to let caller override the $$slots variable of the child component, like this:

<!-- Title.svelte -->
<script>
	export let slots = $$slots // allow to override $$slots
</script>

<div>
	{#if slots.title}<h3><slot name="title" /></h3>{/if}
	<slot />
</div>

@Rich-Harris
Copy link
Member

Closing since slots are superseded by snippets in Svelte 5, which provide the necessary flexibility

@Rich-Harris Rich-Harris closed this Apr 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet