diff options
author | 2022-06-23 10:10:54 -0500 | |
---|---|---|
committer | 2022-06-23 10:10:54 -0500 | |
commit | 7373d61cdcaedd64bf5fd60521b157cfa4343558 (patch) | |
tree | db7ba617722a58e4b1b6437f1fcabd7f894fd8b1 /packages/integrations/react | |
parent | 19cd962d0b3433ee305d1d277ca4fc3b93593558 (diff) | |
download | astro-7373d61cdcaedd64bf5fd60521b157cfa4343558.tar.gz astro-7373d61cdcaedd64bf5fd60521b157cfa4343558.tar.zst astro-7373d61cdcaedd64bf5fd60521b157cfa4343558.zip |
Enable named slots in renderers (#3652)
* feat: pass all slots to renderers
* refactor: pass `slots` as top-level props
* test: add named slot test for frameworks
* fix: nested hydration, slots that are not initially rendered
* test: add nested-recursive e2e test
* fix: render unmatched custom element children
* chore: update lockfile
* fix: unrendered slots for client:only
* fix(lit): ensure lit integration uses new slots API
* chore: add changeset
* chore: add changesets
* fix: lit slots
* feat: convert dash-case or snake_case slots to camelCase for JSX
* feat: remove tmpl special logic
* test: add slot components-in-markdown test
* refactor: prefer Object.entries.map() to for/of loop
Co-authored-by: Nate Moore <nate@astro.build>
Diffstat (limited to 'packages/integrations/react')
-rw-r--r-- | packages/integrations/react/client-v17.js | 5 | ||||
-rw-r--r-- | packages/integrations/react/client.js | 5 | ||||
-rw-r--r-- | packages/integrations/react/server-v17.js | 15 | ||||
-rw-r--r-- | packages/integrations/react/server.js | 15 | ||||
-rw-r--r-- | packages/integrations/react/static-html.js | 5 |
5 files changed, 35 insertions, 10 deletions
diff --git a/packages/integrations/react/client-v17.js b/packages/integrations/react/client-v17.js index 1dce34709..443109603 100644 --- a/packages/integrations/react/client-v17.js +++ b/packages/integrations/react/client-v17.js @@ -3,7 +3,10 @@ import { render, hydrate } from 'react-dom'; import StaticHtml from './static-html.js'; export default (element) => - (Component, props, children, { client }) => { + (Component, props, { default: children, ...slotted }, { client }) => { + for (const [key, value] of Object.entries(slotted)) { + props[key] = createElement(StaticHtml, { value, name: key }); + } const componentEl = createElement( Component, props, diff --git a/packages/integrations/react/client.js b/packages/integrations/react/client.js index 2828d2cbe..b41d7845a 100644 --- a/packages/integrations/react/client.js +++ b/packages/integrations/react/client.js @@ -11,8 +11,11 @@ function isAlreadyHydrated(element) { } export default (element) => - (Component, props, children, { client }) => { + (Component, props, { default: children, ...slotted }, { client }) => { if (!element.hasAttribute('ssr')) return; + for (const [key, value] of Object.entries(slotted)) { + props[key] = createElement(StaticHtml, { value, name: key }); + } const componentEl = createElement( Component, props, diff --git a/packages/integrations/react/server-v17.js b/packages/integrations/react/server-v17.js index b48d7b6f4..5d747a832 100644 --- a/packages/integrations/react/server-v17.js +++ b/packages/integrations/react/server-v17.js @@ -2,6 +2,7 @@ import React from 'react'; import ReactDOM from 'react-dom/server.js'; import StaticHtml from './static-html.js'; +const slotName = str => str.trim().replace(/[-_]([a-z])/g, (_, w) => w.toUpperCase()); const reactTypeof = Symbol.for('react.element'); function errorIsComingFromPreactComponent(err) { @@ -50,12 +51,20 @@ function check(Component, props, children) { return isReactComponent; } -function renderToStaticMarkup(Component, props, children, metadata) { +function renderToStaticMarkup(Component, props, { default: children, ...slotted }, metadata) { delete props['class']; - const vnode = React.createElement(Component, { + const slots = {}; + for (const [key, value] of Object.entries(slotted)) { + const name = slotName(key); + slots[name] = React.createElement(StaticHtml, { value, name }); + } + // Note: create newProps to avoid mutating `props` before they are serialized + const newProps = { ...props, + ...slots, children: children != null ? React.createElement(StaticHtml, { value: children }) : undefined, - }); + } + const vnode = React.createElement(Component, newProps); let html; if (metadata && metadata.hydrate) { html = ReactDOM.renderToString(vnode); diff --git a/packages/integrations/react/server.js b/packages/integrations/react/server.js index 776901563..cda839a1c 100644 --- a/packages/integrations/react/server.js +++ b/packages/integrations/react/server.js @@ -2,6 +2,7 @@ import React from 'react'; import ReactDOM from 'react-dom/server'; import StaticHtml from './static-html.js'; +const slotName = str => str.trim().replace(/[-_]([a-z])/g, (_, w) => w.toUpperCase()); const reactTypeof = Symbol.for('react.element'); function errorIsComingFromPreactComponent(err) { @@ -56,12 +57,20 @@ async function getNodeWritable() { return Writable; } -async function renderToStaticMarkup(Component, props, children, metadata) { +async function renderToStaticMarkup(Component, props, { default: children, ...slotted }, metadata) { delete props['class']; - const vnode = React.createElement(Component, { + const slots = {}; + for (const [key, value] of Object.entries(slotted)) { + const name = slotName(key); + slots[name] = React.createElement(StaticHtml, { value, name }); + } + // Note: create newProps to avoid mutating `props` before they are serialized + const newProps = { ...props, + ...slots, children: children != null ? React.createElement(StaticHtml, { value: children }) : undefined, - }); + } + const vnode = React.createElement(Component, newProps); let html; if (metadata && metadata.hydrate) { html = ReactDOM.renderToString(vnode); diff --git a/packages/integrations/react/static-html.js b/packages/integrations/react/static-html.js index ecd76ae9b..9589aaed8 100644 --- a/packages/integrations/react/static-html.js +++ b/packages/integrations/react/static-html.js @@ -7,9 +7,10 @@ import { createElement as h } from 'react'; * As a bonus, we can signal to React that this subtree is * entirely static and will never change via `shouldComponentUpdate`. */ -const StaticHtml = ({ value }) => { +const StaticHtml = ({ value, name }) => { if (!value) return null; - return h('astro-fragment', { + return h('astro-slot', { + name, suppressHydrationWarning: true, dangerouslySetInnerHTML: { __html: value }, }); |