Bootstrap.js not working in Polymer components

Issue

I’m currently working on translating our current Dart Web UI project to Polymer.dart. We use Bootstrap 3 for the front-end styling and many web animations (e.g. dropdown menus, modals, tooltips).

However after I translated one Web UI component into Polymer, all the Bootstrap JavaScript stopped working for the sub component, since it was now encapsulated in the ShadowDOM. To access an element in the ShadowDOM from Dart I have to use the shadowRoot field, and from JavaScript in Chrome I have to use the webkitShadowRoot property, but still I can’t get a single dropdown menu to work properly.

Firstly, after calling the dropdown(‘toggle’) API in bootstrap, the dropdown menu appears but never goes away. Also, it seems impossible to detect which link is triggered by the click event since the event is fired on the window level. This means I can’t find which dropdown menu to display.

Does anyone have experience using twitter-bootstrap and Polymer together?

Solution

Your questison is about the Polymer.dart port, but applies to Polymer.js devs as well 🙂

As you point out, the issue is with Bootstrap’s JS. It doesn’t know about ShadowDOM and attempts to fetch nodes from the DOM using global selectors. For example, in bootstrap-carousel.js, I see things like:

$(document).on('click.carousel.data-api', ...', function (e) { ...});

We explored using Bootstrap components inside of Polymer a little bit ago:
https://github.com/Polymer/more-elements/tree/stable/Bootstrap

The most interesting to you would be <bs-accordion-item> (Demo)

The process is basically to wrap Bootstrap stuff inside a custom element and call into their APIs.

For the CSS: if you include their stylesheet inside your element, things should generally work:

<polymer-element name="my-element">
  <template>
    <link rel="stylesheet" href="bootstrap.css">
    ...
  </template>
  <script>...</script>
</polymer-element>

Keep in mind that if bootstrap.css is coming from a CDN, it needs to be CORS-enabled for the polyfills to work properly. This requirement goes away when native HTML Imports lands.

Answered By – ebidel

Answer Checked By – Clifford M. (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *