Skip to content

Commit b926fca

Browse files
committed
ContainerQuery component update to always return the size object
1 parent d7ba58a commit b926fca

File tree

3 files changed

+14
-47
lines changed

3 files changed

+14
-47
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1818
- Removed the default export. (Both ContainerQuery and withContainerQuery is
1919
still available as named exports.)
2020
- Removed the `stats` prop from ContainerQuery. (Use `meta` instead.)
21+
- `ContainerQuery` no longer returns with a "null" size object when a function
22+
is passed in as the children prop. Instead it returns with `{width: 0, height: 0}`
23+
initially, then updates with the observed container element.
2124

2225
## [3.0.0-alpha.1]
2326

packages/react-container-query/README.md

Lines changed: 10 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ npm install --save @zeecoder/react-container-query
1515
### Set up PostCSS with webpack
1616

1717
[Here](https://github.com/ZeeCoder/container-query/blob/master/docs/postcss.md)
18-
is a documentiation on how to do just that, in order to get the same syntax I've
18+
is a documentation on how to do just that, in order to get the same syntax I've
1919
been using.
2020

2121
## Usage
@@ -39,35 +39,13 @@ Assuming the following CSS:
3939
And assuming that `meta` is the object obtained by running the above CSS
4040
through the [postcss plugin](https://github.com/ZeeCoder/container-query/tree/master/packages/postcss-container-query).
4141

42-
The one thing you need to keep in mind is that the container css class
43-
(".App" in this case) has to be present on the root element.
44-
45-
(A limitation [soon to be addressed](https://github.com/ZeeCoder/container-query/issues/89).)
46-
47-
### `withContainerQuery`
48-
49-
This is probably the easiest way of enabling container queries.
50-
51-
```js
52-
import { withContainerQuery } from "@zeecoder/react-container-query";
53-
// Assuming postcss-loader to be set up
54-
import "./App.pcss";
55-
import meta from "./App.pcss.json";
56-
57-
const App = () => <div className="App">My App</div>;
58-
59-
export default withContainerQuery(App, meta);
60-
```
61-
6242
### `<ContainerQuery>`
6343

6444
A render-prop approach.
6545

6646
```js
6747
import { ContainerQuery } from "@zeecoder/react-container-query";
68-
// Assuming postcss-loader to be set up
69-
import "./App.pcss";
70-
import meta from "./App.pcss.json";
48+
import { meta } from "./App.pcss";
7149

7250
const App = () => (
7351
<ContainerQuery meta={meta}>
@@ -82,16 +60,13 @@ If you're also interested in the component's size:
8260

8361
```js
8462
import { ContainerQuery } from "@zeecoder/react-container-query";
85-
// Assuming postcss-loader to be set up
86-
import "./App.pcss";
87-
import meta from "./App.pcss.json";
63+
import { meta } from "./App.pcss";
8864

8965
const App = () => (
9066
<ContainerQuery meta={meta}>
9167
{size => (
92-
// size can be "null" when size is still not available
9368
<div className="App">
94-
My size is: {size ? `${size.width}x${size.height}` : "?"}
69+
My size is: {size.width}x{size.height}
9570
</div>
9671
)}
9772
</ContainerQuery>
@@ -100,28 +75,17 @@ const App = () => (
10075
export default App;
10176
```
10277

103-
### `<ResizeObserver>`
104-
105-
This component simply observes an element's size changes using `ResizeObserver`.
106-
107-
Useful to allow for rendering parts of the component conditionally, based
108-
on its size.
78+
### `withContainerQuery`
10979

110-
(It uses a [polyfill](https://github.com/que-etc/resize-observer-polyfill), if a native implementation is not available.)
80+
If you prefer Higher-Order Components:
11181

11282
```js
113-
import { ResizeObserver } from "@zeecoder/react-container-query";
83+
import { withContainerQuery } from "@zeecoder/react-container-query";
84+
import { meta } from "./App.pcss";
11485

115-
const App = () => (
116-
<ResizeObserver>
117-
{size => (
118-
// size can be "null" when size is still not available
119-
<div>My size is: {size ? `${size.width}x${size.height}` : "?"}</div>
120-
)}
121-
</ResizeObserver>
122-
);
86+
const App = () => <div className="App">My App</div>;
12387

124-
export default App;
88+
export default withContainerQuery(App, meta);
12589
```
12690

12791
## License

packages/react-container-query/src/ContainerQuery.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default class ContainerQuery extends Component {
77
constructor(props) {
88
super(props);
99

10-
this.state = { size: null };
10+
this.state = { width: 0, height: 0 };
1111

1212
this.containerOptions = { ...this.props.options };
1313

0 commit comments

Comments
 (0)