Bind variable to attribute of <paper-item-body> - data-binding

Is it possible to bind a variable to these <paper-item-body> attributes: two-line and three-line?
For example:
<paper-item>
<paper-item-body {{item.lineCount}}>
<div>Title</div>
<div secondary>Description</div>
</paper-item-body>
<iron-icon></iron-icon>
</paper-item>
where item.lineCount is 'two-line' or 'three-line'.
I have used data bindings on a target attribute in other areas (i.e., style$="color: {{myColor}};"), but that method doesn't seem to work for <paper-item-body>.
Thanks!

Attribute bindings (i.e., with $=) are bound to Boolean variables, where the attribute is set when the bound value is true, and removed when false.
Assuming item.enableTwoLines is a Boolean, you'd bind <paper-item-body>.twoLine like this:
<paper-item-body two-line$="[[item.enableTwoLines]]">
...
</paper-item-body>
HTMLImports.whenReady(() => {
Polymer({
is: 'x-foo'
});
});
<head>
<base href="https://polygit.org/polymer+1.7.0/components/">
<script src="webcomponentsjs/webcomponents-lite.min.js"></script>
<link rel="import" href="paper-item/paper-item.html">
<link rel="import" href="paper-item/paper-item-body.html">
<link rel="import" href="paper-checkbox/paper-checkbox.html">
</head>
<body>
<x-foo></x-foo>
<dom-module id="x-foo">
<template>
<style>
paper-checkbox {
margin: 20px;
}
paper-item-body {
border: solid 1px red;
--paper-item-body-two-line-min-height: 200px;
}
</style>
<paper-checkbox active="{{enableTwoLine}}">Enable 2-line paper-item</paper-checkbox>
<paper-item>
<paper-item-body two-line$="[[enableTwoLine]]">
<div>Profile Photo</div>
<div secondary>Change your Google+ profile photo</div>
</paper-item-body>
</paper-item>
</template>
</dom-module>
</body>
codepen

Related

Polymer - class doesn't load in template?

I have a custom class:
<link rel="import" href="../bower_components/polymer/polymer-element.html">
<link rel="import" href="shared-styles.html">
<dom-module id="my-voltage">
<template is="auto-binding">
<div class="circle">{{volts}}</div>
<div class="circle">{{time}}</div>
<div class="circle">{{temp}}</div>
</template>
<script>
//etc
</script>
</dom-module>
In the shared-styles.html file I have the definition for the circle class:
.circle {
display: inline-block;
width: 64px;
height: 64px;
text-align: center;
color: #555;
border-radius: 50%;
background: #ddd;
font-size: 30px;
line-height: 64px;
}
However, the class doesn't load. I don't get the circle around the text, nor anything else defined there. What am I missing? Can I not use classes in templates? Do I need to import it somehow else?
I'm pretty sure you want something like this:
<link rel="import" href="../bower_components/polymer/polymer-element.html">
<link rel="import" href="shared-styles.html">
<dom-module id="my-voltage">
<template is="auto-binding">
<style include="shared-styles">
/* any extra styles can go here */
</style>
<div class="circle">{{volts}}</div>
<div class="circle">{{time}}</div>
<div class="circle">{{temp}}</div>
</template>
<script>
//etc
</script>
</dom-module>
Note the <style> block.

How to overwrite polymer component style css

I want to change css of Polymer component paper-dropdown-menu. I want to change the width of inner element - iron-dropdown (or paper-listbox) that is in the shadow dom of dropdown.
Should I make new custom element and how to change css of this element?
I tried this:
<dom-module id="my-button">
<style is="custom-style">
paper-dropdown-menu {
--paper-dropdown-menu-ripple: {
width: 500px;
}
}
</style>
<template>
<content></content>
</template>
<script>
Polymer({
is: 'my-button'
})
</script>
</dom-module>
<my-button>
<paper-dropdown-menu label="Your favourite pastry">
<paper-listbox class="dropdown-content">
<paper-item>Croissant</paper-item>
<paper-item>Donut</paper-item>
<paper-item>Financier</paper-item>
<paper-item>Madeleine</paper-item>
</paper-listbox>
</paper-dropdown-menu>
</my-button>
Are you trying to use a button to trigger a dropdown menu like this? Then you can use 'paper-menu-button' element.
If you want to use just the paper-dropdown-menu then
You can use any of the paper-input-container and paper-menu-button
style mixins and custom properties to style the internal input and
menu button respectively.
To change width:
paper-dropdown-menu{
-paper-input-container: {
width: 100px;
}
}
To set the width of the paper-listbox, change your selector to paper-listbox:
paper-listbox {
width: 500px;
}
To set the widths of paper-listbox and paper-dropdown-menu to be the same:
paper-dropdown-menu,
paper-listbox {
width: 500px;
}
<head>
<base href="https://polygit.org/polymer+1.7.0/components/">
<script src="webcomponentsjs/webcomponents-lite.min.js"></script>
<link rel="import" href="paper-listbox/paper-listbox.html">
<link rel="import" href="paper-item/paper-item.html">
<link rel="import" href="paper-dropdown-menu/paper-dropdown-menu.html">
</head>
<body>
<x-foo></x-foo>
<dom-module id="x-foo">
<template>
<style>
paper-dropdown-menu,
paper-listbox {
width: 500px;
}
</style>
<paper-dropdown-menu label="Your favourite pastry">
<paper-listbox class="dropdown-content">
<paper-item>Croissant</paper-item>
<paper-item>Donut</paper-item>
<paper-item>Financier</paper-item>
<paper-item>Madeleine</paper-item>
</paper-listbox>
</paper-dropdown-menu>
</template>
<script>
HTMLImports.whenReady(function() {
Polymer({ is: 'x-foo' });
});
</script>
</dom-module>
</body>
codepen

Polymer element doesn't display background-image passed with Custom CSS properties

I'm having trouble understanding why I can't pass a custom CSS property for a background image to a polymer element. Using CCS properties for styling Polymer elements is explained here: https://www.polymer-project.org/1.0/docs/devguide/styling.html#xscope-styling-details and this works for some elements (example the grey background in my code below).
<body>
<dom-module id="my-element">
<template>
<style>
:host {
display: block;
height: 300px;
background-color: grey;
color: var(--container-text, white);
background-image: url(var(--container-background, 'http://www.inflexusmgmt.com/wp-content/uploads/2014/08/Hero-material-graphene-1.jpg'));
/* THIS WORKS
background-image: url( 'http://www.inflexusmgmt.com/wp-content/uploads/2014/08/Hero-material-graphene-1.jpg');
*/
}
</style>
<div class='container'>Text color is correct, but no background image</div>
</template>
<script>
Polymer({ is: 'my-element'});
</script>
</dom-module>
<my-element></my-element>
</body>
I have two question:
Why isn't this working?
What is the best way to solve this?
Code is here: http://jsbin.com/yuxobexepe/edit?html,output
Thanks!
Paul
You are mixing the definition/usage of CSS property. Here is a working JSBIN of your problem:
<html>
<head>
<base href="http://polygit.org/polymer/components/">
<script src="webcomponentsjs/webcomponents-lite.min.js"></script>
<link href="polymer/polymer.html" rel="import">
<link rel="import" href="polymer/polymer.html">
</head>
<body>
<dom-module id="my-element">
<template>
<style>
:host {
display: block;
height: 300px;
background-color: grey;
color: var(--container-text, blue);
background-image: var(--container-background, "default.png");
--container-background: url( 'http://www.inflexusmgmt.com/wp-content/uploads/2014/08/Hero-material-graphene-1.jpg');
}
</style>
<div class='container'>Text color is correct, but no background image</div>
</template>
<script>
addEventListener('WebComponentsReady', function() {
Polymer({ is: 'my-element'});
});
</script>
</dom-module>
<my-element></my-element>
</body>
</html>
background-image is using the value of --container-background if it exists, or default to what you give. Then you define separately the --container-background property, usually, outside of the component itself.

How do I use polymer 1.0 paper-styles colors?

How can I set the background-color of a paper-toolbar to --paper-teal-500?
<script src="bower_components/webcomponentsjs/webcomponents-lite.min.js"></script>
<link rel="import" href="bower_components/paper-styles/paper-styles.html">
<style is="custom-style">
paper-toolbar {
background-color: paper-teal-500;
}
</style>
</head>
<body style="margin: 0;">
<link rel="import" href="bower_components/paper-toolbar/paper-toolbar.html">
<paper-toolbar>
<div title>Tabara</div>
</paper-toolbar>
Well I managed to figure it out.
background-color: var(--paper-teal-500);
will do.
Hope it helps anyone in need!

Polymer: Layout screwed up in Firefox, fine in Chrome

So, I am aware the web components, Shadow DOM and such is only implememted natively in Chrome today.
For support in Firefox, Polyfill is needed. According to the website, Polymer has polyfill support in Firefox:
https://www.polymer-project.org/resources/compatibility.html
but when I have made a very simple page, it looks completely screwed up i Firefox. But, if I try the Polymer website in Firefox, it works there without any obvious problems.
Test URL: http://misc.snapcode.se/polymer/
Here is how my test-site looks in Chrome:
and in Firefox:
The code can be seen below.
But they say that Firefox has Polyfill support, and it supports CSS, so why is the layout/design so screwed up?
How come they get their own site to work in Firefox, but a super-simple site I build is screwed up?
If I try out the paper-dropdown in FF, it works fine on their demo site, but if I use the paper-dropdown on my own site, constructed the same way as described on the polymer website, its completely screwed up. Why?
What am I missing?
EDIT 1
I found out that to get the header panel "right" in Firefox, I have to remove the CSS in index.php for the div {...}:
That seems to me like the Shadow DOM isnt working correctly, even though I have imported webcomponents.js, which should be the Polyfill needed.
EDIT 2
I have looked in Firefox using Firebug and I can see the following:
As I can see it, webcomponents.min.js is imported (I tested different js files), and there is some stuff talking about ShadowDOMPolyfill. So, it is even weirder now I think.
EDIT 3
I debugged Firefox using Firebug, image below. As I see it, ShadowDOM using Polyfill is indeed detected and used. Do you agree? =)
index.php
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>LEO</title>
<script src="/components/webcomponentsjs/webcomponents.js"></script>
<link rel="import" href="/components/font-roboto/roboto.html">
<link rel="import" href="/components/core-header-panel/core-header-panel.html">
<link rel="import" href="/components/core-toolbar/core-toolbar.html">
<link rel="import" href="/components/core-icons/core-icons.html">
<link rel="import" href="/components/paper-shadow/paper-shadow.html">
<link rel="import" href="/components/paper-button/paper-button.html">
<link rel="import" href="/my-components/logout-button/logout-button.html">
<link rel="import" href="/my-components/assignment-card/assignment-card.html">
<style>
html, body {
height: 100%;
margin: 0px;
background-color: #E5E5E5;
font-family: 'RobotoDraft', sans-serif;
}
paper-shadow {
width: 300px;
background: #FFF;
margin: 10px;
padding: 10px;
}
div {
padding: 10px;
margin: 10px;
}
</style>
</head>
<body fullbleed layout vertical>
<?php
session_start();
if (!isset($_SESSION['session_userId']))
{
echo "Not logged in";
}
?>
<core-header-panel flex layout>
<core-toolbar>
<div flex>LEO 1</div>
<div>
<logout-button></logout-button>
</div>
</core-toolbar>
<div id="id1" horizontal layout >
<assignment-card></assignment-card>
</div>
</core-header-panel>
<script>
</script>
</body>
</html>
assignment-card.html
<link rel="import" href="/components/polymer/polymer.html">
<link rel="import" href="/components/core-icons/core-icons.html">
<link rel="import" href="/components/paper-button/paper-button.html">
<link rel="import" href="/components/core-item/core-item.html">
<link rel="import" href="/components/core-menu/core-menu.html">
<link rel="import" href="/components/core-dropdown/core-dropdown.html">
<link rel="import" href="/components/core-dropdown-menu/core-dropdown-menu.html">
<link rel="import" href="/components/paper-item/paper-item.html">
<link rel="import" href="/components/paper-menu/paper-menu.html">
<link rel="import" href="/components/paper-dropdown/paper-dropdown.html">
<link rel="import" href="/components/paper-dropdown-menu/paper-dropdown-menu.html">
<link rel="import" href="/components/core-ajax/core-ajax.html">
<link rel="import" href="/components/core-tooltip/core-tooltip.html">
<polymer-element name="assignment-card">
<template>
<style>
input {
padding: 10px;
font-family: 'RobotoDraft', sans-serif;
font-size: 16px;
margin: 0px;
}
core-icon[icon="error"] {
width: 40px;
height: 40px;
color: red;
}
core-icon[icon="perm-identity"] {
width: 40px;
height: 40px;
}
core-icon[icon="lock-outline"] {
width: 40px;
height: 40px;
}
core-icon[icon="arrow-forward"] {
color: #e4e4e4;
}
core-icon {
color: #808080;
}
paper-button {
background-color: #6fd177;
margin: 0px;
}
core-field {
margin-bottom: 10px;
}
div[id="container"] {
background: #C0C0C0;
padding: 5px;
}
div
{
margin: 10px;
font-size: 12px;
}
</style>
<div id="container" layout vertical >
<div layout horizontal>
<core-label >Starttid: 14:13</core-label>
<core-label flex></core-label>
<core-label >Uppdrags-id: 13213241</core-label>
</div>
<div><core-label>Kertin Karlsson,</core-label></div>
<div layout horizontal relative>
<paper-dropdown-menu raised label="-Välj" style='background: #fff; padding: 5px; margin: 0px; margin-right: 15px; ' flex>
<paper-dropdown class="dropdown" layered="true">
<core-menu class="menu">
<template repeat="{{assistant in assistants}}">
<paper-item name="{{assistant.id}}">{{assistant.name}}</paper-item>
</template>
</core-menu>
</paper-dropdown>
</paper-dropdown-menu>
<paper-button raised>Tilldela</paper-button>
</div>
</div>
<core-ajax
id="coreAjax1"
url="http://192.168.1.108/relay.php"
method="post"
params='{{json}}'
handleAs="json"
on-core-response="{{handleResponse}}">
</core-ajax>
</template>
<script>
Polymer('assignment-card', {
ready: function() {
this.assistants = [
{id: 1, name: 'Kalle'},
{id: 2, name: 'Ted'},
{id: 3, name: 'Micke'},
{id: 4, name: 'Bengt'},
];
}
});
</script>
</polymer-element>
logout-button.html
<link rel="import" href="/components/polymer/polymer.html">
<link rel="import" href="/components/core-icons/core-icons.html">
<link rel="import" href="/components/paper-button/paper-button.html">
<link rel="import" href="/components/core-ajax/core-ajax.html">
<polymer-element name="logout-button">
<template>
<style>
paper-button
{
background: #DF0101;
color: white;
}
</style>
<core-ajax
id="coreAjax1"
url="http://192.168.1.108/logout.php"
on-core-response="{{handleResponse}}">
</core-ajax>
<paper-button raised id="btnLogout" on-click="{{onLogoutClicked}}">Logga ut
<core-icon icon="highlight-remove"></core-icon>
</paper-button>
</template>
<script>
Polymer('logout-button', {
onLogoutClicked: function()
{
this.$.coreAjax1.go();
},
handleResponse: function(e)
{
document.location.href = '/index.php';
}
});
</script>
</polymer-element>
The problem persist because firefox does not create shadow DOM but it displays shadow content directly. So the following snippet screws the overall view:
paper-shadow {
width: 300px;
background: #FFF;
margin: 10px;
padding: 10px;
}
div {
padding: 10px;
margin: 10px;
}
If you remove that code and add some specific code then it will work. The snippet for index.php with specific code is given below.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>LEO</title>
<script src="/components/webcomponentsjs/webcomponents.js"></script>
<link rel="import" href="/components/font-roboto/roboto.html">
<link rel="import" href="/components/core-header-panel/core-header-panel.html">
<link rel="import" href="/components/core-toolbar/core-toolbar.html">
<link rel="import" href="/components/core-icons/core-icons.html">
<link rel="import" href="/components/paper-shadow/paper-shadow.html">
<link rel="import" href="/components/paper-button/paper-button.html">
<link rel="import" href="/my-components/logout-button/logout-button.html">
<link rel="import" href="/my-components/assignment-card/assignment-card.html">
<style>
html, body {
height: 100%;
margin: 0px;
background-color: #E5E5E5;
font-family: 'RobotoDraft', sans-serif;
}
assignment-card,
logout-button{
margin: 10px;
}
logout-button paper-button{
top: 3px;
}
</style>
</head>
<body fullbleed layout vertical>
<?php
session_start();
if (!isset($_SESSION['session_userId']))
{
echo "Not logged in";
}
?>
<core-header-panel flex layout>
<core-toolbar>
<div flex>LEO 1</div>
<div>
<logout-button></logout-button>
</div>
</core-toolbar>
<div id="id1" horizontal layout >
<assignment-card></assignment-card>
</div>
</core-header-panel>
<script>
</script>
</body>
</html>

Resources