From 527f55c2f3fecfbc1e991263d646a32de4ffcded Mon Sep 17 00:00:00 2001
From: Roman O <ceo.roman@gmail.com>
Date: Tue, 3 Oct 2017 15:34:44 +0300
Subject: [PATCH] Offset option for dropdown can be function (#24222)

* Offset option can be function (Popper.js)

* Fix...add function type for offset option

* Remove constants for popper config

* Optimize code. Remove foreach loop.

* Refactoring. Remove getOffset method
---
 docs/4.0/components/dropdowns.md |  2 +-
 js/src/dropdown.js               | 15 +++++++++++----
 2 files changed, 12 insertions(+), 5 deletions(-)

diff --git a/docs/4.0/components/dropdowns.md b/docs/4.0/components/dropdowns.md
index cdf713b03f..d38b410c78 100644
--- a/docs/4.0/components/dropdowns.md
+++ b/docs/4.0/components/dropdowns.md
@@ -588,7 +588,7 @@ Options can be passed via data attributes or JavaScript. For data attributes, ap
   <tbody>
     <tr>
       <td>offset</td>
-      <td>number | string</td>
+      <td>number | string | function</td>
       <td>0</td>
       <td>Offset of the dropdown relative to its target. For more information refer to Popper.js's <a href="https://popper.js.org/popper-documentation.html#modifiers..offset.offset">offset docs</a>.</td>
     </tr>
diff --git a/js/src/dropdown.js b/js/src/dropdown.js
index f76f84ef0a..6681df668c 100644
--- a/js/src/dropdown.js
+++ b/js/src/dropdown.js
@@ -80,7 +80,7 @@ const Dropdown = (() => {
   }
 
   const DefaultType = {
-    offset      : '(number|string)',
+    offset      : '(number|string|function)',
     flip        : 'boolean'
   }
 
@@ -246,12 +246,19 @@ const Dropdown = (() => {
     }
 
     _getPopperConfig() {
+      const offsetConf = {}
+      if (typeof this._config.offset === 'function') {
+        offsetConf.fn = (data) => {
+          data.offsets = $.extend({}, data.offsets, this._config.offset(data.offsets) || {})
+          return data
+        }
+      } else {
+        offsetConf.offset = this._config.offset
+      }
       const popperConfig = {
         placement : this._getPlacement(),
         modifiers : {
-          offset : {
-            offset : this._config.offset
-          },
+          offset : offsetConf,
           flip : {
             enabled : this._config.flip
           }
-- 
GitLab