I did something similar-ish a while back, but in C.
https://bsky.app/profile/mikkomononen.bsky.social/post/3ldrldirbz52r
(there are few more post around that time)
I wanted to find a way to do 1 pass imgui with flex box like layouting. I ended up defining small parts of the UI in stack, layout, and then call the logic.
I really liked the separation of layout and logic (even if it was hard to find the name for the logic function).
The 1 pass does limit nested reuse, but that was just my obsession :)