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

use-container-queries

v1.0.0

Published

A react hook that tracks a containers size and the range that width falls into within a list of breakpoints. This allows better responsive styling, where the user can style DOM elements based on their container, rather than the browser viewport.

Downloads

1,742

Readme

use-container-queries

A react hook that tracks a containers size and the range that width falls into within a list of breakpoints. This allows better responsive styling, where the user can style DOM elements based on their container, rather than the browser viewport.

Try it a demo here

Install

💡 Note: Since this module uses React Hooks, you'll need to have version >=16.8.0 of react and react-dom installed in your project

npm install use-container-queries --save-dev
# or
yarn add use-container-queries --dev

🚨 Important: This hook uses the Resize Observer internally to measure the changes of size on your element. Browser support for the API is relatively wide and can be viewed here. For now, this library does not ship with a polyfill (open to changing this) but you can include one in your project from here

Quick Start

import React from 'react';
import { useContainerQueries } from 'use-container-queries';

const breakpoints = {
    small: [0, 300],
    med: [301, 600],
    large: [601, 900],
    xl: [901],
};

function App() {
    const { ref, active, width } = useElementInView({ breakpoints });

    return (
        <div ref={ref}>
            The current width is: {width}
            This matches your breakpoint: {active}
        </div>
    );
}

Background

"Container queries allow an author to control styling based on the size of a containing element rather than the size of the user’s viewport." Editors Draft - WICG

We've been using Media Queries to handle responsive web design for years, but as we've started to adopt a more modular and component based design system, the shortcomings of this approach become obvious. A component may behave and look differently depending on where it is placed within your layout. Components should be responsive and independent of purely just the viewport size.

Container Queries aim to solve this by allowing you to observe the container's width to allow you to adjust your styles accordingly.

How it works

The hook accepts an object of key/value pairs that signify the width ranges per breakpoint. These ranges are the 'min' and 'max' values in pixels for that breakpoint. For example, you want to set breakpoints to be notified when a containers width falls between 0-300, 301-600 and 601+. We'll label these as small, med, and large:

const breakpoints = {
    small: [0, 300],
    med: [301, 600],
    large: [601],
};

If the containers current width is 430px, the hook would return { active: 'med', width: 430 }. For your largest breakpoint, you can omit the 'max' value as it is implied this breakpoint is from the minimum value and above.

API

useContainerQueries(options)

function useContainerQueries<T extends HTMLElement>(
    options: ContainerQueryProps
): ContainerQueryResult<T>;

Options: ContainerQueryProps

| Name | Type | Default | Required | Description | | ---------------- | ---------------------------------- | ------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | breakpoints | [key: string]: [number, number?] | | true | A key/value object of the breakpoints you wish to use. The key will label that breakpoint range. Each value should have a min and max range (Eg { small: [0, 300], med: [301, 600]} ) | | ignoreDimensions | boolean | true | false | Flag to ignore updating state for container width changes when they don't trigger a breakpoint change, thus saving renders. Width will always return 0 |

Returns ContainerQueryResult<T>

| Name | Type | Description | | ------ | ---------------- | -------------------------------------------------------------------------------------------------------------------- | | ref | RefCallback<T> | A callback ref to be assigned to the element you wish to observe changes on. This must be assigned | | width | number | The current width of the observed element | | active | string | The current active breakpoint. This key will match a property from the breakpoints supplied to the hook by the user. |

Examples