
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
react-hidden
Advanced tools
A React component to conditionally render children based on matching breakpoints or custom media queries with plenty on options.
react-hidden is a React component designed to offer a flexible and efficient way to control the visibility of UI elements based on the viewport's size, custom media queries, and user preferences. It's an ideal solution for developers looking to implement responsive design patterns without cluttering their components with conditional rendering logic.
react-hidden integrates smoothly without impacting your application's load times.Hidden component centralizes and simplifies this logic, making it more maintainable and readable.Hidden component can help reduce the load on the browser, leading to faster rendering times and a smoother user experience.Hidden component allows developers to tailor the UI to meet these needs, enhancing the overall user experience.Hidden component fits perfectly into this paradigm by allowing developers to declaratively specify which components should be visible under which conditions.Hidden component helps manage this space more effectively by hiding non-essential elements, thus reducing clutter and focusing the user's attention on the most important information.The Hidden component is particularly useful in scenarios where conditional visibility of UI elements is required. It's most beneficial:
To install react-hidden, run the following command in your project directory:
npm install react-hidden
react-hidden accepts a comprehensive set of props to control the visibility of its children elements dynamically. Below is a table detailing these props:
| Prop | Type | Default Value | Description |
|---|---|---|---|
xs | boolean | undefined | Extra small screens (<576px). If not specified, does not apply. |
sm | boolean | undefined | Small screens (≥576px to <768px). If not specified, does not apply. |
md | boolean | undefined | Medium screens (≥768px to <992px). If not specified, does not apply. |
lg | boolean | undefined | Large screens (≥992px to <1200px). If not specified, does not apply. |
xl | boolean | undefined | Extra large screens (≥1200px to <1600px). If not specified, does not apply. |
xxl | boolean | undefined | Extra extra large screens (≥1600px). If not specified, does not apply. |
media | string | string[] | undefined | Apply custom media query or queries for visibility control. Can be a single string or an array of strings. |
children | React.ReactNode | Required | The content that will be conditionally displayed. This prop is required. |
invert | boolean | false | Inverts the visibility condition, showing content when conditions are not met. |
breakpoints | object | defaultBreakpoints | Custom breakpoints object to override default breakpoints for responsive design. The default breakpoints are defined as shown in the defaultBreakpoints object. |
onShow | () => void | undefined | Callback function that is executed when the content becomes visible. |
onHide | () => void | undefined | Callback function that is executed when the content becomes hidden. |
debounce | number | 100 | Debounce time in milliseconds for handling resize or orientation change events. |
xs, sm, md, lg, xl, xxl Props: These boolean props are used to determine visibility based on the viewport's width. They correspond to commonly used breakpoints but can be overridden by the breakpoints prop for custom responsiveness.
media Prop: This prop allows for more granular control over visibility with custom media queries. It accepts either a single query string or an array of query strings. This is particularly useful for complex responsiveness conditions beyond standard breakpoints.
invert Prop: When set to true, the visibility logic is reversed. By default, the component hides content based on the specified conditions. With invert set to true, the content is shown when the conditions are met instead.
breakpoints Prop: This object prop allows developers to define their own custom breakpoints, making the component highly adaptable to various design systems. It should match the structure of the defaultBreakpoints object.
onShow and onHide Callbacks: These props provide a way to run custom logic when the visibility of the component's children changes. This can be used for analytics, loading resources, or triggering animations.
debounce Prop: This numeric prop defines the debounce time in milliseconds for the event listeners attached to window resize and orientation change events. It helps in optimizing performance by limiting the frequency of visibility checks during rapid viewport changes.
The react-hidden component offers a flexible way to manage component visibility in response to screen size, orientation, and custom media queries. Below are examples showcasing how to utilize the component from basic usage to complex configurations.
Hide content on extra small screens.
import Hidden from 'react-hidden';
<Hidden xs>
<p>This text is hidden on extra small screens (<576px).</p>
</Hidden>
invert PropShow content only on extra small screens by inverting the condition.
<Hidden xs invert>
<p>This text is visible only on extra small screens (<576px).</p>
</Hidden>
Hide content based on a custom media query.
<Hidden media="(max-width: 768px)">
<p>This text is hidden on screen widths up to 768px.</p>
</Hidden>
Hide content based on multiple media query conditions.
<Hidden media={["(max-width: 768px)", "(orientation: portrait)"]}>
<p>This content is hidden on screens narrower than 768px or in portrait orientation.</p>
</Hidden>
Define and use custom breakpoints to match your design system.
const customBreakpoints = {
xs: "(max-width: 640px)",
sm: "(min-width: 641px) and (max-width: 768px)",
// Define more custom breakpoints as needed
};
<Hidden breakpoints={customBreakpoints} xs>
<p>This uses custom breakpoints for extra small screens.</p>
</Hidden>
Execute custom logic when content becomes visible or hidden.
<Hidden
media="(max-width: 768px)"
onShow={() => console.log("Content is now visible")}
onHide={() => console.log("Content is now hidden")}
>
<p>Content with visibility callbacks.</p>
</Hidden>
Control the debounce time for resize and orientation change events.
<Hidden media="(max-width: 768px)" debounce={200}>
<p>This text is hidden on screen widths up to 768px, with debounced resize events.</p>
</Hidden>
Combine various props for complex responsive design needs.
<Hidden
breakpoints={{ xs: "(max-width: 500px)", md: "(min-width: 501px) and (max-width: 1200px)" }}
xs md
invert
media={["(orientation: landscape)", "(prefers-color-scheme: dark)"]}
onShow={() => console.log("Showing in specific conditions")}
onHide={() => console.log("Hiding in specific conditions")}
debounce={150}
>
<div>
<p>This content shows under complex conditions: either in landscape orientation, prefers dark mode, or not within the xs and md breakpoints.</p>
<p>It also employs a custom debounce time for resize/orientation change events and executes callbacks on visibility changes.</p>
</div>
</Hidden>
This example demonstrates the react-hidden component's capability to handle intricate design and responsiveness challenges by combining multiple features. By leveraging custom breakpoints, invert logic, media queries (including arrays of conditions), debounce handling, and visibility callbacks, developers can precisely control component visibility across a wide range of devices and user preferences.
Hidden componentsNesting Hidden components can provide a powerful way to create complex, responsive designs that cater to very specific conditions. Here's an example scenario where nesting can be particularly effective:
Imagine an e-commerce product page with the following requirements for displaying promotional banners:
import Hidden from 'react-hidden';
function ProductPage() {
return (
<div>
{/* General Promotional Banner (hidden on extra small screens) */}
<Hidden xs>
<div className="banner general-promotion">
<p>Don't miss our special offers!</p>
</div>
</Hidden>
{/* Exclusive Mobile Offer */}
<Hidden sm md lg xl xxl>
<div className="banner mobile-exclusive">
<p>Exclusive offer for mobile users!</p>
</div>
</Hidden>
{/* Nested Hidden components for complex conditions */}
<Hidden xs sm md>
{/* This will only show on lg and xl screens */}
<div className="banner free-shipping">
<p>Free shipping on orders over $50!</p>
</div>
{/* Additional "Sale" banner for large devices during sale events */}
<Hidden invert media="(min-width: 992px)">
<div className="banner sale-announcement">
<p>Sale! Up to 50% off!</p>
</div>
</Hidden>
</Hidden>
{/* Sale Banner for Extra Small Devices */}
<Hidden sm md lg xl xxl>
<div className="banner sale-announcement">
<p>Mobile Sale! Exclusive discounts!</p>
</div>
</Hidden>
</div>
);
}
xs) devices using the Hidden component. This ensures that users on small devices aren't overwhelmed by too many promotions.lg and above), the Free Shipping Announcement is displayed alongside the general promotional banner. This is achieved by nesting a Hidden component within another Hidden component set to activate at larger breakpoints.Hidden component reveals a sale announcement on larger screens without hiding the free shipping announcement.This example demonstrates how nesting Hidden components allows for nuanced visibility control, enabling the creation of responsive layouts that adapt content visibility to a wide range of conditions, enhancing user experience across different devices.
Contributions are welcome! Please read our contributing guidelines for details on how to submit pull requests, report issues, or suggest enhancements.
react-hidden is MIT licensed.
FAQs
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
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.