Skip to content
This repository was archived by the owner on Oct 21, 2022. It is now read-only.

Commit a6fb95d

Browse files
authored
Create cq.js
1 parent e752915 commit a6fb95d

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

cq.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Minimalist Container Queries
2+
// 2020 @scottjehl, @filamentgroup / MIT License
3+
class CQ extends HTMLElement {
4+
constructor() {
5+
super();
6+
this._init = this._init.bind(this);
7+
this._observer = new MutationObserver(this._init);
8+
}
9+
connectedCallback() {
10+
if (this.children.length) {
11+
this._init();
12+
}
13+
this._observer.observe(this, { childList: true });
14+
}
15+
_init() {
16+
var self = this;
17+
this._rObserver = new ResizeObserver((entries) => {
18+
for (const entry of entries) {
19+
self.setActiveBPs(entry);
20+
}
21+
});
22+
this._rObserver.observe(this);
23+
}
24+
25+
setActiveBPs(entry) {
26+
var bps = window.getComputedStyle(entry.target).getPropertyValue("--breakpoints");
27+
var activeBPs = [];
28+
if (bps) {
29+
bps = bps.replace('"', "");
30+
bps.split(" ").forEach((bp) => {
31+
var bpnum = parseFloat(bp);
32+
if (entry.contentRect.width >= bpnum) {
33+
activeBPs.push(bpnum);
34+
}
35+
});
36+
}
37+
entry.target.setAttribute("data-min-width", activeBPs.join(" "));
38+
}
39+
}
40+
customElements.define("c-q", CQ);

0 commit comments

Comments
 (0)