Skip to content

Commit

Permalink
Update to popper.js v2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
Johann-S authored and XhmikosR committed Dec 6, 2020
1 parent 5f89ea3 commit adfdf71
Show file tree
Hide file tree
Showing 26 changed files with 145 additions and 297 deletions.
6 changes: 3 additions & 3 deletions build/build-plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const rollup = require('rollup')
const { babel } = require('@rollup/plugin-babel')
const banner = require('./banner.js')

const rootPath = path.resolve(__dirname, '../js/dist/')
const plugins = [
babel({
// Only transpile our source code
Expand All @@ -39,7 +40,6 @@ const bsPlugins = {
Toast: path.resolve(__dirname, '../js/src/toast.js'),
Tooltip: path.resolve(__dirname, '../js/src/tooltip.js')
}
const rootPath = path.resolve(__dirname, '../js/dist/')

const defaultPluginConfig = {
external: [
Expand Down Expand Up @@ -87,9 +87,9 @@ const getConfigByPluginKey = pluginKey => {

if (pluginKey === 'Dropdown' || pluginKey === 'Tooltip') {
const config = Object.assign(defaultPluginConfig)
config.external.push(bsPlugins.Manipulator, 'popper.js')
config.external.push(bsPlugins.Manipulator, '@popperjs/core')
config.globals[bsPlugins.Manipulator] = 'Manipulator'
config.globals['popper.js'] = 'Popper'
config.globals['@popperjs/core'] = 'Popper'
return config
}

Expand Down
2 changes: 1 addition & 1 deletion build/generate-sri.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const files = [
configPropertyName: 'js_bundle_hash'
},
{
file: 'node_modules/popper.js/dist/umd/popper.min.js',
file: 'node_modules/@popperjs/core/dist/umd/popper.min.js',
configPropertyName: 'popper_hash'
}
]
Expand Down
9 changes: 5 additions & 4 deletions build/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
const path = require('path')
const { babel } = require('@rollup/plugin-babel')
const { nodeResolve } = require('@rollup/plugin-node-resolve')
const replace = require('@rollup/plugin-replace')
const banner = require('./banner.js')

const BUNDLE = process.env.BUNDLE === 'true'
const ESM = process.env.ESM === 'true'

let fileDest = `bootstrap${ESM ? '.esm' : ''}`
const external = ['popper.js']
const external = ['@popperjs/core']
const plugins = [
babel({
// Only transpile our source code
Expand All @@ -19,15 +20,15 @@ const plugins = [
})
]
const globals = {
'popper.js': 'Popper'
'@popperjs/core': 'Popper'
}

if (BUNDLE) {
fileDest += '.bundle'
// Remove last entry in external array to bundle Popper
external.pop()
delete globals['popper.js']
plugins.push(nodeResolve())
delete globals['@popperjs/core']
plugins.push(replace({ 'process.env.NODE_ENV': '"production"' }), nodeResolve())
}

const rollupConfig = {
Expand Down
4 changes: 2 additions & 2 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,5 @@ params:
js_hash: "sha384-supZtwqjyYg6XvvTCi4/w6J6Hm6IKqXaaeoyGhIhonCdkSvA70sSucW7OqXIo4lZ"
js_bundle: "https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
js_bundle_hash: "sha384-G/J8d6sz9bTod37AsZzNtTwT77J24FKjJEO1YsU2vW7iPcmYP3/tznu+LcK824t9"
popper: "https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"
popper_hash: "sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN"
popper: "https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"
popper_hash: "sha384-a46n7BtEJaPKKs2SeVxZzwKkapYzBUr8c7DyCLEpkRrs4LE03nlh53ZSOPgkJB7U"
56 changes: 15 additions & 41 deletions js/src/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* --------------------------------------------------------------------------
*/

import * as Popper from '@popperjs/core'

import {
getjQuery,
onDOMContentLoaded,
Expand All @@ -18,7 +20,6 @@ import {
import Data from './dom/data'
import EventHandler from './dom/event-handler'
import Manipulator from './dom/manipulator'
import Popper from 'popper.js'
import SelectorEngine from './dom/selector-engine'
import BaseComponent from './base-component'

Expand Down Expand Up @@ -58,7 +59,6 @@ const CLASS_NAME_DROPEND = 'dropend'
const CLASS_NAME_DROPSTART = 'dropstart'
const CLASS_NAME_MENUEND = 'dropdown-menu-end'
const CLASS_NAME_NAVBAR = 'navbar'
const CLASS_NAME_POSITION_STATIC = 'position-static'

const SELECTOR_DATA_TOGGLE = '[data-bs-toggle="dropdown"]'
const SELECTOR_FORM_CHILD = '.dropdown form'
Expand All @@ -76,7 +76,7 @@ const PLACEMENT_LEFT = isRTL ? 'right-start' : 'left-start'
const Default = {
offset: 0,
flip: true,
boundary: 'scrollParent',
boundary: 'clippingParents',
reference: 'toggle',
display: 'dynamic',
popperConfig: null
Expand Down Expand Up @@ -176,14 +176,7 @@ class Dropdown extends BaseComponent {
}
}

// If boundary is not `scrollParent`, then set position to `static`
// to allow the menu to "escape" the scroll parent's boundaries
// https://github.com/twbs/bootstrap/issues/24251
if (this._config.boundary !== 'scrollParent') {
parent.classList.add(CLASS_NAME_POSITION_STATIC)
}

this._popper = new Popper(referenceElement, this._menu, this._getPopperConfig())
this._popper = Popper.createPopper(referenceElement, this._menu, this._getPopperConfig())
}

// If this is a touch-enabled device we add extra
Expand Down Expand Up @@ -233,6 +226,7 @@ class Dropdown extends BaseComponent {
super.dispose()
EventHandler.off(this._element, EVENT_KEY)
this._menu = null

if (this._popper) {
this._popper.destroy()
this._popper = null
Expand All @@ -242,7 +236,7 @@ class Dropdown extends BaseComponent {
update() {
this._inNavbar = this._detectNavbar()
if (this._popper) {
this._popper.scheduleUpdate()
this._popper.update()
}
}

Expand Down Expand Up @@ -296,44 +290,24 @@ class Dropdown extends BaseComponent {
return Boolean(this._element.closest(`.${CLASS_NAME_NAVBAR}`))
}

_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
}

_getPopperConfig() {
const popperConfig = {
placement: this._getPlacement(),
modifiers: {
offset: this._getOffset(),
flip: {
enabled: this._config.flip
},
preventOverflow: {
boundariesElement: this._config.boundary
modifiers: [{
name: 'preventOverflow',
options: {
altBoundary: this._config.flip,
rootBoundary: this._config.boundary
}
}
}]
}

// Disable Popper if we have a static display
if (this._config.display === 'static') {
popperConfig.modifiers.applyStyle = {
popperConfig.modifiers = [{
name: 'applyStyles',
enabled: false
}
}]
}

return {
Expand Down
Loading

0 comments on commit adfdf71

Please sign in to comment.