Skip to content

Commit 2d8c02e

Browse files
remeikaXhmikosR
andauthored
tooltip/popover: add a customClass option (#31834)
Co-authored-by: XhmikosR <[email protected]>
1 parent ffa88de commit 2d8c02e

File tree

5 files changed

+88
-0
lines changed

5 files changed

+88
-0
lines changed

js/src/tooltip.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const DefaultType = {
4141
container: '(string|element|boolean)',
4242
fallbackPlacement: '(string|array)',
4343
boundary: '(string|element)',
44+
customClass: '(string|function)',
4445
sanitize: 'boolean',
4546
sanitizeFn: '(null|function)',
4647
whiteList: 'object',
@@ -70,6 +71,7 @@ const Default = {
7071
container: false,
7172
fallbackPlacement: 'flip',
7273
boundary: 'scrollParent',
74+
customClass: '',
7375
sanitize: true,
7476
sanitizeFn: null,
7577
whiteList: DefaultWhitelist,
@@ -284,6 +286,7 @@ class Tooltip {
284286
this._popper = new Popper(this.element, tip, this._getPopperConfig(attachment))
285287

286288
$(tip).addClass(CLASS_NAME_SHOW)
289+
$(tip).addClass(this._getCustomClass())
287290

288291
// If this is a touch-enabled device we add extra
289292
// empty mouseover listeners to the body's immediate children;
@@ -731,6 +734,10 @@ class Tooltip {
731734
this.config.animation = initConfigAnimation
732735
}
733736

737+
_getCustomClass() {
738+
return this.element.getAttribute('data-custom-class') || this.config.customClass
739+
}
740+
734741
// Static
735742

736743
static _jQueryInterface(config) {

js/tests/unit/popover.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,19 @@ $(function () {
6161
.bootstrapPopover('show')
6262
})
6363

64+
QUnit.test('should render popover element with additional classes', function (assert) {
65+
assert.expect(2)
66+
var done = assert.async()
67+
$('<a href="#" title="mdo" data-content="https://twitter.com/mdo" data-custom-class="a b">@mdo</a>')
68+
.appendTo('#qunit-fixture')
69+
.on('shown.bs.popover', function () {
70+
assert.strictEqual($('.popover').hasClass('popover fade bs-popover-right show'), true, 'has default classes')
71+
assert.strictEqual($('.popover').hasClass('a b'), true, 'has custom classes')
72+
done()
73+
})
74+
.bootstrapPopover('show')
75+
})
76+
6477
QUnit.test('should store popover instance in popover data object', function (assert) {
6578
assert.expect(1)
6679
var $popover = $('<a href="#" title="mdo" data-content="https://twitter.com/mdo">@mdo</a>').bootstrapPopover()

js/tests/unit/tooltip.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,4 +1283,54 @@ $(function () {
12831283

12841284
assert.strictEqual(popperConfig.placement, 'left')
12851285
})
1286+
1287+
QUnit.test('additional classes can be applied via data attribute', function (assert) {
1288+
assert.expect(2)
1289+
1290+
$('<a href="#" rel="tooltip" data-trigger="click" title="Another tooltip" data-custom-class="a b"/>')
1291+
.appendTo('#qunit-fixture')
1292+
.bootstrapTooltip()
1293+
.bootstrapTooltip('show')
1294+
1295+
var tooltip = $('.tooltip')
1296+
1297+
assert.strictEqual(tooltip.hasClass('a b'), true)
1298+
assert.strictEqual(tooltip.hasClass('tooltip fade bs-tooltip-top show'), true)
1299+
})
1300+
1301+
QUnit.test('additional classes can be applied via config string', function (assert) {
1302+
assert.expect(2)
1303+
1304+
$('<a href="#" rel="tooltip" data-trigger="click" title="Another tooltip" />')
1305+
.appendTo('#qunit-fixture')
1306+
.bootstrapTooltip({
1307+
customClass: 'a b'
1308+
})
1309+
.bootstrapTooltip('show')
1310+
1311+
var tooltip = $('.tooltip')
1312+
1313+
assert.strictEqual(tooltip.hasClass('a b'), true)
1314+
assert.strictEqual(tooltip.hasClass('tooltip fade bs-tooltip-top show'), true)
1315+
})
1316+
1317+
QUnit.test('additional classes can be applied via function', function (assert) {
1318+
assert.expect(2)
1319+
1320+
var getClasses = function () {
1321+
return 'a b'
1322+
}
1323+
1324+
$('<a href="#" rel="tooltip" data-trigger="click" title="Another tooltip" />')
1325+
.appendTo('#qunit-fixture')
1326+
.bootstrapTooltip({
1327+
customClass: getClasses
1328+
})
1329+
.bootstrapTooltip('show')
1330+
1331+
var tooltip = $('.tooltip')
1332+
1333+
assert.strictEqual(tooltip.hasClass('a b'), true)
1334+
assert.strictEqual(tooltip.hasClass('tooltip fade bs-tooltip-top show'), true)
1335+
})
12861336
})

site/content/docs/4.5/components/popovers.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,15 @@ Note that for security reasons the `sanitize`, `sanitizeFn` and `whiteList` opti
269269
<td>Allow to specify which position Popper will use on fallback. For more information refer to
270270
Popper.js's <a href="https://popper.js.org/docs/v1/#modifiers..flip.behavior">behavior docs</a></td>
271271
</tr>
272+
<tr>
273+
<td>customClass</td>
274+
<td>string | function</td>
275+
<td>''</td>
276+
<td>
277+
<p>Add classes to the popover when it is shown. Note that these classes will be added in addition to any classes specified in the template. To add mutiple classes, separate them with spaces: <code>'a b'</code>.</p>
278+
<p>You can also pass a function that should return a single string containing additional class names.</p>
279+
</td>
280+
</tr>
272281
<tr>
273282
<td>boundary</td>
274283
<td>string | element</td>

site/content/docs/4.5/components/tooltips.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,15 @@ Note that for security reasons the `sanitize`, `sanitizeFn` and `whiteList` opti
250250
<td>Allow to specify which position Popper will use on fallback. For more information refer to
251251
Popper.js's <a href="https://popper.js.org/docs/v1/#modifiers..flip.behavior">behavior docs</a></td>
252252
</tr>
253+
<tr>
254+
<td>customClass</td>
255+
<td>string | function</td>
256+
<td>''</td>
257+
<td>
258+
<p>Add classes to the popover when it is shown. Note that these classes will be added in addition to any classes specified in the template. To add mutiple classes, separate them with spaces: <code>'a b'</code>.</p>
259+
<p>You can also pass a function that should return a single string containing additional class names.</p>
260+
</td>
261+
</tr>
253262
<tr>
254263
<td>boundary</td>
255264
<td>string | element</td>

0 commit comments

Comments
 (0)