Skip to content

Commit 9181a34

Browse files
committed
[dropdown] Add offset props
1 parent 6f51552 commit 9181a34

File tree

2 files changed

+35
-19
lines changed

2 files changed

+35
-19
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
> Committed but unreleased changes are put here, at the top. Older releases are detailed chronologically below.
44
5+
#### Added
6+
7+
- `dropdown`: `:offset-x` and `:offset-y` (number) props.
8+
9+
510
## 2.24.1 (2025-02-19)
611

712
#### Fixed

src/re_com/dropdown.cljs

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
:default "re-com.dropdown/backdrop"
9797
:type "part"
9898
:validate-fn part?
99-
:description (str "Renders a visual overlay, behind the `:anchor` and `:body` parts, when the dropdown is open.")}
99+
:description "Renders a visual overlay, behind the `:anchor` and `:body` parts, when the dropdown is open."}
100100
{:name :show-backdrop?
101101
:required? false
102102
:type "boolean"
@@ -113,11 +113,17 @@
113113
{:name :body-header
114114
:type "part"
115115
:validate-fn part?
116-
:description (str "Appears at the top of the :body part.")}
116+
:description "Appears at the top of the :body part."}
117117
{:name :body-footer
118118
:type "part"
119119
:validate-fn part?
120-
:description (str "Appears at the bottom of the :body part.")}
120+
:description "Appears at the bottom of the :body part."}
121+
{:name :offset-x
122+
:type "number"
123+
:description [:span [:code ":dropdown"] " adds this to the body's dynamically-generated x-position."]}
124+
{:name :offset-y
125+
:type "number"
126+
:description [:span [:code ":dropdown"] " adds this to the body's dynamically-generated y-position."]}
121127
{:name :disabled?
122128
:required false
123129
:type "boolean | r/atom"}
@@ -249,7 +255,7 @@
249255
250256
In other words, the body slides left & right within the anchor width,
251257
and blinks up & down, to find the least cut-off position."
252-
[anchor-el body-el & {:keys [direction]}]
258+
[anchor-el body-el & {:keys [direction offset-y offset-x]}]
253259
(let [a-rect (.getBoundingClientRect anchor-el)
254260
a-x (.-x a-rect)
255261
a-y (.-y a-rect)
@@ -280,13 +286,16 @@
280286
best-y (case v-pos :low lo-y :high hi-y)
281287
best-y (case direction :up hi-y :down lo-y best-y)]
282288
(->> [best-x best-y]
283-
(mapv + [a-x a-y]))))
289+
(map + [a-x a-y])
290+
(mapv + [offset-x offset-y]))))
284291

285-
(defn body-wrapper [{:keys [anchor-ref body-ref anchor-position direction parts]}]
292+
(defn body-wrapper [{:keys [anchor-ref body-ref anchor-position direction parts offset-y offset-x]}]
286293
(let [set-body-ref! #(reset! body-ref %)
287294
optimize-position! #(reset! anchor-position
288295
(optimize-position! @anchor-ref @body-ref
289-
{:direction direction}))
296+
{:direction direction
297+
:offset-y (u/deref-or-value offset-y)
298+
:offset-x (u/deref-or-value offset-x)}))
290299
animation-id (atom nil)
291300
start-loop! (fn start-loop []
292301
(reset! animation-id
@@ -306,17 +315,17 @@
306315
(fn [{:keys [theme children post-props]}]
307316
(let [[left top] (deref anchor-position)]
308317
(u/part (get parts :body-wrapper (get parts ::body-wrapper))
309-
{:theme theme
310-
:part ::body-wrapper
311-
:props
312-
{:position :fixed
313-
:anchor-top top
314-
:anchor-left left
315-
:top top
316-
:left left
317-
:attr {:ref set-body-ref!}
318-
:children children}
319-
:post-props post-props})))})))
318+
{:theme theme
319+
:part ::body-wrapper
320+
:props
321+
{:position :fixed
322+
:anchor-top top
323+
:anchor-left left
324+
:top top
325+
:left left
326+
:attr {:ref set-body-ref!}
327+
:children children}
328+
:post-props post-props})))})))
320329

321330
(defn indicator [{:keys [state style]}]
322331
[:span {:style style}
@@ -328,7 +337,7 @@
328337

329338
(defn dropdown
330339
"A clickable anchor above an openable, floating body."
331-
[& {:keys [model no-clip?] :or {model (reagent/atom nil)}}]
340+
[& {:keys [model no-clip? offset-x offset-y] :or {model (reagent/atom nil)}}]
332341
(let [default-model model
333342
[focused? anchor-ref body-ref anchor-position] (repeatedly #(reagent/atom nil))
334343
anchor-ref! #(reset! anchor-ref %)
@@ -443,6 +452,8 @@
443452
(when (= :open (:openable state))
444453
[body-wrapper
445454
{:anchor-ref anchor-ref
455+
:offset-x offset-x
456+
:offset-y offset-y
446457
:body-ref body-ref
447458
:anchor-position anchor-position
448459
:direction direction

0 commit comments

Comments
 (0)