1 | # React Container Query
|
2 |
|
3 | [![npm version](https://badge.fury.io/js/react-container-query.svg)](https://badge.fury.io/js/react-container-query)
|
4 | [![Circle CI](https://circleci.com/gh/d6u/react-container-query/tree/master.svg?style=svg)](https://circleci.com/gh/d6u/react-container-query/tree/master)
|
5 | [![Build Status](https://saucelabs.com/open_sauce/build_status/react-cq.svg)](https://saucelabs.com/beta/builds/8b89748df1f0408a9311b0d33d545cee)
|
6 | [![codecov](https://codecov.io/gh/d6u/react-container-query/branch/master/graph/badge.svg)](https://codecov.io/gh/d6u/react-container-query)
|
7 |
|
8 | [![Build Status](https://saucelabs.com/open_sauce/build_matrix/react-cq.svg)](https://saucelabs.com/beta/builds/8b89748df1f0408a9311b0d33d545cee)
|
9 |
|
10 | ## Intro
|
11 |
|
12 | **True modularity in styling responsive component.**
|
13 |
|
14 | Modularity is the heart of component based UI. With most JavaScript modularized, CSS failed to catch up. When developing responsive webpage, we use media queries to toggle styles based on the size of the viewport. This creates problems when creating component level styles. The same component will behave differently when it is placed in different locations on a page. It seriously breaks the modularity of a component. We need components to be responsive and independent of viewport sizes.
|
15 |
|
16 | ## What is Container Query
|
17 |
|
18 | **For real usage see [usage](#usage) section.**
|
19 |
|
20 | Container query is a work in process CSS feature. "Container queries allow an author to control styling based on the size of a containing element rather than the size of the user’s viewport." (from [Container Query](http://responsiveimagescg.github.io/container-queries/)). [Container Queries: Once More Unto the Breach](http://alistapart.com/article/container-queries-once-more-unto-the-breach) is the inspiration of this repo.
|
21 |
|
22 | With below CSS, `.box` will be blue when `.container` is wider than 600px, green when width between 400px and 599px, and red for the rest of time.
|
23 |
|
24 | ```css
|
25 | .box {
|
26 | background-color: red;
|
27 | }
|
28 |
|
29 | .container:media(min-width: 400px) {
|
30 | .box {
|
31 | background-color: green;
|
32 | }
|
33 | }
|
34 |
|
35 | .container:media(min-width: 600px) {
|
36 | .box {
|
37 | background-color: blue;
|
38 | }
|
39 | }
|
40 | ```
|
41 |
|
42 | ## Usage
|
43 |
|
44 | ```sh
|
45 | npm install --save-dev react-container-query
|
46 | ```
|
47 |
|
48 | **react-container-query doesn't specify peerDependencies in package.json, but you should have react and react-dom available.**
|
49 |
|
50 | ### `applyContainerQuery(Component, query, [opts])`
|
51 |
|
52 | Create [higher order component](https://gist.github.com/sebmarkbage/ef0bf1f338a7182b6775) for container.
|
53 |
|
54 | ```js
|
55 | import React, { Component } from 'react';
|
56 | import { render } from 'react-dom';
|
57 | import classnames from 'classnames';
|
58 | import { applyContainerQuery } from 'react-container-query';
|
59 |
|
60 | class MyComponent extends Component {
|
61 | render() {
|
62 | // `this.props.containerQuery` is a property provided by the higher order
|
63 | // component. It will look like:
|
64 | //
|
65 | // {
|
66 | // width_between_400_and_599: true,
|
67 | // width_larger_than_600: false
|
68 | // }
|
69 | return (
|
70 | <div className={classnames('container', this.props.containerQuery)}>
|
71 | <div className='box'>the box</div>
|
72 | </div>
|
73 | );
|
74 | }
|
75 | }
|
76 |
|
77 | const query = {
|
78 | 'width-between-400-and-599': {
|
79 | minWidth: 400,
|
80 | maxWidth: 599
|
81 | },
|
82 | 'width-larger-than-600': {
|
83 | minWidth: 600,
|
84 | }
|
85 | };
|
86 |
|
87 | const HigherOrderComponent = applyContainerQuery(MyComponent, query);
|
88 |
|
89 | render(<HigherOrderComponent/>, document.getElementById('app'));
|
90 | ```
|
91 |
|
92 | #### options
|
93 |
|
94 | - `opts.propName = "containerQuery"`: use this arguments, you can customized the name of property to pass container query state to.
|
95 |
|
96 | ## Demo
|
97 |
|
98 | Checkout CodePen
|
99 |
|
100 | - Adjustable Sidebar http://codepen.io/daiweilu/pen/wMrrZM
|
101 | - Responsive Component Layout http://codepen.io/daiweilu/pen/XXexrj
|
102 |
|
103 | You can also checkout [examples](./examples).
|
104 |
|
105 | ## Performance
|
106 |
|
107 | React Contianer Query is using [element-resize-detector](https://www.npmjs.com/package/element-resize-detector) to make sure it's performant. It's completely event based, so no excessive code runs if no changes on element sizes.
|