Skip to content

Instantly share code, notes, and snippets.

@ianmcnally
Last active February 24, 2019 11:21
Show Gist options
  • Select an option

  • Save ianmcnally/3bc8fa64dc1fb35e47deeb7e449265ce to your computer and use it in GitHub Desktop.

Select an option

Save ianmcnally/3bc8fa64dc1fb35e47deeb7e449265ce to your computer and use it in GitHub Desktop.

Revisions

  1. ianmcnally revised this gist Oct 19, 2017. No changes.
  2. ianmcnally renamed this gist Oct 19, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. ianmcnally renamed this gist Oct 19, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. ianmcnally renamed this gist Oct 19, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. ianmcnally revised this gist Oct 19, 2017. 1 changed file with 10 additions and 4 deletions.
    14 changes: 10 additions & 4 deletions grid-auto-placement-polyfill.js
    Original file line number Diff line number Diff line change
    @@ -46,7 +46,13 @@ const runPolyfill = () => {
    toArray(grids).forEach(placeItemsOnGrid)
    }

    // covers dynamic and static pages, at the cost of multiple DOM traversals
    window.addEventListener('popstate', runPolyfill)
    document.addEventListener('DOMContentLoaded', runPolyfill)
    document.body.addEventListener('DOMSubtreeModified', runPolyfill)
    const supportsGrid = typeof CSS !== 'undefined' &&
    typeof CSS.supports !== 'undefined' &&
    CSS.supports('display', 'grid')

    if (!supportsGrid) {
    // covers dynamic and static pages, at the cost of multiple DOM traversals
    window.addEventListener('popstate', runPolyfill)
    document.addEventListener('DOMContentLoaded', runPolyfill)
    document.body.addEventListener('DOMSubtreeModified', runPolyfill)
    }
  6. ianmcnally renamed this gist Oct 19, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  7. ianmcnally revised this gist Oct 19, 2017. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions grid-rows.css
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    .auto {
    -ms-grid-rows: auto;
    grid-template-rows: auto;
    }
  8. ianmcnally revised this gist Oct 19, 2017. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions display.css
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    .grid {
    display: -ms-grid;
    display: grid;
    }
  9. ianmcnally revised this gist Oct 19, 2017. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions grid-template-columns.css
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    .of12 {
    -ms-grid-columns: (1fr)[12];
    grid-template-columns: repeat(12, 1fr);
    }
  10. ianmcnally revised this gist Oct 19, 2017. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion grid-auto-placement-polyfill.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    import { columns, display, gridSpans, margin } from 'design-system-styles'
    import { columns, display, gridSpans, margin } from './css-modules-styles'

    const COLUMNS = 12

    const toArray = arrayLike => Array.prototype.slice.call(arrayLike)
  11. ianmcnally revised this gist Oct 19, 2017. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions grid-auto-placement-polyfill.js
    Original file line number Diff line number Diff line change
    @@ -45,8 +45,7 @@ const runPolyfill = () => {
    toArray(grids).forEach(placeItemsOnGrid)
    }

    setTimeout(runPolyfill, 10000)

    // covers dynamic and static pages, at the cost of multiple DOM traversals
    window.addEventListener('popstate', runPolyfill)
    document.addEventListener('DOMContentLoaded', runPolyfill)
    document.body.addEventListener('DOMSubtreeModified', runPolyfill)
  12. ianmcnally created this gist Oct 19, 2017.
    52 changes: 52 additions & 0 deletions grid-auto-placement-polyfill.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    import { columns, display, gridSpans, margin } from 'design-system-styles'
    const COLUMNS = 12

    const toArray = arrayLike => Array.prototype.slice.call(arrayLike)

    const placeItemsOnGrid = grid => {
    const withGutters = grid.classList.contains(columns.withGutters)
    let currentColumnSpansInRow = 0
    let currentRow = 1
    let currentColumn = 1

    toArray(grid.children).forEach(row => {
    let columnSpan

    for (let i = 0; i <= COLUMNS; i++) {
    if (row.classList.contains(gridSpans[`column${i}`])) {
    columnSpan = i
    break
    }
    }
    const wouldNextSpansExceedColumnCount =
    currentColumnSpansInRow + Number(columnSpan) > COLUMNS

    if (wouldNextSpansExceedColumnCount) {
    currentRow += 1
    currentColumnSpansInRow = 0
    currentColumn = 1
    }

    currentColumnSpansInRow = currentColumnSpansInRow + Number(columnSpan)

    row.style['-ms-grid-column-span'] = columnSpan
    row.style['-ms-grid-column'] = currentColumn
    row.style['-ms-grid-row'] = currentRow
    if (withGutters) {
    row.classList.add(margin.leftOne)
    }

    currentColumn += columnSpan
    })
    }

    const runPolyfill = () => {
    const grids = document.getElementsByClassName(display.grid)
    toArray(grids).forEach(placeItemsOnGrid)
    }

    setTimeout(runPolyfill, 10000)

    window.addEventListener('popstate', runPolyfill)
    document.addEventListener('DOMContentLoaded', runPolyfill)
    document.body.addEventListener('DOMSubtreeModified', runPolyfill)