Compare commits
6 commits
main
...
rohit/main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
98fe781fa5 | ||
|
|
7270c8ddc3 | ||
|
|
7ab1dfea03 | ||
|
|
2090329d54 | ||
|
|
5d22c10d08 | ||
|
|
5c8890687a |
4 changed files with 86 additions and 3 deletions
|
|
@ -72,7 +72,7 @@ const PLACEMENT_RIGHT = isRTL ? 'left-start' : 'right-start'
|
||||||
const PLACEMENT_LEFT = isRTL ? 'right-start' : 'left-start'
|
const PLACEMENT_LEFT = isRTL ? 'right-start' : 'left-start'
|
||||||
|
|
||||||
const Default = {
|
const Default = {
|
||||||
offset: 0,
|
offset: [0, 2],
|
||||||
flip: true,
|
flip: true,
|
||||||
boundary: 'clippingParents',
|
boundary: 'clippingParents',
|
||||||
reference: 'toggle',
|
reference: 'toggle',
|
||||||
|
|
@ -81,7 +81,7 @@ const Default = {
|
||||||
}
|
}
|
||||||
|
|
||||||
const DefaultType = {
|
const DefaultType = {
|
||||||
offset: '(number|string|function)',
|
offset: '(array|string|function)',
|
||||||
flip: 'boolean',
|
flip: 'boolean',
|
||||||
boundary: '(string|element)',
|
boundary: '(string|element)',
|
||||||
reference: '(string|element)',
|
reference: '(string|element)',
|
||||||
|
|
@ -289,6 +289,20 @@ class Dropdown extends BaseComponent {
|
||||||
return this._element.closest(`.${CLASS_NAME_NAVBAR}`) !== null
|
return this._element.closest(`.${CLASS_NAME_NAVBAR}`) !== null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_getOffset() {
|
||||||
|
const { offset } = this._config
|
||||||
|
|
||||||
|
if (typeof offset === 'string') {
|
||||||
|
return offset.split(',').map(val => Number.parseInt(val, 10))
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof offset === 'function') {
|
||||||
|
return popperData => offset(popperData, this._element)
|
||||||
|
}
|
||||||
|
|
||||||
|
return offset
|
||||||
|
}
|
||||||
|
|
||||||
_getPopperConfig() {
|
_getPopperConfig() {
|
||||||
const popperConfig = {
|
const popperConfig = {
|
||||||
placement: this._getPlacement(),
|
placement: this._getPlacement(),
|
||||||
|
|
@ -304,6 +318,12 @@ class Dropdown extends BaseComponent {
|
||||||
options: {
|
options: {
|
||||||
fallbackPlacements: ['top', 'right', 'bottom', 'left']
|
fallbackPlacements: ['top', 'right', 'bottom', 'left']
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'offset',
|
||||||
|
options: {
|
||||||
|
offset: this._getOffset()
|
||||||
|
}
|
||||||
}]
|
}]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -54,6 +54,54 @@ describe('Dropdown', () => {
|
||||||
expect(dropdown.toggle).toHaveBeenCalled()
|
expect(dropdown.toggle).toHaveBeenCalled()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should create offset modifier correctly when offset option is a function', done => {
|
||||||
|
fixtureEl.innerHTML = [
|
||||||
|
'<div class="dropdown">',
|
||||||
|
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>',
|
||||||
|
' <div class="dropdown-menu">',
|
||||||
|
' <a class="dropdown-item" href="#">Secondary link</a>',
|
||||||
|
' </div>',
|
||||||
|
'</div>'
|
||||||
|
].join('')
|
||||||
|
|
||||||
|
const getOffset = jasmine.createSpy('getOffset').and.returnValue([10, 20])
|
||||||
|
const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
|
||||||
|
const dropdown = new Dropdown(btnDropdown, {
|
||||||
|
offset: getOffset,
|
||||||
|
popperConfig: {
|
||||||
|
onFirstUpdate: state => {
|
||||||
|
expect(getOffset).toHaveBeenCalledWith({
|
||||||
|
popper: state.rects.popper,
|
||||||
|
reference: state.rects.reference,
|
||||||
|
placement: state.placement
|
||||||
|
}, btnDropdown)
|
||||||
|
done()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
const offset = dropdown._getOffset()
|
||||||
|
|
||||||
|
expect(typeof offset).toEqual('function')
|
||||||
|
|
||||||
|
dropdown.show()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should create offset modifier correctly when offset option is a string into data attribute', () => {
|
||||||
|
fixtureEl.innerHTML = [
|
||||||
|
'<div class="dropdown">',
|
||||||
|
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown" data-bs-offset="10,20">Dropdown</button>',
|
||||||
|
' <div class="dropdown-menu">',
|
||||||
|
' <a class="dropdown-item" href="#">Secondary link</a>',
|
||||||
|
' </div>',
|
||||||
|
'</div>'
|
||||||
|
].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`', () => {
|
it('should allow to pass config to Popper with `popperConfig`', () => {
|
||||||
fixtureEl.innerHTML = [
|
fixtureEl.innerHTML = [
|
||||||
'<div class="dropdown">',
|
'<div class="dropdown">',
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@
|
||||||
display: none; // none by default, but block on "open" of the menu
|
display: none; // none by default, but block on "open" of the menu
|
||||||
min-width: $dropdown-min-width;
|
min-width: $dropdown-min-width;
|
||||||
padding: $dropdown-padding-y $dropdown-padding-x;
|
padding: $dropdown-padding-y $dropdown-padding-x;
|
||||||
margin: $dropdown-spacer 0 0; // override default ul
|
margin: 0; // Let Popper handle the space by offset
|
||||||
@include font-size($dropdown-font-size);
|
@include font-size($dropdown-font-size);
|
||||||
color: $dropdown-color;
|
color: $dropdown-color;
|
||||||
text-align: left; // Ensures proper alignment if parent has it changed (e.g., modal footer)
|
text-align: left; // Ensures proper alignment if parent has it changed (e.g., modal footer)
|
||||||
|
|
@ -174,6 +174,11 @@
|
||||||
|
|
||||||
.dropdown-menu.show {
|
.dropdown-menu.show {
|
||||||
display: block;
|
display: block;
|
||||||
|
|
||||||
|
&:not([data-popper-placement]) {
|
||||||
|
margin: $dropdown-spacer 0 0; // override default ul for dropdown not using Popper
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dropdown section headers
|
// Dropdown section headers
|
||||||
|
|
|
||||||
|
|
@ -896,6 +896,16 @@ Options can be passed via data attributes or JavaScript. For data attributes, ap
|
||||||
<td><code>'dynamic'</code></td>
|
<td><code>'dynamic'</code></td>
|
||||||
<td>By default, we use Popper for dynamic positioning. Disable this with <code>static</code>.</td>
|
<td>By default, we use Popper for dynamic positioning. Disable this with <code>static</code>.</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td><code>offset</code></td>
|
||||||
|
<td>array | string | function</td>
|
||||||
|
<td><code>[0, 0]</code></td>
|
||||||
|
<td>
|
||||||
|
<p>Offset of the dropdown relative to its target.</p>
|
||||||
|
<p>When a function is used to determine the offset, it is called with an object containing the <code>popper</code> instance, the <code>refecence</code> Element and the <code>placement</code> as its first argument. The function must return an array with two numbers: <code>[<a href="https://popper.js.org/docs/v2/modifiers/offset/#skidding-1">skidding</a>, <a href="https://popper.js.org/docs/v2/modifiers/offset/#distance-1">distance</a>]</code>. The triggering element DOM node is passed as the second argument.</p>
|
||||||
|
<p>For more information refer to Popper.js's <a href="https://popper.js.org/docs/v2/modifiers/offset/#options">offset docs</a>.</p>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td><code>popperConfig</code></td>
|
<td><code>popperConfig</code></td>
|
||||||
<td>null | object</td>
|
<td>null | object</td>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue