Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for tooltip offset option to be a function #28130

Merged
merged 3 commits into from
Feb 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions js/src/tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const DefaultType = {
html : 'boolean',
selector : '(string|boolean)',
placement : '(string|function)',
offset : '(number|string)',
offset : '(number|string|function)',
container : '(string|element|boolean)',
fallbackPlacement : '(string|array)',
boundary : '(string|element)'
Expand Down Expand Up @@ -285,9 +285,7 @@ class Tooltip {
this._popper = new Popper(this.element, tip, {
placement: attachment,
modifiers: {
offset: {
offset: this.config.offset
},
offset: this._getOffset(),
flip: {
behavior: this.config.fallbackPlacement
},
Expand Down Expand Up @@ -450,6 +448,25 @@ class Tooltip {

// Private

_getOffset() {
const offset = {}

if (typeof this.config.offset === 'function') {
offset.fn = (data) => {
data.offsets = {
...data.offsets,
...this.config.offset(data.offsets, this.element) || {}
}

return data
}
} else {
offset.offset = this.config.offset
}

return offset
}

_getContainer() {
if (this.config.container === false) {
return document.body
Expand Down
37 changes: 37 additions & 0 deletions js/tests/unit/tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -1069,4 +1069,41 @@ $(function () {

assert.strictEqual(tooltip._isEnabled, true)
})

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 $trigger = $('<a href="#" rel="tooltip" data-trigger="click" title="Another tooltip"/>')
.appendTo('#qunit-fixture')
.bootstrapTooltip({
offset: getOffset
})

var tooltip = $trigger.data('bs.tooltip')
var offset = tooltip._getOffset()

assert.ok(typeof offset.offset === 'undefined')
assert.ok(typeof offset.fn === 'function')
})

QUnit.test('should create offset modifier correctly when offset option is not a function', function (assert) {
assert.expect(2)

var myOffset = 42
var $trigger = $('<a href="#" rel="tooltip" data-trigger="click" title="Another tooltip"/>')
.appendTo('#qunit-fixture')
.bootstrapTooltip({
offset: myOffset
})

var tooltip = $trigger.data('bs.tooltip')
var offset = tooltip._getOffset()

assert.strictEqual(offset.offset, myOffset)
assert.ok(typeof offset.fn === 'undefined')
})
})
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,11 @@
},
{
"path": "./dist/js/bootstrap.bundle.min.js",
"maxSize": "21 kB"
"maxSize": "21.25 kB"
},
{
"path": "./dist/js/bootstrap.js",
"maxSize": "22.5 kB"
"maxSize": "23 kB"
},
{
"path": "./dist/js/bootstrap.min.js",
Expand Down
8 changes: 6 additions & 2 deletions site/docs/4.2/components/tooltips.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,13 @@ Options can be passed via data attributes or JavaScript. For data attributes, ap
</tr>
<tr>
<td>offset</td>
<td>number | string</td>
<td>number | string | function</td>
<td>0</td>
<td>Offset of the tooltip 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>
<td>
<p>Offset of the tooltip relative to its target.</p>
<p>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.</p>
<p>For more information refer to Popper.js's <a href="https://popper.js.org/popper-documentation.html#modifiers..offset.offset">offset docs</a>.</p>
</td>
</tr>
<tr>
<td>fallbackPlacement</td>
Expand Down