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

Anchored position: Flip to alternative alignments on overflow #78

Merged
merged 5 commits into from
Apr 13, 2022

Conversation

siddharthkp
Copy link
Member

@siddharthkp siddharthkp commented Mar 30, 2022

tl;dr: If there isn't enough space for align: 'start', flip alignment to align: 'end' (based on side alternateOrders implementation)

Before After
while in bounds, tooltip is positioned incorrectly tooltip position is flipped

@changeset-bot
Copy link

changeset-bot bot commented Mar 30, 2022

🦋 Changeset detected

Latest commit: a909c07

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@primer/behaviors Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@siddharthkp siddharthkp changed the title AnchoredPosition: Add alternative alignments Anchored position: Add alternative alignments Apr 4, 2022
@siddharthkp siddharthkp self-assigned this Apr 4, 2022
@siddharthkp siddharthkp added bug Something isn't working enhancement New feature or request labels Apr 4, 2022
@siddharthkp siddharthkp marked this pull request as ready for review April 4, 2022 10:56
@siddharthkp siddharthkp changed the title Anchored position: Add alternative alignments Anchored position: Flip to alternative alignments on overflow Apr 4, 2022
@siddharthkp siddharthkp added react behaviors and removed enhancement New feature or request labels Apr 4, 2022
@siddharthkp siddharthkp requested a review from rezrah April 4, 2022 10:58
return currentPos.left < containerDimensions.left
} else if (align === 'start' || align === 'center') {
return currentPos.left + elementDimensions.width > containerDimensions.left + containerDimensions.width
}
Copy link

@khiga8 khiga8 Apr 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we'll need a different logic for determining whether center overflows. For instance, checking whether the center of floating element and center of anchored element is equivalent. Notably, this logic for determining overflow may need to consider whether anchorSide is top or bottom, rather than left or right.

Here, shouldRecalculateAlignment returns false unexpectedly:
Screenshot of tooltip that is off centered, in the south direction

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oooh that's a great point! I understand what you mean, will play around with it a bit more

Copy link
Member Author

@siddharthkp siddharthkp Apr 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to force the above example, does this feel like the correct behavior?

It stays center as long as it can, then switches to end if there isn't enough space. I wasn't able to get it in the same position as your screenshot 😅

Screen.Recording.2022-04-11.at.5.45.21.PM.mov

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Notably, this logic for determining overflow may need to consider whether anchorSide is top or bottom, rather than left or right.

Not sure I understand when this breaks. If the anchorSide is right and there isn't enough space, it switches to left before we re-evaluate alignment. 🤔 What scenario am I missing?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with you. I don't think shouldRecalculateAlignment would ever be called for the anchorSide left or right scenario 👍 If I'm understanding correctly, this method is only relevant for anchorSide top or bottom.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep! It's only relevant if anchorSide is top or bottom 👍

Copy link

@rezrah rezrah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link

@khiga8 khiga8 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great 🙌 Thank you!

return currentPos.left < containerDimensions.left
} else if (align === 'start' || align === 'center') {
return currentPos.left + elementDimensions.width > containerDimensions.left + containerDimensions.width
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with you. I don't think shouldRecalculateAlignment would ever be called for the anchorSide left or right scenario 👍 If I'm understanding correctly, this method is only relevant for anchorSide top or bottom.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
behaviors bug Something isn't working react
Projects
None yet
Development

Successfully merging this pull request may close these issues.

getAnchoredPosition returns position without perfectly aligned edges
3 participants