Open iSuslov opened 7 years ago
@iSuslov I was able to get this working in 2.0, but I could not get the class mixin functionality to work which lead me here. I have no experience with Polymer 1.0 so it took a bit of digging and trial to find that 1.0 syntax works. In any case here's an element that wraps google-chart to make it responsive.
<link rel="import" href="../bower_components/polymer/polymer-element.html">
<link rel="import" href="../bower_components/iron-resizable-behavior/iron-resizable-behavior.html">
<link rel="import" href="../bower_components/google-chart/google-chart.html">
<link rel="import" href="shared-styles.html">
<dom-module id="google-chart-responsive">
<template>
<style include="shared-styles">
#googleChart {
height: 500px;
width: 100%;
}
</style>
<google-chart
type='area'
id='googleChart'
options='{"title": "Distribution of days in 2001Q1"}'
data="[[data]]">
</google-chart>
</template>
<script>
let tm = null
Polymer({
is: 'google-chart-responsive',
behaviors: [
Polymer.IronResizableBehavior
],
properties: {
data: {
type: Array,
value: [],
},
},
listeners: {
'iron-resize': '_onIronResize'
},
attached: function() {
this.async(this.notifyResize, 1);
},
get parent() {
if (this.parentNode.nodeType === Node.DOCUMENT_FRAGMENT_NODE) {
return this.parentNode.host;
}
return this.parentNode;
},
_onIronResize: function() {
if (tm) { return }
tm = setTimeout(() => {
this.$.googleChart.redraw()
clearTimeout(tm)
tm = null
}, 50)
}
});
</script>
</dom-module>
@ralphsmith80 yep, in hybrid mode it will work, but in 2.0 mode it's not
Definitely need new documentation to explain how to use this properly in 2.0 with mixins
@iSuslov @esd100
https://meowni.ca/posts/polymer-2-cheatsheet/
I refer to her cheat sheet often and I found her section on mixins more useful than the docs :)
If someone got this to work in 2.0, can you share your changes?
@yebrahim I am using it like so in my 2.0 elements
class MyElement extends Polymer.mixinBehaviors([Polymer.IronResizableBehavior], Polymer.Element) {
static get is() { return 'my-element'; }
}
@BootsSiR Are you then adding a listeners
property with iron-resize
event handler? Doing so didn't trigger the event for me.
@yebrahim Sorry, my particular implementation involves manually calling notifyResize
@yebrahim mb its a bit too late for this answer, but still. In the Polymer 2.0 there are no more listeners
property (see https://www.polymer-project.org/2.0/docs/upgrade#removed-apis). Now you have to add listeners manually in connectedCallback
:
class MyElement extends Polymer.mixinBehaviors(Polymer.IronResizableBahavior, Polymer.Element) {
...
connectedCallback() {
super.connectedCallback();
this.addEventListener('iron-resize', e => console.log('handled!'));
}
}
I was able to get it to work with Polymer 2.0
I added this to my bower.json:
"iron-resizable-behavior": "PolymerElements/iron-resizable-behavior#^2.1.0”
Then I added the import at the top of my component file:
<link rel="import" href="/components/iron-resizable-behavior/iron-resizable-behavior.html">
Then in my class declaration I added the behavior:
class MyElement extends Polymer.mixinBehaviors([Polymer.IronResizableBehavior], Polymer.MutableData(Polymer.Element)) {
I added the ready function with an event listener that called a function when the event was fired:
ready() { super.ready(); this.addEventListener("iron-resize", this._resizeAware); }
I defined my function to log the width every time the function was fired:
_resizeAware() { console.log("Width is now: " + this.offsetWidth); }
Please suggest if iron-grid is a good for grid system to make responsive app ? https://github.com/The5heepDev/iron-grid
Polymer.IronResizableBehavior not working. No documentation. App crashes when use it like this:
Polymer.mixinBehaviors([Polymer.IronResizableBehavior], Polymer.Element)