react-resizable vs react-grid-layout vs react-splitter-layout
React 布局和调整库
react-resizablereact-grid-layoutreact-splitter-layout类似的npm包:

React 布局和调整库

在现代 Web 开发中,布局和调整是构建响应式和用户友好的界面的关键。React 提供了一些强大的库来帮助开发者实现灵活的布局和可调整的组件。这些库各自具有独特的功能,能够满足不同的需求,从而提升用户体验和开发效率。

npm下载趋势

3 年

统计详情

npm包名称
下载量
Stars
大小
Issues
发布时间
License
react-resizable3,631,867-44.3 kB-4 个月前MIT
react-grid-layout2,894,122-479 kB-21 天前MIT
react-splitter-layout23,280---8 年前MIT

功能对比: react-resizable vs react-grid-layout vs react-splitter-layout

布局功能

  • react-resizable:

    react-resizable 提供了简单的可调整大小功能,允许用户通过拖动边缘来改变组件的大小。它的实现非常轻量,适合需要基本调整大小功能的场景。

  • react-grid-layout:

    react-grid-layout 提供了一个强大的网格系统,支持响应式设计和拖放功能。它允许开发者定义网格项的大小和位置,并支持动态添加和删除网格项,非常适合构建复杂的仪表板和布局。

  • react-splitter-layout:

    react-splitter-layout 提供了一个分隔布局,允许用户通过拖动分隔条来调整面板的大小。它支持水平和垂直分隔,适合需要灵活面板布局的应用。

用户交互

  • react-resizable:

    react-resizable 允许用户通过简单的拖动操作来调整组件大小,提供了直观的用户交互体验,适合需要用户自定义大小的场景。

  • react-grid-layout:

    react-grid-layout 提供了丰富的用户交互功能,包括拖放、调整大小和响应式布局,能够提升用户体验,使用户能够自由定制界面。

  • react-splitter-layout:

    react-splitter-layout 通过拖动分隔条来调整面板大小,提供了直观的操作方式,用户可以根据需要灵活调整布局。

灵活性和扩展性

  • react-resizable:

    react-resizable 设计简单,易于集成到现有项目中,适合需要快速实现可调整大小功能的场景。

  • react-grid-layout:

    react-grid-layout 具有很高的灵活性,支持多种布局配置和自定义选项,开发者可以根据具体需求进行扩展,适合复杂的应用场景。

  • react-splitter-layout:

    react-splitter-layout 提供了灵活的面板配置选项,支持多种布局方式,适合需要动态调整面板大小的应用。

学习曲线

  • react-resizable:

    react-resizable 的学习曲线较平缓,易于上手,适合初学者和需要快速实现功能的开发者。

  • react-grid-layout:

    react-grid-layout 的学习曲线相对较陡,特别是对于复杂的布局和响应式设计,开发者需要花费时间理解其 API 和配置选项。

  • react-splitter-layout:

    react-splitter-layout 的学习曲线适中,提供了简单的 API,开发者可以快速实现可分割布局。

性能

  • react-resizable:

    react-resizable 的性能表现良好,适合处理少量组件的调整大小操作,能够保持良好的响应速度。

  • react-grid-layout:

    react-grid-layout 在处理大量网格项时可能会面临性能挑战,开发者需要注意优化渲染和更新,以确保流畅的用户体验。

  • react-splitter-layout:

    react-splitter-layout 在面板数量较多时可能会影响性能,开发者应注意合理配置和优化,以确保流畅的操作体验。

如何选择: react-resizable vs react-grid-layout vs react-splitter-layout

  • react-resizable:

    选择 react-resizable 如果你只需要简单的可调整大小功能,适合需要用户调整组件大小的场景,且希望保持简单的实现。

  • react-grid-layout:

    选择 react-grid-layout 如果你需要一个强大的网格布局系统,支持拖放和响应式设计,适合构建复杂的仪表板和布局。

  • react-splitter-layout:

    选择 react-splitter-layout 如果你需要在应用中实现可分割的布局,允许用户通过拖动分隔条来调整面板的大小,适合需要灵活面板布局的应用。

react-resizable的README

React-Resizable

npm version npm downloads Build Status

View the Demo

A simple widget that can be resized via one or more handles.

You can either use the <Resizable> element directly, or use the much simpler <ResizableBox> element.

See the example and associated code in ExampleLayout and ResizableBox for more details.

Table of Contents

Installation

$ npm install --save react-resizable

Extracting Styles

You must include the associated styles in your application, otherwise the resize handles will not be visible and will not work properly.

// In your JS/TS entry point:
import 'react-resizable/css/styles.css';

Or import it in your CSS:

@import 'react-resizable/css/styles.css';

If you're using a bundler that doesn't support CSS imports, you can find the styles at node_modules/react-resizable/css/styles.css and include them manually.

TypeScript

As of 4.0.0, the library is authored in TypeScript and ships bundled type declarations in build/*.d.ts. You do not need to install @types/react-resizable; if you previously installed it, remove it so the bundled types take precedence:

npm uninstall @types/react-resizable
# or
yarn remove @types/react-resizable

Public types are re-exported from the package root:

import {
  Resizable,
  ResizableBox,
  // types
  type ResizeCallbackData,
  type ResizeHandleAxis,
  type Axis,
  type Props as ResizableProps,
} from 'react-resizable';

Flow

Flow is no longer supported as of 4.0.0. Earlier versions shipped *.js.flow sidecar files generated from the Flow-annotated source; those have been removed.

If you still need Flow types, you can vendor the last Flow-annotated source locally from the 3.2.0 tag. They will not be updated to reflect changes landing after 4.0.0. The official recommendation is to migrate to TypeScript.

Compatibility

VersionReact VersionTypes
4.x>= 16.3TypeScript (bundled)
3.x>= 16.3Flow (*.js.flow)
2.xSkipped—
1.x14 - 17Flow

Usage

This package has two major exports:

  • <Resizable>: A raw component that does not have state. Use as a building block for larger components, by listening to its callbacks and setting its props.
  • <ResizableBox>: A simple <div {...props} /> element that manages basic state. Convenient for simple use-cases.

<Resizable>

import { Resizable } from 'react-resizable';
import 'react-resizable/css/styles.css';

class Example extends React.Component {
  state = {
    width: 200,
    height: 200,
  };

  onResize = (event, {node, size, handle}) => {
    this.setState({width: size.width, height: size.height});
  };

  render() {
    return (
      <Resizable
        height={this.state.height}
        width={this.state.width}
        onResize={this.onResize}
      >
        <div
          className="box"
          style={{width: this.state.width + 'px', height: this.state.height + 'px'}}
        >
          <span>Contents</span>
        </div>
      </Resizable>
    );
  }
}

<ResizableBox>

import { ResizableBox } from 'react-resizable';
import 'react-resizable/css/styles.css';

class Example extends React.Component {
  render() {
    return (
      <ResizableBox
        width={200}
        height={200}
        draggableOpts={{grid: [25, 25]}}
        minConstraints={[100, 100]}
        maxConstraints={[300, 300]}
      >
        <span>Contents</span>
      </ResizableBox>
    );
  }
}

Props

These props apply to both <Resizable> and <ResizableBox>. Unknown props that are not in the list below will be passed to the child component.

type ResizeCallbackData = {
  node: HTMLElement;
  size: {width: number; height: number};
  handle: ResizeHandleAxis;
};

type ResizeHandleAxis = 's' | 'w' | 'e' | 'n' | 'sw' | 'nw' | 'se' | 'ne';

type ResizableProps = {
  children: React.ReactElement<any>;
  width: number;
  height: number;
  // Either a ReactElement to be used as handle, or a function
  // returning an element that is fed the handle's location as its first argument.
  handle?:
    | React.ReactElement<any>
    | ((resizeHandle: ResizeHandleAxis, ref: React.RefObject<HTMLElement>) => React.ReactElement<any>);
  // If you change this, be sure to update your css. Default: [20, 20].
  handleSize?: [number, number];
  lockAspectRatio?: boolean;                       // default: false
  axis?: 'both' | 'x' | 'y' | 'none';              // default: 'both'
  minConstraints?: [number, number];               // default: [20, 20]
  maxConstraints?: [number, number];               // default: [Infinity, Infinity]
  onResizeStop?:  (e: React.SyntheticEvent, data: ResizeCallbackData) => any;
  onResizeStart?: (e: React.SyntheticEvent, data: ResizeCallbackData) => any;
  onResize?:      (e: React.SyntheticEvent, data: ResizeCallbackData) => any;
  // Forwarded to react-draggable's <DraggableCore>.
  draggableOpts?: Partial<React.ComponentProps<typeof import('react-draggable').DraggableCore>>;
  resizeHandles?: ResizeHandleAxis[];              // default: ['se']
  // If `transform: scale(n)` is set on the parent, this should be set to `n`.
  transformScale?: number;                         // default: 1
};

The following props can also be used on <ResizableBox>:

{
  style?: React.CSSProperties; // styles the returned <div />
}

If a width or height is passed to <ResizableBox>'s style prop, it will be ignored as it is required for internal function.

You can pass options directly to the underlying DraggableCore instance by using the prop draggableOpts. See the demo for more on this.

Resize Handle

If you override the resize handle, we expect that any ref passed to your new handle will represent the underlying DOM element.

This is required, as react-resizable must be able to access the underlying DOM node to attach handlers and measure position deltas.

There are a few ways to do this:

Native DOM Element

This requires no special treatment.

<Resizable handle={<div className="foo" />} />

Custom React Component

You must forward the ref and props to the underlying DOM element.

Class Components

class MyHandleComponent extends React.Component {
  render() {
    const {handleAxis, innerRef, ...props} = this.props;
    return <div ref={innerRef} className={`foo handle-${handleAxis}`} {...props} />
  }
}
const MyHandle = React.forwardRef((props, ref) => <MyHandleComponent innerRef={ref} {...props} />);

<Resizable handle={<MyHandle />} />

Functional Components

const MyHandle = React.forwardRef((props, ref) => {
  const {handleAxis, ...restProps} = props;
  return <div ref={ref} className={`foo handle-${handleAxis}`} {...restProps} />;
});

<Resizable handle={<MyHandle />} />

Custom Function

You can define a function as a handle, which will simply receive an axis (see above ResizeHandleAxis type) and ref. This may be more clear to read, depending on your coding style.

const MyHandle = (props) => {
  return <div ref={props.innerRef} className="foo" {...props} />;
};

<Resizable handle={(handleAxis, ref) => <MyHandle innerRef={ref} className={`foo handle-${handleAxis}`} />} />

License

MIT