Skip to content

Commit

Permalink
fix: renderer behavior with no children (#2078)
Browse files Browse the repository at this point in the history
* fix: renderer behavior with no children

* [ci] Prettier fix

* Force CI

* fix: properly handle falsy values

* [ci] Prettier fix

* chore: force ci

* [experiment] netlify ignore

Co-authored-by: GitHub Action <github-action@users.noreply.github.com>
  • Loading branch information
natemoo-re and GitHub Action committed Dec 2, 2021
1 parent 62a5e98 commit ac3e870
Show file tree
Hide file tree
Showing 27 changed files with 356 additions and 19 deletions.
10 changes: 10 additions & 0 deletions .changeset/many-schools-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'astro': patch
'@astrojs/renderer-preact': patch
'@astrojs/renderer-react': patch
'@astrojs/renderer-svelte': patch
'@astrojs/renderer-vue': patch
'@astrojs/renderer-solid': patch
---

Fix behavior of renderers when no children are passed in
2 changes: 1 addition & 1 deletion netlify.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[build]
ignore = "git diff --quiet main HEAD www/ docs/"
ignore = "git diff --quiet $COMMIT_REF $CACHED_COMMIT_REF -- docs/ www/"
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { h, Fragment } from 'preact';
import { useState } from 'preact/hooks'

export default function Counter({ children, count: initialCount, case: id }) {
const [count, setCount] = useState(initialCount);
const add = () => setCount((i) => i + 1);
const subtract = () => setCount((i) => i - 1);

return (
<>
<div className="counter">
<button onClick={subtract}>-</button>
<pre>{count}</pre>
<button onClick={add}>+</button>
</div>
<div id={id} className="counter-message">
{children || <h1>Fallback</h1>}
</div>
</>
);
}
11 changes: 11 additions & 0 deletions packages/astro/test/fixtures/slots-preact/src/pages/index.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
import Counter from '../components/Counter.jsx'
---
<main>
<Counter case="default-self-closing" client:visible/>
<Counter case="default-empty" client:visible></Counter>
<Counter case="zero" client:visible>{0}</Counter>
<Counter case="false" client:visible>{false}</Counter>
<Counter case="string" client:visible>{''}</Counter>
<Counter case="content" client:visible><h1 id="slotted">Hello world!</h1></Counter>
</main>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React, { useState } from 'react';

export default function Counter({ children, count: initialCount, case: id }) {
const [count, setCount] = useState(initialCount);
const add = () => setCount((i) => i + 1);
const subtract = () => setCount((i) => i - 1);

return (
<>
<div className="counter">
<button onClick={subtract}>-</button>
<pre>{count}</pre>
<button onClick={add}>+</button>
</div>
<div id={id} className="counter-message">
{children || <h1>Fallback</h1>}
</div>
</>
);
}
11 changes: 11 additions & 0 deletions packages/astro/test/fixtures/slots-react/src/pages/index.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
import Counter from '../components/Counter.jsx'
---
<main>
<Counter case="default-self-closing" client:visible/>
<Counter case="default-empty" client:visible></Counter>
<Counter case="zero" client:visible>{0}</Counter>
<Counter case="false" client:visible>{false}</Counter>
<Counter case="string" client:visible>{''}</Counter>
<Counter case="content" client:visible><h1 id="slotted">Hello world!</h1></Counter>
</main>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React, { useState } from 'react';

export default function Counter({ children, count: initialCount, case: id }) {
return (
<>
<div className="counter">
<pre>{0}</pre>
</div>
<div id={id} className="counter-message">
{children || <h1>Fallback</h1>}
</div>
</>
);
}
11 changes: 11 additions & 0 deletions packages/astro/test/fixtures/slots-solid/src/pages/index.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
import Counter from '../components/Counter.jsx'
---
<main>
<Counter case="default-self-closing" client:visible/>
<Counter case="default-empty" client:visible></Counter>
<Counter case="zero" client:visible>{0}</Counter>
<Counter case="false" client:visible>{false}</Counter>
<Counter case="string" client:visible>{''}</Counter>
<Counter case="content" client:visible><h1 id="slotted">Hello world!</h1></Counter>
</main>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<script>
let count = 0;
export let id;
function add() {
count += 1;
}
function subtract() {
count -= 1;
}
</script>

<div class="counter">
<button on:click={subtract}>-</button>
<pre>{ count }</pre>
<button on:click={add}>+</button>
</div>
<div id={id}>
<slot>
<h1 id="fallback">Fallback</h1>
</slot>
</div>

<style>
.counter{
display: grid;
font-size: 2em;
grid-template-columns: repeat(3, minmax(0, 1fr));
margin-top: 2em;
place-items: center;
}
.message {
text-align: center;
}
</style>
11 changes: 11 additions & 0 deletions packages/astro/test/fixtures/slots-svelte/src/pages/index.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
import Counter from '../components/Counter.svelte'
---
<main>
<Counter id="default-self-closing" client:visible/>
<Counter id="default-empty" client:visible></Counter>
<Counter case="zero" client:visible>{0}</Counter>
<Counter case="false" client:visible>{false}</Counter>
<Counter case="string" client:visible>{''}</Counter>
<Counter id="content" client:visible><h1 id="slotted">Hello world!</h1></Counter>
</main>
46 changes: 46 additions & 0 deletions packages/astro/test/fixtures/slots-vue/src/components/Counter.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<template>
<div class="counter">
<button @click="subtract()">-</button>
<pre>{{ count }}</pre>
<button @click="add()">+</button>
</div>
<div :id="case" class="counter-message">
<slot>
<h1>Fallback</h1>
</slot>
</div>
</template>

<script>
import { ref } from 'vue';
export default {
props: {
case: String,
},
setup(props) {
const count = ref(0);
const add = () => (count.value = count.value + 1);
const subtract = () => (count.value = count.value - 1);
return {
case: props.case,
count,
add,
subtract,
};
},
};
</script>

<style>
.counter {
display: grid;
font-size: 2em;
grid-template-columns: repeat(3, minmax(0, 1fr));
margin-top: 2em;
place-items: center;
}
.counter-message {
text-align: center;
}
</style>
11 changes: 11 additions & 0 deletions packages/astro/test/fixtures/slots-vue/src/pages/index.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
import Counter from '../components/Counter.vue'
---
<main>
<Counter case="default-self-closing" client:visible/>
<Counter case="default-empty" client:visible></Counter>
<Counter case="zero" client:visible>{0}</Counter>
<Counter case="false" client:visible>{false}</Counter>
<Counter case="string" client:visible>{''}</Counter>
<Counter case="content" client:visible><h1 id="slotted">Hello world!</h1></Counter>
</main>
24 changes: 24 additions & 0 deletions packages/astro/test/slots-preact.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { expect } from 'chai';
import cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';

describe('Slots: Preact', () => {
let fixture;

before(async () => {
fixture = await loadFixture({ projectRoot: './fixtures/slots-preact/', renderers: ['@astrojs/renderer-preact'] });
await fixture.build();
});

it('Renders default slot', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);

expect($('#default-self-closing').text().trim()).to.equal('Fallback');
expect($('#default-empty').text().trim()).to.equal('Fallback');
expect($('#zero').text().trim()).to.equal('0');
expect($('#false').text().trim()).to.equal('');
expect($('#string').text().trim()).to.equal('');
expect($('#content').text().trim()).to.equal('Hello world!');
});
});
24 changes: 24 additions & 0 deletions packages/astro/test/slots-react.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { expect } from 'chai';
import cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';

describe('Slots: React', () => {
let fixture;

before(async () => {
fixture = await loadFixture({ projectRoot: './fixtures/slots-react/', renderers: ['@astrojs/renderer-react'] });
await fixture.build();
});

it('Renders default slot', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);

expect($('#default-self-closing').text().trim()).to.equal('Fallback');
expect($('#default-empty').text().trim()).to.equal('Fallback');
expect($('#zero').text().trim()).to.equal('0');
expect($('#false').text().trim()).to.equal('');
expect($('#string').text().trim()).to.equal('');
expect($('#content').text().trim()).to.equal('Hello world!');
});
});
24 changes: 24 additions & 0 deletions packages/astro/test/slots-solid.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { expect } from 'chai';
import cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';

describe('Slots: Solid', () => {
let fixture;

before(async () => {
fixture = await loadFixture({ projectRoot: './fixtures/slots-solid/', renderers: ['@astrojs/renderer-solid'] });
await fixture.build();
});

it('Renders default slot', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);

expect($('#default-self-closing').text().trim()).to.equal('Fallback');
expect($('#default-empty').text().trim()).to.equal('Fallback');
expect($('#zero').text().trim()).to.equal('0');
expect($('#false').text().trim()).to.equal('');
expect($('#string').text().trim()).to.equal('');
expect($('#content').text().trim()).to.equal('Hello world!');
});
});
24 changes: 24 additions & 0 deletions packages/astro/test/slots-svelte.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { expect } from 'chai';
import cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';

describe('Slots: Svelte', () => {
let fixture;

before(async () => {
fixture = await loadFixture({ projectRoot: './fixtures/slots-svelte/', renderers: ['@astrojs/renderer-svelte'] });
await fixture.build();
});

it('Renders default slot', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);

expect($('#default-self-closing').text().trim()).to.equal('Fallback');
expect($('#default-empty').text().trim()).to.equal('Fallback');
expect($('#zero').text().trim()).to.equal('');
expect($('#false').text().trim()).to.equal('');
expect($('#string').text().trim()).to.equal('');
expect($('#content').text().trim()).to.equal('Hello world!');
});
});
24 changes: 24 additions & 0 deletions packages/astro/test/slots-vue.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { expect } from 'chai';
import cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';

describe('Slots: Vue', () => {
let fixture;

before(async () => {
fixture = await loadFixture({ projectRoot: './fixtures/slots-vue/', renderers: ['@astrojs/renderer-vue'] });
await fixture.build();
});

it('Renders default slot', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);

expect($('#default-self-closing').text().trim()).to.equal('Fallback');
expect($('#default-empty').text().trim()).to.equal('Fallback');
expect($('#zero').text().trim()).to.equal('0');
expect($('#false').text().trim()).to.equal('');
expect($('#string').text().trim()).to.equal('');
expect($('#content').text().trim()).to.equal('Hello world!');
});
});
2 changes: 1 addition & 1 deletion packages/renderers/renderer-preact/client.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { h, render } from 'preact';
import StaticHtml from './static-html.js';

export default (element) => (Component, props, children) => render(h(Component, props, h(StaticHtml, { value: children })), element);
export default (element) => (Component, props, children) => render(h(Component, props, children != null ? h(StaticHtml, { value: children }) : children), element);
2 changes: 1 addition & 1 deletion packages/renderers/renderer-preact/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function check(Component, props, children) {
}

function renderToStaticMarkup(Component, props, children) {
const html = render(h(Component, { ...props, children: h(StaticHtml, { value: children }), innerHTML: children }));
const html = render(h(Component, props, children != null ? h(StaticHtml, { value: children }) : children));
return { html };
}

Expand Down
9 changes: 8 additions & 1 deletion packages/renderers/renderer-react/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,11 @@ import { hydrate } from 'react-dom';
import StaticHtml from './static-html.js';

export default (element) => (Component, props, children) =>
hydrate(createElement(Component, { ...props, suppressHydrationWarning: true }, createElement(StaticHtml, { value: children, suppressHydrationWarning: true })), element);
hydrate(
createElement(
Component,
{ ...props, suppressHydrationWarning: true },
children != null ? createElement(StaticHtml, { value: children, suppressHydrationWarning: true }) : children
),
element
);
2 changes: 1 addition & 1 deletion packages/renderers/renderer-react/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function renderToStaticMarkup(Component, props, children, metadata) {
delete props['class'];
const vnode = React.createElement(Component, {
...props,
children: React.createElement(StaticHtml, { value: children }),
children: children != null ? React.createElement(StaticHtml, { value: children }) : undefined,
});
let html;
if (metadata && metadata.hydrate) {
Expand Down
7 changes: 5 additions & 2 deletions packages/renderers/renderer-solid/client.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { hydrate, createComponent } from 'solid-js/web';

export default (element) => (Component, props, childHTML) => {
const children = document.createElement('astro-fragment');
children.innerHTML = childHTML;
let children;
if (childHTML != null) {
children = document.createElement('astro-fragment');
children.innerHTML = childHTML;
}

// Using Solid's `hydrate` method ensures that a `root` is created
// in order to properly handle reactivity. It also handles
Expand Down

0 comments on commit ac3e870

Please sign in to comment.