Created
January 7, 2019 03:28
-
-
Save whoisryosuke/2a7f0fbe59d6c552a924bf8c981731c7 to your computer and use it in GitHub Desktop.
JS / CSS-in-JS - Calculate width of grid element using column size and number of columns (e.g. [1,2] = 50% column)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Calculates width of a grid element. | |
* | |
* Accepts an array of two numbers, the column size | |
* and total number of columns (respectively). | |
* | |
* Uses the total columns to determine total width, | |
* then multiplies by the column size to calculate | |
* current column width. | |
* | |
* For example: a two column grid, with 50% wide columns, | |
* would be an array of `[1,2]`. 2 total columns, with a | |
* column taking up 1 of the 2 (so 50%). Same as `[3,6]`. | |
* | |
* @param {array} columns [Column size, Number of columns] | |
*/ | |
const calculateGridWidth = columns => { | |
if (columns) { | |
return `width: ${(100 / columns[1]) * columns[0]}%;`; | |
} | |
}; | |
export default calculateGridWidth; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment