├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # Compiled binary addons (http://nodejs.org/api/addons.html) 20 | build/Release 21 | 22 | # Dependency directory 23 | # Commenting this out is preferred by some people, see 24 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git- 25 | node_modules 26 | 27 | # Users Environment Variables 28 | .lock-wscript 29 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # 1.1.0 - 2015-03-20 2 | 3 | ✨ Adds `gutter()` value helper that returns calculated gutter size. 4 | 5 | # 1.0.0 - 2015-02-05 6 | 7 | ✨ First release, minimal features. 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Ned Baldessin 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PostCSS Flexgrid 2 | 3 | [PostCSS](https://github.com/postcss/postcss) helpers for working with a flexible grid. Very early stage, you probably don't want to use this. 4 | 5 | ## Installation 6 | 7 | ```console 8 | $ npm install --save-dev postcss-flexgrid 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```js 14 | // dependencies 15 | var fs = require("fs") 16 | var postcss = require("postcss") 17 | var flexgrid = require("postcss-flexgrid") 18 | 19 | // css to be processed 20 | var css = fs.readFileSync("input.css", "utf8") 21 | 22 | var options = { 23 | max_columns: 12, // Number of columns of the grid 24 | column_width: 70, // Width of one column, in pixels 25 | gutter_width: 30, // Width of a gutter, in pixels 26 | }; 27 | 28 | // process css 29 | var output = postcss() 30 | .use(flexgrid(options)) 31 | .process(css) 32 | .css 33 | ``` 34 | 35 | ```css 36 | .container { 37 | span: 5; 38 | } 39 | .container .sub-container { 40 | span: 2 of 5; 41 | } 42 | .container .other-sub-container { 43 | span: 3 of 5; 44 | margin-right: 0; 45 | } 46 | ``` 47 | 48 | ## [Changelog](CHANGELOG.md) 49 | 50 | ## [License](LICENSE) 51 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var _ = require('lodash'); 2 | 3 | var options = {}; 4 | var defaults = { 5 | max_columns: 12, 6 | column_width: 70, // in px 7 | gutter_width: 30, // in px 8 | }; 9 | 10 | var flexGrid = function(columns, container_columns) { 11 | container_columns = container_columns || options.max_columns; 12 | var width = columns * options.column_width + (columns - 1) * options.gutter_width; 13 | var container_width = container_columns * options.column_width + (container_columns - 1) * options.gutter_width; 14 | return (width / container_width) * 100; 15 | }; 16 | 17 | var flexGutter = function(container_columns, gutter){ 18 | container_columns = container_columns || options.max_columns; 19 | gutter = gutter || options.gutter_width; 20 | var container_width = container_columns * options.column_width + (container_columns - 1) * options.gutter_width; 21 | return (gutter / container_width) * 100; 22 | }; 23 | 24 | 25 | var flexGridProcessor = function(style, opts) { 26 | options = _.merge(options, defaults, opts); 27 | 28 | var asFunctionRE = /span\(\s*(\d+)\s*(?:of\s*(\d+)\s*)?\)/; 29 | var asValueRE = /\s*(\d+)\s*(?:of\s*(\d+)\s*)?/; 30 | style.eachDecl(function transformDecl(decl) { 31 | var cols, 32 | columns, 33 | container_columns, 34 | val; 35 | 36 | // Span as a value. 37 | // Examples: 38 | // width: span(); 39 | // width: span( of ); 40 | if (decl.value !== null && decl.value.indexOf('span(') !== -1) { 41 | cols = decl.value.match(asFunctionRE); 42 | columns = cols[1]; 43 | container_columns = cols[2]; 44 | decl.value = flexGrid(columns, container_columns) + '%'; 45 | } 46 | 47 | // Span as a helper 48 | // Examples: 49 | // div { 50 | // span: 5; 51 | // span: 4 of 6; 52 | // } 53 | if (decl.prop === 'span') { 54 | cols = decl.value.match(asValueRE); 55 | columns = cols[1]; 56 | container_columns = cols[2]; 57 | decl.parent.append({prop: 'float', value: 'left'}); 58 | decl.parent.append({prop: 'width', value: flexGrid(columns, container_columns) + '%'}); 59 | decl.parent.append({prop: 'margin-right', value: flexGutter(container_columns) + '%'}); 60 | decl.removeSelf(); 61 | } 62 | 63 | // Add margins that are grid aware 64 | // Examples: 65 | // div { 66 | // pre: 6; 67 | // post: 6; 68 | // pre: 4 of 10; 69 | // post: 4 of 10; 70 | // } 71 | if (decl.prop === 'pre' || decl.prop === 'post') { 72 | cols = decl.value.match(asValueRE); 73 | columns = cols[1]; 74 | container_columns = cols[2]; 75 | val = flexGrid(columns, container_columns) + flexGutter(container_columns); 76 | decl.parent.append({prop: (decl.prop === 'pre' ? 'margin-left': 'margin-right'), value: val + '%'}); 77 | decl.removeSelf(); 78 | } 79 | 80 | // Helper that returns gutter width as a percentage value 81 | 82 | // This helper receives 2 parameters: 83 | // `container_columns` => Amount of columns of the container element 84 | // `gutter_width` => To override the main gutter width (Only if you know what you're doing) 85 | 86 | // Examples: 87 | // outerDiv { 88 | // span: 6 of 12; 89 | // } 90 | // innerDiv { 91 | // width: span(4); 92 | // margin-left: gutter(6); 93 | // } 94 | if (decl.value !== null && decl.value.indexOf('gutter(') !== -1) { 95 | cols = decl.value.match(asValueRE); 96 | container_columns = cols && cols[1]; 97 | gutter_width = cols && cols[2]; 98 | decl.value = flexGutter(container_columns, gutter_width) + '%'; 99 | } 100 | 101 | }); 102 | 103 | 104 | }; 105 | 106 | 107 | module.exports = flexGridProcessor; 108 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "postcss-flexgrid", 3 | "version": "1.1.0", 4 | "description": "PostCSS helpers for defining a flexible grid in PostCSS", 5 | "repository": { 6 | "type": "git", 7 | "url": "https://github.com/area17/postcss-flexgrid.git" 8 | }, 9 | "main": "index.js", 10 | "scripts": { 11 | "test": "echo \"Error: no test specified\" && exit 1" 12 | }, 13 | "keywords": [ 14 | "postcss", 15 | "postcss-plugins", 16 | "grid" 17 | ], 18 | "author": "Ned Baldessin ", 19 | "license": "MIT", 20 | "dependencies": { 21 | "lodash": "^3.1.0" 22 | } 23 | } 24 | --------------------------------------------------------------------------------