[GH-ISSUE #87] [Proposal] Anchor constrained positioning #19

Open
opened 2026-03-02 23:43:54 +03:00 by kerem · 3 comments
Owner

Originally created by @Adictya on GitHub (Aug 26, 2025).
Original GitHub issue: https://github.com/anomalyco/opentui/issues/87

Originally assigned to: @Adictya on GitHub.

Proposal

Add support for minute subset of css anchor-positioning like api to enable positioning elements relative to non parent renderables.

Similar behavior can already be achieved by directly referencing the renderables and doing some calculations in user space, this would just be a neater way of doing the same.

The api could look something like this for phase 1 atleast:

export interface LayoutOptions {
...
'positionAnchor': Renderable
'top/left/bottom/right': ...existing | 'anchor-top' | 'anchor-bottom' | 'anchor-left' | 'anchor-right'
'justifySelf': ...existing | 'anchor-center' 
}

Implementation thoughts

  1. Anchored elements would be excluded from the layout
  2. Position anchored renderables after layout calculations with the updated anchor element position
  3. Children of anchored renderables would be relatively positioned.

All in all shouldn't be too hard to implement?

Originally created by @Adictya on GitHub (Aug 26, 2025). Original GitHub issue: https://github.com/anomalyco/opentui/issues/87 Originally assigned to: @Adictya on GitHub. ## Proposal Add support for minute subset of [css anchor-positioning](https://developer.chrome.com/blog/anchor-positioning-api) like api to enable positioning elements relative to non parent renderables. Similar behavior can already be achieved by directly referencing the renderables and doing some calculations in user space, this would just be a neater way of doing the same. The api could look something like this for phase 1 atleast: ```ts export interface LayoutOptions { ... 'positionAnchor': Renderable 'top/left/bottom/right': ...existing | 'anchor-top' | 'anchor-bottom' | 'anchor-left' | 'anchor-right' 'justifySelf': ...existing | 'anchor-center' } ``` ## Implementation thoughts 1. Anchored elements would be excluded from the layout 2. Position anchored renderables after layout calculations with the updated anchor element position 3. Children of anchored renderables would be relatively positioned. All in all shouldn't be too hard to implement?
Author
Owner

@kommander commented on GitHub (Aug 26, 2025):

Good proposal!

You cannot exclude anchored elements from the layout, as then their own layout including the children would not be calculated anymore, unless you call calculate layout from the anchored element for the subtree.

I think it would be simpler to force position the anchored element absolute and in the x/y getters use the anchor.x/y as relation/anchor instead of parent.x/y?

<!-- gh-comment-id:3225384019 --> @kommander commented on GitHub (Aug 26, 2025): Good proposal! You cannot exclude anchored elements from the layout, as then their own layout including the children would not be calculated anymore, unless you call calculate layout from the anchored element for the subtree. I think it would be simpler to force position the anchored element absolute and in the x/y getters use the anchor.x/y as relation/anchor instead of parent.x/y?
Author
Owner

@Adictya commented on GitHub (Aug 26, 2025):

I think it would be simpler to force position the anchored element absolute and in the x/y getters use the anchor.x/y as relation/anchor instead of parent.x/y?

Yeah makes sense, much simpler

<!-- gh-comment-id:3225391979 --> @Adictya commented on GitHub (Aug 26, 2025): > I think it would be simpler to force position the anchored element absolute and in the x/y getters use the anchor.x/y as relation/anchor instead of parent.x/y? Yeah makes sense, much simpler
Author
Owner

@kommander commented on GitHub (Aug 26, 2025):

We would have to make sure that you cannot anchor to a child. Also, anchoring to an element in another branch makes it complicated, as that branch might be rendered later and does not have updated position yet. So you are right that we have to defer rendering of anchored elements until after the anchor is rendered.

I would do that by letting the anchor know which elements use it as anchor, then stop rendering the branch when encountering an anchored element and resume in the anchor by rendering all anchored elements it has referenced.

Sounds complicated lol, it's really not.

<!-- gh-comment-id:3225600807 --> @kommander commented on GitHub (Aug 26, 2025): We would have to make sure that you cannot anchor to a child. Also, anchoring to an element in another branch makes it complicated, as that branch might be rendered later and does not have updated position yet. So you are right that we have to defer rendering of anchored elements until after the anchor is rendered. I would do that by letting the anchor know which elements use it as anchor, then stop rendering the branch when encountering an anchored element and resume in the anchor by rendering all anchored elements it has referenced. Sounds complicated lol, it's really not.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/opentui#19
No description provided.