{"id":2014,"date":"2012-11-27T15:43:44","date_gmt":"2012-11-27T12:43:44","guid":{"rendered":"http:\/\/blog.railsware.com\/?p=2014"},"modified":"2021-08-13T15:47:29","modified_gmt":"2021-08-13T12:47:29","slug":"ui-library-writing-css","status":"publish","type":"post","link":"https:\/\/railsware.com\/blog\/ui-library-writing-css\/","title":{"rendered":"UI Library: effective way to organize interface components. Writing CSS"},"content":{"rendered":"\n<p><style>   #content {&lt;br \/>    font-size: 1.15em;&lt;br \/>    line-height: 1.5;&lt;br \/>  }&lt;br \/>  #content p {&lt;br \/>    margin-bottom: 15px;&lt;br \/>  }&lt;br \/>  #content .article-image {&lt;br \/>    border: 1px solid #ddd;&lt;br \/>    margin: 0 0 20px 20px;&lt;br \/>    padding: 7px;&lt;br \/>    box-shadow: 0 1px 2px rgba(0,0,0,.1);&lt;br \/>    -webkit-box-shadow: 0 1px 2px rgba(0,0,0,.1);&lt;br \/>  }&lt;br \/>  #content h1,&lt;br \/>  #content h2,&lt;br \/>  #content h3,&lt;br \/>  #content h4 {&lt;br \/>    text-transform: none;&lt;br \/>  }&lt;br \/>  #content code {&lt;br \/>    background-color: #FFFCBB;&lt;br \/>    font-family: Consolas, Monaco, monospace;&lt;br \/>  }&lt;br \/>  div.wp-geshi-highlight-wrap5,&lt;br \/>  .wp-geshi-highlight-wrap4,&lt;br \/>  .wp-geshi-highlight-wrap3,&lt;br \/>  .wp-geshi-highlight-wrap2,&lt;br \/>  .wp-geshi-highlight-wrap,&lt;br \/>  .wp-geshi-highlight,&lt;br \/>  .ruby,&lt;br \/>  pre.de1 {&lt;br \/>    background: none!important;&lt;br \/>    padding: 0!important;&lt;br \/>    margin: 0!important;&lt;br \/>    border: 0!important;&lt;br \/>    width: auto !important;&lt;br \/>    border: none!important;&lt;br \/>    display: block!important;&lt;br \/>    border-radius: 0 !important;&lt;br \/>    position: static!important;&lt;br \/>  }&lt;br \/>  pre.de1 {&lt;br \/>    border: 1px solid #e6e6e6 !important;&lt;br \/>    border-left-width: 6px !important;&lt;br \/>    font: 1.15em\/1.5 Consolas, Monaco, monospace !important;&lt;br \/>    padding: 15px 20px !important;&lt;br \/>  }&lt;br \/><\/style>A while ago we\u2019ve touched an introduction to <a href=\"https:\/\/railsware.com\/blog\/ui-library\/\">UI Library approach<\/a> where you can see an overview of how you can organize &amp; document user interface components and become more productive in Front-end and Design.<\/p>\n\n\n\n<p>It does not matter what framework you use, but your code should be logically structured following one particular convention and containing only up to date functionality.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Creating CSS module<\/h2>\n\n\n\n<div class=\"row pts\"><img decoding=\"async\" class=\"article-image\" title=\"Different buttons on a single page example. RoR UI design.\" src=\"\/\/railsware.com\/blog\/wp-content\/uploads\/2012\/11\/userarea1.png\" alt=\"Different buttons on a single page example. RoR UI design.\" align=\"right\"><br>Think component based, not page based. Looking at a design try to analyze every single component whether it\u2019s a button, a checkbox, or a link. Find what\u2019s common between similar components and what\u2019s different. Here is an example with buttons:<br>We can see 2 different sizes and color skins here. All they have same behavior and font.<br>Now let\u2019s create a markup for our button and then start styling it with CSS:<\/div>\n\n\n\n<div class=\"mbm\">\n<pre lang=\"html5\"><!-- This is our basic button. All other will inherit from it -->\n<\/pre>\n<p><a class=\"button\" href=\"#\">Button<\/a><\/p>\n<pre lang=\"html5\">&nbsp;<\/pre>\n<\/div>\n\n\n\n<pre class=\"wp-block-preformatted\">\/* Button module \/stylesheets\/modules\/button.css *\/\n.button {\n  background-color: transparent;\n  border: 1px solid #444;\n  color: #ff5a00;\n  cursor: pointer;\n  display: inline-block;\n  font-size: 18px;\n  line-height: normal;\n  padding: 8px 15px;\n  position: relative;\n  text-decoration: none;\n  vertical-align: middle;\n}\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Adding module modifier<\/h2>\n\n\n\n<p>Then let\u2019s add our orange button as a <code>Modifier<\/code> of the base <code>Button Module<\/code>:<\/p>\n\n\n\n<div class=\"mbm\">\n<pre lang=\"html5\"><!-- Add modifier as an additional class -->\n<\/pre>\n<p><a class=\"button is-firm\" href=\"#\">Firm button<\/a><\/p>\n<pre lang=\"html5\">&nbsp;<\/pre>\n<\/div>\n\n\n\n<div class=\"mbm\">\n<pre lang=\"css\">\/* Button module \/stylesheets\/modules\/button.css *\/\n.button {...}\n\n\/* Modifier should go below a base module *\/\n.button.is-firm {\n  background: #ff5a00;\n  border-color: #fe7a33;\n  border-bottom-color: #e55000;\n  color: #fff;\n}\n<\/pre>\n<\/div>\n\n\n\n<p>Modifier name should always start with prefix <code>is-<\/code> (or any prefix you like) in order not to create naming collisions between different modules.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Extending module<\/h2>\n\n\n\n<div class=\"row pts\">\n<p><img decoding=\"async\" class=\"article-image\" title=\"Button with icon. RoR UI Library.\" src=\"\/\/railsware.com\/blog\/wp-content\/uploads\/2012\/11\/button-with-icon.png\" alt=\"Button with icon. RoR UI Library.\" align=\"right\">Sometimes buttons can have an icon inside. It is called a <code>Module object<\/code>.<\/p>\n<p>By adding a class we allow styling it in the future. Also it is semantically correct and defines the meaning of what the object is about instead of just a <code>span<\/code> tag that means nothing until you see how it looks styled.<\/p>\n<p>As an additional benefit you can get out of it is avoiding style inheritance between module objects when adding new ones.<\/p>\n<\/div>\n\n\n\n<div class=\"mbm\">\n<pre lang=\"html5\"><!-- Prepend parent module name as a prefix to identify Module Object -->\n<\/pre>\n<p><a class=\"button is-firm\" href=\"#\"> Button with icon <\/a><\/p>\n<pre lang=\"html5\">&nbsp;<\/pre>\n<\/div>\n\n\n\n<div class=\"mbm pbs\">\n<pre lang=\"css\">\/* Add Button Icon declarations below parent module and its modifiers *\/\n.button-icon {\n  background: url('images\/button\/icons.png') no-repeat 0 0;\n  display: inline-block;\n  height: 16px;\n  margin-right: 4px;\n  vertical-align: middle;\n  width: 16px;\n}\n<\/pre>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Module object vs Separate module<\/h2>\n\n\n\n<p>In the case above, <code>Button icon<\/code> can grow pretty fast if you need to add many modifiers to it and they all will become a part of the Button module.<\/p>\n\n\n\n<p>Also, probably, you will need to re-use icons inside other modules (Menu, Tabs and so on), so it\u2019s time to create a new <code>Icon<\/code> module.<\/p>\n\n\n\n<p>Move all Button icon related code to a new <code>\/stylesheets\/modules\/icon.css<\/code> stylesheet. Don\u2019t forget to update the markup and move icons to an icon folder inside images.<\/p>\n\n\n\n<div class=\"mbm\">\n<pre lang=\"html5\"><!-- Updated markup of the new Icon module -->\n<\/pre>\n<p><a class=\"button is-firm\" href=\"#\"> Button with icon <\/a><\/p>\n<pre lang=\"html5\">&nbsp;<\/pre>\n<\/div>\n\n\n\n<div class=\"mbm pbs\">\n<pre lang=\"css\">\/* Icon module \/stylesheets\/modules\/icon.css *\/\n.icon {\n  background: url('images\/icon\/sprite.png') no-repeat 0 0;\n  display: inline-block;\n  height: 16px;\n  margin-right: 4px;\n  vertical-align: middle;\n  width: 16px;\n}\n<\/pre>\n<\/div>\n\n\n\n<p>Thus we reserved a unique name in the system \u2014 <code>icon<\/code>. Now we can say where exactly the Icon related code is without even seeing a web inspector. Also we know where to find all images related&nbsp;to&nbsp;it.<\/p>\n\n\n\n<p>Hope it will help you to get started with creating your own custom UI components. In the next article, we will touch layouts and utilities for organizing them.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A while ago we\u2019ve touched an introduction to UI Library approach where you can see an overview of how you can organize &amp; document user interface components and become more productive in Front-end and Design. It does not matter what framework you use, but your code should be logically structured following one particular convention and&#8230;<\/p>\n","protected":false},"author":29,"featured_media":3028,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[3],"tags":[],"coauthors":["Sergii Iurevych"],"class_list":["post-2014","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development"],"acf":[],"aioseo_notices":[],"categories_data":[{"name":"Engineering","link":"https:\/\/railsware.com\/blog?category=development"}],"post_thumbnails":"\/\/railsware.com\/blog\/wp-content\/uploads\/2012\/11\/userarea1.png","amp_enabled":true,"_links":{"self":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts\/2014","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/users\/29"}],"replies":[{"embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/comments?post=2014"}],"version-history":[{"count":176,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts\/2014\/revisions"}],"predecessor-version":[{"id":14040,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts\/2014\/revisions\/14040"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/media\/3028"}],"wp:attachment":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/media?parent=2014"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/categories?post=2014"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/tags?post=2014"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/coauthors?post=2014"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}