Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tender-slots-unwrap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix conditional named slot callbacks receiving arguments from `Astro.slots.render()`.
11 changes: 9 additions & 2 deletions packages/astro/src/core/render/slots.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { type ComponentSlots, renderSlotToString } from '../../runtime/server/index.js';
import { renderJSX } from '../../runtime/server/jsx.js';
import { isRenderTemplateResult } from '../../runtime/server/render/astro/index.js';
import { chunkToString } from '../../runtime/server/render/index.js';
import { isRenderInstruction } from '../../runtime/server/render/instruction.js';
import type { SSRResult } from '../../types/public/internal.js';
Expand All @@ -8,9 +9,15 @@ import type { AstroLogger } from '../logger/core.js';

function getFunctionExpression(slot: any) {
if (!slot) return;
const expressions = slot?.expressions?.filter((e: unknown) => isRenderInstruction(e) === false);
const expressions = slot?.expressions?.filter(
(e: unknown) => isRenderInstruction(e) === false || isRenderTemplateResult(e),
);
if (expressions?.length !== 1) return;
return expressions[0] as (...args: any[]) => any;
const expression = expressions[0];
if (isRenderTemplateResult(expression)) {
return getFunctionExpression(expression);
}
return expression as (...args: any[]) => any;
}

export class Slots {
Expand Down
7 changes: 7 additions & 0 deletions packages/astro/test/astro-slots.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,13 @@ describe('Slots', () => {
assert.deepEqual(afterDiv.text(), 'Test Content AFTER');
});

it('Arguments can be passed to conditional named slots with Astro.slots.render()', async () => {
const html = await fixture.readFile('/conditional-slotted-callback/index.html');
const $ = cheerio.load(html);

assert.equal($('#conditional-block').text(), 'Block: block');
});

it('Unused slot builds without error', async () => {
const html = await fixture.readFile('/unused-slot/index.html');
const $ = cheerio.load(html);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
let html = '';
if (Astro.slots.has('block')) {
html = await Astro.slots.render('block', [{ test: 'block' }]);
}
---

<Fragment set:html={html} />
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
import ConditionalSlottedCallback from '../components/ConditionalSlottedCallback.astro';
---

<ConditionalSlottedCallback>
{
true && (
<fragment slot="block">
{({ test }) => <div id="conditional-block">Block: {test}</div>}
</fragment>
)
}
</ConditionalSlottedCallback>
Loading