react-resizable vs react-split-pane vs react-splitter-layout
リサイズ可能なコンポーネント
react-resizablereact-split-panereact-splitter-layout類似パッケージ:

リサイズ可能なコンポーネント

リサイズ可能なコンポーネントは、ユーザーがインターフェース内の要素のサイズを動的に変更できるようにするUIコンポーネントです。これにより、ユーザーはコンテンツの表示方法をカスタマイズでき、特に限られたスペースでの作業や、異なるデバイスや画面サイズに合わせてレイアウトを調整する際に便利です。リサイズ可能なコンポーネントは、ドラッグアンドドロップインターフェース、分割ペイン、または単純なリサイズハンドルを使用して実装されることが多く、Webアプリケーションやダッシュボード、デザインツールなど、さまざまなアプリケーションで使用されます。

npmのダウンロードトレンド

3 年

GitHub Starsランキング

統計詳細

パッケージ
ダウンロード数
Stars
サイズ
Issues
公開日時
ライセンス
react-resizable2,518,0612,57154.6 kB42ヶ月前MIT
react-split-pane377,9743,373104 kB317日前MIT
react-splitter-layout26,722432-387年前MIT

機能比較: react-resizable vs react-split-pane vs react-splitter-layout

リサイズ機能の範囲

  • react-resizable:

    react-resizableは、リサイズ可能なハンドルを持つ単一のコンポーネントにリサイズ機能を追加します。

  • react-split-pane:

    react-split-paneは、2つのペインを分割し、ユーザーがそれらのサイズを調整できるようにします。

  • react-splitter-layout:

    react-splitter-layoutは、複数のペインを水平または垂直に分割し、より複雑なレイアウトを作成します。

レイアウトの柔軟性

  • react-resizable:

    react-resizableは、リサイズ可能な要素を任意のコンポーネントに追加できるため、非常に柔軟です。

  • react-split-pane:

    react-split-paneは、2つのペインを分割するためのシンプルなインターフェースを提供しますが、ペインの数は2つに制限されています。

  • react-splitter-layout:

    react-splitter-layoutは、複数のペインを分割できるため、より高い柔軟性を提供します。

カスタマイズの容易さ

  • react-resizable:

    react-resizableは、リサイズハンドルやリサイズの動作を簡単にカスタマイズできます。

  • react-split-pane:

    react-split-paneは、ペインのサイズや分割線のスタイルをカスタマイズできますが、カスタマイズの範囲は限られています。

  • react-splitter-layout:

    react-splitter-layoutは、ペインのサイズ、分割線のスタイル、リサイズの動作など、より多くのプロパティをカスタマイズできます。

依存関係

  • react-resizable:

    react-resizableは、リサイズ機能を提供するための軽量なライブラリで、追加の依存関係はありません。

  • react-split-pane:

    react-split-paneは、2つのペインを分割するためのシンプルなコンポーネントで、特別な依存関係はありません。

  • react-splitter-layout:

    react-splitter-layoutは、複数のペインを分割するためのコンポーネントで、特別な依存関係はありません。

コード例

  • react-resizable:

    react-resizableを使用したリサイズ可能なコンポーネントの例

    import React from 'react';
    import { ResizableBox } from 'react-resizable';
    import 'react-resizable/css/styles.css';
    
    const ResizableComponent = () => {
      return (
        <ResizableBox width={200} height={200} minConstraints={[100, 100]} maxConstraints={[300, 300]}>
          <div style={{ border: '1px solid black', padding: '10px' }}>
            リサイズ可能なコンポーネント
          </div>
        </ResizableBox>
      );
    };
    
    export default ResizableComponent;
    
  • react-split-pane:

    react-split-paneを使用した分割ペインの例

    import React from 'react';
    import SplitPane from 'react-split-pane';
    
    const SplitPaneExample = () => {
      return (
        <SplitPane split="vertical" minSize={50} defaultSize={200} maxSize={-100}>
          <div style={{ background: '#f0f0f0' }}>左側のペイン</div>
          <div style={{ background: '#e0e0e0' }}>右側のペイン</div>
        </SplitPane>
      );
    };
    
    export default SplitPaneExample;
    
  • react-splitter-layout:

    react-splitter-layoutを使用した複数ペインの例

    import React from 'react';
    import { SplitterLayout } from 'react-splitter-layout';
    import 'react-splitter-layout/lib/index.css';
    
    const SplitterLayoutExample = () => {
      return (
        <SplitterLayout>
          <div style={{ background: '#f0f0f0' }}>ペイン1</div>
          <div style={{ background: '#e0e0e0' }}>ペイン2</div>
          <div style={{ background: '#d0d0d0' }}>ペイン3</div>
        </SplitterLayout>
      );
    };
    
    export default SplitterLayoutExample;
    

選び方: react-resizable vs react-split-pane vs react-splitter-layout

  • react-resizable:

    react-resizableを選択するのは、リサイズ機能を特定のコンポーネントに追加したい場合です。これは、リサイズ可能なハンドルを持つ単一の要素を作成するための柔軟で軽量なソリューションです。

  • react-split-pane:

    react-split-paneを選択するのは、2つのペインを分割し、ユーザーがそれらのサイズを調整できるようにしたい場合です。これは、サイドバーとメインコンテンツエリアのように、2つの領域を分割する必要があるアプリケーションに最適です。

  • react-splitter-layout:

    react-splitter-layoutを選択するのは、複数のペインを水平または垂直に分割し、より複雑なレイアウトを作成したい場合です。これは、3つ以上の領域を分割する必要があるアプリケーションに適しています。

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.

Compatibility

VersionReact Version
3.x>= 16.3
2.xSkipped
1.x14 - 17

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.Element<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: ReactElement<any> | (resizeHandle: ResizeHandleAxis, ref: ReactRef<HTMLElement>) => ReactElement<any>,
  // If you change this, be sure to update your css
  handleSize: [number, number] = [10, 10],
  lockAspectRatio: boolean = false,
  axis: 'both' | 'x' | 'y' | 'none' = 'both',
  minConstraints: [number, number] = [10, 10],
  maxConstraints: [number, number] = [Infinity, Infinity],
  onResizeStop?: ?(e: SyntheticEvent, data: ResizeCallbackData) => any,
  onResizeStart?: ?(e: SyntheticEvent, data: ResizeCallbackData) => any,
  onResize?: ?(e: SyntheticEvent, data: ResizeCallbackData) => any,
  draggableOpts?: ?Object,
  resizeHandles?: ?Array<ResizeHandleAxis> = ['se'],
  // If `transform: scale(n)` is set on the parent, this should be set to `n`.
  transformScale?: number = 1
};

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

{
  style?: Object // 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