When I’m working on a custom element that has a slot, sometimes I’d like to wrap the slot in a parent element. For example, in this component the slot element is a child of card-body:
Since my-form has a footer, I don’t want to put it inside the card-body of card. Is there a way to conditionally omit the card-body tag in card.html? Something like:
<card title="My Card" no-body="true">
<my-form>
<input /> etc.
</my-form>
</card>
when using the template and:
card.html:
<template>
<card-header>
${title}
</card-header>
<card-body (include this wrapping tag if no-body is false)>
<slot></slot> (but include this always)
</card-body>
</template>
Thanks for any and all help! (I accidentally submitted the post before I was done writing… it’s complete now)
You could do this with processContent() @bigopon has a good writeup here
If you need help, set up your non-working example on codesandbox, post it here, and I will give it a shot this weekend
Thanks for the welcome and the recommendations!! I’m reading through @bigopon’s post and the rest of the thread now and will try a few things out. processContent is new to me so I’ll take some time to learn it. If I figure it out I’ll post my solution. Otherwise I’ll post the non-working example.
When I was reading through the thread my first thought would be to remove the card-body wrapper (while keeping its children) using the node parameter of processContent. I imagined that processContent would give me the whole custom element in node but realized it gives me the slot contents only. Calling node.parentNode doesn’t seem to work either.
My second idea was to go into the node and unwrap the first direct child which is a card-body element. This didn’t work since the slot contents aren’t compiled at this point and my form-base component uses a nested card-body which hasn’t been compiled yet.
Will continue to work on this.
The end result I’m looking for is that when I have form-base as a child of tab, only one card-body element will be used.
Here’s an image that shows what I’m trying to accomplish:
A small note for your form buttons: you need to clearly define which buttons are able to submit your form, and which are not. Button inside a form will have type=submit, which could be very undesirable.
So it should look like this in your form base:
About what you want to achieve, it seems to me if you define card-body in each of the element, you should have card-body, it’s expected. Maybe change how you compose your tab? Normally I would advise against needlessly creating custom element, in your case, it’s the <form-base/>. Aurelia, with its close to bare html characteristic, and many powerful binding should give you an easy time dealing with big big html view, unlike other html in JSS frameworks.