Problem/Motivation

When attempting to add a dropdown to the navbar in a Drupal site, a JavaScript error occurs, preventing the dropdown from functioning correctly. The error message indicates that the createPopper function from the Popper.js library is not recognized, leading to a TypeError.

Uncaught TypeError: Popper__namespace.createPopper is not a function
    at Dropdown._createPopper (dropdown.js?showaf:220:40)
    at Dropdown.show (dropdown.js?showaf:137:12)
    at Dropdown.toggle (dropdown.js?showaf:124:51)
    at HTMLButtonElement.<anonymous> (dropdown.js?showaf:390:40)
    at HTMLDocument.handler (event-handler.js?showaf:75:21)
The Responsive Theme Preview module is loading its Popper.js library

dropdown.js and tooltip.js are allocating the library by @popperjs/core

Not by it’s physical file location and name space.

@popperjs/core@2.9.2 is working but 2.11.8 is not

  • Add a dropdown menu to the navbar in your Drupal site.
  • Ensure that the dropdown.js file is included and that Popper.js is supposed to be loaded.
  • Attempt to toggle the dropdown menu by clicking on it.
  • Observe the JavaScript console for the error message mentioned above.

Proposed resolution

#3471477: Fix Popper.js Error When Adding Navbar Dropdown in Varbase Components

Add aBootstrap Fixer
Add custom Bootstrap fixes and changes over the default provided
Bootstrap 5.3.3 package.

Using UMD in Varbase Components and Vartheme BS5
The UMD format in Bootstrap is using ESM format.
`dropdown.js` and `tooltip.js` are allocating the library by `@popperjs/core`

  • Add Popper global namespace fixer for the `dropdown.js` file in Bootstrap.
  • Add Popper global namespace fixer for the `tooltip.js` file in Bootstrap.
  • Drop the Popper.js library file from the Responsive Theme preview module. Varbase Components and Vartheme BS5 are providing that using Node.js with Bootstrap 5.

Remaining tasks

  • ✅ File an issue about this project
  • ✅ Addition/Change/Update/Fix to this project
  • ✅ Testing to ensure no regression
  • ➖ Automated unit/functional testing coverage
  • ➖ Developer Documentation support on feature change/addition
  • ➖ User Guide Documentation support on feature change/addition
  • ✅ UX/UI designer responsibilities
  • ✅ Accessibility and Readability
  • ✅ Code review from 1 Varbase core team member
  • ✅ Full testing and approval
  • ✅ Credit contributors
  • ✅ Review with the product owner
  • ✅ Update Release Notes and Update Helper on new feature change/addition
  • ✅ Release varbase-10.0.2, vartheme_bs5-3.0.3

Varbase update type

  • ✅ No Update
  • ➖ Optional Update
  • ➖ Forced Update
  • ➖ Forced Update if Unchanged

User interface changes

  • N/A

API changes

  • N/A

Data model changes

  • N/A

Release notes snippet

Comments

mohammedodeh created an issue. See original summary.

rajab natshah’s picture

Issue summary: View changes
rajab natshah’s picture

I confirm the issue

Which version of Varbase or its components (Varbase 10.0.x-dev development, the latest release, Varbase Components, or Vartheme BS5) are you facing this issue with?

How did the issue develop after the changes in #3456935: Fix dropdown menu limited to only two levels? Could you describe what happened?

#3456935 was committed, but not released yet.

mohammedodeh’s picture

I'm using Varbase 10.0.x-dev, Varbase Components version 2.0.2+1-dev, and Vartheme BS5 version 3.0.0-rc1+1-dev.

I don't believe the issue is related to the changes from #3456935: Fix dropdown menu limited to only two levels, as the problem persists regardless of that patch.

rajab natshah’s picture

Status: Active » Needs work

Thanks, Odeh, for reporting.
I faced the issue in https://www.drupal.org/project/varbase_components/issues/3456935#comment...

Noted;
Let us have this fixed with a merge request.
Maybe in both Varbase Components and Vartheme BS5

rajab natshah’s picture

rajab natshah’s picture

Title: Error: Uncaught TypeError: Popper__namespace.createPopper is not a function when adding a dropdown in the navbar » Fix Popper.js Error When Adding Navbar Dropdown
rajab natshah’s picture

Title: Fix Popper.js Error When Adding Navbar Dropdown » Fix Popper.js Error When Adding Navbar Dropdown in Vartheme BS5
rajab natshah’s picture

Assigned: Unassigned » rajab natshah
Status: Needs work » Active
rajab natshah’s picture

Progress findings:

The Responsive Theme Preview module is loading its Popper.js library

dropdown.js and tooltip.js are allocating the library by @popperjs/core

Not by it’s physical file location and name space.

@popperjs/core@2.9.2 is working but 2.11.8 is not

(function (global, factory) {
  typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('@popperjs/core'), require('./base-component.js'), require('./dom/event-handler.js'), require('./dom/manipulator.js'), require('./dom/selector-engine.js'), require('./util/index.js')) :
  typeof define === 'function' && define.amd ? define(['@popperjs/core', './base-component', './dom/event-handler', './dom/manipulator', './dom/selector-engine', './util/index'], factory) :
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Dropdown = factory(global["@popperjs/core"], global.BaseComponent, global.EventHandler, global.Manipulator, global.SelectorEngine, global.Index));
})(this, (function (Popper, BaseComponent, EventHandler, Manipulator, SelectorEngine, index_js) { 'use strict';

Bootstrap 5.3.3 is using "@popperjs/core": "^2.11.8",

https://github.com/twbs/bootstrap/blob/main/package.json#L110C5-L110C33

The 3 file formats are:
  • esm (works with import syntax )
  • umd (works with
    tags or RequireJS)
  • cjs (works with require() syntax)
Using umd in Varbase Components, and Vartheme BS5 The umd format in Bootstrap is doing esm format. This is the issue.
rajab natshah’s picture

Issue summary: View changes
ahmad khader’s picture

hi @rajab_natshah
I have switched to bootstrap.bundle.min.js in my projects due to this error, along with other issues related to Bootstrap components. You can read more about it here: https://stackoverflow.com/questions/68750341/bootstrap-5-uncaught-typeer....

However, if you prefer to resolve the issue without transitioning to the bundled version, you must modify the global property from global["@popperjs/core"] to global. Popper in dropdown.js. This adjustment is necessary as it addresses the underlying issue. It is essential to ensure that the global properties are correctly defined and accessible. Utilizing string-based property access (global["@popperjs/core"]) is less common and may lead to complications if the global object properties are not configured as anticipated.

rajab natshah’s picture

Using bootstrap.bundle.min.js will definitely fix the issue.

But that is not the right way.

That will work if you did this in projects ), but not as it was designed and structured for!!

As we work with SDC - it was requested by Razem ( not to load duplicate libraries, - or not to load extra not-needed JS in components )
This is a step toward moving our work with SDC with React Components.

  • Issue #3423174: Added Bootstrap ~5.3.0 JavaScript libraries in Varbase Components and only attach them with needed SDC components
  • Issue #3423178: Moved all Bootstrap ~5.3.0 JavaScript libraries to Single Directory Components (SDC) in Varbase Components

We must find a way to make this work, maybe compile js files in our way.

rajab natshah’s picture

NOTICE: The Responsive Theme Preview module is loading its Popper.js library. ( @popperjs/core v2.11.6 )
It is using an old version of popper .. not the 2.11.8 one with Bootstrap 5.3.3

ahmad khader’s picture

@rajab_natshah,
Hmmm, It's differently an issue but I think it's unrelated to this error since disabling the Responsive Theme Preview module won't fix the error.

I added a patch to test global object properties fix.

rajab natshah’s picture

That will work for ones, but when we update bootstrap?
it is a nodejs package too ( not a composer one )

The right way to do this is in the webpack.config.init.js
a JS script to re-compile, string manipulate the dropdown.js and tooltip.js
as in this comment

This will allow this to work in Vartheme BS5 and any custom generated cloned theme

we need a commend for updates .. like drush updb but it could be something like drush var-update-theme mytheme

Now the command to do this is yarn theme:init

I will continue with your fix idea .. but in the webpack.config.init.js file

rajab natshah’s picture

Issue summary: View changes
rajab natshah’s picture

rajab natshah’s picture

Issue summary: View changes

  • rajab natshah committed fd9e8c3e on 3.0.x
    Issue #3469222: Fix Popper.js Error When Adding Navbar Dropdown in...
rajab natshah’s picture

rajab natshah’s picture

Steps to follow to run the Bootstrap Fixer in old projects

Open the terminal and change directory to the custom theme

cd MY_PROJECT/themes/cusotm/mytheme

Get the Bootstrap Fixer file.

wget https://git.drupalcode.org/project/varbase_components/-/raw/2.0.x/bootstrap.fixer.js

Change "theme:init" in package.json to "theme:init": "webpack --progress --config webpack.config.init.js ; node bootstrap.fixer.js",

Add "bootstrap:fixer": "node bootstrap.fixer.js", to "scripts" in the package.json file.

The package.json file should look as in this link

run the following:

yarn bootstrap:fixer
rajab natshah’s picture

Issue summary: View changes
rajab natshah’s picture

Issue summary: View changes
Issue tags: +varbase-10.0.2, +vartheme_bs5-3.0.3
rajab natshah’s picture

Status: Needs review » Fixed
rajab natshah’s picture

Issue summary: View changes

✅ Released vartheme_bs5-3.0.3

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

rajab natshah’s picture

Issue summary: View changes

✅ Released varbase-10.0.2