Bootstrap.js not working in Polymer components
Asked Answered
V

3

7

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?

Vogeley answered 21/8, 2013 at 7:44 Comment(0)
I
9

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.

Ido answered 21/8, 2013 at 16:44 Comment(7)
Thanks ebidel! I'll have some experiments to see if I can get a dart version running.Vogeley
Btw, the demo in the stable branch seems not working on chrome and firefox in Linux, however the one in the master branch does.Vogeley
I had a similar problem, #18262096 -- the key points to get this working were a) load the bootstrap css file from a local location, and b) apply author styles via code, not markup.Reisman
The two links provided above seem to be stale. Here's the repo, for anyone looking: github.com/PolymerLabs/BootstrapGleda
It seems applyAuthorStyles is deprecated in newer versions. Here's another way: japhr.blogspot.com/2014/06/…Slew
what if I have multiple components that require bootstrap in this fashion, isn't it a huge redundancy?Rayford
is this still the case in polymer 1.0?Tapp
S
1

I managed to get bootstrap.js working for my polymer components by adding the lightdom attribute to their definition.

<polymer-element name="my-element-with-bootstrap-js" lightdom 
apply-author-styles> 
<template>
<div>fancy template markup here</div>
</template> 
<script type="application/dart" src="my-element-with-bootstrap-js.dart"></script> 
</polymer-element>

And the host HTML could look like this:

<!DOCTYPE html>
<html>
<head>
<title>Unarin</title>
<meta name="viewport"
  content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />


<link rel="import" href="elements/my-element-with-bootstrap-js.html">

<script type="application/dart">export 'package:polymer/init.dart';</script>
<script src="packages/browser/dart.js" type="text/javascript"></script>

<link rel="stylesheet"
  href="//netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css">
<script
  src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"
  type="text/javascript"></script>
<script
  src="//netdna.bootstrapcdn.com/bootstrap/3.0.2/js/bootstrap.min.js"
  type="text/javascript"></script>
</head>

<body>
  <my-element-with-bootstrap-js></my-element-with-bootstrap-js>
</body>
</html>

Please note: Using the lightdom attribute will replace your components from the shadow DOM to the light DOM, which can cause conflicts between your component's styling and the existing styles of the global context.

The related discussion: https://groups.google.com/a/dartlang.org/forum/#!topic/web-ui/ps6b9wlg1Vk

Based on the discussion I wasn't a 100% sure if the lightdom modifier will stay the standard way to achieve this kind of functionality for polymer components. Perhaps the authors could comment on that?

Solan answered 2/12, 2013 at 0:13 Comment(0)
M
1

The problem as pointed out in the other answers is the shadow dom. Bootstrap does not see the elements in there, and thus can not do its magic. You can disable the shadowdom and apply the author styles like this:

import 'package:polymer/polymer.dart';
import 'dart:html';

@CustomTag('main-messages')
class MainMessagesElement extends PolymerElement {
  MainMessagesElement.created() : super.created();

  //This enables the styling via bootstrap.css
  bool get applyAuthorStyles => true;
  //This enables the bootstrap javascript to see the elements
  @override
  Node shadowFromTemplate(Element template) {
    var dom = instanceTemplate(template);
    append(dom);
    shadowRootReady(this, template);
    return null; // no shadow here, it's all bright and shiny
  }   
}

You will need to remove the shadowdom for all parent elements as well.

WARNING: You will use the on-click event handler by using lightdom :(

Monarchy answered 16/1, 2014 at 15:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.