props.forEach((x) => {
if (x instanceof HTMLElement) {
if (!fragment) fragment = new DocumentFragment();
fragment.appendChild(x);
} else if (x === null) {
return;
} else if (typeof x === "function") {
(x as Visitor<HTMLElementTagNameMap[E]>)(el);
} else if (
typeof (x as Applicative<HTMLElementTagNameMap[E]>).ap === "function"
) {
(x as Applicative<HTMLElementTagNameMap[E]>).ap(el);
} else if (
typeof (x as Setter<HTMLElementTagNameMap[E]>).set === "function"
) {
(x as Setter<HTMLElementTagNameMap[E]>).set(() => el);
} else {
if (!fragment) fragment = new DocumentFragment();
fragment.textContent = `${fragment.textContent}${x}`;
}
});
if (fragment) el.appendChild(fragment);
}
return el;
};
export { HTML };
Hi! You seem to be asking whether you can use this custom HTML function instead of JSX for building your DOM elements.
Short answer:
Yes, you can use an approach like this to create and compose DOM elements without JSX, but there are trade-offs.
What your code does:
The HTML function is a small templating helper to create DOM elements by tag name.
It accepts a variable number of "props," which can be:
HTMLElements to append as children.
Strings or numbers to add as text content.
Visitor functions—functions that receive the created HTMLElement and can modify it.
Applicatives or Setters—custom interfaces with ap or set methods for additional effects.
You create elements by calling, e.g., HTML("div", "Hello", someChildElement, (el) => el.style.color = "red").
Returns the constructed element.
Can it replace JSX?
Yes, but with caveats:
Aspect
JSX + React
Your HTML function
Syntax
Declarative, very concise and familiar syntax
More verbose, function call style
Type safety
Good with TypeScript and React typings
You have strong typings for tags and visitors
Reactivity
Reactivity and reconciliation out-of-the-box
Just creates elements; no reactivity or diffing
Component abstraction
Easy to create reusable components
Must manually create functions returning HTMLElements
You can use this kind of function instead of JSX for DOM creation, but it lacks the ergonomics and ecosystem benefits of JSX-based frameworks like React or Preact. If you're fine with a small custom solution and want minimal abstraction, this is a reasonable approach.
If you want help converting JSX components or building up this pattern further, feel free to ask!
it works well with my custom reactive framework import {
DraggableAndResizable,
FC,
Render,
HTML,
CSS,
Box,
Signal,
IO,
Reader,
Cleanup,
} from "../../lib/index";
import { Button } from "./Controls";
Thanks for sharing your code! It looks like you have a well-thought-out custom reactive framework that uses your HTML function as a DOM builder and combines reactive primitives like Signal, IO, Reader, Box, and a rendering layer with Render and FC.
How your usage fits with your custom HTML function
Your HTML function acts as a minimal JSX replacement that creates elements, applies styles (via your CSS helper), and accepts children or visitor functions for further mutation.
You integrate reactive signals (Signal<boolean>) to represent mutable state, e.g., minimized.
Effects (IO) are used to set up lifecycle or imperative behaviors (event handlers, animations).
The Render component puts it all together, reacting to state changes and re-rendering your DOM subtree.
Advantages and how it fits your reactive approach
Minimal overhead: No virtual DOM or JSX compilation needed.
Explicit handling: You directly manage DOM nodes and side effects, which is great for fine-grained control.
Type safety and clarity: Your HTML function is typed with HTMLElement generics, enhancing editor support and correctness.
Flexibility: Visitors and applicatives let you attach imperative logic cleanly.
CSS as first-class: Your CSS helper returns objects or class names that get applied inline or otherwise, which meshes well with dynamic styling patterns.
A couple of notes / tips
Performance:
As your app grows, watch for full re-renders triggered by signals (Render and your Signal type). Your framework likely limits re-renders intelligently, but keep an eye on potential DOM thrashing.
Children handling in HTML:
I noticed you sometimes pass Box<HTMLElement> as a child, e.g., contentRef. Make sure the Box type (likely a wrapper around an HTMLElement or nullable ref) plays nicely with your HTML function — your current code seems to accept a Visitor, so I guess Box might have an ap or set method? Just keep that consistent.
Animations and styling:
Your minimize animation with the Web Animations API integrates nicely. The imperative nature fits well given your manual DOM model.
State persistence with localStorage:
Nice pattern to use Reader and IO to handle loading/saving window state separately from rendering logic.
Example summary of how your Window component works
Window is a function component (FC) that declares some state (minimized), references (contentRef), and renders a DOM tree via HTML.
Your Render subscribes to signals and re-renders the DOM upon changes.
Side effects like event listeners, animation, and persistence setup happen within IO effects that run during or after rendering.
Your CSS helpers apply styles scoped to elements.
Buttons and drag/resize logic are composed via imported controls and utilities.
Overall
Your approach is valid and elegant for writing reactive UI without JSX or virtual DOM. If it works well for you and fits your mental model, it's a great custom framework tailored specifically to your needs.
If you want:
Suggestions on adding more abstraction layers, hooks, or utilities,
How to integrate this with other web features,
How to further optimize handling of children or effects,
feel free to ask! But as it stands, this is a nice pattern for a minimal reactive framework using your HTML builder.
Happy to help if you want me to review or extend any specific part!