Skip to content

fix: fix html fragment support #1131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
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
6 changes: 5 additions & 1 deletion html/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
"tasks": {
"test": "deno test --allow-env"
},
"imports": {
"imports": {},
"compilerOptions": {
"jsx": "react",
"jsxFactory": "h",
"jsxFragmentFactory": "h.fragment"
}
}
2 changes: 1 addition & 1 deletion html/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { render, setEventSanitizer, setNodeSanitizer } from "./render.ts";
export { debug, setDebug } from "./logger.ts";
export { Fragment, h, type VNode } from "./jsx.ts";
export { h, type VNode } from "./jsx.ts";
24 changes: 14 additions & 10 deletions html/src/jsx.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import type { Cell, Stream } from "@commontools/runner";
// declare global {
// namespace JSX {
// interface IntrinsicElements {
// [elemName: string]: any;
// }
// }
// }
declare global {
namespace JSX {
interface IntrinsicElements {
[elemName: string]: any;
}
}
}

export const Fragment = "Fragment";
const FRAGMENT_ELEMENT = "common-fragment";

export function h(
export const h = Object.assign(function h(
name: string | ((...args: any[]) => any),
props: { [key: string]: any } | null,
...children: Child[]
Expand All @@ -27,7 +27,11 @@ export function h(
children: children.flat(),
};
}
}
}, {
fragment({ children }: { children: Child[] }) {
return h(FRAGMENT_ELEMENT, null, children);
},
});

/**
* Dynamic properties. Can either be string type (static) or a Mustache
Expand Down
64 changes: 64 additions & 0 deletions html/test/jsx.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { beforeEach, describe, it } from "@std/testing/bdd";
import * as assert from "./assert.ts";
import { h } from "../src/jsx.ts";

describe("jsx dom fragments supprot", () => {
it("dom fragments should work", () => {
const fragment = (
<>
<p>Hello world</p>
</>
);

assert.matchObject(
fragment,
<common-fragment>
<p>Hello world</p>
</common-fragment>,
);
});

it("dom fragments with multiple children", () => {
const fragment = (
<>
<p>Grocery List</p>
<ul>
<li>Buy Milk</li>
</ul>
</>
);

assert.matchObject(
fragment,
<common-fragment>
<p>Grocery List</p>
<ul>
<li>Buy Milk</li>
</ul>
</common-fragment>,
);
});

it("fragments inside the element", () => {
const grocery = (
<>
<p>Grocery List</p>
<ul>
<li>Buy Milk</li>
</ul>
</>
);

assert.matchObject(
<div>{grocery}</div>,
<div>
<common-fragment>
<p>Grocery List</p>
<ul>
<li>Buy Milk</li>
</ul>
</common-fragment>
</div>,
);
});
});
1 change: 0 additions & 1 deletion jumble/src/components/DitherCube.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,6 @@ export const DitheredCube = ({
<MorphingMesh animate={animate} animationSpeed={animationSpeed} />
<OrbitControls enableZoom={false} />
<Effects>
{/* @ts-expect-error DitheringPass is properly extended but TS doesn't recognize it */}
<ditheringPass />
</Effects>
</Canvas>
Expand Down
50 changes: 50 additions & 0 deletions recipes/counter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { h } from "@commontools/html";
import {
cell,
derive,
handler,
ifElse,
JSONSchema,
NAME,
recipe,
schema,
str,
UI,
} from "@commontools/builder";

// Different way to define the same schema, using 'schema' helper function,
// let's as leave off `as const satisfies JSONSchema`.
const model = schema({
type: "object",
properties: {
value: { type: "number", default: 0, asCell: true },
},
default: { value: 0 },
});

const increment = handler({}, model, (_, state) => {
state.value.set(state.value.get() + 1);
});

const decrement = handler({}, model, (_, state) => {
state.value.set(state.value.get() - 1);
});

const isOdd = (n: number) => n % 2 > 0;

export default recipe(model, model, (cell) => {
return {
[NAME]: str`Simple counter: ${derive(cell.value, String)}`,
[UI]: (
<div>
<button type="button" onClick={increment(cell)}>+</button>
{/* use html fragment to test that it works */}
<>

Check failure on line 42 in recipes/counter.tsx

View workflow job for this annotation

GitHub Actions / Workspace Checks and Tests

Unnecessary Fragment detected
<b>{cell.value}</b>
</>
<button type="button" onClick={decrement(cell)}>-</button>
</div>
),
value: cell.value,
};
});
2 changes: 1 addition & 1 deletion runner/src/runtime/local-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export const tsToExports = async (
strict: true,
jsx: ts.JsxEmit.React,
jsxFactory: "h",
jsxFragmentFactory: "Fragment",
jsxFragmentFactory: "h.fragment",
esModuleInterop: true,
sourceMap: true, // Enable source map generation
inlineSources: false, // Don't include original source in source maps
Expand Down
10 changes: 10 additions & 0 deletions ui/src/components/common-fragment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export class CommonFragmentElement extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: "open" })
// Add a slot to display the children
.appendChild(document.createElement("slot"));
}
}

customElements.define("common-fragment", CommonFragmentElement);
1 change: 1 addition & 0 deletions ui/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * as CommonCharm from "./common-charm.ts";
export * as CommonDatatable from "./common-datatable.ts";
export * as CommonDict from "./common-dict.ts";
export * as CommonForm from "./common-form.ts";
export * as CommonFragment from "./common-fragment.ts";
export * as CommonGoogleOauth from "./common-google-oauth.ts";
export * as CommonGrid from "./common-grid.ts";
export * as CommonHeroLayout from "./common-hero-layout.ts";
Expand Down
Loading