Have you ever struggled with creating dynamic and resizable layouts in your React applications? If so, you're not alone. Building flexible and user-friendly UIs can be challenging, but fear not! In this blog post, we'll dive into the world of React resizable panels and explore how they can simplify the process of creating customizable layouts.
To start with, we need to create a new React project. This can be done using the create-react-app command. This command sets up a new React project with a default structure and configuration. Once the project is set up, we can start adding our components and functionality.
Before we can start using the react-resizable-panels package, we first need to install it in our project. This can be done using either npm or yarn. Open your terminal, navigate to your project directory, and run the following command:
1npm install react-resizable-panels
or
1yarn add react-resizable-panels
Once the package is installed, we can import it into our project. This package provides a set of React components that make it easy to create resizable panels. To import the package, we can use the import statement at the top of our React component file. For example, we could write:
1import { Panel, PanelGroup, PanelResizeHandle } from "react-resizable-panels";
This line of code imports the Panel, PanelGroup, and PanelResizeHandle components from the react-resizable-panels package, which we can then use in our components.
Now that we have imported the necessary components, we can use them to create our first resizable panel. To do this, we can include the PanelGroup and Panel components in the render method of our component. We can specify the initial size of the panel using the defaultSize prop.
1function MyComponent() { 2 return ( 3 <PanelGroup autoSaveId="example" direction="horizontal"> 4 <Panel defaultSize={25}> 5 This is a resizable panel. 6 </Panel> 7 <PanelResizeHandle /> 8 <Panel> 9 This is another resizable panel. 10 </Panel> 11 </PanelGroup> 12 ); 13}
In this code, we create a horizontal group of resizable panels. The first panel has an initial size of 25% of the group, and the second panel takes up the rest of the space. The PanelResizeHandle component is used to allow the user to resize the panels.
A resizable component in the react-resizable-panels package is a type of React component that can be resized by the user. This is achieved by wrapping the content of the component within a Panel component. The Panel component is a div with a classname of 'resizable' and it is this div that gets resized when the user interacts with the resize handle.
The behavior of the resizable component is controlled by a set of props. These props allow you to specify the initial size of the component, the minimum and maximum size, and the behavior of the resize handle.
In React, there are two main types of components: functional components and class components, also referred to as simple React components. A functional component is a JavaScript function that returns a React element. It does not have its own state or lifecycle methods, and it is typically used for components that simply render HTML and props.
On the other hand, a simple React component is a JavaScript class that extends React.Component. It has its own state and lifecycle methods, and it is typically used for components that need to manage their state or interact with the React lifecycle.
When creating resizable components, you can use either type of component, depending on your needs. However, functional components are often simpler and easier to understand, especially for beginners.
Props are a way of passing data from parent components to child components in React. They are used to control the behavior and appearance of components.
In the case of resizable components, props are used to specify the initial size of the component, the minimum and maximum size, and the behavior of the resize handle. For example, you can use the defaultSize prop to set the initial size of the component, the minSize and maxSize props to set the minimum and maximum size, and the onResize prop to specify a function to be called when the component is resized.
The minimum and maximum sizes of the panels are controlled by the minSize and maxSize props of the Panel component. These props accept a numeric value between 1 and 100, representing the percentage of the total size of the panel group. For instance, a minSize of 10 means the panel can't be resized to less than 10% of the total size of the panel group. Similarly, a maxSize of 50 means the panel can't be resized to more than half of the total size of the panel group.
1<Panel minSize={10} maxSize={50}> 2 This is a resizable panel. 3</Panel>
You can style the panels and the panel group using the style prop. This prop accepts a JavaScript object where the keys are the CSS properties and the values are the CSS values.
1<Panel style={{backgroundColor: 'lightgray', border: '1px solid black'}}> 2 This is a resizable panel. 3</Panel>
In this example, the panel will have a light gray background and a black border.
The react-resizable-panels package provides default behaviors and styles for the panels and the panel group. However, you can override these defaults using the props provided by the package. For instance, you can make a panel collapsible by setting the collapsible prop to true. When a panel is collapsible, it can be resized to its collapsedSize, which defaults to 0 but can be set to any value between 0 and the minSize.
1<Panel collapsible={true} collapsedSize={0}> 2 This is a collapsible panel. 3</Panel>
In this example, the panel can be collapsed to a size of 0.
In the context of resizable panels, a resize event is triggered when the user interacts with the resize handle, which is the part of the panel that the user can drag to resize the panel. The react-resizable-panels package provides the PanelResizeHandle component for this purpose.
Event listeners are functions that are called when a particular event occurs. In this case, the onDragging prop of the PanelResizeHandle component is an event listener that is called when the user starts or stops dragging the handle.
1<PanelResizeHandle onDragging={(isDragging) => console.log(`Dragging: ${isDragging}`)} />
In this example, the onDragging prop is set to a function that logs whether the handle is currently being dragged.
The PanelResizeHandle component provided by the react-resizable-panels package is used as the resize handle. This component can be placed between two Panel components in a PanelGroup to allow the user to resize the panels.
1<PanelGroup> 2 <Panel> 3 This is a resizable panel. 4 </Panel> 5 <PanelResizeHandle /> 6 <Panel> 7 This is another resizable panel. 8 </Panel> 9</PanelGroup>
In this example, the PanelResizeHandle component is placed between two panels, allowing the user to resize both panels.
The react-resizable-panels package allows you to create custom event handlers by setting the onDragging prop of the PanelResizeHandle component to a custom function. This function is called when the user starts or stops dragging the handle, and it is passed a boolean indicating whether the handle is currently being dragged.
1<PanelResizeHandle onDragging={(isDragging) => console.log(`Dragging: ${isDragging}`)} />
In this example, the onDragging prop is set to a function that logs whether the handle is currently being dragged.
The react-resizable-panels package allows you to create panel groups that can be resized either horizontally or vertically. This is controlled by the direction prop of the PanelGroup component, which can be set to either "horizontal" or "vertical".
1<PanelGroup direction="horizontal"> 2 <Panel> 3 This is a horizontally resizable panel. 4 </Panel> 5 <PanelResizeHandle /> 6 <Panel> 7 This is another horizontally resizable panel. 8 </Panel> 9</PanelGroup>
In this example, the panels can be resized horizontally.
React hooks allow you to use state and other React features in functional components. In the context of resizable panels, hooks can be used to manage the state of the panels, such as their sizes and whether they are collapsed.
For instance, you can use the useState hook to store the size of a panel, and the useEffect hook to update the size of the panel when it is resized.
1function MyComponent() { 2 const [size, setSize] = useState(50); 3 4 useEffect(() => { 5 console.log(`Panel resized to ${size}`); 6 }, [size]); 7 8 return ( 9 <Panel onResize={setSize}> 10 This is a resizable panel. 11 </Panel> 12 ); 13}
In this example, the useState hook is used to store the size of the panel, and the useEffect hook is used to log the new size of the panel when it is resized.
Building a complex UI with multiple resizable panels involves combining all the concepts we've discussed so far. You can create a PanelGroup with multiple Panel components, use the PanelResizeHandle component to allow the user to resize the panels, and use props to customize the behavior and appearance of the panels.
You can also use hooks to manage the state of the panels, and create custom event handlers to respond to resize events. By combining these features, you can create a complex, interactive, and user-friendly UI.
1function MyComponent() { 2 const [sizes, setSizes] = useState([25, 50, 25]); 3 4 return ( 5 <PanelGroup onLayout={setSizes} direction="horizontal"> 6 <Panel defaultSize={sizes[0]}> 7 This is a resizable panel. 8 </Panel> 9 <PanelResizeHandle /> 10 <Panel defaultSize={sizes[1]}> 11 This is another resizable panel. 12 </Panel> 13 <PanelResizeHandle /> 14 <Panel defaultSize={sizes[2]}> 15 This is a third resizable panel. 16 </Panel> 17 </PanelGroup> 18 ); 19}
In this example, we create a horizontal group of three resizable panels. The sizes of the panels are stored in the state using the useState hook, and they are updated when the panels are resized using the onLayout prop of the PanelGroup component. Each panel has a default size that is set to its corresponding size in the state.
In this blog post, we have explored the power and flexibility of React Resizable Panels. By understanding and using these concepts, you can create highly interactive and user-friendly interfaces in your React applications. Whether you're building a simple app or a complex project, React Resizable Panels provides a powerful tool for enhancing your UI.
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.