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

tw-screens

v1.0.0

Published

React hooks for using Tailwind CSS breakpoints easily and efficiently.

Downloads

80

Readme

tw-screensDocs


tw-screens

tw-screens is a lightweight TypeScript library for managing responsive breakpoints dynamically. Inspired by Tailwind CSS, tw-screens lets you define custom breakpoints and use hooks to handle UI responsiveness, all while staying compatible with Tailwind's screen configuration.

  • Size: 4.19 KB (compressed)
  • Type-safe: Full TypeScript support for optimal DX
  • Coverage: 90%+ test coverage
  • Compatibility: Tailwind CSS breakpoint API
  • Performance: Optimized BreakpointManager for minimal overhead
  • Zero Dependencies: Minimal footprint

Features

  • Dynamic Breakpoints: Customizable breakpoints on the fly.
  • Tailwind Compatible: Same screen API for easy integration.
  • Responsive Hooks: Includes useScreen, useScreenReverse, useScreenValue, and useScreenEffect.
  • Optimized Performance: Efficient management via BreakpointManager without requiring a provider.
  • Lightweight: 4.19 KB size keeps your bundle small.
  • SSR Compatible: Works seamlessly with server-side rendering.

Installation

Install tw-screens via npm or yarn:

npm install tw-screens

or

yarn add tw-screens

Getting Started

tw-screens is flexible for defining breakpoints and using them in your components.

Step-by-Step Example: Tailwind CSS Breakpoints

Easily integrate tw-screens with your Tailwind CSS configuration.

  1. Define Screens Configuration

Define screens similar to Tailwind CSS using min, max, or raw.

// screens.ts
export const screens = {
  sm: "640px",
  md: "768px",
  lg: "1024px",
  xl: "1280px",
  "2xl": "1536px",
  custom: { min: "600px", max: "900px" },
  wide: { raw: "(min-width: 1600px)" },
} as const;
  1. Create Hooks Using tw-screens

Use the create function to generate hooks for managing breakpoints.

// hooks.ts
import { create } from "tw-screens";
import { screens } from "./screens";

export const { useScreen, useScreenReverse, useScreenValue, useScreenEffect } = create(screens);
  1. Integrate with Tailwind CSS

Add your screens to Tailwind to sync breakpoints.

// tailwind.config.js
const { screens } = require('./screens');

module.exports = {
  theme: {
    screens: {
      ...screens,
    },
  },
};
  1. Use Hooks in Components

Use the generated hooks in your components.

// ExampleComponent.tsx
import React from "react";
import { useScreen } from "./hooks";

const ExampleComponent = () => {
  const isDesktop = useScreen("md");

  return <div>{isDesktop ? "Desktop View" : "Mobile View"}</div>;
};

export default ExampleComponent;

Raw CSS Breakpoints for Flexibility

Define custom breakpoints directly using CSS media queries.

import { useBreakpoint } from 'tw-screens/hooks';

function CustomRawBreakpointComponent() {
  const isWideScreen = useBreakpoint({ raw: "screen and (min-width: 1400px)" });

  return <div>{isWideScreen ? "Wide Screen View" : "Regular View"}</div>;
}

Two Ways to Use tw-screens

  1. With Tailwind Screens: Define breakpoints with Tailwind and sync them:

    theme: {
      screens: {
        ...screens
      }
    }

    Default Tailwind configuration users can create hooks without a custom object:

    import { create } from 'tw-screens';
    export const { useScreen, useScreenReverse, useScreenValue, useScreenEffect } = create();
  2. Ad-hoc Breakpoints with useBreakpoint: Use for one-off breakpoints:

    import { useBreakpoint } from 'tw-screens/hooks';
    
    const ExampleComponent = () => {
      const isMediumScreen = useBreakpoint("850px");
      return <div>{isMediumScreen ? "Medium Screen" : "Other Screen"}</div>;
    };

API Documentation

  • create(screens: ScreensConfig)

    • Generates hooks like useScreen, useScreenReverse, and others for custom breakpoints.
  • useBreakpoint(breakpoint: ScreenValue, options?: UseBreakpointOptions): boolean

    • breakpoint: A string or object ({ min: '640px', max: '1024px' }).
  • useBreakpointReverse(breakpoint: ScreenValue): boolean

    • Provides the opposite of useBreakpoint.

For more details, check the documentation.


Problem Solved

tw-screens simplifies responsive UIs in React, supporting custom breakpoints and easy Tailwind integration while ensuring type safety and performance.

  • Type Safety: Reliable, type-checked breakpoints.
  • Tailwind Integration: Share breakpoints with Tailwind without loading the full config.
  • Performance: Efficient breakpoint management without providers.
  • SSR Compatible: Works smoothly with server-side rendering.
  • No Dependencies: Lightweight and conflict-free.

Contributing

We welcome contributions to improve tw-screens. Feel free to open issues, suggest features, or contribute code!


License

tw-screens is MIT licensed.


For full documentation, please check our documentation folder.