@dschz/solid-auto-sizer

中文文档
0.1.3发布于 2025/06/06MITTypeScriptES Module零依赖近 30 天下载 6728

SolidJS component that automatically measures and provides the width and height of its parent — useful for virtualized lists, grids, and charts.

原始使用文档
文档目录20 节
  1. ✨ Features
  2. 📦 Installation
  3. 🚀 Quick Start
  4. 📖 Usage Examples
  5. Basic Usage
  6. With Initial Dimensions
  7. With Resize Callback
  8. With Custom Styling
  9. 📚 API Reference
  10. Props
  11. Types
  12. 🎮 Interactive Playground
  13. Running the Playground
  14. 💡 Common Use Cases
  15. Charts and Visualizations
  16. Virtualized Lists
  17. Responsive Grids
  18. 🌐 Browser Support
  19. 📄 License
  20. 🙏 Acknowledgments

SolidJS AutoSizer

@dschz/solid-auto-sizer

License: MIT npm version Bundle Size JSR CI

A SolidJS component that automatically measures and provides the width and height of its parent container, making it perfect for responsive layouts and virtualized components.

Inspired by React Virtualized Auto Sizer, this component brings the same powerful auto-sizing capabilities to SolidJS with modern improvements like ResizeObserver API and reactive signals for better performance and developer experience.

✨ Features

  • 🔍 Modern ResizeObserver API - Better performance and reliability
  • ⚡ Reactive updates - Uses SolidJS signals for efficient re-rendering
  • 🎛️ Customizable initial dimensions - Set default width/height for initial render
  • 📞 Optional resize callbacks - Get notified when size changes
  • 🎨 Style and class prop support - Full styling flexibility
  • 📦 Lightweight - Minimal bundle size with zero dependencies

📦 Installation

Install via your favorite package manager:

npm install solid-js @dschz/solid-auto-sizer
pnpm install solid-js @dschz/solid-auto-sizer
yarn install solid-js @dschz/solid-auto-sizer
bun install solid-js @dschz/solid-auto-sizer

This is a peer dependency, so it must be installed manually:

  • solid-js@>=1.6.0

🚀 Quick Start

import { AutoSizer } from "@dschz/solid-auto-sizer";

function MyComponent() {
  return (
    <div style={{ width: "100%", height: "400px" }}>
      <AutoSizer>
        {({ width, height }) => (
          <div style={{ width: `${width}px`, height: `${height}px` }}>
            Content that adapts to container size: {width} × {height}
          </div>
        )}
      </AutoSizer>
    </div>
  );
}

📖 Usage Examples

Basic Usage

import { AutoSizer } from "@dschz/solid-auto-sizer";

<AutoSizer>{({ width, height }) => <canvas width={width} height={height} />}</AutoSizer>;

With Initial Dimensions

<AutoSizer initialWidth={400} initialHeight={300}>
  {({ width, height }) => <MyChart width={width} height={height} data={data} />}
</AutoSizer>

With Resize Callback

<AutoSizer
  onResize={({ width, height }) => {
    console.log(`Container resized to: ${width}x${height}`);
  }}
>
  {({ width, height }) => <VirtualizedList width={width} height={height} items={items} />}
</AutoSizer>

With Custom Styling

<AutoSizer
  class="my-auto-sizer"
  style={{
    "background-color": "#f0f0f0",
    border: "1px solid #ccc",
    padding: "10px",
  }}
>
  {({ width, height }) => (
    <div>
      Styled container: {width} × {height}
    </div>
  )}
</AutoSizer>

📚 API Reference

Props

PropTypeDefaultDescription
children(size: Size) => JSX.ElementRequiredRender prop that receives the measured dimensions
initialWidthnumber0Default width for initial render before measurement
initialHeightnumber0Default height for initial render before measurement
onResize(size: Size) => voidundefinedCallback fired when container size changes
classstringundefinedCSS class for the container element
styleJSX.CSSPropertiesundefinedInline styles for the container (width/height are reserved)

Types

type Size = {
  readonly width: number;
  readonly height: number;
};

type AutoSizerProps = {
  readonly class?: string;
  readonly style?: Omit<JSX.CSSProperties, "width" | "height">;
  readonly initialWidth?: number;
  readonly initialHeight?: number;
  readonly onResize?: (size: Size) => void;
  readonly children: (size: Size) => JSX.Element;
};

🎮 Interactive Playground

Explore AutoSizer's capabilities with our comprehensive playground app! The playground includes:

  • Basic Examples - Simple responsive content, resize callbacks, custom dimensions, flexbox integration
  • Charts & Visualizations - Responsive bar charts, line charts, pie charts, and dashboards
  • Virtual Lists & Grids - Performance demos with 1K-100K items, searchable lists, variable heights
  • Real-time Demos - Interactive examples you can resize and modify

Running the Playground

# Clone the repository
git clone https://github.com/dsnchz/solid-auto-sizer.git
cd solid-auto-sizer

# Install dependencies
npm|pnpm|yarn|bun install

# Start the development server
npm|pnpm|yarn|bun start

The playground will be available at http://localhost:3000 and showcases real-world use cases including:

  • 📊 Chart Integration - See how AutoSizer works with data visualizations
  • 📋 Virtual Scrolling - Performance testing with large datasets
  • 🎨 Styling Examples - Custom themes and responsive designs
  • ⚡ Performance Monitoring - Real-time resize event tracking

💡 Common Use Cases

Charts and Visualizations

import { AutoSizer } from "@dschz/solid-auto-sizer";
import { LineChart } from "my-chart-library";

<div style={{ width: "100%", height: "400px" }}>
  <AutoSizer>
    {({ width, height }) => <LineChart width={width} height={height} data={chartData} />}
  </AutoSizer>
</div>;

Virtualized Lists

import { AutoSizer } from "@dschz/solid-auto-sizer";
import { VirtualList } from "my-virtualization-library";

<div style={{ width: "100%", height: "600px" }}>
  <AutoSizer>
    {({ width, height }) => (
      <VirtualList
        width={width}
        height={height}
        itemCount={items.length}
        itemSize={50}
        renderItem={({ index }) => <div>{items[index].name}</div>}
      />
    )}
  </AutoSizer>
</div>;

Responsive Grids

import { AutoSizer } from "@dschz/solid-auto-sizer";

<AutoSizer>
  {({ width }) => {
    const columns = Math.floor(width / 200); // 200px per column
    return (
      <div
        style={{
          display: "grid",
          "grid-template-columns": `repeat(${columns}, 1fr)`,
          gap: "16px",
        }}
      >
        {items.map((item) => (
          <GridItem key={item.id} {...item} />
        ))}
      </div>
    );
  }}
</AutoSizer>;

🌐 Browser Support

AutoSizer uses the ResizeObserver API, which is supported in:

  • Chrome 64+
  • Firefox 69+
  • Safari 13.1+
  • Edge 79+

For older browsers, you can use a ResizeObserver polyfill.

📄 License

MIT License - see LICENSE file for details.

🙏 Acknowledgments

版本与发布记录

@dschz/solid-auto-sizer 共发布 4 个版本,首次发布于 2025/05/27;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.1.3latest2025/06/061.4 年前
0.1.22025/05/281.5 年前
0.1.12025/05/281.5 年前
0.1.02025/05/271.5 年前

关于 @dschz/solid-auto-sizer 的常见问题

@dschz/solid-auto-sizer 是什么?

SolidJS component that automatically measures and provides the width and height of its parent — useful for virtualized lists, grids, and charts.

如何安装 @dschz/solid-auto-sizer?

在项目目录运行 npm install @dschz/solid-auto-sizer,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:solid-js(>=1.6.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@dschz/solid-auto-sizer 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@dschz/solid-auto-sizer 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@dschz/solid-auto-sizer 可以商用吗?

@dschz/solid-auto-sizer 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@dschz/solid-auto-sizer 的下载量有多少?

根据 npm 官方下载统计,@dschz/solid-auto-sizer 在 2026/10/13 – 2026/11/11 这 30 天内被下载 6,728 次,日均约 224 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@dschz/solid-auto-sizer 还在维护吗?

最新版本 0.1.3 发布于 2025/06/06(1.4 年前);至今共发布 4 个版本,其中过去 12 个月发布了 0 个;首次发布于 2025/05/27。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。