Skip to content

Commit 18dd21e

Browse files
committed
useContainerQuery react hook
1 parent 02ad622 commit 18dd21e

File tree

20 files changed

+332
-21
lines changed

20 files changed

+332
-21
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
66
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
77

8+
## [3.2.0]
9+
10+
### Added
11+
12+
- `container-query`
13+
- `handleResize` is no longer called with null, and width / height props are
14+
always set.
15+
- `use-container-query`
16+
- New `useContainerQuery` react hook. (Requires 16.8.0 or higher.)
17+
- `react-container-query`
18+
- Relaxed the react and react-dom peer dep version ranges.
19+
820
## [3.1.0]
921

1022
### Added

docs/react.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,52 @@ You might have the following CSS:
4242
}
4343
```
4444

45+
### useContainerQuery hook
46+
47+
This is probably the easiest way to use Container Queries up in React.
48+
49+
Note that to use [React hooks](https://reactjs.org/docs/hooks-intro.html), you'll
50+
need React 16.8.0 or higher.
51+
52+
```js
53+
import React from "react";
54+
import useContainerQuery from "@zeecoder/use-container-query";
55+
import { meta } from "./App.pcss";
56+
57+
const App = () => {
58+
const ref = useContainerQuery(meta);
59+
60+
return (
61+
<div className="App" ref={ref}>
62+
My App
63+
</div>
64+
);
65+
};
66+
67+
export default App;
68+
```
69+
70+
To get the component's size, you can do the following:
71+
72+
```js
73+
import React, { useState } from "react";
74+
import useContainerQuery from "@zeecoder/use-container-query";
75+
import { meta } from "./App.pcss";
76+
77+
const App = () => {
78+
const [size, handleResize] = useState({ width: 1, height: 1 });
79+
const ref = useContainerQuery(meta, { handleResize });
80+
81+
return (
82+
<div className="App" ref={ref}>
83+
My App {size.width}x{size.height}
84+
</div>
85+
);
86+
};
87+
88+
export default App;
89+
```
90+
4591
### \<ContainerQuery\> with (children) Render Prop
4692

4793
Using render props.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
"postcss-nested": "^4.1.1",
4242
"prettier": "^1.15.2",
4343
"prop-types": "^15.6.2",
44-
"react": "^16.6.3",
45-
"react-dom": "^16.6.3",
44+
"react": "^16.8.1",
45+
"react-dom": "^16.8.1",
4646
"regenerator-runtime": "^0.13.1",
4747
"style-loader": "^0.23.1",
4848
"webpack": "^4.28.2",

packages/react-container-query/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# react-container-query
1+
# @zeecoder/react-container-query
22

33
This module is part of a [monorepo](https://github.com/ZeeCoder/container-query).
44

packages/react-container-query/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
"size-limit": "^0.21.0"
4040
},
4141
"peerDependencies": {
42-
"react": "^15.0.0 || ^16.0.0",
43-
"react-dom": "^15.0.0 || ^16.0.0"
42+
"react": ">=15.0.0",
43+
"react-dom": ">=15.0.0"
4444
},
4545
"keywords": [
4646
"react",

packages/use-container-query/.babelrc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"presets": [
3+
[
4+
"@babel/preset-env",
5+
{
6+
"modules": false,
7+
"targets": {
8+
"browsers": [
9+
"IE >= 10",
10+
"Chrome >= 10",
11+
"Opera >= 15",
12+
"FF >= 10",
13+
"Edge >= 12"
14+
]
15+
}
16+
}
17+
]
18+
]
19+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
src/
2+
rollup/
3+
.babelrc
4+
yarn.lock
5+
yarn-error.log
6+
.size-limit
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[
2+
{
3+
path: "dist/bundle.cjs.js",
4+
limit: "15.1KB"
5+
},
6+
{
7+
path: "dist/bundle.esm.js",
8+
limit: "15.1KB"
9+
}
10+
]

packages/use-container-query/LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
The MIT License (MIT)
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# @zeecoder/use-container-query
2+
3+
This module is part of a [monorepo](https://github.com/ZeeCoder/container-query).
4+
5+
Detailed documentation can be found [here](https://github.com/ZeeCoder/container-query/blob/master/docs/react.md).
6+
7+
## License
8+
9+
MIT
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
{
2+
"name": "@zeecoder/use-container-query",
3+
"description": "React hook to use Container Queries.",
4+
"homepage": "https://github.com/ZeeCoder/container-query/tree/master/packages/use-container-query",
5+
"version": "3.0.1",
6+
"license": "MIT",
7+
"author": "Viktor Hubert <rpgmorpheus@gmail.com>",
8+
"main": "dist/bundle.cjs.js",
9+
"module": "dist/bundle.esm.js",
10+
"scripts": {
11+
"test": "yarn size",
12+
"build:esm": "rollup -c rollup/rollup.esm.js",
13+
"build:cjs": "rollup -c rollup/rollup.cjs.js",
14+
"build": "npm-run-all build:*",
15+
"size": "size-limit",
16+
"size:why": "size-limit --why",
17+
"prepare": "yarn build",
18+
"prepublishOnly": "yarn test"
19+
},
20+
"dependencies": {
21+
"@zeecoder/container-query": "^3.0.0"
22+
},
23+
"devDependencies": {
24+
"@babel/core": "^7.1.6",
25+
"@babel/preset-env": "^7.1.6",
26+
"npm-run-all": "^4.1.2",
27+
"rollup": "^0.67.3",
28+
"rollup-plugin-babel": "^4.0.3",
29+
"rollup-plugin-terser": "^3.0.0",
30+
"size-limit": "^0.21.0"
31+
},
32+
"peerDependencies": {
33+
"react": ">=18.0.0",
34+
"react-dom": ">=18.0.0"
35+
},
36+
"keywords": [
37+
"react",
38+
"hooks",
39+
"rwd",
40+
"responsive-design",
41+
"responsive",
42+
"postcss",
43+
"postcss-plugin",
44+
"container-query",
45+
"container-queries",
46+
"element-query",
47+
"element-queries",
48+
"media-query",
49+
"media-queries",
50+
"css",
51+
"browser",
52+
"query",
53+
"javascript",
54+
"component",
55+
"module"
56+
],
57+
"publishConfig": {
58+
"access": "public"
59+
}
60+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import sharedConfig from "./rollup.shared";
2+
3+
sharedConfig.output = [
4+
{
5+
// Muting the warning about CJS default export being added to the ".default"
6+
// prop. It's fine.
7+
exports: "named",
8+
file: __dirname + "/../dist/bundle.cjs.js",
9+
format: "cjs"
10+
}
11+
];
12+
13+
export default sharedConfig;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import sharedConfig from "./rollup.shared";
2+
3+
sharedConfig.output = [
4+
{
5+
file: __dirname + "/../dist/bundle.esm.js",
6+
format: "es"
7+
}
8+
];
9+
10+
export default sharedConfig;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import babel from "rollup-plugin-babel";
2+
import { terser } from "rollup-plugin-terser";
3+
4+
export default {
5+
input: __dirname + "/../src/index.js",
6+
plugins: [babel(), terser()],
7+
external: ["react", "react-dom", "@zeecoder/container-query"]
8+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import React, { useRef, useEffect } from "react";
2+
import Container from "@zeecoder/container-query";
3+
4+
export default function(meta, options = {}) {
5+
const ref = useRef();
6+
7+
useEffect(() => {
8+
new Container(ref.current, meta, options);
9+
}, []);
10+
11+
return ref;
12+
}

tests/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ import "babel-regenerator-runtime";
1616
// "Registering" test suits
1717
import "./react/manual";
1818
import "./react/hoc";
19+
import "./react/hook";
1920
import "./react/non-oocss";
20-
// Weirdly unless basic tests are the last, they throw errors in IE
21-
import "./react/basic";
21+
import "./react/basic"; // Weirdly unless basic tests are the last, they throw errors in IE

tests/react/hook/Test/Test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import React, { useState } from "react";
2+
import useContainerQuery from "../../../../packages/use-container-query/dist/bundle.esm";
3+
import { meta } from "./Test.pcss";
4+
5+
const Test = () => {
6+
const [size, handleResize] = useState({ width: 1, height: 1 });
7+
const ref = useContainerQuery(meta, {
8+
valuePrecision: 2,
9+
// todo make handleResize always return an object with some width / height
10+
// value
11+
handleResize
12+
});
13+
14+
// todo test getting the size here
15+
return (
16+
<div className="Test" ref={ref}>
17+
{size.width}x{size.height}
18+
</div>
19+
);
20+
};
21+
22+
export default Test;

tests/react/hook/Test/Test.pcss

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
body {
2+
background: rgb(200, 200, 200);
3+
}
4+
5+
.Test {
6+
@define-container;
7+
position: absolute;
8+
width: 100%;
9+
height: 100%;
10+
background-color: rgb(255, 0, 0);
11+
--w: 1rw;
12+
13+
@container (width > 100px) {
14+
background-color: rgb(0, 255, 0);
15+
}
16+
}

tests/react/hook/index.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import React from "react";
2+
import Test from "./Test/Test";
3+
import {
4+
renderTestComponent,
5+
clearDOM,
6+
waitForTestComponentToHaveStyle,
7+
changeRootSize,
8+
waitForTestComponentToHaveCustomProperties,
9+
waitForElementToHaveStyle
10+
} from "../../utils";
11+
12+
// Features covered:
13+
// Running the same tests as we do for the HoC version
14+
describe("useContainerQuery", () => {
15+
beforeAll(() => {
16+
clearDOM();
17+
renderTestComponent(<Test />, {
18+
width: 100,
19+
height: 50
20+
});
21+
});
22+
23+
it("should apply styles to the body", async () => {
24+
await waitForElementToHaveStyle(document.body, {
25+
backgroundColor: "rgb(200, 200, 200)"
26+
});
27+
});
28+
29+
it("should not have any of the container queries applied", async () => {
30+
await waitForTestComponentToHaveStyle({
31+
backgroundColor: "rgb(255, 0, 0)"
32+
});
33+
34+
await waitForTestComponentToHaveCustomProperties({
35+
"--w": "1px"
36+
});
37+
});
38+
39+
it("should react to width change", async () => {
40+
changeRootSize({ width: 101 });
41+
await waitForTestComponentToHaveStyle({
42+
backgroundColor: "rgb(0, 255, 0)"
43+
});
44+
45+
await waitForTestComponentToHaveCustomProperties({
46+
"--w": "1.01px"
47+
});
48+
});
49+
});

0 commit comments

Comments
 (0)