diff --git a/js/src/dropdown.js b/js/src/dropdown.js index 8824935d7..177c7f7d5 100644 --- a/js/src/dropdown.js +++ b/js/src/dropdown.js @@ -290,22 +290,22 @@ class Dropdown extends BaseComponent { } _getOffset() { - let offset = [0, 0] + let { offset } = this._config - if (!this._config.offset) { - return offset + if (!offset) { + return [0, 0] } - if (typeof this._config.offset === 'number') { - offset[0] = this._config.offset - } else if (typeof this._config.offset === 'string') { + if (typeof offset === 'number') { + offset = [offset, 0] + } else if (typeof offset === 'string') { offset = offset.split(',') if (offset.length === 1) { offset = [offset[0], 0] } offset = offset.map(val => Number.parseInt(val, 10)) - } else if (typeof this._config.offset === 'function') { + } else if (typeof offset === 'function') { offset = (popper, reference, placement) => { return offset({ popper, reference, placement }, this._element) } diff --git a/js/tests/unit/dropdown.spec.js b/js/tests/unit/dropdown.spec.js index d2171f369..c59cd3b02 100644 --- a/js/tests/unit/dropdown.spec.js +++ b/js/tests/unit/dropdown.spec.js @@ -54,6 +54,42 @@ describe('Dropdown', () => { expect(dropdown.toggle).toHaveBeenCalled() }) + it('should create offset modifier correctly when offset option is a function', () => { + fixtureEl.innerHTML = [ + '
offset0"0,0"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 popper offsets object data as its first argument. The function must return an array with two numbers: [skidding, distance]. The triggering element DOM node is passed as the second argument.