Quantcast
Channel: Active questions tagged svelte - Stack Overflow
Viewing all articles
Browse latest Browse all 1541

Conditional default slot detection in svelte

$
0
0

I have a svelte component with a default slot and a named slot. I have an instance of that component and use the named slot, and use the default slow inside an {#if} statement checking for a boolean.

The problem is inside my component, I have logic that checks for the default slot and renders some padding and border. It seems even when the boolean is false and the {#if} is false, that the default slow is still there, just empty.

Child:

const hasDefaultSlot = Object.keys($$slots).includes('default');

Parent:

<MyComponent><div slot="header"> header </div>{#if checked}<div>body</div>{/if}</MyComponent>

My expectation is that when checked is false, Object.keys($$slots).includes('default') would be false, or $$slots.default would be false, or that $$slots.default wouldn't be a boolean, but an object that contains other info that I can use to check if it's empty.

Sadly, it seems the Svelte compiler sees the #if statement result as always true, even though it changes in real time and starts off false.

Without being able to conditionally detect the slot, I am always rendering the body and body styles for a card item, which messes up the css for the component.


Viewing all articles
Browse latest Browse all 1541

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>