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

responsive-jsx

v1.2.0

Published

Higher-order React components for responsive JSX

Downloads

11

Readme

responsive-jsx

Higher-order React components for responsive JSX.

Getting Started

Install with npm.

$ npm install responsive-jsx --save

Once responsive-jsx is installed, import the library in your React projects.

import Responsive, {
  breakpoints,
  setBreakpoints,
  useQuery,
} from 'responsive-jsx';

API

responsive-jsx comes with a higher-order component, and other functions for global configuration.

Responsive

The Responsive component is a simple wrapper for your JSX that renders its children only if the given media query matches. If the query is invalid, its children will always be returned. Responsive takes a breakpoint name and a query type, or a css media query.

import Responsive from 'responsive-jsx';

const MyComponent = () => {
  return (
    <Fragment>
      <Responsive small up>
        You can see me on windows larger than small
      </Responsive>

      <Responsive medium only>
        You can see me only when window is exactly medium
      </Responsive>

      <Responsive large down>
        You can see me when window is smaller than large
      </Responsive>

      <Responsive query={'(min-width: 320px)'}>
        You can see me on windows at least as large as mobile
      </Responsive>
    </Fragment>
  );
};

A breakpoint is one of small, medium, and large, or any of the breakpoints set by user. Type is one of up, down, and only. The query prop takes in a css media query.

Note: In case of multiple breakpoints or types, the first in the order above is prioritized, and if both css query and custom breakpoint are defined, the css query takes priority.

Callback Function

The Responsive component will pass in a boolean value if a callback function is passed in as its children for cases where your application needs to handle unmatched query differently.

import Responsive from 'responsive-jsx';

const MyComponent = () => {
  return (
    <Fragment>
      <Responsive small up>
        {(match) => {
          return match ? 'Hello' : 'Goodbye';
        }}
      </Responsive>
    </Fragment>
  );
};

setBreakpoints

setBreakpoints is a function that lets you set your custom breakpoints globally.

import Responsive, { setBreakpoints } from 'responsive-jsx';

setBreakpoints({
  mobile: 320,
  tablet: 720,
  desktop: 1440,
});

const MyComponent = () => {
  return (
    <Responsive mobile up>
      You can see me on windows larger than mobile
    </Responsive>
  );
};

It takes an object of breakpoint name to its width in pixels. Setting your custom breakpoints allows you to use them anywhere in your app. Define your breakpoints in the entry point of your React app.

useQuery

useQuery is a custom hook in case you need to handle responsiveness programmatically in Javascript.

import Responsive, { useQuery } from 'responsive-jsx';

const MyComponent = () => {
  const match = useQuery('(max-width: 720px)');

  return match && 'You can only see me on windows smaller than 720px';
};

The hook takes in a css query as a string and returns a local state boolean which updates according to adjustments in the window size.

breakpoints

breakpoints is a variable for the currently defined breakpoints of the app.

Built With

responsive-jsx is built with React 16.8, which introduced hooks.

Libraries used:

Author

Gino Jacob - Github

Feedback is very much welcomed.

License

This project is licensed under the MIT License - see the LICENSE.md file for details