tailwindcss-intersect

repository·main·Indexed 42 days ago

https://github.com/heidkaemper/tailwindcss-intersect

A Tailwind CSS plugin that provides an `intersect:` variant to trigger styles and animations based on element visibility using the Intersection Observer API. Includes utility modifiers for thresholds (`intersect-half`, `intersect-full`, `intersect-[value]`), margins (`intersect-margin-[value]`), and one-time triggers (`intersect-once`). Version 3 requires Tailwind CSS v4.

Tokens
1.7K
Snippets
10
Records
12
Agent score
53%

What's inside tailwindcss-intersect

  1. Migrate from version 2.x to 3.x

    main

    Version 3 requires Tailwind CSS v4. If you are using Tailwind CSS v3, continue using the 2.x releases.

    In version 3, the Observer has moved to a dedicated entry point. You must update your import path:

    Old: import { Observer } from 'tailwindcss-intersect';

    New: import { Observer } from 'tailwindcss-intersect/observer';

  2. Initialize the Intersection Observer

    main

    The plugin requires a JavaScript snippet to run the IntersectionObserver logic. You can include it via CDN or import it into your JS bundle.

    Via CDN: Include this script tag in your HTML:

    <script defer src="https://unpkg.com/tailwindcss-intersect@3.x.x/dist/observer.min.js"></script>

    Via NPM (Recommended for bundles): Import the Observer and call .start(). If you are building a Single Page Application (SPA), call Observer.restart() on route changes to ensure elements on the new route are observed.

    import { Observer } from 'tailwindcss-intersect/observer';
    
    Observer.start();
  3. Register the Tailwind CSS plugin

    main

    Depending on your setup, register the plugin in your CSS file or your Tailwind configuration file.

    Using CSS imports (Tailwind CSS v4):

    @import "tailwindcss";
    @import "tailwindcss-intersect";

    Using a JavaScript configuration file:

    // tailwind.config.js
    module.exports = {
      // ...
      plugins: [
        require('tailwindcss-intersect')
      ],
    }
  4. Force animation on load with no-intersect

    main

    Elements that are already visible on page load may not trigger their intersect: styles because the observer runs after the initial render. To force the animation/style to apply to elements already in view, add the no-intersect attribute.

    <div class="opacity-0 intersect:opacity-100 transition-opacity" no-intersect></div>
  5. How the Observer manages intersection state

    main

    The Observer class manages the lifecycle of elements using the following logic:

    1. State Tracking: When an element enters the viewport (based on its threshold and margin), the no-intersect attribute is removed from the element.
    2. Out of View: When an element leaves the viewport, the no-intersect attribute is added to the element.
    3. One-time Observation: If an element has the intersect-once class, the observer will automatically disconnect from that specific element after its first intersection event, preventing further state changes.

    This allows you to style elements based on their visibility using CSS selectors like [no-intersect] or by targeting the presence/absence of the attribute.

  6. Register the tailwindcss-intersect plugin

    main

    To use the intersection modifiers, add the tailwindcss-intersect plugin to your tailwind.config.js file. The plugin registers a single variant, intersect, which applies styles only when the element does not have the no-intersect attribute.

    // tailwind.config.js
    const intersectPlugin = require('tailwindcss-intersect')
    
    module.exports = {
      // ...
      plugins: [
        intersectPlugin,
      ],
    }
  7. Configure intersection thresholds and modifiers

    main

    You can control when the intersection event triggers using several utility modifiers:

    • intersect-once: Triggers the event only the first time the element becomes visible.
    • intersect-half: Triggers when at least 50% (0.5 threshold) of the element is visible.
    • intersect-full: Triggers when the element is almost entirely visible (0.99 threshold).
    • intersect-[value]: Use an arbitrary value between 0 and 1 for a custom threshold (e.g., intersect-[0.3]).
    • intersect-margin-[value]: Adjust the margin area used for measurement. Supports CSS margin shorthand with px or % (e.g., intersect-margin-[200px] or intersect-margin-[0px_0px_100px_0px]).
    <!-- intersect-once -->
    <div class="intersect:animate-spin intersect-once"></div>
    
    <!-- intersect-half -->
    <div class="intersect:animate-spin intersect-half"></div>
    
    <!-- intersect-full -->
    <div class="intersect:animate-spin intersect-full"></div>
    
    <!-- intersect-[threshold] -->
    <div class="intersect:animate-spin intersect-[0.3]"></div>
    
    <!-- intersect-margin-[margin] -->
    <div class="intersect:animate-spin intersect-margin-[200px]"></div>
    <div class="intersect:animate-spin intersect-margin-[0px_0px_100px_0px]"></div>
  8. Initialize the Intersection Observer with Observer.start()

    main

    To begin observing elements with intersect: utility classes, call Observer.start(). This method ensures the DOM is fully loaded before attempting to find and observe elements. If the document is still in the loading state, it waits for the DOMContentLoaded event before executing.

    import { Observer } from 'tailwindcss-intersect';
    
    Observer.start();
  9. Refresh observations with Observer.restart()

    main

    If you dynamically add new elements to the DOM that use intersection utility classes, or if you need to reset the current state, call Observer.restart(). This method disconnects all existing IntersectionObserver instances, clears the internal observer registry, and re-scans the document for all elements matching the intersection selectors.

    import { Observer } from 'tailwindcss-intersect';
    
    // Call this after injecting new HTML content to ensure new elements are observed
    Observer.restart();