Skip to content
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

Fix missing element dimensions #561

Merged
merged 1 commit into from Mar 17, 2023
Merged

Conversation

vadimdemedes
Copy link
Owner

Fixes #557.

The root cause of the issue was that both layout calculation and rendering of output was in the same onRender function that's throttled to 32ms by default:

ink/src/ink.tsx

Lines 53 to 58 in 025c10f

this.rootNode.onRender = options.debug
? this.onRender
: throttle(this.onRender, 32, {
leading: true,
trailing: true
});

And here's the function that would be called:

ink/src/renderer.ts

Lines 16 to 23 in 025c10f

node.yogaNode.calculateLayout(undefined, undefined, Yoga.DIRECTION_LTR);
const output = new Output({
width: node.yogaNode.getComputedWidth(),
height: node.yogaNode.getComputedHeight()
});
renderNodeToOutput(node, output, {skipStaticElements: true});

So when component is mounted/unmounted frequently, as in the original issue, and it calls measureElement in a useEffect, there's a condition when useEffect is executed before Yoga layout is calculated. This leads to NaN being returned instead of element dimensions.

This PR fixes that by separating code that calculates layout and renders output to the terminal. Calculation of layout is now always instant, while output rendering continues to be throttled.

@vadimdemedes vadimdemedes merged commit 5f09368 into master Mar 17, 2023
@vadimdemedes vadimdemedes deleted the brilliant-now-memories branch March 17, 2023 21:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Quickly toggling the visibility of a component causes measureElement to return NaN until the next rerender
1 participant