Skip to content

Comments

core: Custom element objects#245

Merged
bterlson merged 7 commits intoalloy-framework:mainfrom
witemple-msft:witemple-msft/custom-element
Sep 5, 2025
Merged

core: Custom element objects#245
bterlson merged 7 commits intoalloy-framework:mainfrom
witemple-msft:witemple-msft/custom-element

Conversation

@witemple-msft
Copy link
Contributor

This PR adds a new type of child, a CustomChildElement, that is defined by the presence of a well-known symbol property.

During rendering, this type of child is normalized into a function by binding the symbol prop to the child element itself.

@github-actions
Copy link
Contributor

github-actions bot commented Jul 22, 2025

All changed packages have been documented.

  • @alloy-js/core
Show changes

@alloy-js/core - feature ✏️

Adds a new type of Alloy child, CustomChildElement, that is based on the presence of a symbol property.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Jul 22, 2025

Open in StackBlitz

npm i https://pkg.pr.new/alloy-framework/alloy/@alloy-js/core@245

commit: f0424f5

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whats the use case for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use case is to be able to create objects that act as children in the tree. These can be class instances that carry additional metadata, for example, but have some essence of component-ness within them.

I have a thing I've been using called an "ExpressionBuilder" that allows me to compose syntax using a fluent-style API that is highly type-driven. Think like, StringShape.literal("asdf").encodeBuffer("utf-8") composes an expression that takes the literal string "asdf" and encodes it into a Buffer object, but it doesn't actually do that computation, it builds syntax that will do that in the output tree. But when I want to insert the built expression into the alloy tree, I have to destructure it first in a kind of complicated way, because it has a "preamble" part and then an expression part. What I want to do is be able to make the ExpressionBuilder itself usable as if it were an Alloy component, so for example if I want to call an expression I've built up, I can just say <ts.FunctionCall target={expressionBuilderInstance} /> without having to wrap it in a special "destructure ExpressionBuilder" component. This thing is really designed such that it should be usable as an expression and having a little protocol like this in Alloy to be able to just implement the behavior of rendering an ExpressionBuilder directly is a little bit more convenient than having to wrap everywhere I want to insert one of these things into the tree.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that in a way a more generic version of this #237

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the JSON object child in that PR could be implemented in terms of this, yes. You would just implement this custom child interface to render the JSON object instead of having a special symbol that tells the renderer to use a specific implementation.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do like this idea in general because I don't really like the way we have of treating json as text to render it. But I don't fully get form the one test how this is to be used.

If that's not too much work could you add more test/show how the json test I wrote in that other pr would work

@bterlson bterlson merged commit 375a1e6 into alloy-framework:main Sep 5, 2025
6 checks passed
dimmyjing pushed a commit to dimmyjing/alloy that referenced this pull request Sep 19, 2025
This PR adds a new type of child, a CustomChildElement, that is defined
by the presence of a well-known symbol property.

During rendering, this type of child is normalized into a function by
binding the symbol prop to the child element itself.

---------

Co-authored-by: Will Temple <will@wtemple.net>
Co-authored-by: Brian Terlson <brian.terlson@microsoft.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants