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

headless-tree-select

v1.0.4

Published

A high-performance, lightweight, and fully customizable headless tree selection component for React.

Readme

Headless Tree Select

npm version license

A high-performance, lightweight, and fully customizable tree selection component for React. It handles the complex recursive selection logic, indeterminate states, and expansion management while giving you total control over the UI.

Why Headless?

Most tree-select components come with heavy styles and rigid DOM structures. Headless Tree Select decouples logic from presentation. Whether you're using Tailwind CSS, Styled Components, or plain CSS, you can inject your own components or classes without fighting the library's defaults.

Features

  • 🌲 Infinite Depth: Support for deeply nested tree structures.
  • ✅ Cascading Selection: Built-in logic for parent-child selection linkage.
  • ➖ Indeterminate State: Accurate calculation of partial selection status.
  • 🎨 Granular Customization: Override specific parts (Checkbox, Label, Toggle) or just inject class names.
  • 🚀 Zero Dependencies: Micro-sized footprint with no external runtime dependencies.
  • ♿ Accessible: Optimized for use with semantic HTML and standard event handling.

Installation

npm install headless-tree-select
# or
yarn add headless-tree-select

Quick Start

The simplest way to get started is using the default built-in components.

import { TreeSelect } from 'headless-tree-select';
import { useState } from 'react';

const data = [
  {
    id: '1',
    label: 'Engineering',
    children: [
      { id: '1-1', label: 'Frontend' },
      { id: '1-2', label: 'Backend' },
    ],
  },
];

function App() {
  const [checked, setChecked] = useState([]);

  return (
    <TreeSelect 
      data={data} 
      value={checked} 
      onChange={(res) => setChecked(res.checked)} 
    />
  );
}

Customization

1. Granular Component Overrides

You can replace individual parts of the tree row by passing React components.

import { CheckIcon, ChevronRight } from 'lucide-react';

<TreeSelect
  data={data}
  value={checked}
  CheckBox={({ isChecked, isIndeterminate }) => (
    <div className={`custom-cb ${isChecked ? 'checked' : ''}`}>
       {isChecked && <CheckIcon size={12} />}
    </div>
  )}
  ToggleExpand={({ isExpanded, onToggle, hasChildren }) => (
    hasChildren && (
      <button onClick={onToggle} style={{ transform: isExpanded ? 'rotate(90deg)' : 'none' }}>
        <ChevronRight />
      </button>
    )
  )}
  Label={({ node }) => <span className="custom-label">{node.label}</span>}
/>

2. Tailoring with classNames

If you logic the default DOM but want different styles (e.g., using Tailwind CSS):

<TreeSelect
  data={data}
  classNames={{
    container: "space-y-1",
    item: "hover:bg-blue-50 rounded px-2 py-1 flex items-center gap-2",
    checkBox: "rounded border-blue-300 text-blue-600 focus:ring-blue-500",
    label: "text-sm text-gray-700 font-medium",
    toggleExpand: "text-gray-400 hover:text-blue-500",
  }}
/>

API Reference

Props

| Property | Type | Default | Description | | :--- | :--- | :--- | :--- | | data | TreeNode[] | [] | The tree data structure to render. | | value | string[] | [] | Array of currently checked node IDs (controlled). | | onChange | (result: TreeSelectChangeResult) => void | - | Callback triggered when selection or state changes. | | defaultExpandedIds | string[] | [] | IDs of nodes that should be expanded on initial render. | | CheckBox | React.ComponentType<CheckBoxProps> | - | Custom component for the checkbox. | | ToggleExpand | React.ComponentType<ToggleExpandProps> | - | Custom component for the expansion needle/toggle. | | Label | React.ComponentType<LabelProps> | - | Custom component for the node label. | | Toggle | React.ComponentType<ToggleProps> | - | Additional optional toggle component (if needed). | | classNames | TreeSelectClassNames | {} | CSS class name slots for styling. |

Data Structures

TreeNode

interface TreeNode {
  id: string;          // Unique identifier
  label: string;       // Display text
  children?: TreeNode[]; // Child nodes
  disabled?: boolean;  // If true, the node cannot be toggled
}

TreeSelectChangeResult

interface TreeSelectChangeResult {
  checked: string[];     // All fully selected node IDs
  halfChecked: string[]; // All indeterminate node IDs
}

Development

# Clone the repository
git clone https://github.com/imwind/headless-tree-select.git

# Install dependencies
npm install

# Start development build
npm run dev

# Run tests
npm run test

# Build production bundle
npm run build

License

MIT © imwind