react-resizable-panels

repository·main·Indexed 26 days ago

https://github.com/bvaughn/react-resizable-panels

A set of React components for creating resizable panel groups and layouts. It includes the Group component for managing horizontal or vertical layouts, the Panel component for resizable content with support for size constraints and collapsibility, and the Separator component for draggable handles. The library provides built-in TypeScript definitions and an imperative API to programmatically get and set layouts.

Tokens
3.8K
Snippets
4
Records
28
Agent score
90%

What's inside react-resizable-panels

  1. Add React-specific lint rules to ESLint

    main

    To add React-specific linting, install eslint-plugin-react-x and eslint-plugin-react-dom. You can then include these plugins in your eslint.config.js and spread their recommended rules into the rules object.

    // eslint.config.js
    import reactX from 'eslint-plugin-react-x'
    import reactDom from 'eslint-plugin-react-dom'
    
    export default tseslint.config({
      plugins: {
        // Add the react-x and react-dom plugins
        'react-x': reactX,
        'react-dom': reactDom,
      },
      rules: {
        // other rules...
        // Enable its recommended typescript rules
        ...reactX.configs['recommended-typescript'].rules,
        ...reactDom.configs.recommended.rules,
      },
    })
  2. Enable type-aware ESLint rules in Vite projects

    main

    For production applications, it is recommended to upgrade your ESLint configuration to use type-aware lint rules. This requires updating the extends array to use recommendedTypeChecked, strictTypeChecked, or stylisticTypeChecked and configuring parserOptions to point to your tsconfig files.

    export default tseslint.config({
      extends: [
        // Remove ...tseslint.configs.recommended and replace with this
        ...tseslint.configs.recommendedTypeChecked,
        // Alternatively, use this for stricter rules
        ...tseslint.configs.strictTypeChecked,
        // Optionally, add this for stylistic rules
        ...tseslint.configs.stylisticTypeChecked,
      ],
      languageOptions: {
        // other options...
        parserOptions: {
          project: ['./tsconfig.node.json', './tsconfig.app.json'],
          tsconfigRootDir: import.meta.dirname,
        },
      },
    })
  3. Use the Separator component

    main

    The Separator component is the draggable handle between Panel components. While not strictly required, it is recommended for improved keyboard accessibility.

    Key Features:

    • Styling: Use the data-separator attribute to apply custom hover and active styles.
    • Accessibility: Automatically renders required WAI-ARIA properties.
    • Double-click behavior: By default, double-clicking a separator resets the adjacent panel to its default size. Set disableDoubleClick to true to prevent this.

    Note: Separator elements must be direct DOM children of their parent Group.

  4. Use the Panel component

    main

    The Panel component wraps resizable content. It can be configured with size constraints and collapsible behavior.

    Size Units Supported:

    • Numbers: Interpreted as pixels (e.g., 200).
    • Strings without units: Interpreted as percentages (e.g., "50").
    • Explicit CSS units: Strings ending in px, %, em, rem, vh, or vw (e.g., "10rem").

    Key Features:

    • Collapsibility: Enable via collapsible. A panel collapses when its size is less than minSize. Use collapsedSize to set the size when collapsed (defaults to 0%).
    • Resize Behavior: Use groupResizeBehavior to control how the panel reacts when the parent Group is resized:
      • "preserve-relative-size" (default): Retains current percentage of the Group.
      • "preserve-pixel-size": Retains current pixel size.
      • Note: A Group must contain at least one Panel with preserve-relative-size behavior.
    • Imperative API: Use panelRef to access collapse(), expand(), getSize(), isCollapsed(), and resize(size).
  5. Use the Group component

    main

    The Group component wraps a set of resizable Panel components. It manages the layout and allows resizing either horizontally or vertically.

    Key Features:

    • Orientation: Set via the orientation prop ("horizontal" or "vertical"; defaults to "horizontal").
    • Layout Persistence: Use defaultLayout to allow layouts to be remembered between page reloads.
    • Imperative API: Use the groupRef to access getLayout() and setLayout(layout).
    • Layout Callbacks:
      • onLayoutChange: Called continuously during a resize (e.g., while a pointer is moving).
      • onLayoutChanged: Called after a resize is complete. Recommended for saving layouts to storage. The second argument includes isUserInteraction to distinguish between user input and programmatic changes.

    Note: Panel and Separator components must be direct DOM children of the Group.

  6. Configure Panel props

    main

    The Panel component accepts the following optional props:

    NameDescription
    classNameCSS class name (applied to a nested HTMLDivElement).
    idUnique identifier. Falls back to useId. Assigned to data-panel attribute.
    styleCSS properties (applied to a nested HTMLDivElement).
    collapsedSizeSize when collapsed (default: 0%).
    collapsibleEnables collapsing behavior.
    defaultSizeInitial size. Supports pixels, percentages, or CSS units.
    disabledPrevents the panel from being resized directly or indirectly.
    elementRefRef attached to the root HTMLDivElement.
    groupResizeBehavior"preserve-relative-size" (default) or "preserve-pixel-size".
    maxSizeMaximum size constraint.
    minSizeMinimum size constraint.
    onResizeCalled when panel sizes change. Params: panelSize, id, prevPanelSize.
    panelRefExposes collapse(), expand(), getSize(), isCollapsed(), and resize(size).
  7. Configure Separator props

    main

    The Separator component accepts the following optional props:

    NameDescription
    classNameCSS class name.
    idUnique identifier. Falls back to useId. Assigned to data-separator attribute.
    styleCSS properties. Use data-separator for hover/active styles. (Cannot override flex-grow or flex-shrink).
    disabledPrevents the separator from being used to resize neighboring panels.
    disableDoubleClickIf true, double-clicking will not reset the adjacent panel to its default size.
    elementRefRef attached to the root HTMLDivElement.