Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@alloy-js/core"
---

Adds a new type of Alloy child, CustomChildElement, that is based on the presence of a symbol property.
7 changes: 7 additions & 0 deletions packages/core/src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ import {
Children,
Component,
isComponentCreator,
isRenderableObject,
Props,
RENDERABLE,
} from "./runtime/component.js";
import { IntrinsicElement, isIntrinsicElement } from "./runtime/intrinsic.js";
import { flushJobs, flushJobsAsync } from "./scheduler.js";
Expand Down Expand Up @@ -548,6 +550,9 @@ function normalizeChild(child: Child): NormalizedChildren {

return sfContext.reference({ refkey: child });
};
} else if (isRenderableObject(child)) {
// For custom renderable objects, we will just normalize them to a bound function.
return child[RENDERABLE].bind(child);
} else if (isCustomContext(child)) {
return child;
} else if (isIntrinsicElement(child)) {
Expand All @@ -573,6 +578,8 @@ function debugPrintChild(child: Children): string {
return "$ref";
} else if (isIntrinsicElement(child)) {
return `<${child.name}>`;
} else if (isRenderableObject(child)) {
return `CustomChildElement(${JSON.stringify(child)})`;
} else {
return JSON.stringify(child);
}
Expand Down
31 changes: 31 additions & 0 deletions packages/core/src/runtime/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,38 @@ import { CustomContext } from "../reactivity.js";
import { Refkey } from "../refkey.js";
import { IntrinsicElement } from "./intrinsic.js";

export const RENDERABLE = Symbol.for("Alloy.CustomElement");

/**
* A renderable object is any object that has an `[ay.RENDERABLE]` method that
* returns children. This is used to allow custom object types to be used as
* children in Alloy components.
*/
export interface RenderableObject {
/**
* Renders this object to children.
*/
[RENDERABLE](): Children;
}

/**
* Returns true if the item is a renderable object, meaning it has an `[ay.RENDERABLE]`
* method.
*
* @param item - The item to check.
* @returns True if the item is a renderable object.
*/
export function isRenderableObject(item: unknown): item is RenderableObject {
return (
typeof item === "object" &&
item !== null &&
RENDERABLE in item &&
typeof (item as any)[RENDERABLE] === "function"
);
}

export type Child =
| RenderableObject
| string
| boolean
| number
Expand Down
36 changes: 35 additions & 1 deletion packages/core/test/rendering/basic.test.tsx
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

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
import { Children } from "../../src/runtime/component.js";
import { Children, RENDERABLE } from "../../src/runtime/component.js";
import "../../testing/extend-expect.js";
describe("string nodes", () => {
it("renders string nodes with substitutions", () => {
Expand Down Expand Up @@ -49,6 +49,40 @@ describe("component nodes", () => {
</>,
).toRenderTo("Str Str");
});

it("renders custom elements", () => {
const customElement = {
[RENDERABLE]() {
return (
<>
<Str /> <Str />
</>
);
},
};

expect(customElement).toRenderTo("Str Str");
});

it("renders nested custom elements", () => {
const e1 = {
[RENDERABLE]() {
return <Str />;
},
};

const e2 = {
[RENDERABLE]() {
return (
<>
{e1} {e1}
</>
);
},
};

expect(e2).toRenderTo("Str Str");
});
});

describe("memo nodes", () => {
Expand Down
Loading