|
2 | 2 |
|
3 | 3 | This module is part of a [monorepo](https://github.com/ZeeCoder/container-query). |
4 | 4 |
|
5 | | -Detailed documentation can be found there. |
6 | | - |
7 | | -## Install |
8 | | - |
9 | | -``` |
10 | | -yarn add @zeecoder/react-container-query |
11 | | -# or |
12 | | -npm install --save @zeecoder/react-container-query |
13 | | -``` |
14 | | - |
15 | | -### Set up PostCSS with webpack |
16 | | - |
17 | | -[Here](https://github.com/ZeeCoder/container-query/blob/master/docs/postcss.md) |
18 | | -is a documentation on how to do just that, in order to get the same syntax I've |
19 | | -been using. |
20 | | - |
21 | | -## Usage |
22 | | - |
23 | | -Assuming the following CSS: |
24 | | - |
25 | | -```pcss |
26 | | -.App { |
27 | | - background: red; |
28 | | - font-size: 20px; |
29 | | - color: white; |
30 | | - padding: 10px; |
31 | | - border: none; |
32 | | -
|
33 | | - @container (width > 900px) { |
34 | | - background: green; |
35 | | - } |
36 | | -} |
37 | | -``` |
38 | | - |
39 | | -And assuming that `meta` is the object obtained by running the above CSS |
40 | | -through the [postcss plugin](https://github.com/ZeeCoder/container-query/tree/master/packages/postcss-container-query). |
41 | | - |
42 | | -### `<ContainerQuery>` |
43 | | - |
44 | | -A render-prop approach. |
45 | | - |
46 | | -```js |
47 | | -import React, { Component } from "react"; |
48 | | -import { ContainerQuery } from "@zeecoder/react-container-query"; |
49 | | -import { meta } from "./App.pcss"; |
50 | | - |
51 | | -const App = () => ( |
52 | | - <ContainerQuery meta={meta}> |
53 | | - <div className="App">My App</div> |
54 | | - </ContainerQuery> |
55 | | -); |
56 | | - |
57 | | -export default App; |
58 | | -``` |
59 | | - |
60 | | -If you're also interested in the component's size: |
61 | | - |
62 | | -```js |
63 | | -import React, { Component } from "react"; |
64 | | -import { ContainerQuery } from "@zeecoder/react-container-query"; |
65 | | -import { meta } from "./App.pcss"; |
66 | | - |
67 | | -const App = () => ( |
68 | | - <ContainerQuery meta={meta}> |
69 | | - {size => ( |
70 | | - <div className="App"> |
71 | | - My size is: {size.width}x{size.height} |
72 | | - </div> |
73 | | - )} |
74 | | - </ContainerQuery> |
75 | | -); |
76 | | - |
77 | | -export default App; |
78 | | -``` |
79 | | - |
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 | | - |
130 | | -### `withContainerQuery` |
131 | | - |
132 | | -If you prefer Higher-Order Components: |
133 | | - |
134 | | -```js |
135 | | -import { withContainerQuery } from "@zeecoder/react-container-query"; |
136 | | -import { meta } from "./App.pcss"; |
137 | | - |
138 | | -const App = () => <div className="App">My App</div>; |
139 | | - |
140 | | -export default withContainerQuery(App, meta); |
141 | | -``` |
| 5 | +Detailed documentation can be found [here](https://github.com/ZeeCoder/container-query/blob/master/docs/react.md). |
142 | 6 |
|
143 | 7 | ## License |
144 | 8 |
|
|
0 commit comments