Skip to content

Commit 6766570

Browse files
committed
ContainerQuery refactor with the "as" prop
1 parent bd47f38 commit 6766570

File tree

2 files changed

+47
-38
lines changed

2 files changed

+47
-38
lines changed

CHANGELOG.md

+15-10
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,29 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
77

88
## [Unreleased]
99

10+
## [3.0.0-alpha.2]
11+
1012
### Removed
1113

1214
- `react-container-query`
13-
- [BREAKING] Removed the "render" prop in favour of using children. (The
14-
former was not even documented.)
15-
- [BREAKING] Removed the `ResizeObserver` component in favour of the
15+
- **[BREAKING]** Removed the "render" prop from ContainerQuery in favour of
16+
using children. (The former was not even documented.)
17+
- **[BREAKING]** Removed the `ResizeObserver` component in favour of the
1618
[react-resize-observer](https://github.com/ZeeCoder/react-resize-observer) and
1719
[use-resize-observer](https://github.com/ZeeCoder/use-resize-observer) packages.
18-
- [BREAKING] Removed the default export. (Both ContainerQuery and withContainerQuery
20+
- **[BREAKING]** Removed the default export. (Both ContainerQuery and withContainerQuery
1921
is still available as named exports.)
20-
- [BREAKING] Removed the `stats` prop from ContainerQuery. (Use `meta` instead.)
22+
- **[BREAKING]** Removed the `stats` prop from ContainerQuery. (Use `meta` instead.)
2123

2224
### Changed
2325

2426
- `react-container-query`
2527
- `ContainerQuery` no longer returns with a "null" size object when a function
2628
is passed in as the children prop. Instead it returns with `{width: 0, height: 0}`
2729
initially, then updates with the observed container element.
30+
- **[BREAKING]** `ContainerQuery` now renders a div root node by default, inside
31+
of which it renders all children. (Also accepts an `as` prop to change the
32+
tag type.)
2833

2934
## [3.0.0-alpha.1]
3035

@@ -42,22 +47,22 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
4247
### Removed
4348

4449
- "test" script is now only in the root, instead of in every package.
45-
- [BREAKING] No longer testing for Node 7
50+
- **[BREAKING]** No longer testing for Node 7
4651

4752
### Changed
4853

4954
- `container-query-meta-builder`
5055

5156
- Removed the engines field package.json field
52-
- [BREAKING] Removed the UMD build
57+
- **[BREAKING]** Removed the UMD build
5358

5459
- `postcss-container-query`
55-
- [BREAKING] Minimum Node version lifted to v8
56-
- [BREAKING] Dropped the `getJSON` option, and no json is saved by default.
60+
- **[BREAKING]** Minimum Node version lifted to v8
61+
- **[BREAKING]** Dropped the `getJSON` option, and no json is saved by default.
5762
The new behaviour is to use the ICSS `:export` syntax, which then can be
5863
imported through css-loader. (The meta object is still passed down in the
5964
postcss plugin messages in case it's needed.)
60-
- [BREAKING] `saveMeta` is removed
65+
- **[BREAKING]** `saveMeta` is removed
6166

6267
## [2.7.4]
6368

Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
import React, { Component } from "react";
2-
import ReactDOM from "react-dom";
32
import PropTypes from "prop-types";
43
import Container from "@zeecoder/container-query";
54

6-
export default class ContainerQuery extends Component {
5+
class ContainerQuery extends Component {
76
constructor(props) {
87
super(props);
98

10-
this.state = { width: 0, height: 0 };
11-
12-
this.containerOptions = { ...this.props.options };
9+
this.options = { ...this.props.options };
1310

1411
// Listen to size changes only if needed
1512
if (typeof this.props.children === "function") {
16-
this.containerOptions.handleResize = this.handleResize;
13+
this.options.handleResize = this.handleResize;
1714
}
1815
}
1916

17+
state = { width: 0, height: 0 };
18+
19+
ref = React.createRef();
20+
2021
handleResize = size => {
2122
if (this.__willUnmount) {
2223
return;
@@ -25,33 +26,33 @@ export default class ContainerQuery extends Component {
2526
this.setState(size);
2627
};
2728

28-
instantiateContainer() {
29-
if (!this.props.meta) {
30-
return;
31-
}
32-
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-
}
38-
}
39-
4029
componentDidMount() {
41-
this.instantiateContainer();
42-
}
43-
44-
componentDidUpdate() {
45-
this.instantiateContainer();
30+
new Container(this.ref.current, this.props.meta, this.options);
4631
}
4732

4833
componentWillUnmount() {
4934
this.__willUnmount = true;
5035
}
5136

5237
render() {
38+
// Removing all props only used by this component, and only passing
39+
// through the rest that was supposedly meant for the wrapped child
40+
const { options, as, meta, children, ...props } = this.props;
41+
42+
// Needs to start with a capital letter for the jsx compiler.
43+
// @see https://stackoverflow.com/a/38823404
44+
const TagName = as;
45+
46+
return (
47+
<TagName {...props} ref={this.ref}>
48+
{this.doRender()}
49+
</TagName>
50+
);
51+
}
52+
53+
doRender() {
5354
if (typeof this.props.children === "function") {
54-
return this.props.children(this.state.size);
55+
return this.props.children(this.state);
5556
} else if (this.props.children) {
5657
return this.props.children;
5758
}
@@ -61,12 +62,15 @@ export default class ContainerQuery extends Component {
6162
}
6263

6364
ContainerQuery.defaultProps = {
64-
meta: null,
65-
options: {}
65+
options: {},
66+
as: "div"
6667
};
6768

6869
ContainerQuery.propTypes = {
69-
meta: PropTypes.object,
7070
options: PropTypes.object,
71-
children: PropTypes.oneOfType([PropTypes.element, PropTypes.func])
71+
as: PropTypes.string,
72+
meta: PropTypes.object.isRequired,
73+
children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]).isRequired
7274
};
75+
76+
export default ContainerQuery;

0 commit comments

Comments
 (0)