npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@libertymutual/react-responsive

v2.0.1

Published

A set of components and utilities for working with responsive thresholds and responsive props.

Downloads

194

Readme

@libertymutual/react-responsive

Build Status

npm install @libertymutual/react-responsive

yarn add @libertymutual/react-responsive

This library provides a set of components and hooks that are responsive based on the defined thresholds. This library relies on react hooks which require a React version of at least ^16.8.0. With these components you can get the current threshold, create responsive props, or hide / show content.

The most valuable functionality provided by this library is the ability to make props on your components 'responsive props'.

<ResponsiveProvider />

import { ResponsiveProvider } from '@libertymutual/react-responsive'

The <ResponsiveProvider /> component is an optional component if you wish to change any of the defaults. The default thresholdMap is { xs: 0, sm: 480, md: 768, lg: 960, xl: 1280 }. This can be changed by passing a new object to the thresholdMap prop. There are no restrictions to the number of thresholds you can have or what they need to be named. The keys you specify here are the values the other components will use. Based on the default the keys are 'xs', 'sm', 'md', 'lg', 'xl'.

You're first threshold must start with the value of 0 and your thresholds must be order from smallest to largest. Thresholds are matched from the value provided to the next threshold -1.

Providers can be nested for specialized cases. You can define a set of specific threshold for a single component or section of your application. Components will always get their thresholdMap from the nearest provider.

<ResponsiveProvider thresholdMap={{'small-phone': 0, 'big-phone': 480, 'tablet': 768, 'largest': 960 }}>{children}</ReponsiveProvider>

With the above configuration the keys used by the other components would be 'small-phone', 'big-phone', 'tablet', and 'largest'.

useThreshold()

import { useThreshold } from '@libertymutual/react-responsive'

The React hook returns the current threshold value from the thresholds you have provided to the <ResponsiveProvider />. Based on what we passed to the above example this hook would return 'small-phone', 'big-phone', 'tablet', or 'largest'. This hook is stateful and will trigger a re-render if the threshold changes.

const threshold = useThreshold()

<WithThreshold />

import { WithThreshold } from '@libertymutual/react-responsive'

<WithThreshold /> is the higher order component version of the useThreshold() hook. It sets the value of the current threshold on a prop named threshold. This component is stateful and will trigger a re-render if the threshold changes.

export default WithThreshold(YourComponent)

<Hide /> and <Show />

import { Hide } from '@libertymutual/react-responsive'

import { Show } from '@libertymutual/react-responsive'

<Hide /> and <Show /> are utility components that conditionally render their content based on the values you provide to the thresholds prop. You can provide a single value or an array of values.

Based on what we passed to the above provider component example the following would not render their content on thresholds that matched 'small-phone' or 'large-phone' which range in size from 0 to 767px. Inversely show would only render it's content for those thresholds.

<Hide thresholds={['small-phone', 'large-phone']}>{children}</Hide>

<Show thresholds={['small-phone', 'large-phone']}>{children}</Show>

<WithResponsiveProps />

import { WithResponsiveProps } from '@libertymutual/react-responsive'

<WithResponsiveProps /> is a higher order component that lets you turn any prop into a responsive prop. When a prop is responsive it goes from size="small" to also being able to accept an object value like size={{'small-phone': 'xsmall', 'tablet': 'large'}}. The value for the size prop will be determined based on the current threshold. This component is stateful and will trigger a re-render if the threshold changes.

When a prop is made responsive it follows a cascade to find it's value similar to how a responsive grid works. In the situation where a value has not been provided for the actual current threshold we look to the next lowest threshold, and repeat, until we find a value. In the above example if the threshold were 'large-phone' it would use the value 'xsmall' from 'small-phone' because we did not provide a value for 'large-phone'.

It is important to provide a value for the smallest threshold that you have defined or the prop will have a value of null if the current threshold is lower than the lowest threshold value provided.

<WithResponsiveProps /> accepts a configuration object. This object contains the names of the props on your component you wish to become responsive. Note that a responsive prop doesn't require it's value always be an object. If the value passed is not an object it will be treated normally.

export default WithResponsiveProps({ propKeys: ['size'] })(YourComponent)


responsivePropBuilder()

This is a pure JavaScript utility function that translates the collection of responsive props into name/value pairs. It is not intended for general use but we expose it in case you prefer not to use <WithResponsiveProps />.

import {
  responsivePropBuilder,
  ResponsiveContext,
  useThreshold
} from '@libertymutual/react-responsive';

export const YourComponent = props => {
    const responsiveContext = useContext(ResponsiveContext);
    const thresholdMap = responsiveContext.getThresholdMap();
    const currentThreshold = useThreshold();
    const responsivePropsConfig = { propKeys: ['size'] };

    // this converts something like size={{'small-phone': 'sm', 'tablet': 'lg'}} to {size:'sm'} if the current threshold was 'small-phone'
    const responsiveProps = responsivePropBuilder(
        currentThreshold,
        props,
        responsivePropsConfig,
        thresholdMap
    );

    // destructure all your props
    // this will replace the value for size from responsive props if it was responsive
    // if was not responsive size would come from props
    const { size } = { ...props, ...responsiveProps };

    return (<div>{size}</div>)
});