-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Enable named slots in renderers #3652
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
3d33e76
feat: pass all slots to renderers
natemoo-re a01d91e
refactor: pass `slots` as top-level props
natemoo-re 401a156
test: add named slot test for frameworks
natemoo-re de35199
fix: nested hydration, slots that are not initially rendered
natemoo-re c001f73
test: add nested-recursive e2e test
natemoo-re 44dcc14
fix: render unmatched custom element children
natemoo-re 82a6f88
chore: update lockfile
natemoo-re 3510876
fix: unrendered slots for client:only
natemoo-re 41a3c67
Merge branch 'main' into feat/slots
natemoo-re f59056d
fix(lit): ensure lit integration uses new slots API
natemoo-re c9b93f7
chore: add changeset
natemoo-re 056b564
chore: add changesets
natemoo-re 1f5d99b
fix: lit slots
natemoo-re fd09a1b
Merge branch 'main' into feat/slots
natemoo-re 04add2a
feat: convert dash-case or snake_case slots to camelCase for JSX
natemoo-re e0dd5c4
Merge branch 'main' into feat/slots
natemoo-re 0b9fe10
feat: remove tmpl special logic
natemoo-re 3cf6f76
test: add slot components-in-markdown test
natemoo-re 41344a4
refactor: prefer Object.entries.map() to for/of loop
natemoo-re b6fdaba
Merge branch 'main' into feat/slots
natemoo-re File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'@astrojs/lit': minor | ||
--- | ||
|
||
Adds support for passing named slots from `.astro` => Lit components. | ||
|
||
All slots are treated as Light DOM content. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
--- | ||
'@astrojs/preact': minor | ||
'@astrojs/react': minor | ||
'@astrojs/solid-js': minor | ||
--- | ||
|
||
Add support for passing named slots from `.astro` => framework components. | ||
|
||
Each `slot` is be passed as a top-level prop. For example: | ||
|
||
```jsx | ||
// From .astro | ||
<Component> | ||
<h2 slot="title">Hello world!</h2> | ||
<h2 slot="slot-with-dash">Dash</h2> | ||
<div>Default</div> | ||
</Component> | ||
|
||
// For .jsx | ||
export default function Component({ title, slotWithDash, children }) { | ||
return ( | ||
<> | ||
<div id="title">{title}</div> | ||
<div id="slot-with-dash">{slotWithDash}</div> | ||
<div id="main">{children}</div> | ||
</> | ||
) | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'astro': patch | ||
--- | ||
|
||
Add renderer support for passing named slots to framework components. | ||
|
||
**BREAKING**: integrations using the `addRenderer()` API are now passed all named slots via `Record<string, string>` rather than `string`. Previously only the default slot was passed. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
'@astrojs/svelte': minor | ||
'@astrojs/vue': minor | ||
--- | ||
|
||
Adds support for passing named slots from `.astro` => framework components. | ||
|
||
Inside your components, use the built-in `slot` API as you normally would. |
12 changes: 12 additions & 0 deletions
12
packages/astro/e2e/fixtures/nested-recursive/astro.config.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { defineConfig } from 'astro/config'; | ||
import preact from '@astrojs/preact'; | ||
import react from '@astrojs/react'; | ||
import svelte from '@astrojs/svelte'; | ||
import vue from '@astrojs/vue'; | ||
import solid from '@astrojs/solid-js'; | ||
|
||
// https://astro.build/config | ||
export default defineConfig({ | ||
// Enable many frameworks to support all different kinds of components. | ||
integrations: [preact(), react(), svelte(), vue(), solid()], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"name": "@e2e/nested-recursive", | ||
"version": "0.0.0", | ||
"private": true, | ||
"devDependencies": { | ||
"@astrojs/preact": "workspace:*", | ||
"@astrojs/react": "workspace:*", | ||
"@astrojs/solid-js": "workspace:*", | ||
"@astrojs/svelte": "workspace:*", | ||
"@astrojs/vue": "workspace:*", | ||
"astro": "workspace:*" | ||
}, | ||
"dependencies": { | ||
"preact": "^10.7.3", | ||
"react": "^18.1.0", | ||
"react-dom": "^18.1.0", | ||
"solid-js": "^1.4.3", | ||
"svelte": "^3.48.0", | ||
"vue": "^3.2.36" | ||
}, | ||
"scripts": { | ||
"dev": "astro dev" | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
packages/astro/e2e/fixtures/nested-recursive/src/components/PreactCounter.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { useState } from 'preact/hooks'; | ||
|
||
/** a counter written in Preact */ | ||
export default function PreactCounter({ children, id }) { | ||
const [count, setCount] = useState(0); | ||
const add = () => setCount((i) => i + 1); | ||
const subtract = () => setCount((i) => i - 1); | ||
|
||
return ( | ||
<div id={id} class="counter"> | ||
<button class="decrement" onClick={subtract}>-</button> | ||
<pre id={`${id}-count`}>{count}</pre> | ||
<button id={`${id}-increment`} class="increment" onClick={add}>+</button> | ||
<div class="children">{children}</div> | ||
</div> | ||
); | ||
} |
17 changes: 17 additions & 0 deletions
17
packages/astro/e2e/fixtures/nested-recursive/src/components/ReactCounter.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { useState } from 'react'; | ||
|
||
/** a counter written in React */ | ||
export default function ReactCounter({ children, id }) { | ||
const [count, setCount] = useState(0); | ||
const add = () => setCount((i) => i + 1); | ||
const subtract = () => setCount((i) => i - 1); | ||
|
||
return ( | ||
<div id={id} className="counter"> | ||
<button className="decrement" onClick={subtract}>-</button> | ||
<pre id={`${id}-count`}>{count}</pre> | ||
<button id={`${id}-increment`} className="increment" onClick={add}>+</button> | ||
<div className="children">{children}</div> | ||
</div> | ||
); | ||
} |
17 changes: 17 additions & 0 deletions
17
packages/astro/e2e/fixtures/nested-recursive/src/components/SolidCounter.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { createSignal } from 'solid-js'; | ||
|
||
/** a counter written with Solid */ | ||
export default function SolidCounter({ children, id }) { | ||
const [count, setCount] = createSignal(0); | ||
const add = () => setCount(count() + 1); | ||
const subtract = () => setCount(count() - 1); | ||
|
||
return ( | ||
<div id={id} class="counter"> | ||
<button class="decrement" onClick={subtract}>-</button> | ||
<pre id={`${id}-count`}>{count()}</pre> | ||
<button id={`${id}-increment`} class="increment" onClick={add}>+</button> | ||
<div class="children">{children}</div> | ||
</div> | ||
); | ||
} |
29 changes: 29 additions & 0 deletions
29
packages/astro/e2e/fixtures/nested-recursive/src/components/SvelteCounter.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
|
||
<script> | ||
export let id; | ||
let children; | ||
let count = 0; | ||
|
||
function add() { | ||
count += 1; | ||
} | ||
|
||
function subtract() { | ||
count -= 1; | ||
} | ||
</script> | ||
|
||
<div {id} class="counter"> | ||
<button class="decrement" on:click={subtract}>-</button> | ||
<pre id={`${id}-count`}>{ count }</pre> | ||
<button id={`${id}-increment`} class="increment" on:click={add}>+</button> | ||
<div class="children"> | ||
<slot /> | ||
</div> | ||
</div> | ||
|
||
<style> | ||
.counter { | ||
background: white; | ||
} | ||
</style> |
34 changes: 34 additions & 0 deletions
34
packages/astro/e2e/fixtures/nested-recursive/src/components/VueCounter.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<template> | ||
<div :id="id" class="counter"> | ||
<button class="decrement" @click="subtract()">-</button> | ||
<pre :id="`${id}-count`">{{ count }}</pre> | ||
<button :id="`${id}-increment`" class="increment" @click="add()">+</button> | ||
<div class="children"> | ||
<slot /> | ||
</div> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
import { ref } from 'vue'; | ||
export default { | ||
props: { | ||
id: { | ||
type: String, | ||
required: true | ||
} | ||
}, | ||
setup(props) { | ||
const count = ref(0); | ||
const add = () => (count.value = count.value + 1); | ||
const subtract = () => (count.value = count.value - 1); | ||
|
||
return { | ||
id: props.id, | ||
count, | ||
add, | ||
subtract, | ||
}; | ||
}, | ||
}; | ||
</script> |
28 changes: 28 additions & 0 deletions
28
packages/astro/e2e/fixtures/nested-recursive/src/pages/index.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
--- | ||
import ReactCounter from '../components/ReactCounter.jsx'; | ||
import PreactCounter from '../components/PreactCounter.tsx'; | ||
import SolidCounter from '../components/SolidCounter.tsx'; | ||
import VueCounter from '../components/VueCounter.vue'; | ||
import SvelteCounter from '../components/SvelteCounter.svelte'; | ||
--- | ||
|
||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<meta name="viewport" content="width=device-width" /> | ||
<link rel="icon" type="image/x-icon" href="/favicon.ico" /> | ||
</head> | ||
<body> | ||
<main> | ||
<ReactCounter id="react-counter" client:idle> | ||
<PreactCounter id="preact-counter" client:idle> | ||
<SolidCounter id="solid-counter" client:idle> | ||
<SvelteCounter id="svelte-counter" client:idle> | ||
<VueCounter id="vue-counter" client:idle /> | ||
</SvelteCounter> | ||
</SolidCounter> | ||
</PreactCounter> | ||
</ReactCounter> | ||
</main> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { test as base, expect } from '@playwright/test'; | ||
import { loadFixture } from './test-utils.js'; | ||
|
||
const test = base.extend({ | ||
astro: async ({}, use) => { | ||
const fixture = await loadFixture({ root: './fixtures/nested-recursive/' }); | ||
await use(fixture); | ||
}, | ||
}); | ||
|
||
let devServer; | ||
|
||
test.beforeEach(async ({ astro }) => { | ||
devServer = await astro.startDevServer(); | ||
}); | ||
|
||
test.afterEach(async () => { | ||
await devServer.stop(); | ||
}); | ||
|
||
test.describe('Recursive Nested Frameworks', () => { | ||
test('React counter', async ({ astro, page }) => { | ||
await page.goto('/'); | ||
|
||
const counter = await page.locator('#react-counter'); | ||
await expect(counter, 'component is visible').toBeVisible(); | ||
|
||
const count = await counter.locator('#react-counter-count'); | ||
await expect(count, 'initial count is 0').toHaveText('0'); | ||
|
||
const increment = await counter.locator('#react-counter-increment'); | ||
await increment.click(); | ||
|
||
await expect(count, 'count incremented by 1').toHaveText('1'); | ||
}); | ||
|
||
test('Preact counter', async ({ astro, page }) => { | ||
await page.goto('/'); | ||
|
||
const counter = await page.locator('#preact-counter'); | ||
await expect(counter, 'component is visible').toBeVisible(); | ||
|
||
const count = await counter.locator('#preact-counter-count'); | ||
await expect(count, 'initial count is 0').toHaveText('0'); | ||
|
||
const increment = await counter.locator('#preact-counter-increment'); | ||
await increment.click(); | ||
|
||
await expect(count, 'count incremented by 1').toHaveText('1'); | ||
}); | ||
|
||
test('Solid counter', async ({ astro, page }) => { | ||
await page.goto('/'); | ||
|
||
const counter = await page.locator('#solid-counter'); | ||
await expect(counter, 'component is visible').toBeVisible(); | ||
|
||
const count = await counter.locator('#solid-counter-count'); | ||
await expect(count, 'initial count is 0').toHaveText('0'); | ||
|
||
const increment = await counter.locator('#solid-counter-increment'); | ||
await increment.click(); | ||
|
||
await expect(count, 'count incremented by 1').toHaveText('1'); | ||
}); | ||
|
||
test('Vue counter', async ({ astro, page }) => { | ||
await page.goto('/'); | ||
|
||
const counter = await page.locator('#vue-counter'); | ||
await expect(counter, 'component is visible').toBeVisible(); | ||
|
||
const count = await counter.locator('#vue-counter-count'); | ||
await expect(count, 'initial count is 0').toHaveText('0'); | ||
|
||
const increment = await counter.locator('#vue-counter-increment'); | ||
await increment.click(); | ||
|
||
await expect(count, 'count incremented by 1').toHaveText('1'); | ||
}); | ||
|
||
test('Svelte counter', async ({ astro, page }) => { | ||
await page.goto('/'); | ||
|
||
const counter = await page.locator('#svelte-counter'); | ||
await expect(counter, 'component is visible').toBeVisible(); | ||
|
||
const count = await counter.locator('#svelte-counter-count'); | ||
await expect(count, 'initial count is 0').toHaveText('0'); | ||
|
||
const increment = await counter.locator('#svelte-counter-increment'); | ||
await increment.click(); | ||
|
||
await expect(count, 'count incremented by 1').toHaveText('1'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can now have many
astro-slot
elements, so we do aquerySelectorAll
.