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

@designbycode/alpine-headroom

v1.0.7

Published

A lightweight Alpine.js directive that toggles CSS classes based on scroll position, it handles pinned, unpinned, top, bottom, frozen states with custom options for offset and tolerance

Readme

Alpine Headroom Plugin

This plugin is an Alpine.js directive called x-headroom, designed to implement a smart sticky header behavior (commonly known as "headroom" logic) on any DOM element—typically navigation bars or headers. It dynamically adds and removes CSS classes based on the user's scroll position, enabling smooth animations and responsive UI changes such as hiding the header when scrolling down and showing it when scrolling up.

The plugin is inspired by the popular Headroom.js library but implemented as a lightweight, native solution using Alpine.js, with first-class support for Tailwind CSS via flexible class handling.

npm version npm NPM ts GitHub stars

Features

  • Easy setup with Alpine.js directive
  • Customizable options for offset, tolerance, classes
  • Supports Tailwind CSS classes
  • Performance optimized with requestAnimationFrame, passive event listeners
  • Optional frozen modifier for static state

Installation

Install via npm or bun in your JavaScript project root directory:

npm install @designbycode/alpine-headroom
# or
bun add @designbycode/alpine-headroom

Ensure you use Tailwind CSS v4 only in your build config

Usage

Import and register the plugin in your entry point (eg main.js):

import Alpine from "alpinejs"
import Headroom from "@designbycode/alpine-headroom"

Alpine.plugin(Headroom)
Alpine.start()

HTML Markup

Add the x-headroom directive to any element:

<header x-data x-headroom="{ offset: 50, tolerance: 10, classes: { pinned: 'bg-white shadow', unpinned: 'opacity-75', frozen: 'bg-gray-500' } }">
  <!-- header content -->
</header>
  • offset (number) minimum scroll Y before toggling pinned/unpinned
  • tolerance (number) scroll delta threshold to ignore small moves
  • classes (object) override default Tailwind CSS v4 class names

    .headroom {
        will-change: transform; /* Optimize for animation */
        transition: transform 0.5s ease-in-out; /* Smooth transition */
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        z-index: 1000;
    }

    /* Styles when the element is pinned (visible) */
    .headroom--pinned {
        transform: translateY(0%);
    }

    /* Styles when the element is unpinned (hidden) */
    .headroom--unpinned {
        transform: translateY(-100%); /* Slide up and out of view */

    }

    /* Optional: Styles for when the page is at the very top */
    .headroom--top {
        /* Example: Add a shadow only when not at the top */
        box-shadow: none;
    }

    .headroom--not-top {
        box-shadow: 0 3px rgba(0,0,0,25);
    }

Use modifiers in the directive to apply frozen state at init:

<nav x-data x-headroom.frozen="">
  <!-- nav content -->
</nav>

Options

Default options are:

{
  offset: 0,
  tolerance: 0,
  classes: {
    initial: "headroom",
    pinned: "headroom--pinned",
    unpinned: "headroom--unpinned",
    top: "headroom--top",
    notTop: "headroom--not-top",
    bottom: "headroom--bottom",
    notBottom: "headroom--not-bottom",
    frozen: "headroom--frozen"
  }
}

Override any of these by passing an object to x-headroom="..." expression, customising only what you need, classes supports multiple Tailwind utility classes separated by spaces

Code Example

// headroom.ts
import Alpine from "alpinejs"
import Headroom from "@designbycode/alpine-headroom"

Alpine.plugin(Headroom)
Alpine.start()
<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Alpine Headroom Demo</title>
  <link href="/dist/output.css" rel="stylesheet">
</head>
<body>
  <header x-data x-headroom.x="{ offset: 100, tolerance: 20 }" class="fixed top-0 w-full p-4 transition-all duration-300">
    <h1 class="text-xl font-bold">My Site</h1>
  </header>
  <main class="mt-20 p-4">
    <p>Scroll down to see header hide, scroll up to show</p>
    <!-- more content -->
  </main>
  <script src="/dist/main.js"></script>
</body>
</html>

Contributing

  1. fork the repo
  2. install dependencies
  3. make your changes
  4. open a pull request

License

MIT License