Gathering detailed insights and metrics for react-container-query
Gathering detailed insights and metrics for react-container-query
Gathering detailed insights and metrics for react-container-query
Gathering detailed insights and metrics for react-container-query
npm install react-container-query
Typescript
Module System
Node Version
NPM Version
84.9
Supply Chain
93.6
Quality
77.8
Maintenance
100
Vulnerability
100
License
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
JavaScript (74.85%)
TypeScript (23.2%)
Shell (1.95%)
Total Downloads
6,494,748
Last Day
593
Last Week
11,702
Last Month
73,375
Last Year
954,204
890 Stars
272 Commits
47 Forks
13 Watching
12 Branches
15 Contributors
Minified
Minified + Gzipped
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
Cumulative downloads
Total Downloads
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
2
38
True modularity in styling responsive component.
1npm i -D react-container-query
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.
Compare the hook style code with the original example from https://github.com/react-container-query/react-container-query#containerquery-queryquery-initialsizewidth-height
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};
React.forwardRef
.ref
and set it on the actual native DOM element it renders (e.g, div
, span
, etc) - as seen in th example below1import 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};
<MyCustomWrapper />
would forward the containerRef
and set it on the div
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'));
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 optional width
or height
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 time initialSize
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.
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.
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.
Checkout CodePen
You can also check out examples directory.
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
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
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
63 existing vulnerabilities detected
Details
Score
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