with-style-mixin
v0.1.3
Published
An Ember mixin to allow you to bind style properties on your view/controller/whatever properties. Very simple live demo available [here](http://huafu.github.io/with-style-mixin/)
Readme
with-style-mixin 
An Ember mixin to allow you to bind style properties on your view/controller/whatever properties. Very simple live demo available here
Installation
npm install --save-dev with-style-mixin
Using
To use in your app with any view, import and use the mixin:
import Ember from 'ember';
import WithStyleMixin from 'with-style-mixin/mixins/with-style';
export default Ember.View.extend(WithStyleMixin, {
styleBindings: [
'width[px]', 'color', 'fontSize:font-size[em]', 'margin[px]',
'show:display?block:none', 'visible:visibility?:hidden'
],
width: 10, // => 'width: 10px;'
color: 'red', // => 'color: red;'
fontSize: '3pt', // => 'font-size: 3pt;'
margin: 0, // => 'margin: 0;'
show: true // => 'display: block;' (if true => 'display: block;')
visible: false // => visibility: hidden; (if true => '')
});You'll then get that in the generated HTML:
<div style="width: 10px; color: red; font-size: 3pt; margin: 0; display: block; visibility: hidden;"></div>...well, ok it doesn't make sense as a style but it is to show the different features.
You can also use the {{bind-style ...}} helper if you need to bind styles on any other element than
the main element of a view. It's working exactly the same thinking that you give as arguments each entry
you'd put in the view's styleBindings property:
<div {{bind-style 'width[px]' 'color' 'fontSize:font-size[em]' 'margin[px]'
'show:display?block:none' 'visible:visibility?:hidden'}}>
<p>hello!<p>
</div>You can also combine all in one string, separating each of them with a space:
<div {{bind-style 'width[px] color fontSize:font-size[em] margin[px]'
'show:display?block:none visible:visibility?:hidden'}}>
<p>hello!<p>
</div>API
- All style properties have to be defined on
styleBindingsas an array - Each property can be:
- The name of the css property:
width(thewidthEmber property will then be used as source forwidthcss property)
- The name of your Ember property +
:+ the name of the css property:myWidth:width(themyWidthEmber property will then be used as source forwidthcss property)
- One of the 2 above + a unit between
[and]:width[px](thewidthEmber property will then be used as source forwidthcss property, appendpxat the end of the value)myWidth:width[px](themyWidthEmber property will then be used as source forwidthcss property, appendpxat the end of the value)
- One of the 3 above (well 4 exactly) +
?+ the value to use if truthy +:+ the value to use if falsy:display?block:none(blockwill be used ifdisplayis truthy, elsenone)isVisible:display?:none(nothing used ifisVisibleis truthy,noneis used ifisVisibleis falsy)isLarge:line-height[px]?30:20(line-heightwill be30pxifisLargeis truthy, else20px)
- The name of the css property:
- If the property value is
undefined,nullor''(empty string), it'll not be included in thestyle - When a unit is specified, it'll be appended to the value unless the value is
0or not numeric, which allow you to do:width[px]:widthis0:width: 0;widthis10or'10':width: 10px;(works also with float values)widthis'10%':width: 10%;
