📜 ⬆️ ⬇️

Buttons do not happen much

Less About six months ago I read the article “Why use preprocessors” . In it, the author describes the advantages of preprocessors on the example of the implementation of custom buttons. The article turned out to be informative and very interesting. The author cited various preprocessors as examples and explained why he stopped at Sass. On account of the color of pencils and taste, I have no right to argue, everyone has their own views. But even then, the description that Less was not powerful enough seemed to me to be unfair. Even then, I wanted to do something similar, to embody the idea of ​​the author in other preprocessor languages ​​(Less, Stylus). At the time of reading the article, there was no time to do something with it. And just recently, one weekend, I remembered this article and decided to implement the idea of ​​the author - only with Less. I didn’t see the point of doing something new, so I decided to help the author in the development of the project, made a fork from his repository, did the necessary work and created a pull request.

Less or Sass or Stylus or something else there?


It all depends on the specifics of your project and on your needs. If you are not familiar with any of the above preprocessor languages, it's time to start to get acquainted. Not only will you discover many new and interesting things, so also get a good level up. In our projects we use Less. And you know, for two and a half years, I never thought that Less was deprived of functionality, speed, or something else there. Yes, in the first implementations it was damp and demanded serious improvements, but at the moment it is very functional and you can implement a lot of your ideas on it, you just need to read the documentation and combine the features that this preprocessor language offers us. I often hear that preprocessors are hard, they only make it difficult to write code, so additional tools are needed to use them. Do not listen to anyone and start using preprocessors.

Advantages of preprocessors


Variables, mixins, imports, nesting and more. It’s not for nothing that I was the first to call variables, mixins, imports, and nesting - they constitute the majority of the entire code, all other features of this or that preprocessor language complement them. This fun four allows us to easily implement the principle of DRY , to make the support of the code simpler and easier, to get rid of unnecessary copy-paste.

Variables

I can not imagine how now, in the realities of the rapid development of the Front End, you can live without variables. Those who say that variables are not needed, they only interfere, clutter up the code - they blatantly lie are wrong. A simple example: you need to change the color of the link from red to blue in two files in four places. The task is simple, but what if there are not two files, but twenty-two, and you need to change not in four places, but, say, on every fifth line of each file. And here it begins: open all twenty-two files, search for the desired color, change, save, check the work. Error, come on new. And if there was a file with variables, everything was much simpler - open this file, change only one line, save, profit. The result is obvious. And the work is less and the percentage of errors is much less (I would say that it tends to zero). A simple example of a file with variables that I used: _variables.less
_variables.less
@brands-name: amazon, delicious, digg, disqus, eventbrite, eventful, intensedebate, lanyrd, pinboard, songkick, stumbleupon, gmail, google, evernote, grooveshark, instapaper, itunes, opentable, logmein, ninetyninedesigns, paypal, pocket, scribd, spotify, statusnet, stripe, yahoo, yelp, wikipedia, wordpress, ycombinator, bitcoin, cloudapp, creativecommons, dropbox, html5, ie, podcast, rss, skype, steam, windows, android, appstore, googleplay, macstore, appnet, blogger, bitbucket, dribbble, facebook, flattr, flickr, foursquare, github, googleplus, gowalla, instagram, klout, lastfm, linkedin, meetup, myspace, pinterest, quora, reddit, plurk, smashing, soundcloud, stackoverflow, tumblr, twitter, viadeo, vimeo, vk, weibo, xing, youtube, csgo, dota2, gm, lol, minecraft, tf2, quake, wot; @brands-color: rgb(242,158,55), rgb(50,113,203), rgb(22,70,115), rgb(46,159,255), rgb(255,86,22), rgb(55,96,149), rgb(0,153,225), rgb(46,106,194), rgb(0,0,255), rgb(255,0,80), rgb(235,73,36), rgb(219,64,56), rgb(78,108,247), rgb(107,177,48), rgb(233,122,47), rgb(34,34,34), rgb(82,81,82), rgb(153,0,0), rgb(0,0,0), rgb(7,34,67), rgb(50,104,154), rgb(222,90,95), rgb(35,28,26), rgb(96,175,0), rgb(130,157,37), rgb(47,126,214), rgb(162,0,194), rgb(230,0,16), rgb(17,17,17), rgb(70,70,70), rgb(255,102,0), rgb(235,151,55), rgb(49,44,42), rgb(0,0,0), rgb(0,126,229), rgb(255,54,23), rgb(0,161,217), rgb(147,101,206), rgb(255,127,37), rgb(0,162,237), rgb(0,0,0), rgb(72,187,239), rgb(164,198,57), rgb(0,0,0), rgb(185,193,62), rgb(0,125,203), rgb(49,120,189), rgb(238,90,34), rgb(32,80,129), rgb(234,76,137), rgb(72,99,174), rgb(138,186,66), rgb(255,0,132), rgb(0,114,177), rgb(23,21,21), rgb(221,74,56), rgb(255,114,10), rgb(63,114,155), rgb(227,74,37), rgb(220,26,35), rgb(0,131,168), rgb(255,0,38), rgb(0,0,0), rgb(201,22,24), rgb(168,36,0), rgb(255,87,0), rgb(207,104,47), rgb(255,79,39), rgb(255,69,0), rgb(241,132,54), rgb(55,74,97), rgb(70,192,251), rgb(231,115,46), rgb(0,162,205), rgb(69,104,142), rgb(215,88,77), rgb(10,93,94), rgb(255,0,0), rgb(182,115,0), rgb(195,61,43), rgb(17,148,240), rgb(215, 153, 34), rgb(148,200,100), rgb(179,82,21), rgb(205,24,0), rgb(255,64,5); @brands-count: length(@brands-name); 


In it, we store brand names ( @ brands-name ), colors ( @ brands-color ) that correspond to these brands, and the total number of items on the list ( @ brands-count ).
')
Mixins

Impurities, mixins - already one name tells us that we can take several pieces of code and put them under one name. Moreover, inside you can specify the names of classes, use the parent selector, specify local variables, set conditions and much more. The most obvious advantage - once written, many times used throughout the project. Mixins are very similar to functions in programming languages. They can be created both with parameters and without parameters , with conditions , used as functions that return the results of their work . Once I had the task to implement tags to products in the form of tags with text with long shadows. Shadows could be of different sizes and colors. The number of tags, their color could also vary. A little thought, reading the official documentation, came to this option . If we did not use Less or any other preprocessor, then on pure CSS such a solution would take many lines of code, the name of each class would need to be written in pens, each step of the shadow should be explicitly indicated. Of course, I love challenging tasks, but not to such an extent. This example can be implemented not only in Less, but also in other preprocessor languages.

Imports

In standard CSS, all @ imort rules should go at the very beginning of the file, with the exception of @ harset, if there is one ( @ imort rule ). Preprocessors add their own features to this directive. In particular, for Less, it does not matter what part of the file will be @ imort. If after the @imort “filename” does not specify the extension, the default file will be imported with the extension .less . It is also possible to import php and css files . Other options are available for @ imort rules . The obvious benefit when using @ import directives is to organize the files within the project and then use them. As we did with mixins and variables files, and then they were imported into other files and reused. An example .

Nesting

This is both an advantage and a disadvantage. Let me explain why the disadvantage. Many immature minds begin to repeat their DOM-tree completely, forgetting that we will get very slow and hard-to-maintain code at the output. We have a Style guide on the project, which describes a lot of interesting things, including what the maximum permissible level of nesting can be. In order not to generate topics for discussion, I will say only that with nesting you need to be extremely careful, otherwise you can come to the situation that in order to redefine one or another style, you will need to resort to the services !important It is also worth mentioning about the parent selector . It opens up unprecedented opportunities for us that were not available to us in pure CSS. Only, again, I repeat, use it with caution, only when you understand the logic of its work 100%. An example .

What else


All those advantages that preprocessors give us, and in particular Less, this is not the whole list. In fact - yummy enough. And this concerns not only Less. Therefore, read manuals, practice, combine several features into one and you will see how powerful and functional preprocessors are.

Thanks for attention.
If there are any inaccuracies in the text of the article - write in a personal.

Source: https://habr.com/ru/post/248895/


All Articles