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 🙏

© 2025 – Pkg Stats / Ryan Hefner

react-tw-breakpoints

v1.2.9

Published

useBreakpoint hooks and tw-based components.

Readme

assets/project.svg

Ask DeepWiki

Optimized SSR-friendly React hooks to get the current breakpoint based on:

  • Viewport: uses matchMedia and is global to window.
  • Container (true per-element): uses ResizeObserver to measure an element and return its breakpoint.

Includes condition helpers (largerThan/lessThan/onlyAt) and is tree-shakeable.

Installation

npm install react-tw-breakpoints

Peer deps: React 18/19 (DOM). Tailwind is NOT required for the hooks. If you use the experimental UI components (Container, Grid), Tailwind CSS is required and you must configure a safelist so their dynamic classes are included. See docs/guides/tailwind-safelist.md.

Documentation

Breakpoints

The library uses Tailwind-aligned breakpoints: xs (0px), sm (640px), md (768px), lg (1024px), xl (1280px), _2xl (1536px), and more.

Note:

  • Hook/constant identifiers use a leading underscore for sizes starting with a number (e.g. _2xl, _3xl) because TypeScript identifiers cannot start with digits. See BreakpointEnum.
  • Tailwind class names and component props use the native Tailwind form without underscore (e.g. 2xl). See Grid.

Scopes:

Quick start

1) Viewport

import { useBreakpoint, useBreakpointCondition } from 'react-tw-breakpoints';

function Example() {
  const bp = useBreakpoint(); // 'xs' | 'sm' | ...
  const isLgUp = useBreakpointCondition({ largerThan: 'lg' });
  const onlyMd = useBreakpointCondition({ onlyAt: 'md' });
  return (
    <div>
      <p>Viewport BP: {bp}</p>
      {isLgUp && <span>≥ lg</span>}
      {onlyMd && <span>md only</span>}
    </div>
  );
}

2) Container (true per-element)

import { useRef } from 'react';
import { useContainerBreakpoint } from 'react-tw-breakpoints';

function Card() {
  const ref = useRef<HTMLDivElement>(null);
  const bp = useContainerBreakpoint(ref); // based on the element width
  return (
    <div ref={ref} style={{ width: '100%' }}>
      {bp === 'xs' && <OneCol />}
      {bp === 'md' && <TwoCols />}
      {bp === 'lg' && <ThreeCols />}
    </div>
  );
}

API Overview

Hooks

useBreakpoint() - Returns the active viewport breakpoint label.

useBreakpointCondition(opts) - Evaluates viewport conditions (largerThan, lessThan, onlyAt).

useBreakpointContainer() - Container breakpoint set (viewport-based).

useContainerBreakpoint(ref) - True per-element breakpoint using ResizeObserver.

Helper Hooks: useBreakpointUp, useBreakpointDown, useBreakpointOnly, useBreakpointBetween

Hooks API Reference

Helpers

getCurrentBreakpoint() - Synchronously get current breakpoint (SSR-safe).

getMediaQuery(query) - Get cached MediaQueryList for custom queries.

Helpers API Reference

Components (Experimental)

[!CAUTION] These components are experimental and may change their API or functionality. They are subject to discussion and improvement proposals, so breaking changes or even removal may occur. Use them at your own risk.

There are some basic layout components to use in your application. These are independent of the hooks in this library, so they are not affected by changes to the API for hooks, helpers, etc.

Why?

Many UI libraries don't have basic layout components. You probably need something simple and straightforward like a <Container>, and you may not want to have to define it in every project you work on if you use the same UI library or another one that doesn't have one.

  • Container - Centered wrapper with max-width constraints.

  • Grid - 12-column responsive grid system.

Components API Reference

Quick Examples

Responsive Navigation

import { useBreakpointCondition } from 'react-tw-breakpoints';

function Navigation() {
  const isMobile = useBreakpointCondition({ lessThan: 'lg' });

  return <nav>{isMobile ? <MobileMenu /> : <DesktopMenu />}</nav>;
}

Adaptive Card

import { useRef } from 'react';
import { useContainerBreakpoint } from 'react-tw-breakpoints';

function Card() {
  const cardRef = useRef<HTMLDivElement>(null);
  const breakpoint = useContainerBreakpoint(cardRef);

  return (
    <div ref={cardRef}>
      {breakpoint === 'xs' && <CompactLayout />}
      {breakpoint === 'lg' && <ExpandedLayout />}
    </div>
  );
}

📚 More Examples

Advanced Topics

CSS @container Queries

For style-based container queries without JavaScript, use native CSS @container. Learn more.

SSR and StrictMode

Hooks use useSyncExternalStore for safe subscriptions. In SSR they return base values (xs or false) and hydrate on the client. No duplicate listeners in StrictMode.

Browser Compatibility

  • matchMedia: All modern browsers
  • ResizeObserver: Chrome/Edge 64+, Safari 13.1+, Firefox 69+
  • CSS @container: Chrome/Edge 105+, Safari 16+, Firefox 110+

FAQ

  • Why two kinds of “container breakpoints”?

    • useBreakpointContainer uses viewport with a different label set (useful if you want two global grids).
    • useContainerBreakpoint is true per element.
  • Can I change breakpoints?

    • Yes, edit src/const/breakpoints.ts and rebuild the package.
  • Tree‑shaking?

    • Yes. package.json exports ESM with sideEffects: false. Import only what you use.

Want to contribute?

Please read the contribution guidelines first.

License

MIT