Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix slot attribute inside expressions #3837

Merged
merged 5 commits into from
Jul 7, 2022
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/smooth-files-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix usage of slots inside expressions
2 changes: 1 addition & 1 deletion packages/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"test:e2e:match": "playwright test -g"
},
"dependencies": {
"@astrojs/compiler": "^0.17.1",
"@astrojs/compiler": "^0.18.0",
"@astrojs/language-server": "^0.13.4",
"@astrojs/markdown-remark": "^0.11.3",
"@astrojs/prism": "0.4.1",
Expand Down
13 changes: 13 additions & 0 deletions packages/astro/src/runtime/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,19 @@ export async function renderSlot(_result: any, slotted: string, fallback?: any):
return fallback;
}

export function mergeSlots(...slotted: unknown[]) {
const slots: Record<string, () => any> = {};
for (const slot of slotted) {
if (!slot) continue;
if (typeof slot === 'object') {
Object.assign(slots, slot);
} else if (typeof slot === 'function') {
Object.assign(slots, mergeSlots(slot()));
}
}
return slots;
}

export const Fragment = Symbol('Astro.Fragment');

function guessRenderers(componentUrl?: string): string[] {
Expand Down
9 changes: 9 additions & 0 deletions packages/astro/test/astro-expr.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,13 @@ describe('Expressions', () => {

expect($('#single-escape').html()).to.equal('Astro &amp; Vite');
});

it('Handles switch statements', async () => {
const html = await fixture.readFile('/switch/index.html');
const $ = cheerio.load(html);

expect($('#red').length).to.equal(0);
expect($('#yellow').length).to.equal(1);
expect($('#blue').length).to.equal(0);
});
});
10 changes: 10 additions & 0 deletions packages/astro/test/astro-slots.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ describe('Slots', () => {
expect($('#default').text().trim()).to.equal('Default');
});

it('Conditional named slots work', async () => {
const html = await fixture.readFile('/conditional/index.html');
const $ = cheerio.load(html);

expect($('#a').text().trim()).to.equal('A');
expect($('#b').text().trim()).to.equal('B');
expect($('#c').text().trim()).to.equal('C');
expect($('#default').text().trim()).to.equal('Default');
});

it('Slots render fallback content by default', async () => {
const html = await fixture.readFile('/fallback/index.html');
const $ = cheerio.load(html);
Expand Down
20 changes: 20 additions & 0 deletions packages/astro/test/fixtures/astro-expr/src/pages/switch.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
let title = 'Switch';
let colors = ['red', 'yellow', 'blue'];
---

<html>
<head>
<title>{title}</title>
</head>
<body>
{() => {
const color = colors[1];
switch (color) {
case 'red': return <div id="red">red</div>;
case 'yellow': return <div id="yellow">yellow</div>
case 'blue': return <div id="blue">blue</div>
}
}}
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
import Slotted from '../components/Slotted.astro';
---

<html>
<head>
<!-- Head Stuff -->
</head>
<body>
<Slotted>
{true && <span slot="a">A</span>}
{true ? <span slot="b">B</span> : null}
{() => <span slot="c">C</span>}
{() => {
const value = 0.33;
if (value > 0.25) {
return <span>Default</span>
} else if (value > 0.5) {
return <span>Another</span>
} else if (value > 0.75) {
return <span>Other</span>
}
return <span>Yet Another</span>
}}
</Slotted>
</body>
</html>
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.