Less String Interpolation not working - string

Hello I have some less code and I'm trying to leverage less to get the following:
What I'm expecting:
#media only screen {
#testphone {
display: block;
background-color: blue;
}
}
My code:
#testPhone {
.testPhone({
& {
display: block;
background-color: blue;
}
});
}
.testPhone( #rules ){
.startQuery();
.screenOnly( #myQuery );
.create( #newQuery; #rules );
}
.create( #newQuery; #rules ){
#{newQuery} {
#rules();
}
}
.screenOnly( #myQuery ){
#newQuery: "#{myQuery} only screen";
}
.startQuery(){
#myQuery: "#media";
}
What I'm getting:
#testPhone "#media only screen" {
display: block;
background-color: blue;
}
What I think is going wrong:
In the .create mixin when I call #{newQuery} to interpolate #newQuery variable, it isn't removing the string off of that variable.

Related

Generate SASS mixins with gulp-svg-sprite?

Im using the gulp-svg-sprite plugin.
https://github.com/jkphl/gulp-svg-sprite
https://github.com/jkphl/svg-sprite
I already have my classes and styles which I would like to sprite:
.header {
background: grey;
&:after {
content: "";
display: block;
height: 30px;
width: 30px;
background: url(images/icon1.svg);
}
}
This is my gulp task:
spriteConfig = {
mode : {
css : {
bust : true,
render : {
scss : true
}
}
}
};
gulp.task('sprite', function() {
gulp.src('images/svg/*.svg')
.pipe(svgSprite(spriteConfig))
.pipe(gulp.dest('dest/'));
});
The task generates this type of SASS:
%svg-common {
background: url("svg/sprite.css-c3700f6a.svg") no-repeat;
}
.svg-icon1 {
#extend %svg-common;
background-position: 50% 0;
}
.svg-icon1-dims {
width: 1024px;
height: 348px;
}
This isnt ideal as I need to import these svg- classes which I wont use on there own, and I then need to use 2 extends:
.header {
background: grey;
&:after {
content: "";
display: block;
#extend .svg-icon1;
#extend .svg-icon1-dims;
}
}
Is there a way of generating mixins instead so I could jsut have something like:
.header {
background: grey;
&:after {
content: "";
display: block;
#include svg-icon1;
}
}
As per the docs:
It comes with a set of Mustache templates for creating stylesheets in
good ol' CSS or one of the major pre-processor formats (Sass, Less and
Stylus). Tweaking the templates or even adding your own custom output
format is really easy, just as switching on the generation of an HTML
example document along with your sprite.
Have a look and customize the following file:
https://github.com/jkphl/svg-sprite/blob/master/tmpl/css/sprite.scss
Danny H was correct. Here is my code. Notice that ive also used a prefix in my spriteConfig.
spriteConfig = {
mode : {
css : {
bust : true,
prefix : "#mixin sprite-%s",
render : {
scss: {
template: 'sprite.scss.handlebars'
}
}
}
}
};
In sprite.scss.handlebars:
{{#hasMixin}}#mixin {{mixinName}} {
background: url("{{{sprite}}}") no-repeat;
}
{{#hasCommon}}.{{commonName}} {
#include {{mixinName}};
}
{{/hasCommon}}{{/hasMixin}}{{^hasMixin}}{{#hasCommon}}.{{/hasCommon}}{{^hasCommon}}#mixin {{/hasCommon}}{{commonName}} {
background: url("{{{sprite}}}") no-repeat;
}
{{/hasMixin}}{{#shapes}}{{#selector.shape}}{{expression}}{{^last}},
{{/last}}{{/selector.shape}} {
{{^hasCommon}}{{#hasMixin}}#include {{mixinName}};{{/hasMixin}}{{^hasMixin}}#include {{commonName}};{{/hasMixin}}
{{/hasCommon}}background-position: {{position.relative.xy}};{{#dimensions.inline}}
width: {{width.outer}}px;
height: {{height.outer}}px;{{/dimensions.inline}}
width: {{width.outer}}px;
height: {{height.outer}}px;
}
{{/shapes}}

compile less preserving blank lines

I have the following string:
/* file: /Users/andrepadez/develop/vigour-io/gaston/test/to-compile/src/styles.less */
.main {
color: white;
}
/* file: /Users/andrepadez/develop/vigour-io/gaston/test/to-compile/global.less */
body {
background-color: black;
}
/* file: /Users/andrepadez/develop/vigour-io/gaston/test/to-compile/src/module/styles.less */
.module {
background: blue;
}
/* file: /Users/andrepadez/develop/vigour-io/gaston/test/to-compile/some-other-global.less */
body {
color: red;
}
and the following code:
var less = require('less');
less.render(str)
.then(function(output){
console.log(output.css);
});
which gives me:
/* file: /Users/andrepadez/develop/vigour-io/gaston/test/to-compile/src/styles.less */
.main {
color: white;
}
/* file: /Users/andrepadez/develop/vigour-io/gaston/test/to-compile/global.less */
body {
background-color: black;
}
/* file: /Users/andrepadez/develop/vigour-io/gaston/test/to-compile/src/module/styles.less */
.module {
background: blue;
}
/* file: /Users/andrepadez/develop/vigour-io/gaston/test/to-compile/some-other-global.less */
body {
color: red;
}
I need my output.css to preserve the original blank lines. Is there an option i could pass to the render function to do this?

LESS mixin as function parameter

..................................................
Existing mixins:
.mixin_1 {
height: 1px;
}
.mixin_2 {
height: 2px;
}
.mixin_3 {
height: 3px;
}
.function(#get) {
#get();
}
CALL:
.a{
.function(mixin_1);
}
.b{
.function(mixin_2);
}
.c{
.function(mixin_3);
}
RESULT:
.a{
height: 1px;
}
.b{
height: 2px;
}
.c{
height: 3px;
}
Question: how to do this? It's possible with current language specifications?
You cannot currently do a dynamic call to a mixin based off a variable directly. You can make your function() mixin into a "caller" or "getter" mixin in which you register the mixins that can be called by your function() mixin, like so (which utilizes pattern matching):
LESS
.function(#get) {
.-(#get); //call for mixin
//register mixins you want to call with function
.-(mixin_1) { .mixin_1; }
.-(mixin_2) { .mixin_2; }
}
.mixin_1 {
height: 1px;
}
.mixin_2 {
height: 2px;
}
#block {
.function(mixin_1);
}
Outputs
.mixin_1 {
height: 1px;
}
.mixin_2 {
height: 2px;
}
#block {
height: 1px;
}
Of course, if you want the mixins invisible to the css, then change them to this:
LESS change (added parenthesis)
.mixin_1() {
height: 1px;
}
.mixin_2() {
height: 2px;
}
New Output
#block {
height: 1px;
}
Do you need it?
That level of abstraction can be useful at times, but often simply a pattern matching on the mixins will suffice. You would have to determine that. So with this simple example, it would be better to reduce to something like this:
.setHeight(1) {
height: 1px;
}
.setHeight(2) {
height: 2px;
}
#block {
.setHeight(1);
}
More complex examples of mixins may not be so easily reduced, and then a mixin like what you want may be useful.
I think your code do same thing like this:
.mixin_1() {
height: 1px;
}
.mixin_2() {
height: 2px;
}
#block {
.mixin_1();
}
And output will be:
#block {
height: 1px;
}
I was asking about little different thing. However I found solution:
.function(#get) {
.-(#get); //call for mixin
//register mixins you want to call with function
.-(mixin) { .mixin; }
}
#block {
.mixin() {
height: 1px;
}
.function(mixin);
}
#block {
.mixin() {
height: 2px;
}
.function(mixin);
}
It outputs exactly what i wanted:
#block {
height: 1px;
}
#block {
height: 2px;
}
Overall, thanks for help.

Is it possible to use a mixin for browser-specific CSS

I'm looking for a solution to use a mixin for browser-specific CSS hacks.
I'm using JavaScript to add the browser tag in the HTML class. Like .ie .ie7 .ie8 .ie9
I would like to use the mixin like:
.box-test {
margin: 10px;
#include browser(ie7) {
margin: 20px;
}
}
DESIRED OUTPUT:
.box-test {
margin: 10px;
}
.ie7 .box-test {
margin: 20px;
}
the mixin i tried to make:
#mixin browser($browserVar) {
#if $browserVar == ie7 {
.ie7 { #content }
}
#else if $browserVar == ie8 {
.ie8 { #content; }
}
#else if $browserVar == ie9 {
.ie9 { #content; }
}
}
the problem is the output is:
.box-test {
margin: 10px; }
.box-test .ie7 {
margin: 20px; }
The absolute simplest mixin would be like so:
#mixin legacy-ie($ver: 7) {
.ie#{$ver} & {
#content;
}
}
Output:
.baz {
background: #CCC;
#include legacy-ie {
background: black;
}
}
If you wanted to emit styles that work for multiple IE versions at once without duplication, then this would be one way to do it:
$default-legacy-ie: 7 8 9 !default;
#mixin legacy-ie($versions: $default-legacy-ie) {
$sel: ();
#each $v in $versions {
$sel: append($sel, unquote('.ie#{$v} &'), comma);
}
#{$sel} {
#content;
}
}
.foo {
background: red;
#include legacy-ie {
background: green;
}
}
.bar {
background: yellow;
#include legacy-ie(7 8) {
background: orange;
}
}
Output:
.foo {
background: red;
}
.ie7 .foo, .ie8 .foo, .ie9 .foo {
background: green;
}
.bar {
background: yellow;
}
.ie7 .bar, .ie8 .bar {
background: orange;
}
If you want to be able to suppress all of the IE kludges all you need to add is one variable and an #if block:
$enable-legacy-ie: true !default;
#mixin legacy-ie($ver: 7) {
#if $enable-legacy-ie {
.ie#{$ver} & {
#content;
}
}
}
Set $enable-legacy-ie to false at the top of the file you don't want to have the IE specific styles, set it to true if you do want the styles included. You could easily write a reverse mixin to hide styles that old IE can't make use of so that the IE specific file stays nice and small.
You're overcomplicating things. :) It could be as simple as that:
.box-test {
margin: 10px;
.ie-7 & {
margin: 20px; } }
Result:
.box-test {
margin: 10px;
}
.ie-7 .box-test {
margin: 20px;
}
I have tried adding mixin for "#-moz-document url-prefix()" FF hack but it was not recognized by SASS and SASS was throwing error. so I think better solution is to create _hack.sass file and add css hacks which will not be compiled by SASS. I include this file whenever required.
#import "hack";
I am adding answer this as I feel it will be useful to someone who is struggling to get mozilla/safari hack works.

GXT3 Grid cell rendering

How can I render a grid column as multiline grid column using GXT 3 grids.
e.g
ColumnConfig<ExceptionEntry, String> name = new ColumnConfig<ExceptionEntry, String>(props.name(), 50, "Name");
name.setColumnStyle(new SafeStyles(){
#Override
public String asString() {
return // what styles to use for multiline rendering;
}
});
name.setCell(new AbstractCell<String>() {
#Override
public void render(com.google.gwt.cell.client.Cell.Context context,
String value, SafeHtmlBuilder sb) {
??? what needs to be done to render the column as multiline column
}
});
You can do that the easy and the hard way.
The easy one:
name.setCell(new AbstractCell<String>() {
#Override
public void render(com.google.gwt.cell.client.Cell.Context context, String value, SafeHtmlBuilder sb) {
sb.appendHtmlConstant("<div style=\"white-space: normal;\" >" + value + "</div>");
}
});
The hard (but much better) way:
1) Create custom GridAppearance to be used instead of default one from your theme:
import com.google.gwt.core.client.GWT;
import com.sencha.gxt.theme.base.client.grid.GridBaseAppearance;
public class YourGridAppearance extends GridBaseAppearance {
public interface YourGridStyle extends GridStyle {
}
public interface YourGridResources extends GridResources {
#Source({ "com/sencha/gxt/theme/base/client/grid/Grid.css", "YourGrid.css" })
#Override
YourGridStyle css();
}
public YourGridAppearance() {
this(GWT.<YourGridResources> create(YourGridResources.class));
}
public YourGridAppearance(YourGridResources resources) {
super(resources);
}
}
2) Copy /theme-you-use/client/grid/Grid.css to YourGrid.css and put it in the same folder where you've created YourGridAppearance class. Here is an example based on Grey theme:
#CHARSET "UTF-8";
.rowHighlight {
border: 1px dotted #535353;
}
.rowAlt .cell {
background-color: #FAFAFA;
}
.rowOver .cell {
background-color: #EEEEEE;
}
.cell {
border-color: #FAFAFA #EDEDED #EDEDED;
border-right: 0 solid #EDEDED;
font: 14px tahoma,arial,verdana,sans-serif;
}
.cellSelected {
background-color: #C9C9C9 !important;
color: #000000;
}
.cellInner {
white-space: normal;
line-height: 15px;
}
.columnLines .cell {
border-right-color: #EDEDED;
}
.rowOver .cell,.rowOver .rowWrap {
border-color: #DDDDDD;
}
.rowWrap {
border-color: #FAFAFA #EDEDED #EDEDED;
border-right: 0 solid #EDEDED;
border-style: solid;
border-width: 1px;
overflow: hidden;
}
.rowSelected .cell,.rowSelected .rowWrap {
background-color: #DFE8F6 !important;
border-color: #A3BAE9;
}
.footer {
background: #F7F7F7 none repeat scroll 0 0;
border-top: 1px solid #DDDDDD;
border-bottom: 1px solid #DDDDDD;
display: block;
overflow: hidden;
position: relative;
}
The most important part of it is this one:
.cellInner {
white-space: normal;
}
3) Replace default grid appearance with your custom one. To do that you have to add the following lines to your-project.gwt.xml:
<replace-with class="package.name.of.your.custom.theme.class.YourGridAppearance">
<when-type-is class="com.sencha.gxt.widget.core.client.grid.GridView.GridAppearance" />
</replace-with>

Resources