Container Queries Mixin: Stylesheet version (using $this as a keyword for matching tags)
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
function containerQuery(selector, test, stylesheet) { | |
var tag = document.querySelectorAll(selector) | |
var style = '' | |
var count = 0 | |
for (var i=0; i<tag.length; i++) { | |
var attr = (selector+test).replace(/\W+/g, '') | |
if (test(tag[i])) { | |
var css = stylesheet.replace(/:self|\$this/g, '[data-' + attr + '="' + count + '"]') | |
tag[i].setAttribute('data-' + attr, count) | |
style += css | |
count++ | |
} else { | |
tag[i].setAttribute('data-' + attr, '') | |
} | |
} | |
return style | |
} |
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
let containerQuery = (selector, test, stylesheet) => { | |
let style = '' | |
let count = 0 | |
document.querySelectorAll(selector).forEach(tag => { | |
const attr = (selector+test).replace(/\W+/g, '') | |
if (test(tag)) { | |
const css = stylesheet.replace(/:self|\$this/g, `[data-${attr}="${count}"]`) | |
tag.setAttribute(`data-${attr}`, count) | |
style += css | |
count++ | |
} else { | |
tag.setAttribute(`data-${attr}`, '') | |
} | |
}) | |
return style | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The ES5 version is a bit incorrect. Template literals are not an ES5 feature so the backticks need to go away.
In the ES6 version, I believe you don't ever change
attr
andcss
in theforEach
call so they should be consts.