Establish starting dimensions and remember the previous sizes of allocations

I successfully implemented a draggable split panel using a code library from johnwalley on GitHub called https://github.com/johnwalley/allotment.

My goal is to achieve the following functionalities:

  1. Clicking on
    Expand or collapse the allotment B
    should expand or collapse the allotment B accordingly.
  2. We should be able to drag the splitter and save its position before collapsing the allotment B, ensuring that we can return to that position when expanding the allotment B.
  3. When initially expanding the allotment B, I want its height to be set at 1/5 of the total height.

I have written the relevant code here: (https://codesandbox.io/s/reset-forked-f2f386?file=/src/App.js). However, I am facing difficulty in achieving points 2) and 3) simultaneously.

If anyone has any insights as to why this could be happening, please let me know!

import React from "react";
import { Allotment } from "allotment";
import "allotment/dist/style.css";
import "./style.css";

// App component logic goes here

Answer №1

My initial thought is to have no default saved value and set the lastExpandedSize to null. In this scenario, one-fifth of the screen height will be passed as the saved height when needed. Once there is a saved value upon closing, the useState function will continue to return that value.

// ...

constructor(props) {
  super(props);
  this.state = {
    expand: true,
    lastExpandedSize: null // initialize to null
  };
  this.containerRef = React.createRef();
  this.myRef = React.createRef();
}

/**
 * Check if LastExpandedSize exists
 * @return {boolean}
 */
checkLastExpandedSize = () => {
  return this.state.lastExpandedSize !== null;
}

/**
 * Get Default Expanded Size
 * @param {number} ratio
 * @param {number} height
 * @return {number[]}
 */
getDefaultExpandSize = (ratio = 5, height = 0) => {
  if (height < 1) {
    height = this.containerRef.current.clientHeight;
  }

  return [
    height * (ratio - 1) / ratio, 
    height / ratio
  ];
}

handleAdjustmentChange = (sizes) => {
  if (sizes.length > 1) {
    if (sizes[1] < 31) {
      this.setState({ expand: true });
    }
    else {
      this.setState({
        expand: false,
        lastExpandedSize: this.checkLastExpandedSize()
          ? sizes
          : this.getDefaultExpandSize()
      });
    }
  }
};

// ...
<div
  ref={this.containerRef}
  style={{
    minWidth: 200,
    height: "100vh",
    overflowX: "auto",
    width: "auto",
    margin: "0px 0px 0px 0px"
  }}
>
  ...
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Storing images as base64 in a mongoDB database can help save space

I am seeking the most efficient method for uploading an image from a mobile phone to my server. Currently, I am utilizing HTML5 to access the camera and capture the image, then converting the file into a base64 string before sending it to the server and st ...

What is the best way to display search results on a separate page using react.js?

As a new MERN stack developer, I am interested in creating a feature where search results for a user-inputted keyword are displayed on a designated page. Can you provide me with some guidance or show an example of code to help me implement this functiona ...

Horizontal rule located within a table but spanning the entire width

I wrote the following code: <table> {item.awards.map((obj,i) => <tbody> <tr> <td>Name</td> <td>:</td> <td>{obj.title}</td> </tr> ...

The CSS transition will only activate when coupled with a `setTimeout` function

After using JavaScript to adjust the opacity of an element from 0 to 1, I expected it to instantly disappear and then slowly fade back in. However, nothing seems to happen as anticipated. Interestingly, if I insert a setTimeout function before applying th ...

Bottom section goes haywire in Chrome when clearfix is applied to a div above

When I remove the clearfix div above the footer, the text aligns correctly on Firefox. However, this causes issues with other elements on the page. It's puzzling how the clearfix is impacting the footer in this way... Here is a link to my page: ...

Using scrollto function after history.push in React

Currently, I am utilizing history.push for routing purposes (using react-router-dom 5.3.4). The majority of the links on my menu trigger scrollTo actions. However, one link directs to /about. When attempting to use a scrollTo link from the /about page, I ...

Display the element only when the input is in a selected state using CSS

Looking for a way to display an element when an input field is selected without using JavaScript? Preferably, the solution should not involve placing the element within the input field. Unfortunately, targeting the element in CSS with the :active selector ...

Using MobX to alter observed observable values outside of actions is not permitted in combination with Ant Design components

When trying to upload files to the server and receive a response, I encountered an issue. If I override the onChange property of the Upload component (from antd), mobx starts throwing errors and the file uploading process gets stuck in the 'uploading& ...

Unable to input any text into the textfield

After implementing redux-form-material-ui for my form, I am facing an issue where I cannot type anything in the textfield. Despite having two textfields and an Autocomplete option, only the Autocomplete box seems to be functioning properly while the textfi ...

Guide to center-aligning ElementUI transfer components

Has anyone successfully incorporated ElementUI's transfer feature inside a card element and centered it? https://jsfiddle.net/ca1wmjLx/ Any suggestions on how to achieve this? HTML <script src="//unpkg.com/vue/dist/vue.js"></ ...

Show a file.jpg from multer and a database of uploads

After successfully configuring Multer in Back with NodeJS, I was able to save an image in my database at 'uploads/img.jpg' and display it in a folder named "uploads" in the backend. Now, I am facing challenges while trying to display images using ...

How to prevent the scroll bar from affecting a fixed div located at the top of the page

Check out this website: I am struggling with setting up a fixed menu div at the top and a content div below it on my site. I want the scroll bar to only apply to the content div and not the header div at the top, but I can't seem to figure out how to ...

Leverage array mapping functionality to generate React components within a separate component

Currently, I am retrieving data from an API and storing the results in an array. The issue arises when trying to map the array to a child component since it is initially empty. How can I ensure that the array mapping only executes when there is data in the ...

What are the recommended margins for various alphabets?

When displaying text, some alphabets take up more space than others. So how can I adjust the white space between elements '#re1' and '#re2' automatically in the scenario described below? In this code snippet, what is the best way to ad ...

What could cause the cookie value in the dynamic route to differ?

After implementing a program with next js, I encountered an issue related to using cookies on dynamic pages. The problem is that the cookie value seems to be shared across different pages, when in fact each page should have its own unique cookie. My goal ...

Ways to style specific dates on a datepicker?

Is there a way to customize the appearance of the first and last selected days in my ui datepicker for booking, similar to the design shown in the image linked below? var dateFormat = "DD/MM/YY", from = $("#checkin,. ...

Organize Dates in React Table

I need help with sorting the Date column in my code. Currently, the sorting is being done alphabetically. Here is the JSON data and code snippet: JSON [ { "date": "Jun-2022" }, { "date": "Jul-2022" } ...

Typescript enums causing Safari to display blank screen in Next.js

The website performs well on Chrome and Edge, but encounters difficulties on Safari for iOS. Although all the elements, styling, and scripts load properly, nothing appears on the screen. After spending countless hours debugging, I discovered that the pro ...

Ways to preserve codes on VS Code Mac?

Struggling with saving my ReactJS code in VS Code on a Mac. Whenever I try to save using command + s, the JSX formatting ends up in a mess. Check out the image here for reference: enter image description here Any solutions or tips on how to resolve this i ...

The term "Cardlist" has not been defined and is therefore causing an

I created a CardList and attempted to add cards into the list using map, but encountered an error import React from 'react'; import Card from './Card'; const CardsContainer = ({robots}) => { const cardComponents = robots.map((r ...