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

@dsmlll/react-scroll

v0.1.2

Published

A React library for synchronizing scroll between a navigation component and content sections, with click-to-scroll functionality.

Readme

📦 @dsmlll/react-scroll

一个用于同步导航组件与内容区滚动位置的 React 库。它提供了自定义 Hook useScrollSync,可监听滚动、根据可见区域自动高亮导航标签,并支持点击导航标签平滑滚动到对应内容区。

English | 简体中文

✨ 特性

  • 点击导航标签可平滑滚动到对应内容区
  • 滚动时自动高亮当前可见内容区对应的导航标签
  • 滚动事件防抖处理,性能更优
  • 支持自定义参数(如防抖延迟、偏移量等)
  • 支持动态内容和基于 ref 的区块定位

🚀 安装

npm install @dsmlll/react-scroll
# 或
yarn add @dsmlll/react-scroll

💡 使用示例

import React, { useRef, useMemo } from 'react';
import { useScrollSync } from '@dsmlll/react-scroll';

const MyPageComponent = () => {
  const scrollContainerRef = useRef<HTMLDivElement>(null);
  const navTabsRef = useRef<HTMLDivElement>(null);

  // 定义区块及其 refs
  const sectionIds = ['section1', 'section2', 'section3'];
  const sectionRefObjects = useMemo(() => {
    const refs: Record<string, React.RefObject<HTMLDivElement>> = {};
    sectionIds.forEach(id => {
      refs[id] = React.createRef<HTMLDivElement>();
    });
    return refs;
  }, [sectionIds]);

  const { activeTab, handleTabClick } = useScrollSync({
    scrollContainerRef, // 滚动容器的 ref
    navTabsRef,         // 导航栏的 ref
    sections: sectionRefObjects, // 区块 id 到 ref 的映射
    initialActiveTab: sectionIds[0], // 可选,初始高亮的区块 id
    options: {
      debounceDelay: 100, // 可选,滚动事件防抖延迟
      // 还可设置 offsetTop、offsetBottom 等参数
    }
  });

  return (
    <div>
      <div ref={navTabsRef} className="navigation-tabs">
        {sectionIds.map(id => (
          <div
            key={id}
            className={`tab ${activeTab === id ? 'active' : ''}`}
            onClick={() => handleTabClick(id)}
          >
            {id.toUpperCase()}
          </div>
        ))}
      </div>

      <div ref={scrollContainerRef} className="scrollable-content">
        {sectionIds.map(id => (
          <div key={id} id={id} ref={sectionRefObjects[id]} className="content-section">
            <h2>{id.toUpperCase()}</h2>
            {/* 区块内容 */}
            <p style={{height: "500px"}}>滚动内容 {id}</p>
          </div>
        ))}
      </div>
    </div>
  );
};

export default MyPageComponent;

📖 API

useScrollSync(props: UseScrollSyncProps): UseScrollSyncReturn

UseScrollSyncProps

  • scrollContainerRef: React.RefObject<HTMLElement | null> - 滚动容器的 ref。
  • navTabsRef: React.RefObject<HTMLElement | null> - 导航栏容器的 ref。
  • sections: Record<string, React.RefObject<HTMLElement | null>> - 区块 id 到 ref 的映射。
  • initialActiveTab?: string - 初始高亮的区块 id。
  • onActiveTabChange?: (tabId: string) => void - 高亮区块变化时的回调。
  • options?: ScrollSyncOptions
    • debounceDelay?: number (默认: 100) - 滚动事件防抖延迟(毫秒)。
    • offsetTop?: number (默认: 0) - 距顶部偏移量。
    • offsetBottom?: number (默认: 0) - 距底部偏移量。
    • behavior?: 'auto' | 'smooth' (默认: 'smooth') - 点击导航时的滚动行为。

UseScrollSyncReturn

  • activeTab: string - 当前高亮的区块 id。
  • handleTabClick: (tabId: string) => void - 点击导航标签时调用,自动滚动到对应区块。

🤝 贡献

欢迎提交 PR 或 issue 参与贡献!

📄 许可证

本项目基于 MIT 协议开源,详见 LICENSE