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 popover placement when there is no enough space on top or bottom of content #303

Merged
merged 5 commits into from
Dec 11, 2020

Conversation

chenesan
Copy link
Contributor

@chenesan chenesan commented Dec 9, 2020

Purpose

修正當 placement 設定為向下 (bottom) 的 popover 下方空間不足時往上長,結果導致看不到 popover 上半部的問題。

這是因為現在 anchored 的邏輯,在原本的 placement 向下且空間不足時,就會往上。因此當 popover 的內容沒有出現捲軸(不超過 max-height),但又比上方空間長時,popover 上面的東西就點不到。

解法:當 popover 的 placement 設定為向下,且下方和上方的空間都不足時,還是向下長。缺點是有機會破圖,但相對不能按應該還是好些。

根據後續的討論,改成在上下都不夠顯示 popover 時,用空間較足夠的那邊作為 placement。並且計算剩餘空間給下面的 <Popover> 作為 max-height 來確保不會高到破版。

Changes

  • a list of what have been done
  • maybe some code change

Risk

會影響到 <Tooltip><Popover> 在 placement 設為 bottom 且上下空間都不夠時的行為,其餘不影響。

TODOs

  • Describe what should be done outside of this PR
  • Maybe in other PRs or some manual actions.

@chenesan chenesan changed the title Let anchor placement be bottom when there is no enough space for content Fix popover placement when there is no enough space on top or bottom of content Dec 9, 2020
@chenesan chenesan marked this pull request as ready for review December 9, 2020 06:02
@chenesan chenesan added this to the v5 milestone Dec 9, 2020
@chenesan chenesan self-assigned this Dec 9, 2020
<div className={BEM.container}>
<div
className={BEM.container}
style={{ maxHeight: remainingSpace ? remainingSpace - popoverPadding : undefined }}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

remainingSpace 是指扣掉箭頭後剩餘的高度,因此還要再扣掉 Popover 自身的 padding

Copy link
Member

Choose a reason for hiding this comment

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

有點隱晦,覺得可以一起放進註解

Comment on lines -63 to +80
positionTop = anchorOffsetTop - selfHeight;
// Make sure user can see whole wrapped component when placement is TOP.
positionTop = Math.max(anchorOffsetTop - selfHeight, 0);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

確保當 placement 為 TOP 時,Popover 最高只會被擺到畫面上緣。如果是負的有可能會看不到 Popover 上半部。

@@ -45,6 +46,8 @@ function Popover({
onClick(event);
};

const popoverPadding = 24;
Copy link
Member

Choose a reason for hiding this comment

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

覺得 popoverPadding 可以移到 function 外宣告
然後直接在這裡算 maxHeight

const maxHeight = remainingSpace ? remainingSpace - popoverPadding : undefined;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

也可,style 比較乾淨

Copy link
Member

@kyoyadmoon kyoyadmoon 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
Contributor

@zhusee2 zhusee2 left a comment

Choose a reason for hiding this comment

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

Comment on lines +34 to +40
expected | defaultVal | situation | anchorTop | anchorHeight | selfHeight | remainingSpace
${TOP} | ${TOP} | ${'enough'} | ${120} | ${30} | ${100} | ${120}
${BOTTOM} | ${TOP} | ${'not enough for top'} | ${90} | ${30} | ${100} | ${648}
${TOP} | ${BOTTOM} | ${'not enough for bottom'} | ${600} | ${100} | ${100} | ${600}
${BOTTOM} | ${BOTTOM} | ${'enough'} | ${300} | ${100} | ${100} | ${368}
${BOTTOM} | ${BOTTOM} | ${'not enough for both, but bottom is larger'} | ${300} | ${100} | ${400} | ${368}
${TOP} | ${BOTTOM} | ${'not enough for both, but top is larger'} | ${450} | ${100} | ${500} | ${450}
Copy link
Contributor

Choose a reason for hiding this comment

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

nice tests 👍

Copy link
Contributor

@a26620236 a26620236 left a comment

Choose a reason for hiding this comment

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

我覺得看起來沒問題!

@chenesan chenesan merged commit 6c23c8c into develop Dec 11, 2020
@chenesan chenesan deleted the bugfix/adjust-anchor-placement branch December 11, 2020 04:59
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.

4 participants