Skip to content

Instantly share code, notes, and snippets.

@coder0107git
Created October 24, 2024 01:27
Show Gist options
  • Save coder0107git/f98e4316c5e1c1957c5d8fef080df684 to your computer and use it in GitHub Desktop.
Save coder0107git/f98e4316c5e1c1957c5d8fef080df684 to your computer and use it in GitHub Desktop.
Astro: function to Element
---
/**
* @param moduleId Path to component
*/
function function2AstroComponent<Args extends any>(
callback: (...arg: Args[]) => any
): (...arg: Args[]) => any & {
isAstroComponentFactory: true;
propagation: "none";
name: string;
moduleId: string;
} {
const fn = (...args: Args[]) => {
return callback(...args);
};
Object.defineProperty(fn, "name", { value: callback.name, writable: false });
fn.isAstroComponentFactory = true;
fn.moduleId = callback.name + ".astro";
fn.propagation = "none";
return fn;
}
const TR = function2AstroComponent((_astroMetadata, props: any, childRenderer: any, ...extra) => {
const children = childRenderer.default();
// Children pieces directly to html string
const res = String.raw({ raw: children.htmlParts }, ...children.expressions);
// Log everything to better break down what information we can use.
console.log(props, children, res, ...extra);
// Astro can handle the child object directly so we just Astro it.
return children;
});
---
<TR x="y">
<br>
my children
<br>
{true ? "hi" : "hello"}
<br>
</TR>
<!--
!
! Renders:
! ```html
! <br>
! my children
! <br> hi<br>
! ```
!
-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment