
Security News
Vite Releases Technical Preview of Rolldown-Vite, a Rust-Based Bundler
Vite releases Rolldown-Vite, a Rust-based bundler preview offering faster builds and lower memory usage as a drop-in replacement for Vite.
react-responsive-breakpoints
Advanced tools
A higher-order React component to translate CSS breakpoints into properties
A higher-order React component to translate CSS breakpoints into properties.
Libraries like react-responsive
and react-responsive-component
let you specify media queries in your React code. But if you're using a responsive CSS framework like Bootstrap, Foundation, or even a homegrown one, you probably want your React components to mirror your existing CSS breakpoints without duplicating them in JavaScript.
Published on npm
as react-responsive-breakpoints
.
npm users:
npm install --save react-responsive-breakpoints
yarn users:
yarn add react-responsive-breakpoints
react-responsive-breakpoints
does not include its own version of React. It will use whatever version is already installed in your project.
Then add a DOM element for each breakpoint size in your application. Each DOM element should only appear at its respective breakpoint. For example, if your application uses Bootstrap v3.3, add responsive elements like:
<div class="visible-xs-block" id="marker-xs"></div>
<div class="visible-sm-block" id="marker-sm"></div>
<div class="visible-md-block" id="marker-md"></div>
<div class="visible-lg-block" id="marker-lg"></div>
For best results, create these responsive DOM markers outside of the React application so they are ready as soon as react-responsive-breakpoints
needs them.
Use withBreakpointsCustom(options, Component)
to create a higher-order component around your custom React component.
options
(Object) - your responsive configuration options
options.breakpoints
(Array) - An array of objects. Each object specifies a responsive breakpoint and contains two properties:
prop
(String) - The name of the property that will be passed to Component
indicating whether the breakpoint is activeselector
(String) - A CSS selector for the DOM marker that appears at the breakpoint, e.g. "#some-id"
or ".some-class"
Component
(ReactElement) - Your React componentimport React from 'react';
import { withBreakpointsCustom } from 'react-responsive-breakpoints';
/**
* The "isSize*" properties will be automatically set by react-responsive-breakpoints.
* All other properties pass through as usual.
*/
const MyComponent = ({ counter, isSizeXs, isSizeSm, isSizeMd, isSizeLg, onIncrement }) => {
return (
<div>
{counter}
<button type="button" onClick={onIncrement}>Increment</button>
{/* Display custom content depending on the screen width */}
{isSizeXs ? (
<div>X-small screen content</div>
) : null}
{isSizeSm ? (
<div>Small screen content</div>
) : null}
{isSizeMd ? (
<div>Medium screen content</div>
) : null}
{isSizeLg ? (
<div>Large screen content</div>
) : null}
</div>
);
};
const breakpoints = [
{ prop: 'isSizeXs', selector: '#marker-xs' },
{ prop: 'isSizeSm', selector: '#marker-sm' },
{ prop: 'isSizeMd', selector: '#marker-md' },
{ prop: 'isSizeLg', selector: '#marker-lg' },
];
const MyResponsiveComponent = withBreakpointsCustom({ breakpoints }, MyComponent);
Lastly, use your new higher-order component in place of your custom component.
let x = 1;
function incrementCounter() {
x++;
}
return (
<MyResponsiveComponent counter={x} onIncrement={incrementCounter}>
);
FAQs
A higher-order React component to translate CSS breakpoints into properties
The npm package react-responsive-breakpoints receives a total of 21 weekly downloads. As such, react-responsive-breakpoints popularity was classified as not popular.
We found that react-responsive-breakpoints demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Vite releases Rolldown-Vite, a Rust-based bundler preview offering faster builds and lower memory usage as a drop-in replacement for Vite.
Research
Security News
A malicious npm typosquat uses remote commands to silently delete entire project directories after a single mistyped install.
Research
Security News
Malicious PyPI package semantic-types steals Solana private keys via transitive dependency installs using monkey patching and blockchain exfiltration.