Skip to content

Commit

Permalink
renderSlots helper
Browse files Browse the repository at this point in the history
  • Loading branch information
JesmoDev committed Aug 23, 2024
1 parent 0a11d35 commit 21bcefc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
2 changes: 2 additions & 0 deletions storyhelpers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './spread-directive';
export * from './render-slots';
22 changes: 22 additions & 0 deletions storyhelpers/render-slots.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { html, nothing, TemplateResult } from 'lit';

export function renderSlots(
slots: TemplateResult[],
): TemplateResult | typeof nothing {
// Filter out any null or undefined slots to avoid rendering empty content
const validSlots = slots.filter(Boolean);

// If there are no valid slots, return an empty html result
if (validSlots.length === 0) {
return nothing;
}

// Join slots with consistent formatting; no extra line breaks between them
// prettier-ignore
const formattedSlots = validSlots.map((slot, index) =>
html`${ index === 0 ? '' : '\n'} ${slot}`
);

// Return the combined template results
return html`${formattedSlots}`;
}

0 comments on commit 21bcefc

Please sign in to comment.