├── README.md ├── bower.json ├── dist └── dom-furnish.min.js └── src └── dom-furnish.js /README.md: -------------------------------------------------------------------------------- 1 | # Furnish JS 2 | 3 | Let the classes on DOM elements generate the CSS for you. 4 | 5 | Furnish JS is simple **lightweight**, ~2KB when minified, utility that **turns the classes applied upon the DOM elements to CSS**. So that you don't have to manually write the CSS for those minor UI enhancements like increasing the padding, adding a little margin, changing the font size, applying a border radius, pumping up the line-height a bit etc. All you have to do is specify the CSS class on an element at any point of timea i.e. either dynamically or statically, it will be *furnished*. 6 | 7 | ## How to Install? 8 | 9 | - You can install the utility via Bower: 10 | ``` 11 | bower install idnan/furnish-js 12 | ``` 13 | Or directly download the repository and place the content of `dist` wherever you can access them. 14 | 15 | - Include the `dom-funish.min.js` or `dom-furnish.js` and you are ready to use the magic of it. 16 | 17 | ## How to Use 18 | In order to use it, all you have to specify from the set of classes in a specific format i.e. in the form of 19 | 20 | ``` 21 | [formula][value][unit] # If you donot provide the unit, `px` will be used. 22 | ``` 23 | 24 | And run 25 | 26 | ``` 27 | // Furnish all the elements in div#some-elem 28 | var domElem = document.getElementById('some-elem') 29 | Funish.apply(domElem) 30 | ``` 31 | or if you want to run it over the whole document 32 | 33 | ``` 34 | Furnish.apply() 35 | ``` 36 | 37 | `domElem` specifies the container whose children are to be furnished, passing nothing will furnish the whole document. 38 | 39 | For example you can specify `mt25` to apply top margin of 25px, `fs14px` to apply font size of 14 px etc. Also you can combine these formulas on an element as well for example an element may have `class="mb20 pt10 pl15 lh20"` etc. 40 | 41 | Currently supported styles are as follows: 42 | 43 | | Formula | CSS Property | Example Usage | 44 | |---------|------------------|--------------------------------------------------| 45 | | `p` | `padding` | `p10` will translate to `padding: 10px` | 46 | | `pt` | `padding-top` | `pt20` will translate to `padding-top: 20px;` | 47 | | `pb` | `padding-bottom` | `pb10` will translate to `padding-bottom: 10px;` | 48 | | `pr` | `padding-right` | `pr20` will translate to `padding-right: 20px;` | 49 | | `pl` | `padding-left` | `pl23` will translate to `padding-left: 23px;` | 50 | | `m` | `margin` | `m20` will translate to `margin: 20px` | 51 | | `mt` | `margin-top` | `mt20` will translate to `margin-top: 20px;` | 52 | | `mb` | `margin-bottom` | `mb20` will translate to `margin-bottom: 20px;` | 53 | | `ml` | `margin-left` | `ml50` will translate to `margin-left: 50px;` | 54 | | `mr` | `margin-right` | `mr30` will translate to `margin-right: 30px;` | 55 | | `w` | `width` | `w200` will translate to `width: 200px` | 56 | | `h` | `height` | `h60` will translate to `height: 60px;` | 57 | | `br` | `border-radius` | `br5` will translate to `border-radius: 5px;` | 58 | | `fs` | `font-size` | `fs15` will translate to `font-size: 15px` | 59 | | `fw` | `font-weight` | `fw400` will translate to `font-weight: 400px` | 60 | | `lh` | `line-height` | `lh20em` will translate to `line-height: 20em` | 61 | | `t` | `top` | `t6` will translate to `top: 6px;` | 62 | | `l` | `left` | `l30` will translate to `left: 30px` | 63 | | `b` | `bottom` | `b20em` will translate to `bottom: 20em;` | 64 | | `r` | `right` | `r20em` will translate to `right: 20em;` | 65 | 66 | 67 | ## Quick Usage Examples 68 | 69 | Let's say that you have a heading and you want to apply bottom margin of `10px`, you will do 70 | 71 | ``` 72 |

Furnish JS

73 |

Turn your classes to CSS

74 | ``` 75 | 76 | Changing the font size to `15px` 77 | ``` 78 |

You can combine formulas as well

79 | ``` 80 | 81 | ## Contributing 82 | 83 | You can contribute to this project by 84 | 85 | - Adding the support for other CSS attributes. (Fork it, add your feature, create the pull request) 86 | - Pointing out any bugs or issues 87 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "furnish-js", 3 | "description": "Let the classes on DOM elements generate the CSS for you", 4 | "version": "1.0", 5 | "main": [ 6 | "./dist/dom-furnish.min.js" 7 | ], 8 | "moduleType": [], 9 | "keywords": [ 10 | "javascript", 11 | "inline-style", 12 | "css", 13 | "javascript-css", 14 | "furnish", 15 | "furnish-js", 16 | "js-furnish" 17 | ], 18 | "authors": [ 19 | "Adnan Ahmed" 20 | ], 21 | "license": "MIT", 22 | "homepage": "https://github.com/idnan", 23 | "ignore": [ 24 | "**/.*", 25 | "node_modules", 26 | "bower_components", 27 | "test", 28 | "tests", 29 | "README.md" 30 | ], 31 | "dependencies": { 32 | } 33 | } -------------------------------------------------------------------------------- /dist/dom-furnish.min.js: -------------------------------------------------------------------------------- 1 | ; 2 | // Inline JS. Easily turn your classes into the style 3 | // v1.0 4 | !function(t,a){function e(t){var a=t&&t.className&&t.className.split(" ")||[];a.forEach(function(a){var e=n(a);e&&e.key&&(t.style[e.key]=e.value)})}function n(t){var a=t.match(/(^[a-z]{1,23})([0-9]{1,4})?/),e=a&&r[a[1]],n=a&&a[3]||"px";return e&&{key:e,value:a[2]+n}}function i(a){var n=a||t.document,i=n.getElementsByTagName("*");for(var r in i){var s=i[r];"object"==typeof s&&(e(s),o.observe(s,{attributes:!0,attributeFilter:["class"]}))}}var r={t:"top",b:"bottom",l:"left",r:"right",w:"width",h:"height",p:"padding",m:"margin",br:"borderRadius",fs:"fontSize",fw:"fontWeight",lh:"lineHeight",mt:"marginTop",mb:"marginBottom",ml:"marginLeft",mr:"marginRight",pt:"paddingTop",pb:"paddingBottom",pl:"paddingLeft",pr:"paddingRight"},o=new MutationObserver(function(t,a){t.forEach(function(t){var a=t.target;a.lastClassName!==a.className&&e(a),a.lastClassName=a.className})});return Furnish={apply:i}}(window,document); -------------------------------------------------------------------------------- /src/dom-furnish.js: -------------------------------------------------------------------------------- 1 | ; 2 | // Inline JS. Easily turn your classes into the style 3 | // v1.0 4 | (function(window, document ) { 5 | 6 | /** 7 | * CSS property aliases. Defines the mapping with CSS properties 8 | * @type {Object} 9 | */ 10 | var propertyAlias = { 11 | t: 'top', 12 | b: 'bottom', 13 | l: 'left', 14 | r: 'right', 15 | 16 | w: 'width', 17 | h: 'height', 18 | 19 | p: 'padding', 20 | m: 'margin', 21 | 22 | br: 'borderRadius', 23 | fs: 'fontSize', 24 | fw: 'fontWeight', 25 | lh: 'lineHeight', 26 | 27 | mt: 'marginTop', 28 | mb: 'marginBottom', 29 | ml: 'marginLeft', 30 | mr: 'marginRight', 31 | 32 | pt: 'paddingTop', 33 | pb: 'paddingBottom', 34 | pl: 'paddingLeft', 35 | pr: 'paddingRight' 36 | }; 37 | 38 | /** 39 | * Observer to keep an eye on element changes and furnishing elements 40 | */ 41 | var observer = new MutationObserver(function(mutations, observer) { 42 | mutations.forEach(function(mutation) { 43 | var target = mutation.target; 44 | target.lastClassName !== target.className && furnishElement(target); 45 | target.lastClassName = target.className; 46 | }); 47 | }); 48 | 49 | /** 50 | * Checks the passed element for applied classes and checks 51 | * to see if the element can be furnished i.e. if any of the known classes are available 52 | * 53 | * @param {object} element The object to be furnisged 54 | * @return {void} 55 | */ 56 | function furnishElement(element) { 57 | var classNames = element && element.className && element.className.split(' ') || []; 58 | // For each of the class names, see if we can furnish the element by this class 59 | classNames.forEach(function(className) { 60 | var style = generateStyle(className); 61 | style && style.key && (element.style[style.key] = style.value); 62 | }); 63 | } 64 | 65 | /** 66 | * Generates the style object from the passed class name 67 | 68 | * @param {string} className The name of from which style is to be generated 69 | * @return {object} An object specifying the CSS property and value 70 | */ 71 | function generateStyle(className) { 72 | var pieces = className.match(/(^[a-z]{1,23})([0-9]{1,4})?/), 73 | cssProperty = pieces && propertyAlias[pieces[1]], 74 | unit = pieces && pieces[3] || 'px'; 75 | 76 | return cssProperty && { 77 | key: cssProperty, 78 | value: pieces[2] + unit 79 | }; 80 | } 81 | 82 | /** 83 | * Furnish the elements in side the passed element 84 | * 85 | * @param {null|object} observeElement The dom element to which the style is applied 86 | * @return {void} 87 | */ 88 | function apply(observeElement) { 89 | 90 | var scope = observeElement || window.document; 91 | var elements = scope.getElementsByTagName('*'); 92 | for (var i in elements) { 93 | var element = elements[i]; 94 | if (typeof element === 'object') { 95 | furnishElement(element); 96 | observer.observe(element, { attributes: true, attributeFilter: ['class'] }); 97 | } 98 | } 99 | } 100 | 101 | return Furnish = { 102 | apply: apply 103 | }; 104 | 105 | })(window, document); --------------------------------------------------------------------------------