zesik / react-splitter-layout

A simple split layout for React and modern browsers. https://zesik.com/react-splitter-layout
MIT License
422 stars 128 forks source link

Set the width of the splitter programatically #68

Open soichih opened 1 year ago

soichih commented 1 year ago

Hello. I am trying to implement a button to toggle the width of the splitter (to "tack away" the secondary pane. I haven't been able to find a way to programmatically set the width of the splitter when a user click on this button. Is there a good way to accomplish this?

hvanphucs commented 1 year ago

To set the secondaryPaneSize of a SplitterLayout using a ref in the "react-splitter-layout" library, you can access the setState method of the ref to update the state. Here's an example:


import React, { useRef } from 'react';
import SplitterLayout from 'react-splitter-layout';

const MyComponent = () => {
  const splitterRef = useRef(null);

  const handleSetSecondaryPaneSize = () => {
    if (splitterRef.current) {
      splitterRef.current.setState({ secondaryPaneSize: 500 });
    }
  };

  return (
    <div>
      <button onClick={handleSetSecondaryPaneSize}>Set Secondary Pane Size</button>
      <SplitterLayout ref={splitterRef}>
        <div>Pane 1</div>
        <div>Pane 2</div>
      </SplitterLayout>
    </div>
  );
};

export default MyComponent;