Skip to content

Commit 054bc13

Browse files
committed
element prop on the ContainerQuery component
1 parent b926fca commit 054bc13

File tree

5 files changed

+97
-30
lines changed

5 files changed

+97
-30
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export const CONDITIONS = "a";
2+
export const ELEMENTS = "b";
3+
export const VALUES = "c";
4+
export const STYLES = "d";
5+
export const SELECTOR = "e";
6+
export const QUERIES = "f";
7+
8+
// Debugging
9+
// export const CONDITIONS = "conditions";
10+
// export const ELEMENTS = "elements";
11+
// export const VALUES = "values";
12+
// export const STYLES = "styles";
13+
// export const SELECTOR = "selector";
14+
// export const QUERIES = "queries";

packages/container-query-meta-builder/src/index.js

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,24 @@ import objectAssign from "object-assign";
22
import isValueUsingContainerUnits from "./isValueUsingContainerUnits";
33
import getConditionsFromQueryParams from "./getConditionsFromQueryParams";
44
import remapMetaSelectors from "./remapMetaSelectors";
5-
6-
export { remapMetaSelectors };
7-
8-
export const CONDITIONS = "a";
9-
export const ELEMENTS = "b";
10-
export const VALUES = "c";
11-
export const STYLES = "d";
12-
export const SELECTOR = "e";
13-
export const QUERIES = "f";
14-
15-
// export const CONDITIONS = "conditions";
16-
// export const ELEMENTS = "elements";
17-
// export const VALUES = "values";
18-
// export const STYLES = "styles";
19-
// export const SELECTOR = "selector";
20-
// export const QUERIES = "queries";
5+
import {
6+
CONDITIONS,
7+
ELEMENTS,
8+
VALUES,
9+
STYLES,
10+
SELECTOR,
11+
QUERIES
12+
} from "./constants";
13+
14+
export {
15+
remapMetaSelectors,
16+
CONDITIONS,
17+
ELEMENTS,
18+
VALUES,
19+
STYLES,
20+
SELECTOR,
21+
QUERIES
22+
};
2123

2224
const isEmpty = obj => Object.keys(obj).length === 0;
2325

packages/container-query-meta-builder/src/remapMetaSelectors.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { SELECTOR, QUERIES, ELEMENTS } from "./index";
1+
import { SELECTOR, QUERIES, ELEMENTS } from "./constants";
22

33
/**
44
* Runs through all selectors of a meta object.

packages/react-container-query/README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ through the [postcss plugin](https://github.com/ZeeCoder/container-query/tree/ma
4444
A render-prop approach.
4545

4646
```js
47+
import React, { Component } from "react";
4748
import { ContainerQuery } from "@zeecoder/react-container-query";
4849
import { meta } from "./App.pcss";
4950

@@ -59,6 +60,7 @@ export default App;
5960
If you're also interested in the component's size:
6061

6162
```js
63+
import React, { Component } from "react";
6264
import { ContainerQuery } from "@zeecoder/react-container-query";
6365
import { meta } from "./App.pcss";
6466

@@ -75,6 +77,56 @@ const App = () => (
7577
export default App;
7678
```
7779

80+
As you can see the ContainerQuery component automatically picks up the child
81+
element as the Container.
82+
83+
To do this, the component internally calls `ReactDOM.findDOMNode(this)`.
84+
If you want to avoid that, you can also pass in the `element` prop:
85+
86+
```js
87+
import React, { Component } from "react";
88+
import { ContainerQuery } from "@zeecoder/react-container-query";
89+
import { meta } from "./App.pcss";
90+
91+
class App extends Component {
92+
constructor(props) {
93+
super(props);
94+
95+
this.state = {
96+
element: null
97+
};
98+
99+
this.ref = React.createRef();
100+
}
101+
102+
updateElementFromRef() {
103+
if (this.state.element !== this.ref.current) {
104+
this.setState({ element: this.ref.current });
105+
}
106+
}
107+
108+
componentDidMount() {
109+
this.updateElementFromRef();
110+
}
111+
112+
componentDidUpdate() {
113+
this.updateElementFromRef();
114+
}
115+
116+
render() {
117+
return (
118+
<ContainerQuery meta={meta} element={this.state.element}>
119+
<div className="App" ref={this.ref}>
120+
My App
121+
</div>
122+
</ContainerQuery>
123+
);
124+
}
125+
}
126+
127+
export default App;
128+
```
129+
78130
### `withContainerQuery`
79131

80132
If you prefer Higher-Order Components:

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

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,27 @@ export default class ContainerQuery extends Component {
2222
return;
2323
}
2424

25-
this.setState({ size });
25+
this.setState(size);
2626
};
2727

28-
componentDidMount() {
28+
instantiateContainer() {
2929
if (!this.props.meta) {
3030
return;
3131
}
3232

33-
this.lastContainer = ReactDOM.findDOMNode(this);
34-
new Container(this.lastContainer, this.props.meta, this.containerOptions);
33+
const element = this.props.element || ReactDOM.findDOMNode(this);
34+
if (element && this.element !== element) {
35+
this.element = element;
36+
new Container(this.element, this.props.meta, this.containerOptions);
37+
}
3538
}
3639

37-
componentDidUpdate() {
38-
if (!this.props.meta) {
39-
return;
40-
}
40+
componentDidMount() {
41+
this.instantiateContainer();
42+
}
4143

42-
const element = ReactDOM.findDOMNode(this);
43-
if (this.lastContainer !== element) {
44-
this.lastContainer = element;
45-
new Container(this.lastContainer, this.props.meta, this.containerOptions);
46-
}
44+
componentDidUpdate() {
45+
this.instantiateContainer();
4746
}
4847

4948
componentWillUnmount() {

0 commit comments

Comments
 (0)