15
15
A PostCSS plugin and Javascript runtime combination, which allows you to write
16
16
** container queries** in your CSS the same way you would write ** media queries** .
17
17
18
+ 🕶 ** Resize Observer** : Uses the native ResizeObserver implentation when available,
19
+ and falls back to a [ polyfill] ( https://github.com/que-etc/resize-observer-polyfill )
20
+ to detect size changes. If you use Chrome, [ you can test] ( https://codesandbox.io/s/l3rmm1rz2l )
21
+ how performant the plugin is with the native implementation. (Shipped in v64.)
22
+
23
+ 📦 ** Bundler Support** : Built with bundlers in mind, like [ Parcel] ( https://parceljs.org ) ,
24
+ [ webpack] ( https://webpack.js.org ) , [ Browserify] ( http://browserify.org/ ) among others.
25
+
26
+ 🎲 ** Component Support** : Built with component-based libraries in mind, like
27
+ [ React] ( https://reactjs.org ) , [ Vue] ( https://vuejs.org/ ) , [ Ember] ( https://emberjs.com/ ) ,
28
+ [ Angular] ( https://angularjs.org/ ) among others.
29
+
30
+ 📄 ** Intuitive syntax** : Uses at-rules, the same way @media queries are:
31
+ ` @container (...) { ... } `
32
+
33
+ 🎉 ** Container Units** : [ rh, rw, rmin, rmax,] ( docs/syntax.md#Units ) which are
34
+ relative to the container element's width and / or height. (Same way viewport
35
+ units are relative to the viewport's size.)
36
+
18
37
## Introduction
19
38
20
- Container queries work the same way media queries do: they allow you to apply
39
+ Container queries are very similar to media queries; they allow you to apply
21
40
styles to elements when certain conditions are met.
22
41
23
- While media queries are relative to the viewport's size, container queries are
24
- relative to a container element's size.
42
+ The key difference is that while media queries are relative to the viewport's
43
+ size, container queries are relative to a container element's size.
44
+
45
+ Thanks to this trait, you can have multiple instances of the same container
46
+ element, all of which changes its own- and its childrens' styles based on the
47
+ containing element's size.
25
48
26
49
** What is a Container?**
27
50
28
- A container is just an HTML element, which may contain other elements.
51
+ A container is just an HTML element, which may (or may not) contain other elements.
29
52
30
53
You may want to think of them as "** Components** " ([ React] ( https://facebook.github.io/react/docs/components-and-props.html ) )
31
54
or "** Blocks** " ([ BEM] ( http://getbem.com/naming/ ) ).
32
55
33
- ## Highlights
56
+ ## Demos
34
57
35
- - Built with webpack / React in mind, but can be used with legacy projects too.
36
- - Uses a [ ResizeObserver polyfill] ( https://github.com/que-etc/resize-observer-polyfill )
37
- to detect size changes. If you use Chrome, you can test how performant the plugin
38
- is with the native ` ResizeObserver ` (shipped in 64): https://codesandbox.io/s/l3rmm1rz2l
39
- - Intuitive media query like syntax: ` @container (...) { /* ... */ } `
40
- - Supports container units: rh, rw, rmin, rmax. (Useful to set font-size
41
- and other properties to a value that's changing with the container's size.)
42
- - Diffing algorithm which applies / removes only the necessary styles when a
43
- change occurs.
58
+ The best way to understand the concept, if you play around with it yourself.
44
59
45
- ## Look and feel
60
+ - [ Nested components] ( https://codesandbox.io/s/k9n28rkkl7 )
61
+ - [ Social Posts] ( https://codesandbox.io/s/0l71yp80w )
62
+ - [ Without React] ( https://codesandbox.io/s/mo7nr90vmj )
63
+
64
+ Note that because these demos are hosted on [ CodeSandbox] ( https://codesandbox.io )
65
+ where webpack or PostCSS cannot (currently) be configured, styles are simply
66
+ imported as strings and processed in the browser.
67
+ (Using [ @zeecoder/cq-demo-utils ] ( https://github.com/ZeeCoder/cq-demo-utils ) .)
68
+
69
+ While this works demo purposes, in a real application it is strongly recommended
70
+ to process styles build-time, as later described in this documentation.
71
+
72
+ ## Look and Feel
73
+
74
+ // todo remove once everything here is covered by other sections
46
75
47
76
``` pcss
48
77
// User.pcss
@@ -83,6 +112,10 @@ The html then could look like this:
83
112
</div >
84
113
```
85
114
115
+ (Note that for container queries and units to work, all elements must be
116
+ descendants of the container, as their conditions and values only makes sense in
117
+ a container's context.)
118
+
86
119
Finally, after you create a new ` Container ` instance, (passing in the container
87
120
HTMLElement, and the extracted metadata) everything will just work.
88
121
@@ -93,68 +126,40 @@ declaration, but it's encouraged to have a dedicated file for each component.
93
126
## Documentation
94
127
95
128
- [ Getting Started] ( docs/getting-started.md )
96
- - [ Usage with webpack and React] ( docs/webpack-and-react.md )
97
- - [ Usage with Gulp] ( docs/gulp.md )
129
+ - [ Parcel] ( docs/parcel.md )
130
+ - [ webpack] ( docs/webpack.md )
131
+ - [ Gulp] ( docs/gulp.md )
132
+ - [ React] ( docs/react.md )
133
+ - [ CSS Modules] ( docs/css-modules.md )
98
134
- [ Multiple Containers] ( docs/multiple-containers.md )
99
- - [ Usage without webpack] ( docs/without-webpack.md )
135
+ - [ Without React] ( docs/without-react.md )
136
+ - [ CSS Preprocessors] ( docs/css-preprocessors.md )
100
137
- [ Syntax] ( docs/syntax.md )
101
- - [ API] ( docs/api.md )
102
- - [ Usage with CSS Preprocessors] ( docs/css-preprocessors.md )
138
+ - [ JS API] ( docs/js-api.md )
139
+ - [ Caveats] ( docs/caveats.md )
140
+ - [ CSS-in-JS] ( docs/css-in-js.md )
103
141
104
- ## Demos
142
+ ## Thoughts on Design
105
143
106
- Note that because these demos are hosted on [ CodeSandbox] ( https://codesandbox.io )
107
- where webpack cannot be configured, styles are simply imported as strings and
108
- processed in the browser. (using [ @zeecoder/cq-demo-utils ] ( https://github.com/ZeeCoder/cq-demo-utils ) )
109
-
110
- While this works for the demos, in a real application it is strongly recommended
111
- to process styles build-time.
112
-
113
- - [ Nested components] ( https://codesandbox.io/s/k9n28rkkl7 )
114
- - [ Social Posts] ( https://codesandbox.io/s/0l71yp80w )
115
- - [ Without React] ( https://codesandbox.io/s/mo7nr90vmj )
116
-
117
- ## Browser Support
118
-
119
- Works with all modern browsers and IE10+
120
-
121
- ## Caveats / Notes
122
-
123
- - The ResizeObserver polyfill reacts in ~ 20ms. For the most part that should be ok, but
124
- if you need more control over when a container applies new styles, however, you
125
- can switch off the observing behaviour, and call the ` adjust ` method on the
126
- Container instance manually, when you see fit.
127
- Due to this 20ms reaction time, the more you nest containers, the slower change
128
- propagates from top to bottom. (** This is a no longer an issue** if the native
129
- ` ResizeObserver ` is available, for example in Chrome 64 and up.)
130
- - Styles are applied with the ` Element.style.setProperty ` method by default.
131
- This logic will probably be configurable in the future (#50 ) which will allow for
132
- different approaches. (Using [ Styletron] ( https://github.com/rtsao/styletron ) , for
133
- instance.)
134
- - With element / container query solutions, circularity issues may arise. While
135
- [ an attempt] ( https://github.com/ZeeCoder/container-query/issues/20 ) to tackle
136
- this was made, the same is still unfortunately true to this library as well.
137
- Use your best judgement when setting up container queries / units to avoid these
138
- issues.
139
-
140
- ## Thoughts on design
141
-
142
- In case you're wondering about the tool's design, here is a list of goals I
143
- had in mind when I started:
144
+ In case you're wondering about the tool's design, here is a list of goals I had
145
+ in mind when I started:
144
146
145
147
- Should be tested,
146
- - Should use containers instead of elements,
148
+ - Should use containers instead of elements (As opposed to other "element-query"
149
+ libraries.),
147
150
- Should use a media query-like syntax so that it's familiar and easy to use,
148
- - Should be easy enough to use, but a transpiling step would be assumed,
149
151
- Should uses PostCSS for preprocessing, instead of having a runtime parser,
150
- - Should use JS modules, so it plays nicely with bundlers (webpack, Browserify,
151
- etc.) and Component-oriented UI libraries (React, Vue, etc.),
152
- - Shouldn't be limited to CSS syntax. (Utilising custom at-rules for instance),
153
- - Should work with component naming methodologies - like BEM or SUIT - the best.
152
+ - Should be easy enough to use, but a transpiling step would be assumed (Due to
153
+ the reason above.),
154
+ - Should use ES modules, so it plays nicely with bundlers (Parcel, webpack
155
+ Browserify, etc.) and with Component-oriented UI libraries (React, Vue, etc.),
156
+ - Should work best with component naming methodologies, like BEM or SUIT, but
157
+ should also work without them.
158
+ - Should work with CSS Modules.
154
159
155
- ## Next up
160
+ ## Next Up
156
161
157
- [ Ideas for enhancement ] ( https://goo.gl/7XtjDe )
162
+ [ Ideas for Enhancement ] ( https://goo.gl/7XtjDe )
158
163
159
164
## Alternatives
160
165
@@ -168,15 +173,25 @@ convinced by this solution, then I encourage you to look at these alternatives:
168
173
169
174
## WICG
170
175
171
- We at the WICG dived into 2018 with renewed effort to make native
172
- Container Queries a reality in browsers.
176
+ The WICG dived into 2018 with renewed effort to make native Container Queries
177
+ a reality in browsers.
173
178
174
179
If you're interested in how things are progressing, please feel free to visit
175
180
the following links, where the disussions are happening:
176
181
177
- - https://github.com/WICG/cq-usecases
178
- - https://github.com/WICG/container-queries
179
182
- [ Slack] ( https://join.slack.com/t/containerqueries/shared_invite/enQtMzA2OTc5MDUwNjk1LTEwMWEzNjcwMTY1MGYzYWMyOGMxM2MzNDM1OGZjMjM3NDNiMDMxYTk0YjQxN2FjYTZkYmZkMDZmOWE1ODRkZWI )
183
+ - [ Use Cases] ( https://github.com/WICG/cq-usecases )
184
+ - [ General Discussion] ( https://github.com/WICG/container-queries )
185
+
186
+ ## Related
187
+
188
+ - [ @zeecoder/react-resize-observer ] ( https://github.com/ZeeCoder/react-resize-observer )
189
+ - [ use-resize-observer] ( https://github.com/ZeeCoder/use-resize-observer )
190
+
191
+ ## Support
192
+
193
+ Please consider supporting me if you like what I do on my
194
+ [ Patreon] ( https://www.patreon.com/zeecoder ) page.
180
195
181
196
## Big Thanks
182
197
0 commit comments