core: Custom element objects#245
Conversation
|
All changed packages have been documented.
Show changes
|
commit: |
There was a problem hiding this comment.
whats the use case for this?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Is that in a way a more generic version of this #237
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
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>
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.