Skip to content

Commit

Permalink
[dropdown] Change algorithm for dynamic positioning
Browse files Browse the repository at this point in the history
  • Loading branch information
kimo-k committed Jul 20, 2024
1 parent cfba426 commit df37449
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/re_com/dropdown.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,9 @@
(defn optimize-position!
"Returns an [x y] position for popover, relative to anchor.
Considers two possible vertical positions - above or below the anchor.
Picks the vertical position whose midpoint is nearest to the viewport's midpoint.
If one vertical position clips outside the viewport, chooses the opposite position.
If both vertical positions clip, picks the vertical position whose midpoint
is nearest the viewport's midpoint.
Calculates a left-justified horizontal position, constrained by the viewport width
and the right edge of the anchor.
Expand All @@ -236,15 +238,19 @@
a-bot (.-bottom a-rect)
b-h (.-offsetHeight body-el)
b-w (.-offsetWidth body-el)
v-mid-y (/ js/window.innerHeight 2)
w-h js/window.innerHeight
v-mid-y (/ w-h 2)
lo-mid-y (+ a-bot (/ b-w 2))
hi-mid-y (- a-y (/ b-h 2))
bot-clipped? (< w-h (+ a-bot b-h))
top-clipped? (neg? (- a-y b-h))
top-best? (= hi-mid-y (nearest v-mid-y lo-mid-y hi-mid-y))
v-pos (cond
top-clipped? :low
top-best? :high
:else :low)
(not (or top-clipped? bot-clipped?)) :low
bot-clipped? :high
top-clipped? :low
top-best? :high
:else :low)
left-bound (max (- a-x) 0)
right-bound (max (- a-w b-w) 0)
hi-y (- b-h)
Expand Down

0 comments on commit df37449

Please sign in to comment.