aboutsummaryrefslogtreecommitdiff
path: root/js/src/tooltip
diff options
context:
space:
mode:
authorJohann-S <[email protected]>2019-08-14 17:27:58 +0200
committerXhmikosR <[email protected]>2019-08-18 17:19:36 +0300
commitf03c10a1897254f82d1484924b259a921dcf0431 (patch)
tree7fc4154b168a4660b6f89c1b33944de7f381cbf0 /js/src/tooltip
parentb3cf60018c38b7da84e46f0362a39d5eecbfecaa (diff)
downloadbootstrap-f03c10a1897254f82d1484924b259a921dcf0431.tar.xz
bootstrap-f03c10a1897254f82d1484924b259a921dcf0431.zip
allow to pass popper.js configuration for tooltip/popover and dropdown
Diffstat (limited to 'js/src/tooltip')
-rw-r--r--js/src/tooltip/tooltip.js68
-rw-r--r--js/src/tooltip/tooltip.spec.js15
2 files changed, 55 insertions, 28 deletions
diff --git a/js/src/tooltip/tooltip.js b/js/src/tooltip/tooltip.js
index 33f0173a3..8bf998468 100644
--- a/js/src/tooltip/tooltip.js
+++ b/js/src/tooltip/tooltip.js
@@ -56,7 +56,8 @@ const DefaultType = {
boundary: '(string|element)',
sanitize: 'boolean',
sanitizeFn: '(null|function)',
- whiteList: 'object'
+ whiteList: 'object',
+ popperConfig: '(null|object)'
}
const AttachmentMap = {
@@ -84,7 +85,8 @@ const Default = {
boundary: 'scrollParent',
sanitize: true,
sanitizeFn: null,
- whiteList: DefaultWhitelist
+ whiteList: DefaultWhitelist,
+ popperConfig: null
}
const HoverState = {
@@ -129,10 +131,6 @@ const Trigger = {
class Tooltip {
constructor(element, config) {
- /**
- * Check for Popper dependency
- * Popper - https://popper.js.org
- */
if (typeof Popper === 'undefined') {
throw new TypeError('Bootstrap\'s tooltips require Popper.js (https://popper.js.org)')
}
@@ -247,7 +245,7 @@ class Tooltip {
this._timeout = null
this._hoverState = null
this._activeTrigger = null
- if (this._popper !== null) {
+ if (this._popper) {
this._popper.destroy()
}
@@ -301,27 +299,7 @@ class Tooltip {
EventHandler.trigger(this.element, this.constructor.Event.INSERTED)
- this._popper = new Popper(this.element, tip, {
- placement: attachment,
- modifiers: {
- offset: this._getOffset(),
- flip: {
- behavior: this.config.fallbackPlacement
- },
- arrow: {
- element: `.${this.constructor.NAME}-arrow`
- },
- preventOverflow: {
- boundariesElement: this.config.boundary
- }
- },
- onCreate: data => {
- if (data.originalPlacement !== data.placement) {
- this._handlePopperPlacementChange(data)
- }
- },
- onUpdate: data => this._handlePopperPlacementChange(data)
- })
+ this._popper = new Popper(this.element, tip, this._getPopperConfig(attachment))
tip.classList.add(ClassName.SHOW)
@@ -482,6 +460,40 @@ class Tooltip {
// Private
+ _getPopperConfig(attachment) {
+ const defaultBsConfig = {
+ placement: attachment,
+ modifiers: {
+ offset: this._getOffset(),
+ flip: {
+ behavior: this.config.fallbackPlacement
+ },
+ arrow: {
+ element: `.${this.constructor.NAME}-arrow`
+ },
+ preventOverflow: {
+ boundariesElement: this.config.boundary
+ }
+ },
+ onCreate: data => {
+ if (data.originalPlacement !== data.placement) {
+ this._handlePopperPlacementChange(data)
+ }
+ },
+ onUpdate: data => this._handlePopperPlacementChange(data)
+ }
+
+ let resultConfig = defaultBsConfig
+ if (this.config.popperConfig) {
+ resultConfig = {
+ ...defaultBsConfig,
+ ...this.config.popperConfig
+ }
+ }
+
+ return resultConfig
+ }
+
_addAttachmentClass(attachment) {
this.getTipElement().classList.add(`${CLASS_PREFIX}-${attachment}`)
}
diff --git a/js/src/tooltip/tooltip.spec.js b/js/src/tooltip/tooltip.spec.js
index 1e858d369..e4bddcf00 100644
--- a/js/src/tooltip/tooltip.spec.js
+++ b/js/src/tooltip/tooltip.spec.js
@@ -108,6 +108,21 @@ describe('Tooltip', () => {
tooltipInContainerEl.click()
})
+
+ it('should allow to pass config to popper.js thanks to popperConfig', () => {
+ fixtureEl.innerHTML = '<a href="#" rel="tooltip"/>'
+
+ const tooltipEl = fixtureEl.querySelector('a')
+ const tooltip = new Tooltip(tooltipEl, {
+ popperConfig: {
+ placement: 'left'
+ }
+ })
+
+ const popperConfig = tooltip._getPopperConfig('top')
+
+ expect(popperConfig.placement).toEqual('left')
+ })
})
describe('enable', () => {