Installations
npm install react-container-query
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
18.14.0
NPM Version
9.3.1
Score
84.9
Supply Chain
93.6
Quality
77.8
Maintenance
100
Vulnerability
100
License
Releases
v0.12.1
Published on 21 Sept 2022
v0.12.0
Published on 25 Feb 2021
Patch: Un-Prefix a non-legacy React lifecycle methods
Published on 21 Jul 2020
Patch: Prefix legacy React lifecycle methods
Published on 03 Apr 2020
Upgrade CircleCI config to v2 and update docs to reflect new org
Published on 24 Jan 2020
Remove lodash from prod dependencies to reduce size
Published on 05 Feb 2018
Contributors
Unable to fetch Contributors
Languages
JavaScript (74.85%)
TypeScript (23.2%)
Shell (1.95%)
Developer
Download Statistics
Total Downloads
6,494,748
Last Day
593
Last Week
11,702
Last Month
73,375
Last Year
954,204
GitHub Statistics
890 Stars
272 Commits
47 Forks
13 Watching
12 Branches
15 Contributors
Bundle Size
21.39 kB
Minified
6.72 kB
Minified + Gzipped
Package Meta Information
Latest Version
0.13.0
Package Id
react-container-query@0.13.0
Unpacked Size
236.26 kB
Size
51.64 kB
File Count
22
NPM Version
9.3.1
Node Version
18.14.0
Publised On
14 Sept 2023
Total Downloads
Cumulative downloads
Total Downloads
6,494,748
Last day
-81.6%
593
Compared to previous day
Last week
-30%
11,702
Compared to previous week
Last month
-17.2%
73,375
Compared to previous month
Last year
-2.6%
954,204
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
2
Dev Dependencies
38
React Container Query
True modularity in styling responsive component.
Installation
1npm i -D react-container-query
Disclaimer
This code in this repository is provided under an open source license. It's provided by the individuals who contribute to the project in their personal capacity, not by any of their employers.
API
useContainerQuery(query, initialSize?)
Compare the hook style code with the original example from https://github.com/react-container-query/react-container-query#containerquery-queryquery-initialsizewidth-height
Hook Example 1 - Queries against a native DOM element as the container
- Native DOM element refers to
div
,span
, etc.
1import React from 'react'; 2import { useContainerQuery } from 'react-container-query'; 3 4const query = { 5 'width-between-400-and-599': { 6 minWidth: 400, 7 maxWidth: 599, 8 }, 9 'width-larger-than-600': { 10 minWidth: 600, 11 }, 12}; 13 14const MyComponent = () => { 15 const [params, containerRef] = useContainerQuery(query); 16 return <div ref={containerRef} className={classnames(params)}>the box</div>; 17};
Hook Example 2 - Usage for a React component as the container - React.forwardRef
- If the container element we want to measure is a React component, and since we can't measure the size of React component itself, we can use
React.forwardRef
. - The container React component must then forward the
ref
and set it on the actual native DOM element it renders (e.g,div
,span
, etc) - as seen in th example below
1import React from 'react'; 2import { useContainerQuery } from 'react-container-query'; 3 4const query = { 5 'width-between-400-and-599': { 6 minWidth: 400, 7 maxWidth: 599, 8 }, 9 'width-larger-than-600': { 10 minWidth: 600, 11 }, 12}; 13 14const MyCustomWrapper = React.forwardRef((props, ref) => { 15 // MyCustomWrapper really renders a div which wraps the children. 16 // Setting the ref on it allows container query to measure its size. 17 return <div ref={ref}>{props.children}</div> 18}); 19 20const MyComponent = () => { 21 const [params, containerRef] = useContainerQuery(query); 22 return <MyCustomWrapper ref={containerRef} className={classnames(params)}>the box</div>; 23};
- In this example,
<MyCustomWrapper />
would forward thecontainerRef
and set it on thediv
element it is using to wrap the children.
<ContainerQuery query={query} initialSize?={{width?, height?}}>
1import React, {Component} from 'react'; 2import {render} from 'react-dom'; 3import {ContainerQuery} from 'react-container-query'; 4import classnames from 'classnames'; 5 6const query = { 7 'width-between-400-and-599': { 8 minWidth: 400, 9 maxWidth: 599 10 }, 11 'width-larger-than-600': { 12 minWidth: 600, 13 } 14}; 15 16function MyComponent() { 17 /** 18 * `params` in the children function will look like 19 * { 20 * 'width-between-400-and-599': true, 21 * 'width-larger-than-600': false 22 * } 23 */ 24 return ( 25 <ContainerQuery query={query}> 26 {(params) => ( 27 <div className={classnames(params)}>the box</div> 28 )} 29 </ContainerQuery> 30 ); 31}; 32 33/** 34 * This will generate following HTML: 35 * <div class="width-between-400-and-599"></div> 36 */ 37 38render(<MyComponent/>, document.getElementById('app'));
properties
-
props.children
Must be a function to return a single or an array of React elements. The function will be invoked with
params
, which is a key-value pair where keys are class names, values are booleans to indicate if that class name's constraints are all satisfied. -
props.query
"query" is key-value pairs where keys are the class names that will be applied to container element when all constraints are met. The values are the constraints.
-
props.initialSize?
(optional)initialSize
is an object with optionalwidth
orheight
property. Because the limitation on how size is computed based on underlying element, in the initial rendering pass, we don't have the size info (because element must be in the DOM have a valid size). At this timeinitialSize
will be used as the size of the element.
applyContainerQuery(Component, query, initialSize?) -> ReactComponent
1import React, {Component} from 'react'; 2import {render} from 'react-dom'; 3import {applyContainerQuery} from 'react-container-query'; 4import classnames from 'classnames'; 5 6const query = { 7 'width-between-400-and-599': { 8 minWidth: 400, 9 maxWidth: 599 10 }, 11 'width-larger-than-600': { 12 minWidth: 600, 13 } 14}; 15 16class Container extends Component { 17 render() { 18 /** 19 * `this.props.containerQuery` will look like 20 * { 21 * 'width-between-400-and-599': true, 22 * 'width-larger-than-600': false 23 * } 24 */ 25 return <div className={classnames(this.props.containerQuery)}>the box</div>; 26 } 27} 28 29const App = applyContainerQuery(Container, query) 30 31/** 32 * This will generate following HTML: 33 * <div class="width-between-400-and-599"></div> 34 */ 35 36render(<App/>, document.getElementById('app'));
This is a very similar to <ContainerQuery/>
, except it's higher order component style. You don't have to use them together.
Why
Modularity is the heart of component based UI. With most JavaScript modularized, CSS failed to catch up. When developing a responsive web page, 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.
What is container query
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). Container Queries: Once More Unto the Breach is the inspiration of this repo.
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.
1.box { 2 background-color: red; 3} 4 5.container:media(min-width: 400px) { 6 .box { 7 background-color: green; 8 } 9} 10 11.container:media(min-width: 600px) { 12 .box { 13 background-color: blue; 14 } 15}
Note: This library does not provide these CSS features.
Demo
Checkout CodePen
- Adjustable Sidebar http://codepen.io/daiweilu/pen/wMrrZM
- Responsive Component Layout https://codepen.io/daiweilu/pen/EXWRqx
You can also check out examples directory.
Performance
react-container-query is using element-resize-detector in mainstream browsers and ResizeObserver in cutting edge browsers. It's completely event based, so no excessive code runs if no changes on element sizes.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE.md:0
- Info: FSF or OSI recognized license: MIT License: LICENSE.md:0
Reason
Found 4/16 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 27 are checked with a SAST tool
Reason
63 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-6chw-6frg-f759
- Warn: Project is vulnerable to: GHSA-v88g-cgmw-v5xw
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-fwr7-v2mv-hh25
- Warn: Project is vulnerable to: GHSA-wf5p-g6vw-rhxx
- Warn: Project is vulnerable to: GHSA-qwcr-r2fm-qrc7
- Warn: Project is vulnerable to: GHSA-cwfw-4gq5-mrqx
- Warn: Project is vulnerable to: GHSA-g95f-p29q-9xw4
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-x9w5-v3q2-3rhw
- Warn: Project is vulnerable to: GHSA-pxg6-pf52-xh8x
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq
- Warn: Project is vulnerable to: GHSA-434g-2637-qmqr
- Warn: Project is vulnerable to: GHSA-49q7-c7j4-3p7m
- Warn: Project is vulnerable to: GHSA-977x-g7h5-7qgw
- Warn: Project is vulnerable to: GHSA-f7q4-pwc6-w24p
- Warn: Project is vulnerable to: GHSA-fc9h-whq2-v747
- Warn: Project is vulnerable to: GHSA-r7qp-cfhv-p84w
- Warn: Project is vulnerable to: GHSA-q9mw-68c2-j6m5
- Warn: Project is vulnerable to: GHSA-4gmj-3p3h-gm8h
- Warn: Project is vulnerable to: GHSA-jchw-25xp-jwwc
- Warn: Project is vulnerable to: GHSA-cxjh-pqwp-8mfp
- Warn: Project is vulnerable to: GHSA-qqgx-2p2h-9c37
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-76p3-8jx3-jpfq
- Warn: Project is vulnerable to: GHSA-3rfm-jhwj-7488
- Warn: Project is vulnerable to: GHSA-hhq3-ff78-jv3g
- Warn: Project is vulnerable to: GHSA-jf85-cpcp-j695
- Warn: Project is vulnerable to: GHSA-fvqr-27wr-82fm
- Warn: Project is vulnerable to: GHSA-4xc9-xhrj-v574
- Warn: Project is vulnerable to: GHSA-x5rq-j2xg-h7qm
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-p6mc-m468-83gw
- Warn: Project is vulnerable to: GHSA-4xcv-9jjx-gfj3
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-hxm2-r34f-qmc5
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-vh95-rmgr-6w4m / GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-mwcw-c2x4-8c55
- Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-m6fv-jmcg-4jfg
- Warn: Project is vulnerable to: GHSA-cm22-4g7w-348p
- Warn: Project is vulnerable to: GHSA-25hc-qcg6-38wj
- Warn: Project is vulnerable to: GHSA-cqmj-92xf-r6r9
- Warn: Project is vulnerable to: GHSA-3jfq-g458-7qm9
- Warn: Project is vulnerable to: GHSA-r628-mhmh-qjhw
- Warn: Project is vulnerable to: GHSA-9r2w-394v-53qc
- Warn: Project is vulnerable to: GHSA-5955-9wpr-37jh
- Warn: Project is vulnerable to: GHSA-qq89-hq3f-393p
- Warn: Project is vulnerable to: GHSA-f5x3-32g6-xq36
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-7p7h-4mm5-852v
- Warn: Project is vulnerable to: GHSA-fhg7-m89q-25r3
- Warn: Project is vulnerable to: GHSA-wr3j-pwj9-hqq6
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
- Warn: Project is vulnerable to: GHSA-p9pc-299p-vxgp
Score
2.3
/10
Last Scanned on 2024-12-16
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More