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

downtools

v0.1.0

Published

Miscellaneous helpers for React

Downloads

4

Readme

downtools

Miscellaneous helpers for React.

Install

yarn add downtools

withViewport

HOC to inject viewportWidth and viewportHeight properties into your views, for responsive rendering, and update them when the window resizes.

Usage

This example hides the menu inside a button on small screens:

import { withViewport } from "downtools";

const MyComponent = ({viewportWidth, viewportHeight}) => (
    viewportWidth < 800 ? <HamburgerMenu /> : <SideMenu />
)

export default withViewport()(MyComponent);

The properties will be updated whenever the window resizes, for fully responsive updates.

Configuration

The HOC accepts a configuration object when executed, the options are as follows:

export default withViewport({
    browserlessWidth: 1024,
    browserlessHeight: 768,
    handleRehydration: true
})(MyComponent);

With server-side rendering

When rendering on the server, there is no window available, and we cannot yet know how big the viewport is going to be. withViewport will inject defaults in this case, these are set to 1920x1080, but can be overriden with the browserlessWidth and browserlessHeight configuration parameters. A third property isBrowserless will also be injected with value true.

This allows you to detect whether server-side rendering is in progress, and if so possibly render things a little differently. You might want to avoid rendering the viewport-aware parts of your app, and then render them once you know what space they have, rather than first rendering a wrongly-sized version and then updating it after the page loads.

Client rehydration

When first rendering on the client ("rehydration") we have to do a little more work. Now the window is available your component might render differently -- but that's not what we actually want! In rehyrdation you want the page to render exactly as it was on the server, otherwise React gives errors.

So to circumvent this the HOC takes a third configuration parameter, handleRehydration. If this set to true then the HOC will perform a special first-pass render where it ignores the window and provides the same default/browserless dimensions (and also isBrowserless true). The idea is you can perform a rehydration render with exactly the same state as you did on the server.

The component will then immediately update its own state after mounting (state is updated a setImmediate call). This will cause an additional render where you have the real width and height.

TODO: Handling user session

Tests

git clone https://github.com/downplay/downtools
cd downtools
yarn
yarn test

Version History

0.1.0

  • Initial release, includes only withViewport

Copyright

©2017 Downplay Ltd

Distributed under MIT license. See LICENSE for full details.