-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add experimentalReactChildren option to React integration (#8082)
* wip: support true react vnodes in renderer * Add new experimentalReactChildren option to React integration * Update the test * Add docs * Update packages/integrations/react/server.js Co-authored-by: Nate Moore <[email protected]> * Update with a better test * Update .changeset/yellow-snakes-jam.md Co-authored-by: Sarah Rainsberger <[email protected]> * Update packages/integrations/react/README.md Co-authored-by: Sarah Rainsberger <[email protected]> * Update packages/integrations/react/README.md Co-authored-by: Sarah Rainsberger <[email protected]> --------- Co-authored-by: Nate Moore <[email protected]> Co-authored-by: Nate Moore <[email protected]> Co-authored-by: Sarah Rainsberger <[email protected]>
- Loading branch information
1 parent
7177f75
commit 16a3fdf
Showing
36 changed files
with
218 additions
and
38 deletions.
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,21 @@ | ||
--- | ||
'@astrojs/react': minor | ||
--- | ||
|
||
Optionally parse React slots as React children. | ||
|
||
This adds a new configuration option for the React integration `experimentalReactChildren`: | ||
|
||
```js | ||
export default { | ||
integrations: [ | ||
react({ | ||
experimentalReactChildren: true, | ||
}) | ||
] | ||
} | ||
``` | ||
|
||
With this enabled, children passed to React from Astro components via the default slot are parsed as React components. | ||
|
||
This enables better compatibility with certain React components which manipulate their children. |
5 changes: 0 additions & 5 deletions
5
packages/astro/test/fixtures/react-component/src/components/WithChildren.jsx
This file was deleted.
Oops, something went wrong.
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
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
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 10 additions & 0 deletions
10
packages/integrations/react/test/fixtures/react-component/src/components/WithChildren.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,10 @@ | ||
import React from 'react'; | ||
|
||
export default function ({ children }) { | ||
return ( | ||
<div> | ||
<div className="with-children">{children}</div> | ||
<div className="with-children-count">{children.length}</div> | ||
</div> | ||
); | ||
} |
File renamed without changes.
14 changes: 14 additions & 0 deletions
14
packages/integrations/react/test/fixtures/react-component/src/pages/children.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,14 @@ | ||
--- | ||
import WithChildren from '../components/WithChildren'; | ||
--- | ||
|
||
<html> | ||
<head> | ||
<!-- Head Stuff --> | ||
</head> | ||
<body> | ||
<WithChildren> | ||
<div>child 1</div><div>child 2</div> | ||
</WithChildren> | ||
</body> | ||
</html> |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { parse, walkSync, DOCUMENT_NODE, ELEMENT_NODE, TEXT_NODE } from 'ultrahtml' | ||
import { createElement, Fragment } from 'react'; | ||
|
||
export default function convert(children) { | ||
const nodeMap = new WeakMap(); | ||
let doc = parse(children.toString().trim()); | ||
let root = createElement(Fragment, { children: [] }); | ||
|
||
walkSync(doc, (node, parent, index) => { | ||
let newNode = {}; | ||
if (node.type === DOCUMENT_NODE) { | ||
nodeMap.set(node, root); | ||
} else if (node.type === ELEMENT_NODE) { | ||
const { class: className, ...props } = node.attributes; | ||
newNode = createElement(node.name, { ...props, className, children: [] }); | ||
nodeMap.set(node, newNode); | ||
if (parent) { | ||
const newParent = nodeMap.get(parent); | ||
newParent.props.children[index] = newNode; | ||
|
||
} | ||
} else if (node.type === TEXT_NODE) { | ||
newNode = node.value.trim(); | ||
if (newNode.trim()) { | ||
if (parent) { | ||
const newParent = nodeMap.get(parent); | ||
if (parent.children.length === 1) { | ||
newParent.props.children[0] = newNode; | ||
} else { | ||
newParent.props.children[index] = newNode; | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
|
||
return root.props.children; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.