Pricing Blog

Is it safe to place multiple default slots?

  • .kigel-1434901994325413991

    Kigel

    5 days ago

    Hi 🙂
    I'd like to render a default slot in several places within a component. With named slots this doesn't seem to work, but with default slots it somehow does - the slot content gets "cloned" multiple times without any visible errors. But is it intended behaviour/safe to do so? The docs don't mention this feature, afaik
    🆙2
  • lucasg-1434909563831980147

    Lucas G

    5 days ago

    Can you share a screenshot?
  • lucasg-1434909985380241480

    Lucas G

    5 days ago

    You shouldn’t have the same name for a slot though, that wouldn’t work for when you’re actually trying to place elements in it.

    If you want the same default in multiple components then you’ll have to add the content in each one
  • .kigel-1434959423591284808

    Kigel

    5 days ago

    Sure, here is a very basic example:
    This component called "duplicate-content" has two unnamed slots (=default slots) .
    When using this component, the single provided content gets rendered twice.

    As you said, with named slots this does not work. But with unnamed/default slots it does.

    If this behaviour is intended it would allow some advanced mechanics, like conditionally applying a wrapper or conditionally placing content in different locations within a component.

    A real world example in one of our projects: We have a sidebar, which is wrapped in a dialog on mobile screens, but part of the static flow content on desktop screens. Currently, we need to provide the same sidebar content twice, because we use two different slots and locations in our layout component. Not a big deal, but it could be improved with this feature.
    We've also stumpled upon many more use cases for this, so I was wondering if this is safe to do?
    1434959423083905155-image.png
    1434959423389827083-image.png
  • lucasg-1434963713592528937

    Lucas G

    5 days ago

    Ah I think I understand. So you’ll never use both slots with different content
  • It shouldn’t be an issue imo
  • You’ll just not be able to define different content for each slot of course
  • lucasg-1434964135422201946

    Lucas G

    5 days ago

    Team toddle do you guys see an issue with this use case?
  • andreasmoller-1434964885195722832

    Andreas Møller

    5 days ago

    No this should be fine. There are potential traps.

    Nordcraft will duplicate the content so it can be an issue if you have unique ids
  • .kigel-1435184799689085033

    Kigel

    4 days ago

    Ok cool, thank you!
    🙌1