@geoffcox/react-splitter

中文文档
2.1.2发布于 2023/01/17MITTypeScriptCommonJS近 30 天下载 1.1万

A splitter for React components that leverages CSS grid templates for simple and consistent resizing.

原始使用文档
文档目录10 节
  1. Overview
  2. Vertical Split
  3. Horizontal Split
  4. Set the initial split size
  5. Nest Splits
  6. Constrain minimum pane sizes
  7. Customize the splitter size
  8. Customize the splitter colors
  9. Custom render the splitter
  10. Monitor changes

@geoffcox/react-splitter

A resizable splitter for React that leverages CSS display:grid

Live Demo

Overview

See the ../Readme.md for features, version history, etc.

Installation

npm install --save @geoffcox/react-splitter

Usage

To create vertical or horizontal splits you only need the Split component.

Vertical Split

The default creates a left|right split down the middle, no minimum pane sizes, and renders the default splitter.

<Split>
  <div>This is the left pane.</div>
  <div>This is the right pane.<div>
</Split>

Horizontal Split

Add the horizontal attribute to split top/bottom.

<Split horizontal>
  <div>This is the top pane.</div>
  <div>This is the bottom pane.<div>
</Split>

Set the initial split size

Add the initialPrimarySize property to control where the initial split occurs.

<Split initialPrimarySize='30%'>
  <div>Primary pane</div>
  <div>Secondary pane<div>
</Split>

To support double-clicking to reset back to the initial size, add the resetOnDoubleClick property.

<Split initialPrimarySize='30%' resetOnDoubleClick>
  <div>Primary pane</div>
  <div>Secondary pane<div>
</Split>

Nest Splits

Just nest Split componets to create whatever layout you like. Here is an example of a common layout for a main, detail, and output view.

<Split initialPrimarySize='30%'>
  <div>This is the left pane.</div>
  <Split horizontal initialPrimarySize='60%'>
    <div>This is the right-top pane.</div>
    <div>This is the right-bottom pane.</div>
  </Split>
</Split>

Constrain minimum pane sizes

You can prevent either pane from becoming too small using the minPrimarySize and minSecondarySize properties. For vertical splits, primary is the left pane and secondary is the right pane. For horizontal splits, primary is the top pane and secondary is the bottom pane.

<Split minPrimarySize='250px' minSecondarySize='15%'>
  <div>This pane won't get smaller than 250 pixels.</div>
  <div>This pane won't get any smaller than 15% of the overall size of the split control./<div>
</Split>

Customize the splitter size

You can set the size of the hit area (where the user can click to start draggin the splitter) with the splitterSize property.

<Split splitterSize='10px'>
  <div>Primary pane</div>
  <div>Secondary pane<div>
</Split>

Customize the splitter colors

You can change the colors of the default splitter with the defaultSplitterColors property.

const colors = {
  color: 'red',
  hover: '#00FF00',
  drag: 'blue'
};

<Split defaultSplitterColors={colors}>
  <div>Primary pane</div>
  <div>Secondary pane<div>
</Split>

Custom render the splitter

You can render your own splitter by passing a callback to the renderSplitter property.

const renderSplitter = (props: RenderSplitterProps) => {
  return <div>Your splitter code goes here.</div>
};

<Split renderSplitter={renderSplitter}>
  <div>Primary pane</div>
  <div>Secondary pane<div>
</Split>

The callback receives the RenderSplitterProps to let you know the current size of the splitter, if the split is horizontal, and if the splitter is currently being dragged.

export type RenderSplitterProps = {
  pixelSize: number;
  horizontal: boolean;
  dragging: boolean;
};

Monitor changes

You can use event callbacks to monitor changes to the primary size and the measured sizes. The primary size is a CSS unit string (percentage or initial size). The measured sizes are pixels sizes.

const onSplitChanged = (primarySize: string) => {
  console.log(`The split is: ${primarySize}`);
};

const onMeasuredSizesChanged = (sizes: SplitMeasuredPixelSizes) => {
  console.log(`The primary pane is: ${sizes.primary}px`);
  console.log(`The splitter is: ${sizes.splitter}px`);
  console.log(`The secondary pane is: ${sizes.secondary}px`);
};

<Split onSplitChanged={onSplitChanged} onMeasuredSizesChanged={onMeasuredSizesChanged}>
  <div>Primary pane</div>
  <div>Secondary pane<div>
</Split>

Integrating into a web application

If you are using a style framework like Fluent, Material UI, or Bootstrap then your root div will likely have CSS styles applied that help this splitter work correctly. If you have no root stylesheet, you might have problems with vertical scrolling.

Here are some recommended CSS properties for your top-level divs if you are building a single-page application. In this case #app is the mounting point for React.Render. You can see this approach used in the demo application.

body {
  height: 100vh;
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  overflow: hidden;
  padding: 0;
  margin: 0;
}

body,
div {
  box-sizing: border-box;
}

#app {
  position: relative;
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  outline: none;
  overflow: hidden;
}

版本与发布记录

@geoffcox/react-splitter 共发布 12 个版本,首次发布于 2020/06/12;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.1.2latest2023/01/173 年前
2.1.12021/06/135 年前
2.1.02021/02/265 年前
2.0.32021/01/075 年前
2.0.22021/01/055 年前
2.0.12020/12/195 年前

关于 @geoffcox/react-splitter 的常见问题

@geoffcox/react-splitter 是什么?

A splitter for React components that leverages CSS grid templates for simple and consistent resizing.

如何安装 @geoffcox/react-splitter?

在项目目录运行 npm install @geoffcox/react-splitter,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>=16.8.0 <19.0.0)、react-dom(>=16.8.0 <19.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@geoffcox/react-splitter 支持 TypeScript 吗?

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

@geoffcox/react-splitter 支持 ESM 还是 CommonJS?

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

@geoffcox/react-splitter 可以商用吗?

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

@geoffcox/react-splitter 的下载量有多少?

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

@geoffcox/react-splitter 还在维护吗?

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