2
votes

I'm trying to understand if polymer is built for a specific use-case-- third party web components.

What I need to accomplish is create a web component that takes as input from the caller's page an image url (attributes on an element is ok) and inside the polymer component it renders the image in a special way using HTML5 canvas.

To me, it seems like polymer isn't currently built for third-party usage. Reasons why:

  • one must have enough control over the caller's page to add platform.js to the <head>, specifically the <head>
  • my version of platform.js could potentially be different than the caller page's platform.js (or bare minimum i'm polluting the page with polymer's JS objs, right?)
  • in non-chrome browsers style and other tags are injected into <head>, possibly conflicting with the source page
  • one must have control over the caller's <body> tag if wanting to set options to avoid FOUC

Traditionally all my web components have been built via iframes and i'd like to modernize my approach with a view towards a "shadow-dom future."

Is there a way to use polymer in a third-party safe way? Perhaps a mashup with [lightningjs?

1
Fwiw, if you ultimately do need and (can afford) the kind of armor that iframe can provide, my (untested) impression is that yes, lightningjs.com might be a way to go.Scott Miles
Does platform.js NEED to be in the head? So, it would be nice to create components that are "drop in" for external sites, like facebook's login component. I suppose you could provide snippets that could load polymer's js and embed the polymer tag element right after. Couldn't be any worse than pasting IFrame code for the consumer I suppose.drogon

1 Answers

2
votes

Polymer and Web Components are entirely structured around 3rd party usage, this is a central design pillar.

The broadest notion IMO is that developers will be able to go to the web and find numerous Web Components to choose from. This is not unlike being able to choose from an enormous set of JQuery plugins, but with a much greater degree of interoperability and composition because each instance can be treated as a traditional Element.

platform.js

Platform.js is modeling future browser capabilities called Web Components. There are practical realities of making this work right now, so yes, in order for a third party to use Web Components at all, they will need to opt-in in to platform.js (and all that entails). It's true that this fact makes it's difficult (today) to inject Web Components into somebody's page without their assent.

my version of platform.js could potentially be different than the caller page's

As above, platform.js is required upfront to use Web Components. This is why it's named the way it is. Unless the main page owner includes this capability, he's not providing a platform to which you can supply Web Components.

This is not dissimilar to modern libraries, e.g. JQuery. You can load numerous copies and/or versions of JQuery in one document if you aren't careful, but it's wasteful. Coordination is preferred.

With the exception of platform.js, Web Components is geared around N modules using M dependencies, and that all working together optimally. This is another way sharing is a pillar of the design.

in non-chrome browsers style and other tags are injected into , possibly conflicting with the source page

This all the price of polyfilling. If you need purity of environment, you will have to wait until Web Components are widely implemented natively. As a practical matter, the style tags are very specialized and are unlikely to conflict with anything.

one must have control over the caller's tag if wanting to set options to avoid FOUC

This is not strictly true, you can build Web Components that control their own FOUC up to a point. But it's a lot of extra work, and as a third-party, you really can't know what kind of loading mechanisms or idioms some developer is going to employ, so trying to orchestrate too much without his cooperation is going to be difficult.

Traditionally all my web components have been built via iframes

IFRAME is quite a bit different from Web Components. An IFRAME is a fresh context, and so you have a lot more safety net, but it's heavyweight and there are coordination costs.

Although platform.js, by it's very nature, is changing the shared platform, Custom Elements themselves need not mess with the user's global namespace or his CSS (although they can). Code can be restricted to the element's prototype, and CSS and DOM can be stashed inside ShadowDOM. The overall intent is that none of that need leak out of the Element, unless somebody wants it to.