diff --git a/js/src/dropdown.js b/js/src/dropdown.js index 9aaf5a339ea2..31deaa725ee2 100644 --- a/js/src/dropdown.js +++ b/js/src/dropdown.js @@ -319,24 +319,30 @@ class Dropdown { return $(this._element).closest('.navbar').length > 0 } - _getPopperConfig() { - const offsetConf = {} + _getOffset() { + const offset = {} + if (typeof this._config.offset === 'function') { - offsetConf.fn = (data) => { + offset.fn = (data) => { data.offsets = { ...data.offsets, - ...this._config.offset(data.offsets) || {} + ...this._config.offset(data.offsets, this._element) || {} } + return data } } else { - offsetConf.offset = this._config.offset + offset.offset = this._config.offset } + return offset + } + + _getPopperConfig() { const popperConfig = { placement: this._getPlacement(), modifiers: { - offset: offsetConf, + offset: this._getOffset(), flip: { enabled: this._config.flip }, @@ -352,6 +358,7 @@ class Dropdown { enabled: false } } + return popperConfig } diff --git a/js/tests/unit/dropdown.js b/js/tests/unit/dropdown.js index fd9b7f962958..1ecfd1f8bf8f 100644 --- a/js/tests/unit/dropdown.js +++ b/js/tests/unit/dropdown.js @@ -1361,4 +1361,59 @@ $(function () { $dropdown.hide() assert.ok($dropdown.parent('.dropdown').hasClass('show')) }) + + QUnit.test('should create offset modifier correctly when offset option is a function', function (assert) { + assert.expect(2) + + var getOffset = function (offsets) { + return offsets + } + + var dropdownHTML = + '
Offset of the dropdown relative to its target.
+When a function is used to determine the offset, it is called with an object containing the offset data as its first argument. The function must return an object with the same structure. The triggering element DOM node is passed as the second argument.
+For more information refer to Popper.js's offset docs.
+