@carbon-labs/wc-resizer

中文文档
0.5.0发布于 2026/07/16Apache-2.0TypeScriptCommonJS构建来源已验证近 30 天下载 1.3万

Carbon Labs - Resizer web components for creating resizable panels

原始使用文档
文档目录13 节
  1. Components
  2. Features
  3. Zero-Setup Usage (Declarative)
  4. Composable Usage (Programmatic)
  5. Usage
  6. Declarative Mode (Zero-Setup)
  7. Programmatic Mode (Event-Driven)
  8. CSS Custom Properties
  9. Accessibility
  10. Keyboard Navigation
  11. ARIA Attributes
  12. Browser Support
  13. License

Resizer Web Components

A set of web components for creating resizable panels with support for both declarative (zero-setup) and programmatic (composable) usage modes.

Components

  • clabs-resizer-grid - Container for resizable panels
  • clabs-resizer-panel - Individual resizable panel
  • clabs-resizer-handle - Handle for resizing between panels
  • clabs-resizer-handle-pivot - Corner pivot for advanced resizing

Features

Zero-Setup Usage (Declarative)

Enable direct usage of components without any JavaScript setup. Users can compose layouts purely via HTML and have resizing work out-of-the-box.

Composable Usage (Programmatic)

Allow clabs-resizer-handle and clabs-resizer-handle-pivot to be used as standalone components that emit drag events and delta values. Consumers (e.g., frameworks like React) can handle resizing logic via JavaScript.

Usage

Declarative Mode (Zero-Setup)

Basic Horizontal Layout

<clabs-resizer-grid axis="x">
  <clabs-resizer-panel slot="left"> Left panel content </clabs-resizer-panel>

  <clabs-resizer-handle slot="handle-horizontal"></clabs-resizer-handle>

  <clabs-resizer-panel slot="right"> Right panel content </clabs-resizer-panel>
</clabs-resizer-grid>

Basic Vertical Layout

<clabs-resizer-grid axis="y">
  <clabs-resizer-panel slot="top"> Top panel content </clabs-resizer-panel>

  <clabs-resizer-handle slot="handle-vertical"></clabs-resizer-handle>

  <clabs-resizer-panel slot="bottom">
    Bottom panel content
  </clabs-resizer-panel>
</clabs-resizer-grid>

With Pivot Handle

<clabs-resizer-grid axis="x">
  <clabs-resizer-panel slot="left">
    Left panel content
    <clabs-resizer-handle slot="handle-horizontal">
      <clabs-resizer-handle-pivot></clabs-resizer-handle-pivot>
    </clabs-resizer-handle>
  </clabs-resizer-panel>

  <clabs-resizer-panel slot="right"> Right panel content </clabs-resizer-panel>
</clabs-resizer-grid>

Programmatic Mode (Event-Driven)

The resizer handle can be used independently without a grid container by listening to its events:

Events

  • resize-start - Fired when dragging starts

    • detail.axis - The resize axis ('x' or 'y')
    • detail.startPosition - Starting position {x, y}
  • resize-drag - Fired during dragging

    • detail.axis - The resize axis ('x' or 'y')
    • detail.delta - Movement delta in pixels
    • detail.position - Current position {x, y}
  • resize-end - Fired when dragging ends

    • detail.axis - The resize axis ('x' or 'y')
    • detail.delta - Total movement delta in pixels
    • detail.position - Final position {x, y}
  • resize-reset - Fired on double-tap/double-click

Single Panel Example

<div class="container">
  <div class="panel" id="myPanel">Panel content</div>
  <clabs-resizer-handle></clabs-resizer-handle>
</div>

<script>
  const handle = document.querySelector('clabs-resizer-handle');
  const panel = document.getElementById('myPanel');
  let initialHeight = 0;

  handle.addEventListener('resize-start', (e) => {
    initialHeight = panel.offsetHeight;
  });

  handle.addEventListener('resize-drag', (e) => {
    const newHeight = initialHeight + e.detail.delta;
    panel.style.height = `${Math.max(48, newHeight)}px`;
  });
</script>

Bounded Resize Example

<div class="container" style="height: 400px">
  <div class="panel" id="boundedPanel">Panel content</div>
  <clabs-resizer-handle></clabs-resizer-handle>
</div>

<script>
  const handle = document.querySelector('clabs-resizer-handle');
  const panel = document.getElementById('boundedPanel');
  const container = document.querySelector('.container');
  let initialHeight = 0;

  handle.addEventListener('resize-start', (e) => {
    initialHeight = panel.offsetHeight;
  });

  handle.addEventListener('resize-drag', (e) => {
    const containerHeight = container.offsetHeight;
    const newHeight = initialHeight + e.detail.delta;

    // Constrain between min and max
    const constrainedHeight = Math.max(
      48,
      Math.min(newHeight, containerHeight - 20)
    );

    panel.style.height = `${constrainedHeight}px`;
  });
</script>

CSS Custom Properties

  • --resizer-thickness - Thickness of the resizer handle (default: 4px)
  • --resizer-grab-thickness - Additional grab area thickness (default: 0px)
  • --resizer-grab-color - Color of the grab area (default: transparent)
  • --start-element-size - Size of the start panel (default: 1fr)
  • --end-element-size - Size of the end panel (default: 1fr)

Accessibility

Keyboard Navigation

The resizer handle supports keyboard navigation:

  • Arrow Keys: Move the resizer handle in 5px increments
    • ArrowUp/ArrowDown for horizontal handles (y-axis)
    • ArrowLeft/ArrowRight for vertical handles (x-axis)
  • Shift + Arrow Keys: Move in 25px increments for faster resizing
  • Home: Collapse the start panel to minimum size (grid mode only)
  • End: Expand the start panel to maximum size (grid mode only)
  • Double-click/Double-tap: Reset panels to default sizes

ARIA Attributes

Grid-Based Usage (Automatic)

When using clabs-resizer-handle within a clabs-resizer-grid, ARIA attributes are automatically managed:

  • role="separator" - Identifies the element as a separator
  • aria-orientation - Set to "vertical" or "horizontal" based on axis
  • aria-valuenow - Current split percentage (0-100)
  • aria-valuemin="0" - Minimum value
  • aria-valuemax="100" - Maximum value
  • aria-valuetext - Human-readable split ratio (e.g., "60% / 40%")
  • aria-live="assertive" - Announces changes to screen readers

Standalone Usage (Manual)

When using clabs-resizer-handle without a grid (programmatic mode), you are responsible for managing ARIA attributes to ensure accessibility:

<div class="container">
  <div class="panel" id="myPanel">Panel content</div>
  <clabs-resizer-handle
    aria-valuenow="50"
    aria-valuemin="0"
    aria-valuemax="100"
    aria-valuetext="Panel height: 200px"></clabs-resizer-handle>
</div>

<script>
  const handle = document.querySelector('clabs-resizer-handle');
  const panel = document.getElementById('myPanel');
  let initialHeight = 0;

  handle.addEventListener('resize-start', (e) => {
    initialHeight = panel.offsetHeight;
  });

  handle.addEventListener('resize-drag', (e) => {
    const newHeight = initialHeight + e.detail.delta;
    const constrainedHeight = Math.max(48, newHeight);
    panel.style.height = `${constrainedHeight}px`;

    // Update ARIA attributes for accessibility
    const percentage = Math.round((constrainedHeight / 400) * 100);
    handle.setAttribute('aria-valuenow', percentage.toString());
    handle.setAttribute(
      'aria-valuetext',
      `Panel height: ${constrainedHeight}px`
    );
  });
</script>

Note: The role="separator" attribute requires aria-valuenow to be present for proper accessibility compliance. When using standalone handles, always update these attributes during resize operations.

Browser Support

Modern browsers with Web Components support (Chrome, Firefox, Safari, Edge)

License

Apache-2.0

版本与发布记录

@carbon-labs/wc-resizer 共发布 3 个版本,首次发布于 2026/05/12;过去 12 个月发布了 3 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.5.0latest2026/07/163 个月前
0.3.02026/05/135 个月前
0.2.02026/05/125 个月前

关于 @carbon-labs/wc-resizer 的常见问题

@carbon-labs/wc-resizer 是什么?

Carbon Labs - Resizer web components for creating resizable panels

如何安装 @carbon-labs/wc-resizer?

在项目目录运行 npm install @carbon-labs/wc-resizer,或使用 pnpm add / yarn add / bun add。

@carbon-labs/wc-resizer 支持 TypeScript 吗?

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

@carbon-labs/wc-resizer 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@carbon-labs/wc-resizer 可以商用吗?

@carbon-labs/wc-resizer 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@carbon-labs/wc-resizer 的下载量有多少?

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

@carbon-labs/wc-resizer 还在维护吗?

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