├── LICENSE
├── README.md
├── cq.js
└── package.json
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2020 Filament Group
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, 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,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | :warning: This project is archived and the repository is no longer maintained.
2 |
3 | # cq
4 | A minimal container query web component
5 |
6 |
By Scott Jehl
7 |
8 | This `c-q` custom element enables very simple "min-width" container queries, allowing you to adjust its CSS based on its own rendered width instead of viewport width.
9 |
10 | ## Example:
11 | https://codepen.io/scottjehl/pen/NWrdRQv
12 |
13 | ## Usage
14 |
15 | 1. Wrap a `` element around your HTML and add any classes or attributes you might need to it, such as ``.
16 | 2. Load the JavaScript module ``
17 | 3. Start writing the CSS...
18 |
19 | To use, set a given `c-q`'s `--breakpoints;` custom CSS property to one or more pixel-based breakpoint widths that the JS should be aware of, like this:
20 |
21 | ```css
22 | c-q { --breakpoints: "400 600 800"; }
23 | ```
24 |
25 | One or more of these values will appear in the element's `data-min-width` attribute when they are greater or equal to the element's rendered width. You can style the element based on their presence using the `~=` attribute selector, like this:
26 |
27 | ```css
28 | c-q[data-min-width~="400"] { background: green; }
29 | ```
30 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/cq.js:
--------------------------------------------------------------------------------
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);
41 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "version": "0.1.0",
3 | "name": "@filamentgroup/cq",
4 | "homepage": "https://github.com/filamentgroup/cq/",
5 | "author": {
6 | "name": "Filament Group",
7 | "email": "thegroup@filamentgroup.com"
8 | },
9 | "repository": {
10 | "type": "git",
11 | "url": "git://github.com/filamentgroup/cq.git"
12 | },
13 | "license": "MIT",
14 | "bugs": {
15 | "url": "https://github.com/filamentgroup/cq/issues/"
16 | },
17 | "main": "cq.js"
18 | }
19 |
--------------------------------------------------------------------------------