Repository navigation
feat: createElement in @jimp/layout, for building trees without JSX - #1577
Merged
Merged
Conversation
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>
- 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>
Collaborator
Author
|
🚀 PR was released in |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds
createElementto@jimp/layout: a React-style way to build layout trees without a JSX compiler.Before, the object API needed a
typefield on every node and a: BoxNodeannotation on any function returning one. Without that annotation, literals likejustify: "space-between"widen tostring, and the error shows up atrender()as a long union message, not on the line that's wrong.API
null,falseand""are skipped.children, like React.nullor left out only when all of them are optional.childrenprop: a component without one takes no children,children: stringtakes exactly one string, andLayoutChildrentakes any number.h(List<number>, …)); TypeScript can't infer it throughcreateElement.keyis accepted, like React's, and dropped.layout: createElement takes "box", "text", "image" or a component, not "div".Box/Text/Image, so the nodes are identical to the JSX ones. Plain objects still work.Docs
createElement(imported ash). The snippets were typechecked as one file.renderpage:createElementis 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.createElement.Testing
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, thatkeyis dropped, and the thrown error.@ts-expect-errorcases cover the type checks above.tscfails on any of them that stops being an error.pnpm exec vitest run(110 passing),pnpm lint,tsc --noEmitandpnpm buildare clean inpackages/layout.🤖 Generated with Claude Code