From 5c8890687aa631a315438529346e89f4943d82a8 Mon Sep 17 00:00:00 2001 From: joke2k Date: Sat, 12 Dec 2020 00:54:29 +0000 Subject: [PATCH 1/6] Restore offset option for dropdown component --- js/src/dropdown.js | 31 +++++++++++++++++++ site/content/docs/5.0/components/dropdowns.md | 10 ++++++ 2 files changed, 41 insertions(+) diff --git a/js/src/dropdown.js b/js/src/dropdown.js index 4d65008f8..8824935d7 100644 --- a/js/src/dropdown.js +++ b/js/src/dropdown.js @@ -289,6 +289,31 @@ class Dropdown extends BaseComponent { return this._element.closest(`.${CLASS_NAME_NAVBAR}`) !== null } + _getOffset() { + let offset = [0, 0] + + if (!this._config.offset) { + return offset + } + + if (typeof this._config.offset === 'number') { + offset[0] = this._config.offset + } else if (typeof this._config.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') { + offset = (popper, reference, placement) => { + return offset({ popper, reference, placement }, this._element) + } + } + + return offset + } + _getPopperConfig() { const popperConfig = { placement: this._getPlacement(), @@ -304,6 +329,12 @@ class Dropdown extends BaseComponent { options: { fallbackPlacements: ['top', 'right', 'bottom', 'left'] } + }, + { + name: 'offset', + options: { + offset: this._getOffset() + } }] } diff --git a/site/content/docs/5.0/components/dropdowns.md b/site/content/docs/5.0/components/dropdowns.md index 50dcde2ba..416b80fd0 100644 --- a/site/content/docs/5.0/components/dropdowns.md +++ b/site/content/docs/5.0/components/dropdowns.md @@ -896,6 +896,16 @@ Options can be passed via data attributes or JavaScript. For data attributes, ap 'dynamic' By default, we use Popper for dynamic positioning. Disable this with static. + + offset + number | string | function + 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.

+

For more information refer to Popper.js's offset docs.

+ + popperConfig null | object From 5d22c10d081c3fb77221283715d49f59c8ee17c3 Mon Sep 17 00:00:00 2001 From: joke2k Date: Sat, 12 Dec 2020 01:27:54 +0000 Subject: [PATCH 2/6] tests for dropdown offsets --- js/src/dropdown.js | 14 ++++---- js/tests/unit/dropdown.spec.js | 36 +++++++++++++++++++ site/content/docs/5.0/components/dropdowns.md | 2 +- 3 files changed, 44 insertions(+), 8 deletions(-) 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 = [ + '' + ].join('') + + const getOffset = () => [10, 20] + const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]') + const dropdown = new Dropdown(btnDropdown, { + offset: getOffset + }) + const offset = dropdown._getOffset() + + expect(typeof offset).toEqual('function') + }) + + it('should create offset modifier correctly when offset option is a string into data attribute', () => { + fixtureEl.innerHTML = [ + '' + ].join('') + + const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]') + const dropdown = new Dropdown(btnDropdown) + + expect(dropdown._getOffset()).toEqual([10, 20]) + }) + it('should allow to pass config to Popper with `popperConfig`', () => { fixtureEl.innerHTML = [ '