react-component / react-component/trigger

Incorrect calculation of nextOffsetY

オープン
#333 コメント 0 件 リアクション 3 件 担当者 1 名 GitHub で見る

@zombieJ がすでに取り組んでいます。

2023年3月30日 から。

主要言語
TypeScript
スター
391
フォーク
245
PR マージ指標
30日以内にマージされた PR はありません

説明

When recalculating nextOffsetY when popup goes beyond the viewport boundaries incorrect calculations occur.
I see problems in these lines:
https://github.com/react-component/trigger/blob/a73beef20376e3e32ecb918a0c1fbb0cdabb42f3/src/hooks/useAlign.ts#L313
https://github.com/react-component/trigger/blob/a73beef20376e3e32ecb918a0c1fbb0cdabb42f3/src/hooks/useAlign.ts#L327

Let's analyze each line separately.


nextOffsetX = targetAlignPointTL.x - popupAlignPointBR.x - popupOffsetX; 

suppose
the target's bounding box is

{
  width: 200px
  left: 300px
  right: 500px
}

the popup's bounding box is

{
  width: 600px
  left: 0px
  right: 600px
}

first popup calculation without flip recalculation

{
  width: 600px
  left: 300px
  right: 900px
}

x viewport border for example is 600px
Our popup climbs out of the border, so we implement the recalculation of nextOffsetX.
targetAlignPointTL.x = 300px
popupAlignPointBR.x = 600px
popupOffsetX = 0
nextOffsetX = 300px - 600px - 0 = -300px

Total we get that our popup left offset is -300px
and right 600px - 300px = 300px
And we observe approximately the following picture
image

In order to get the correct offset, you need to subtract the right coordinate of popup not from the left coordinate of the target, but from the right.
In other words, change targetAlignPointTL to targetAlignPointBR and get

nextOffsetX = targetAlignPointBR.x - popupAlignPointBR.x - popupOffsetX; 

The situation is similar with Left to Right

nextOffsetX = targetAlignPointBR.x - popupAlignPointTL.x - popupOffsetX;

if we take fake data
target bb

{
  width: 200px
  left: 100px
  right: 300px
}

popup bb

{
  width: 500px
  left: 0px
  right: 500px
}

popup calculated bb

{
  width: 500px
  left: -200px
  right: 300px
}

viewport left border 0px
popupOffsetX = 0px
then substituting into the current formula we get
nextOffsetX = 300px - 0px - 0px = 300px

Popup offset is 300px this is the right border of the target.
Why not just take the left offset of target and make it equal to nextOffsetX?
Well, or as a last resort, add the right border of the target to the existing offset
nextOffsetX = nextOffsetX + targetAlignPointBR.x = -200px + 300px = 100px
We will get the same 100px


In general, the problem is on the face and it brings a lot of inconvenience. Please pay attention to it!

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。