In my project I need to support Arabic and English, to do so I do the following:
<div class="{{ {'align-right': currentLanguageCode == 'ar'} | tokenList }}">{{languages[currentLanguageCode].supervisorName}}</div>
The above line will be repeated for every text rendering.
So I thought it will be better if I could do something like this:
bilingual-text.html
<polymer-element name="bilingual-text" attributes="languageCode">
<template>
<content></content>
</template>
<script>
Polymer({
languageCodeChanged: function() {
if (this.languageCode == 'ar') {
// add .align-right class.
} else {
// remove .align-right class.
}
}
})
</script>
</polymer-element>
Then
<bilingual-text languageCode="ar">{{languages['ar'].supervisorName}}</bilingual-text>
Is there any way to implement something like that?
Polymeric way
However, a Polymeric way to approach this is to use reflect: true when publishing the languageCode property. In that way, you can style this element based on this attr and/or it's value:
:host([languageCode="ar"]) {
direction: rtl;
}
Here's a working demo:
<script src="http://www.polymer-project.org/platform.js"></script>
<script src="http://www.polymer-project.org/polymer.js"></script>
<polymer-element name="bilingual-text" attributes="languageCode">
<template>
<style>
:host {
display: block;
}
:host([languageCode="ar"]) {
direction: rtl;
}
</style>
<content></content>
</template>
<script>
Polymer({
publish: {
languageCode: {value: null, reflect: true}
}
});
</script>
</polymer-element>
<bilingual-text>I'm LTR</bilingual-text>
<bilingual-text languageCode="ar">I'm RTL</bilingual-text>
Traditional way
Define languageCodeChanged() and call:
this.classList.toggle('align-right', this.languageCode == 'ar');
Related
Not sure if this is possible but I'm trying to display a div if another div which doesn't share the same parent is hovered.
The html looks something like this:
<div class="test">
<div class="hover-me"><p>Hover</p></div>
</div>
// some other content here
<div class="hover-content">
<p>hovered content</p>
</div>
I've tried using
.test:hover + .hover-content {
display: block;
}
But I think this only works if there's no other content in-between? Any suggestions?
Use javascript to listen to the onmouseover event, or jquery to handle the hover event on one and change the display attribute of the other. Using jquery
<script>
$(document).ready(function () {
$(".hover-me").hover(function () {
$(".hover-content").show();
}, function() {
$(".hover-content").hide();
});
});
</script>
If you don't want to use jquery, change your html like so
<div class="test">
<div class="hover-me"
onmouseover="document.getElementById('hover-content').style.display = 'block';"
onmouseout="document.getElementById('hover-content').style.display = 'none';">
<p>Hover</p></div>
</div>
// some other content here
<div class="hover-content" id="hover-content">
<p>hovered content</p>
</div>
notice that I added an id attribute to the hover-content div.
Try this, i think it will help you :
<script>
$(document).ready(function () {
$( ".hover-me" ).mouseenter( function () {
$( ".hover-content" ).show();
}).mouseout(function () {
/*anything you want when mouse leaves the div*/
} );
});
</script>
So you want to display the .hover-content when you hover the test. You can try the following solution. If it does not work, you gotta use javascript to check for the mouseover event. Hope it helps!
.test:hover ~ .hover-content {
display: block;
}
I am trying to figure out how I can define a dynamic class in a parent element, then set the name of the class on a property of a client element and then have the client element use the style for something.
So what options do I have?
Cheers
The example I am trying to beat into shape is here:
<!doctype html>
<html>
<head>
<script src='bower_components/webcomponentsjs/webcomponents-lite.js'></script>
<link rel='import' href='bower_components/polymer/polymer.html'>
</head>
<body unresolved>
<dom-module id='my-element'>
<style>
/* Here is where I would like to dynamically add this
.somerandomname {
#apply(--the-class);
}
where 'somerandomname' is just some name tying to together with the div below
*/
.somerandomname {
#apply(--the-class);
}
</style>
<template>
<!--
Below the class will actually be class$ allowing data binding to the somerandomname
A note here is that there will be many tags that needs mixins and I do not know in advance
what they are called, but the data provided to the custom element includes the class that should
be used for the tag
-->
<div class='somerandomname'>Show me the color</div>
</template>
</dom-module>
<script>
Polymer({
is: 'my-element',
properties: {
myclass: {
type: String,
value: ''
}
}
});
</script>
<dom-module id='base-page'>
<style>
my-element {
--the-class: {
background-color: red;
};
}
</style>
<template>
<my-element myclass='--the-class'></my-element>
</template>
</dom-module>
<script>
Polymer({
is: 'base-page'
});
</script>
<base-page></base-page>
</body>
It seems you're a bit confused about how mixins and custom css properties, I hope this helps with that.
First of all, you don't really need any Polymer properties to make this thing work, so, the myclass property in my-element's definition isn't really needed for this.
Considering that, you naturally don't need the myclass="--the-class" bit either, everything should work fine without those two things.
Then again, bear in mind that since you defined in base-page's style that all my-element children will have that mixin applied to them it
Here's a slightly modified version of your code which should illustrate how the mixins are applied (and here's a working fiddle for it)
<dom-module id="my-element">
<template>
<style>
.customized {
#apply(--customized-div);
}
</style>
<div class="customized">
This div is customized
</div>
<div>
This div isn't customized
</div>
</template>
</dom-module>
<dom-module id="base-page">
<template>
<style>
my-element { /*This applies to all my-element children*/
--customized-div: {
color: red;
};
}
my-element.green { /*This only applies to those with the green class*/
--customized-div: {
color: green;
};
}
</style>
Child 1
<my-element></my-element>
<br>
Child 2
<my-element class="green"></my-element>
</template>
</dom-module>
<script>
Polymer({
is: 'my-element'
});
Polymer({
is: 'base-page'
});
</script>
<base-page></base-page>
I've got a question related to styling in Polymer. I have a responsive element which has different stylings depending on the width of the screen. If the width is 720 or greater, it should have the styling that is marked with "--specific styling 2--". If it isn't, it should have the "--specific styling 2--". It should always have the "--common styling--".
<dom-module>
<template>
<style>
:host {
--common styling--
}
</style>
<iron-media-query query="min-width: 720px" query-matches="{{isWide}}"></iron-media-query>
<template is="dom-if" if="{{!isWide}}">
<style>
:host {
--specific styling 1--
}
</style>
</template>
<template is="dom-if" if="{{isWide}}">
<style>
:host {
--specific styling 2--
}
</style>
</template>
</template>
</dom-module>
If I have the code as above, it always uses the "--specific styling 2--". Is there some way to change this to the wanted behaviour?
Bart
I have core-animated-pages element in my polymer-element:
<link rel="import" href="../public/components/core-animated-pages/core-animated-pages.html">
<polymer-element name="welcom-arrow" attributes="counter">
<template>
<style>
</style>
<core-animated-pages transitions="cross-fade-all" selected="{{welcomPage}}">
<section>
<div class="blueBackground">
<h1>asd</h1>
<h3>asdasdasdn</h3>
<paper-button on-tap="{{Foward}}">
Keep Going
<core-icon icon="forward"></core-icon>
</paper-button>
</div>
</section>
<section>
<div class="blueBackground">
welcom 2
</div>
</section>
</core-animated-pages>
</template>
<script>
Polymer({
welcomPage: 0,
Foward: function() {
this.welcomPage++;
},
Backward: function() {
this.welcomPage--;
}
});
</script>
</polymer-element>
my cross-fade-all transition works perfectly but when I want to change it to a slide effect by swapping the cross-fade-all attribute to slide-from-right the effect is not working. How can I fix this?
You just need to import the transition itself.
<link rel="import" href="../public/components/core-animated-pages/transitions/slide-from-right.html">
And now you can change your transition
<core-animated-pages transitions="slide-from-right" selected="{{welcomPage}}">
The reason why the previous transition worked, is because core-animated-pages already import it.
Hope it helped !
I am building an element with Polymer with this structure:
<template>
<style>
...
</style>
<div class="card-header" layout horizontal center>
<content select="img"></content>
<content select="h1"></content>
</div>
<content></content>
</template>
<script>
Polymer({});
</script>
</polymer-element>
I try to set a default value for the src attribute of the <content select="img"> in case its not set deliberately inside the <e-card> element:
...
<script>
Polymer({
srcimg: 'default-picture.png'
});
</script>
...
On browser the value in the script is applied in the <content select="img"> at the Shadow DOM but not in the <img> element inside the <e-card>.
How can I apply the default value for the src attribute in the <img> element?
it might be easier to just go ahead and make your image tag in the element and publish the the src attribute out side your element. then you could set the default path in js and still have the content be user configurable.
<polymer-element name="test-element" attributes="img height width">
<template>
<style>
...
</style>
<div class="card-header" layout horizontal center>
<img src="{{img}}" height="{{height}}" width="{{width}}">
<content select="h1"></content>
</div>
<content></content>
</template>
<script>
Polymer({
ready: function () {
this.img = "default image url";
this.height = "100px";
this.width = "100px"
}
});
</script>
</polymer-element>