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
Children in components #72
Conversation
2dc233f
to
67b5b54
Compare
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.
Wow! This works great for me.
|
||
/** @internal */ | ||
function childrenToTree(children: string[]) { | ||
return children.map(child => (unified().use(parse, { fragment: true }).parse(child) as any).children.pop()); |
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.
It would be great if we could avoid parsing HTML on ever request. Here's another idea:
What if we could, in the compiler, instead of compiling to the current h() function that returns a string, we use a different h() function when inside children of a framework component. This h() function returns a VNode. Then we just have to convert from out VNode format to the framework h() calls.
It's possible that parsing is actually faster so I'd love to test two implementations.
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.
Finally circling back to this. I started implementing this but realized that your suggestion is effectively just moving where this work happens. The HTML => VNode conversion still needs to happen since it allows components from different frameworks to be nested within each other. IMO moving this logic into the compiler and creating framework-specific h
functions would make this flow much harder to follow.
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.
My suggestion: leave the /frontend/render/utils
where it is, but memoize childrenToVnodes
and childrenToH
so we're not parsing anything more than once.
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.
I think that's an ok compromise for now
4706d37
to
3fb5c10
Compare
Changes
This PR adds support for children inside of framework components using the framework's built-in paradigms. For React and Preact, children can be accessed via the
{ children }
prop. For Vue and Svelte, children can be accessed via a default<slot>
.children-components.mp4
This is accomplished in two different ways:
children
is always astring[]
of HTML. Usingrehype
, we generate anast
of this HTML and convert it to a tree ofvnode
s in that framework's flavor. This allows seamless interop between frameworks—any static component can be used inside of any other component because children are always HTML.string[]
) into framework-specifich/createElement
calls and pass them to the user component. For Svelte, we can skip this step and just pass the original HTML string.Testing
Take a look at
examples/kitchen-sink
Docs