Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
0bab2c2
perf(flex): one allocation-free flex engine with the B5-B10 fixes
chiefcll Oct 3, 2026
5687feb
fix(text): contain subtracts the node's margins; maxLines 1 reads a l…
chiefcll Oct 3, 2026
10ccd23
perf(layout): depth-ordered layout queue; post-mutation pass as a mic…
chiefcll Oct 3, 2026
c5ee883
fix(flex): keep the pre-1.7 wrap alignment apart from B7, B9 and B10
chiefcll Oct 3, 2026
09cc0ab
fix(flex): transition-aware writes, a reentrancy guard, one write loop
chiefcll Oct 4, 2026
f8cd3b4
fix(layout): onLayout after a flexGrow container's children; a robust…
chiefcll Oct 4, 2026
186e59e
perf(flex): keep the transition-aware writers inline
chiefcll Oct 4, 2026
eb568ec
fix(flex): a steady relayout of a grown auto-sizing child writes nothing
chiefcll Oct 4, 2026
2f46fd9
perf(flex): write positions in the per-justify loops again
chiefcll Oct 4, 2026
aecd3cc
fix(nodes): never write unknown names to a renderer v2 handle (B20)
chiefcll Oct 3, 2026
4499e2a
perf(nodes): one accessor per forwarded prop, with a constant name (d…
chiefcll Oct 3, 2026
03e7ac1
perf(nodes): inline the w/h transition alias in _sendToLightningAnima…
chiefcll Oct 3, 2026
c4775a9
test(nodes): each animatable setter animates under its own name
chiefcll Oct 4, 2026
422fa90
bench: an accessor micro-benchmark on real renderer v2 handles
chiefcll Oct 4, 2026
b3c1a18
feat(dom-renderer): insertBefore, as renderer v2's Node.insertBefore
chiefcll Oct 4, 2026
62ad0d4
fix(nodes): draw order follows children (B19); a child list without s…
chiefcll Oct 4, 2026
2b9e85e
docs(nodes): the zIndex caveat of draw order in insertChild
chiefcll Oct 4, 2026
1013ecd
fix(nodes): a child inserted before itself stays in place (B19)
chiefcll Oct 4, 2026
9cdf64e
fix(nodes): render() reparents moved-in children in children order (B19)
chiefcll Oct 4, 2026
86f33fa
fix(dom-renderer): insertBefore leaves destroyed nodes alone, no arra…
chiefcll Oct 4, 2026
402e4af
fix(primitives): a removed Row/Column scrolls without its parent
chiefcll Oct 4, 2026
fd6c3bb
fix(nodes): a removed node keeps its parent link, as in 1.6
chiefcll Oct 4, 2026
eae9568
fix(nodes): a removed node does not lay out its old parent
chiefcll Oct 4, 2026
efccb45
feat(text): Solid measures flex text with TextNode.measure() (T1)
chiefcll Oct 4, 2026
f83dd8a
fix(dom-renderer): `loaded` tells a size measure() already took (T1 r…
chiefcll Oct 4, 2026
6d0a7b2
fix(text): T1 review minors (queue safety, sweep cap, contain, DOM fo…
chiefcll Oct 4, 2026
d6c19e8
fix(text): a text layout prop's transition or animate() relays out it…
chiefcll Oct 4, 2026
d4931a8
fix(text): never sweep the font-waiting list while it is being measur…
chiefcll Oct 4, 2026
28f1b34
fix(text): a text Solid animated hears its layouts for its lifetime (…
chiefcll Oct 4, 2026
173f8c0
test(text): font-waiting cases in a module instance of their own (T1 …
chiefcll Oct 4, 2026
4449ed1
docs(layout): one flex engine; VITE_USE_NEW_FLEX is ignored
chiefcll Nov 19, 2026
ebb6baf
test(childList): a child inserted before itself stays in place (B19)
chiefcll Nov 19, 2026
4d437e1
test(webgl): flex-text imports renderer for the walk counts
chiefcll Nov 19, 2026
3370fe6
test(virtual): hand the DOM animation loop to the rAF stub before the…
chiefcll Nov 20, 2026
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
2 changes: 1 addition & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Run a single test file: `npx vitest tests/flex.spec.ts`
- `src/core/elementNode.ts` — `ElementNode` is the core node abstraction. Manages properties, children, flex layout, focus, states, animations, and syncs to the underlying `@solidtv/renderer` INode.
- `src/core/focusManager.ts` — Handles keyboard/remote input, focus path traversal, and key mapping. Focus walks the component tree via `onUp`/`onDown`/`onLeft`/`onRight`/`onEnter` handlers.
- `src/core/config.ts` — Runtime `Config` object (debug flags, animation settings, font defaults, focus state key).
- `src/core/flex.ts` / `src/core/flexLayout.ts` — Two flex layout implementations; toggled by `VITE_USE_NEW_FLEX` env var.
- `src/core/flexLayout.ts` — The flex layout engine (one pass over one container). `elementNode.ts` runs it from a depth-ordered layout queue in the post-mutation pass.

### Build-Time Globals

Expand Down
80 changes: 80 additions & 0 deletions bench/micro/accessors.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* The cost of ElementNode's forwarded-prop accessors (design 3.6.6), on real
* renderer v2 handles: 100 rendered <view>s, read and written in a tight
* loop. Unthrottled; prints the best of 15 repetitions (no timing assertion).
*
* npx vitest run --config=bench/micro/vitest.config.ts --reporter=verbose
*
* To compare two versions of src/core/elementNode.ts, run it at each (for
* the loop-made accessors before 3.6.6: `git show d342c37:src/core/elementNode.ts`).
* The `[accessor-micro]` line is the result. On an M4 Pro, Chromium 141
* headless, 2026-10-03 (six runs each): loop-made accessors (d342c37)
* 15.9-16.3 ms read / 14.7-15.5 ms write; written out (3.6.6)
* 1.00-1.20 ms read / 1.40-1.60 ms write.
*/
import * as v from 'vitest';
import * as s from 'solid-js';
import type { ElementNode } from '@solidtv/solid';
import { render } from '../../tests/webgl/setup.js';

const NODES = 100;
const LOOPS = 2000;

v.test('accessor micro', () => {
let row!: ElementNode;
const dispose = render(() => (
<view ref={row} width={1920} height={300}>
<s.For each={Array.from({ length: NODES }, (_, i) => i)}>
{(i) => (
<view x={i * 20} y={i} width={200} height={100} alpha={1} scale={1} />
)}
</s.For>
</view>
));
const kids = row.children as ElementNode[];
// 8 reads per node: x, width, y, height, alpha, scale, w, h.
const read = () => {
let sum = 0;
for (let i = 0; i < kids.length; i++) {
const c = kids[i]!;
sum +=
c.x + c.width / 2 + c.y + c.height / 2 + c.alpha + c.scale + c.w + c.h;
}
return sum;
};
// 2 writes per node: x and alpha.
const write = (k: number) => {
for (let i = 0; i < kids.length; i++) {
const c = kids[i]!;
c.x = i * 20 + k;
c.alpha = k & 1 ? 0.5 : 1;
}
};
// Warm up until both are optimized.
for (let w = 0; w < 3000; w++) {
read();
write(w);
}
const bestOf = (fn: () => void) => {
let best = Infinity;
for (let r = 0; r < 15; r++) {
const t0 = performance.now();
fn();
best = Math.min(best, performance.now() - t0);
}
return best;
};
let sink = 0;
const readMs = bestOf(() => {
for (let n = 0; n < LOOPS; n++) sink += read();
});
const writeMs = bestOf(() => {
for (let n = 0; n < LOOPS; n++) write(n);
});
console.info(
`[accessor-micro] read ${readMs.toFixed(2)} ms (${LOOPS * NODES * 8} reads), ` +
`write ${writeMs.toFixed(2)} ms (${LOOPS * NODES * 2} writes)`,
);
v.expect(sink).toBeGreaterThan(0);
dispose();
});
17 changes: 17 additions & 0 deletions bench/micro/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Micro-benchmarks on the real renderer: the browser setup of
// vitest.webgl.config.ts (headless Chromium, renderer v2, WebGL), with
// bench/micro as the only test directory. `npx vitest run` excludes bench/
// and `pnpm test:webgl` only includes tests/webgl, so neither runs these.
//
// npx vitest run --config=bench/micro/vitest.config.ts
import { fileURLToPath } from 'node:url';
import webgl from '../../vitest.webgl.config';

export default {
...webgl,
root: fileURLToPath(new URL('../..', import.meta.url)),
test: {
...webgl.test,
include: ['bench/micro/**/*.test.tsx'],
},
};
38 changes: 16 additions & 22 deletions docs/flow/layout.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,13 @@ The `<text>` element does not require any of these properties, as it will use th

## Flex

A fundamental tool for layout is the Flex container. Currently, there is a minimal implementation of flex (`display: flex`) that supports the following properties: `flexDirection`, `justifyContent`, `alignItems`, `flexOrder`, `flexGrow` and `gap`. This is useful for laying out elements in rows and columns.
A fundamental tool for layout is the Flex container (`display: flex`). SolidTV has one flex engine (`src/core/flexLayout.ts`), modelled on CSS flexbox. It supports `flexDirection`, `justifyContent`, `alignItems`, `alignSelf`, `flexOrder`, `flexGrow`, `flexShrink`, `flexBasis`, `flexWrap`, `gap`, `padding` (a number, an array, or per side) and `margin` (a number, an array, or per side). This is useful for laying out elements in rows and columns.

### New Flex Engine Toggle
The engine allocates nothing during a layout pass and writes only the values that changed, so a relayout that moves nothing writes nothing and starts no transition.

The framework now includes a modern, high-performance, CSS-aligned flex engine alongside the legacy engine. To opt into the new flex engine, define the following environment variable during your Vite build process:
### One flex engine

```bash
VITE_USE_NEW_FLEX=true
```

This will add `flexShrink`, `flexBasis` support and padding / margin full support with array syntax.

Because the engines are conditionally evaluated at build time via `import.meta.env`, your bundler (e.g., Rollup) will automatically perform **Dead-Code Elimination (tree-shaking)**. This means whichever layout engine isn't selected will be completely omitted from your application's final production bundle.
Before 1.8 there were two engines, a legacy one and a CSS-aligned one chosen at build time with the `VITE_USE_NEW_FLEX` environment variable. Since 1.8 there is only the CSS-aligned one, with everything below. The variable is ignored; delete it from your `.env`. If your app ran on the legacy engine, see the 1.8 release notes for the layouts that change.

### Example

Expand Down Expand Up @@ -63,8 +57,8 @@ When a `<view>` with `display: flex` contains text nodes as children, it automat
- **`flexItem`**: boolean (Set to `false` on a child to exclude it from flex calculations.)
- **`flexOrder`**: number (Set the order on children to change the layout order.)
- **`flexGrow`**: number (Set to number on children to specify how much room elements should take up.)
- **`flexShrink`**: number (Set to number on children to specify how much an element should shrink proportionally if the container overflows. Defaults to 0.) Only in new flex engine.
- **`flexBasis`**: number | 'auto' (Set the default size of an item before the remaining space is distributed. Overrides width/height.) Only in new flex engine.
- **`flexShrink`**: number (Set to number on children to specify how much an element should shrink proportionally if the container overflows. Defaults to 0.) With two or more items, a `flexShrink` or `flexGrow` on any of them makes a container without a `flexBoundary` behave as `fixed`: it keeps its own size instead of sizing itself to its items.
- **`flexBasis`**: number | 'auto' (Set the default size of an item before the remaining space is distributed. Overrides width/height for positioning.)
- **`flexWrap`**: 'nowrap' | 'wrap' | 'wrap-reverse' (Set to `wrap` or `wrap-reverse` to have elements flow to the next line on overflow.)
- **`gap`**: number
- **`rowGap`**: number
Expand Down Expand Up @@ -99,23 +93,23 @@ We can also have multiple elements with flexGrow property. Flex will divide up t
Produces:
![Flex Grow](../images/flexGrow-multiple.png)

### Item-Specific Properties in flex containers
### Padding and margin

To control the layout further, you can use the following properties on individual items:
`padding` is read from the flex container: it is the space between the container's edges and its items. `margin` is read from each item. Both take a number for all four sides, or a CSS-style array:

- **`padding`**: number (Specifies padding on the element itself on main axis. Only in legacy flex engine.)

- **`padding`**: number | [number, number, number, number] (Specifies padding on the element itself. Using an array aligns with the CSS `[Top, Right, Bottom, Left]` specification. Available in the new flex engine.)
- **`paddingTop`**: number (Overrides the top value of the padding array. Available in the new flex engine.)
- **`paddingRight`**: number (Overrides the right value of the padding array. Available in the new flex engine.)
- **`paddingBottom`**: number (Overrides the bottom value of the padding array. Available in the new flex engine.)
- **`paddingLeft`**: number (Overrides the left value of the padding array. Available in the new flex engine.)
- **`margin`**: [number, number, number, number] (Specifies margins on the element using the `[Top, Right, Bottom, Left]` CSS array syntax. Available in the new flex engine.)
- **`padding`**: number | [number, number] | [number, number, number] | [number, number, number, number] (Specifies the padding on the container. A four value array is `[Top, Right, Bottom, Left]`, as in CSS. Two values are `[vertical, horizontal]`, three are `[Top, horizontal, Bottom]`.) A padding applies to both axes: items start at the padding on the main axis and, with `alignItems`, `alignSelf` or wrapping, on the cross axis.
- **`paddingTop`**: number (Overrides the top value of `padding`.)
- **`paddingRight`**: number (Overrides the right value of `padding`.)
- **`paddingBottom`**: number (Overrides the bottom value of `padding`.)
- **`paddingLeft`**: number (Overrides the left value of `padding`.)
- **`margin`**: number | [number, number] | [number, number, number] | [number, number, number, number] (Specifies the margins on an item, with the same array forms as `padding`.)
- **`marginBottom`**: number
- **`marginLeft`**: number
- **`marginRight`**: number
- **`marginTop`**: number

A non-zero `marginTop`, `marginRight`, `marginBottom` or `marginLeft` takes precedence over the same side of `margin`.

Note: `alignItems` supports `flexStart`, `flexEnd`, and `center`, but requires the container to have a height/width set.

## Layout Callbacks
Expand Down
120 changes: 119 additions & 1 deletion src/core/dom-renderer/domRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1145,19 +1145,60 @@ function updateDOMTextSize(node: DOMText, emitLoaded = true): void {
break;
}

if (emitLoaded && (!node.loaded || dimensionsChanged)) {
// Against the size last emitted, not the size before this measure: a
// `measure()` (Solid, before its layout) takes the new size without
// emitting, and `loaded` listeners must still hear it.
if (
emitLoaded &&
(!node.loaded ||
dimensionsChanged ||
node.w !== node.loadedW ||
node.h !== node.loadedH)
) {
const payload: lng.NodeTextLoadedPayload = {
type: 'text',
dimensions: {
w: node.w,
h: node.h,
},
};
node.loadedW = node.w;
node.loadedH = node.h;
node.emit('loaded', payload);
node.loaded = true;
}
}

/**
* Whether the text's font is loaded, as `document.fonts.check` says (a
* family no font face names counts as loaded: a system font). Without a
* FontFaceSet, or for a font string it cannot parse, it is taken as loaded.
*/
function isFontLoaded(node: DOMText): boolean {
if (typeof document === 'undefined') {
return true;
}
const fonts = document.fonts as FontFaceSet | undefined;
if (
fonts === undefined ||
fonts === null ||
typeof fonts.check !== 'function'
) {
return true;
}
let font = node.font;
if (font === undefined) {
const props = node.props;
font = `${props.fontStyle} ${props.fontWeight} ${props.fontSize}px "${props.fontFamily}"`;
node.font = font;
}
try {
return fonts.check(font);
} catch {
return true;
}
}

function updateDOMTextMeasurements() {
textNodesToMeasure.forEach((node) => updateDOMTextSize(node));
textNodesToMeasure.clear();
Expand Down Expand Up @@ -1430,6 +1471,59 @@ export class DOMNode extends EventEmitter implements IRendererNode {
updateNodeParent(this);
}

/**
* Renderer v2's `Node.insertBefore`: makes `child` a child of this node,
* before `before` (`null`, or a node that is not a child, appends); a child
* of this node moves, and one placed before itself stays. A destroyed
* parent or child does nothing; a destroyed `before` appends. The div order
* is the paint order among equal z-index, as v2's sibling order is its
* draw order.
*/
insertBefore(child: IRendererNode, before: IRendererNode | null): void {
if (
!(child instanceof DOMNode) ||
child === before ||
!elMap.has(this) ||
!elMap.has(child)
) {
return;
}
const anchor =
before instanceof DOMNode &&
before.props.parent === this &&
elMap.has(before)
? before
: null;

// A new parent: adds it to this node's children and appends its div.
child.parent = this;

const children = this.children;
children.delete(child);
if (anchor === null) {
children.add(child);
this.div.appendChild(child.div);
} else {
// A Set keeps insertion order: add the child at the end, then move the
// anchor and everything after it behind the child (no array: stop at
// the last one before the child, as re-added entries come after it).
let last: DOMNode | undefined;
for (const c of children) last = c;
children.add(child);
let moving = false;
for (const c of children) {
if (c === anchor) moving = true;
if (moving) {
children.delete(c);
children.add(c);
}
if (c === last) break;
}
this.div.insertBefore(child.div, anchor.div);
}
this.markChildrenBoundsDirty();
}

public markChildrenBoundsDirty() {
for (const child of this.children) {
child.boundsDirty = true;
Expand Down Expand Up @@ -1883,6 +1977,11 @@ export class DOMNode extends EventEmitter implements IRendererNode {

class DOMText extends DOMNode {
public loaded = false;
/** The size `loaded` last told (updateDOMTextSize). */
public loadedW = 0;
public loadedH = 0;
/** Its CSS font, for document.fonts.check; undefined once a font prop changes. */
public font: string | undefined = undefined;

constructor(
stage: IRendererStage,
Expand All @@ -1902,6 +2001,21 @@ class DOMText extends DOMNode {
super.destroy();
}

/**
* Renderer v2's `TextNode.measure()`: sizes the text now, synchronously
* (`getBoundingClientRect`), when its font is loaded, and returns true.
* While it is not (document.fonts.check), returns false and measures
* nothing: the async measurement emits `loaded` once it is. False for a
* destroyed node too.
*/
measure(): boolean {
if (!elMap.has(this) || !isFontLoaded(this)) {
return false;
}
updateDOMTextSize(this, false);
return true;
}

get text() {
return this.props.text;
}
Expand All @@ -1917,6 +2031,7 @@ class DOMText extends DOMNode {
set fontFamily(v) {
if (this.props.fontFamily === v) return;
this.props.fontFamily = v;
this.font = undefined;
updateNodeStyles(this);
scheduleUpdateDOMTextMeasurement(this);
}
Expand All @@ -1926,6 +2041,7 @@ class DOMText extends DOMNode {
set fontSize(v) {
if (this.props.fontSize === v) return;
this.props.fontSize = v;
this.font = undefined;
updateNodeStyles(this);
scheduleUpdateDOMTextMeasurement(this);
}
Expand All @@ -1935,6 +2051,7 @@ class DOMText extends DOMNode {
set fontStyle(v) {
if (this.props.fontStyle === v) return;
this.props.fontStyle = v;
this.font = undefined;
updateNodeStyles(this);
scheduleUpdateDOMTextMeasurement(this);
}
Expand All @@ -1944,6 +2061,7 @@ class DOMText extends DOMNode {
set fontWeight(v) {
if (this.props.fontWeight === v) return;
this.props.fontWeight = v;
this.font = undefined;
updateNodeStyles(this);
scheduleUpdateDOMTextMeasurement(this);
}
Expand Down
10 changes: 10 additions & 0 deletions src/core/dom-renderer/domRendererTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ export interface IRendererNodeShaded extends EventEmitter {
) => lng.IAnimationController;
get absX(): number;
get absY(): number;
/** Renderer v2's `Node.destroyed`: true once destroyed. */
get destroyed(): boolean;
/** Renderer v2's `Node.insertBefore` (draw order follows Solid's children). */
insertBefore(child: IRendererNode, before: IRendererNode | null): void;
}

/** Based on {@link lng.INodeProps} */
Expand Down Expand Up @@ -115,6 +119,12 @@ export interface IRendererTextNode
div?: HTMLElement;
props: IRendererTextNodeProps;
renderState: lng.CoreNodeRenderState;
/**
* Renderer v2's `TextNode.measure()`: lays the text out now, outside the
* walk, so `w` and `h` read its size at once; false, changing nothing,
* while its font is not loaded (and for a destroyed node).
*/
measure(): boolean;
}

/** Based on {@link lng.RendererMain} */
Expand Down
Loading
Loading