8000 feat: createElement in @jimp/layout, for building trees without JSX by hipstersmoothie · Pull Request #1577 · jimp-dev/jimp · GitHub
[go: up one dir, main page]

Skip to content

feat: createElement in @jimp/layout, for building trees without JSX - #1577

Merged
hipstersmoothie merged 2 commits into
mainfrom
sesh/add-react-helper-functions
Dec 4, 2026
Merged

hipstersmoothie merged 2 commits into
mainfrom
sesh/add-react-helper-functions

Conversation

@hipstersmoothie
@hipstersmoothie hipstersmoothie commented Dec 4, 2026 •
Copy link
Copy Markdown
Collaborator

Adds createElement to @jimp/layout: a React-style way to build layout trees without a JSX compiler.

Before, the object API needed a type field on every node and a : BoxNode annotation on any function returning one. Without that annotation, literals like justify: "space-between" widen to string, and the error shows up at render() as a long union message, not on the line that's wrong.

import { createElement as h } from "@jimp/layout";

const tag = (label: string) =>
  h(
    "box",
    { padding: [10, 22], radius: 999, background: "#ffd83a" },
    h("text", { font: bold, fontSize: 24 }, "#", label),
  );

const tags = (labels: string[]) =>
  h("box", { direction: "row", wrap: true, gap: 12 }, labels.map(tag));

API

createElement("box", props?, ...children): BoxNode
createElement("text", props?, ...children): TextNode
createElement("image", props): ImageNode   // props need `src`
createElement(component, props, ...children): ReturnType<component>
  • Children follow the JSX rules. Text children are joined into one string; arrays inside a box are flattened, and null, false and "" are skipped.
  • Components:
    • They get one child as it is and several as an array, passed in as children, like React.
    • Their props are type-checked, including unknown props. The props can be null or left out only when all of them are optional.
    • Children are checked against the component's own children prop: a component without one takes no children, children: string takes exactly one string, and LayoutChildren takes any number.
    • The return type is the component's.
    • Generic components need their type argument (h(List<number>, …)); TypeScript can't infer it through createElement.
  • key is accepted, like React's, and dropped.
  • It has one generic signature, not an overload per type, so mistakes are reported directly ("Expected 2 arguments, but got 3", "Type '"middle"' is not assignable to …"). With overloads, TypeScript reported them against whichever overload it tried last.
  • An unknown type string throws: layout: createElement takes "box", "text", "image" or a component, not "div".
  • It's a thin wrapper over the existing Box/Text/Image, so the nodes are identical to the JSX ones. Plain objects still work.

Docs

  • Open Graph guide: the Objects tabs now use createElement (imported as h). The snippets were typechecked as one file.
  • render page: createElement is in the signature, and there's a new "Without JSX" section. That page's Objects tabs still show raw objects, since they document what a node looks like.
  • README: the no-JSX example uses createElement.

Testing

  • New create-element.test.ts (plain .ts, no JSX) checks that it builds the same trees as objects, the null/omitted props, components and their children, that key is dropped, and the thrown error.
  • @ts-expect-error cases cover the type checks above. tsc fails on any of them that stops being an error.
  • pnpm exec vitest run (110 passing), pnpm lint, tsc --noEmit and pnpm build are clean in packages/layout.

🤖 Generated with Claude Code

Like React's createElement: the node's type ("box", "text", "image" or
a component), its props, then its children, with the same children rules
as JSX. Props are checked against the node type where they're written, so
functions that return nodes no longer need a BoxNode annotation and
`type` fields.

The Open Graph guide's Objects tabs, the render page and the README use it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@hipstersmoothie hipstersmoothie added the minor Increment the minor version when merged label Dec 4, 2026
- Children are checked against the component's own `children` prop: none
  for a component without one, one for `children: string`, any number for
  an array type like LayoutChildren
- Props can be left out, not just null, when they're all optional
- `key` is accepted, like React's, and dropped
- One generic signature replaces the overloads, so mistakes are reported
  against the argument that's wrong ("Expected 2 arguments, but got 3")
  rather than against another overload

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@hipstersmoothie
hipstersmoothie merged commit d3bc974 into main Dec 4, 2026
7 checks passed
@hipstersmoothie
hipstersmoothie deleted the sesh/add-react-helper-functions branch December 4, 2026 08:03
@hipstersmoothie hipstersmoothie added released This issue/pull request has been released. and removed released This issue/pull request has been released. labels Dec 4, 2026
@hipstersmoothie
Copy link
Copy Markdown
Collaborator Author

🚀 PR was released in v2.3.0 🚀

@hipstersmoothie hipstersmoothie added the released This issue/pull request has been released. label Dec 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

minor Increment the minor version when merged released This issue/pull request has been released.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

0