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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@imoses.g/react-use-collapsible

v1.0.0

Published

A lightweight, headless React hook for building accessible collapsible/accordion components with smooth height transitions.

Readme

@imoses.g/react-use-collapsible

CI npm version license

A lightweight, headless React hook for building accessible collapsible/accordion components with smooth height transitions.

Features

  • Headless — bring your own markup and styles
  • Smooth height transitions — animates between 0 and scrollHeight with configurable duration & easing
  • Accessible — works naturally with buttons, ARIA attributes, and keyboard navigation
  • Controlled & uncontrolled modes
  • Lifecycle callbacksonOpening, onOpen, onClosing, onClose, onToggle
  • Lazy renderingshouldRender stays false until first open, enabling deferred mount
  • Tiny — zero dependencies, ~1 KB gzipped
  • TypeScript — fully typed with generic element support

Installation

npm install @imoses.g/react-use-collapsible

Peer dependency: React 18 or later.

Quick Start

import { useCollapsible } from '@imoses.g/react-use-collapsible';

function Collapsible() {
    const { isOpen, getTriggerProps, getContentProps } = useCollapsible<
        HTMLDivElement,
        HTMLButtonElement
    >();

    return (
        <div>
            <button {...getTriggerProps()}>{isOpen ? 'Hide' : 'Show'}</button>
            <div {...getContentProps()}>
                <p>Collapsible content here.</p>
            </div>
        </div>
    );
}

API

useCollapsible<ContentType, TriggerType>(options?)

Options

| Option | Type | Default | Description | | ------------ | ----------------------------------------------------- | ---------------------------------- | ------------------------------------------------------------------- | | open | boolean | false | Initial open state (uncontrolled) or current state (controlled) | | disabled | boolean | false | Prevents toggling when true | | duration | number \| [number, number] | 400 | Transition duration in ms. Tuple sets [open, close] independently | | easing | string | 'cubic-bezier(0.45, 0, 0.55, 1)' | CSS easing function | | overflow | string | 'hidden' | CSS overflow value applied when open | | controlled | boolean | false | Enables controlled mode — toggle via open prop changes | | onToggle | (wouldOpen: boolean, event: SyntheticEvent) => void | — | Called when the trigger is clicked | | onOpening | () => void | — | Called when the opening transition starts | | onOpen | TransitionEventHandler | — | Called when the opening transition ends | | onClosing | () => void | — | Called when the closing transition starts | | onClose | TransitionEventHandler | — | Called when the closing transition ends |

Return Value

| Property | Type | Description | | ------------------- | -------------------- | --------------------------------------------------------- | | isOpen | boolean | Current open state | | shouldRender | boolean | true once the collapsible has been opened at least once | | getTriggerProps() | () => TriggerProps | Spread onto the trigger element | | getContentProps() | () => ContentProps | Spread onto the content wrapper element |

Type Exports

import type {
    UseCollapsibleOptions,
    UseCollapsibleOutput,
    TriggerProps,
    ContentProps,
} from '@imoses.g/react-use-collapsible';

Advanced Usage

Controlled Mode

function ControlledCollapsible() {
    const [open, setOpen] = useState(false);
    const { getContentProps } = useCollapsible({
        open,
        controlled: true,
    });

    return (
        <div>
            <button onClick={() => setOpen((o) => !o)}>Toggle</button>
            <div {...getContentProps()}>
                <p>Controlled content.</p>
            </div>
        </div>
    );
}

Custom Duration & Easing

const { getTriggerProps, getContentProps } = useCollapsible({
    duration: [300, 200], // 300ms open, 200ms close
    easing: 'ease-in-out',
});

Lifecycle Callbacks

const { getTriggerProps, getContentProps } = useCollapsible({
    onOpening: () => console.log('Opening…'),
    onOpen: () => console.log('Fully open'),
    onClosing: () => console.log('Closing…'),
    onClose: () => console.log('Fully closed'),
    onToggle: (wouldOpen, event) =>
        console.log(wouldOpen ? 'Will open' : 'Will close'),
});

Lazy Rendering

function LazyCollapsible() {
    const { isOpen, shouldRender, getTriggerProps, getContentProps } =
        useCollapsible<HTMLDivElement, HTMLButtonElement>();

    return (
        <div>
            <button {...getTriggerProps()}>Toggle</button>
            <div {...getContentProps()}>
                {shouldRender && <HeavyComponent />}
            </div>
        </div>
    );
}

License

MIT