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

What is the best way to center align and add an icon to the header in Ionic?

How can I center align an "ion-ios-arrow-up" icon in the header of my Ionic app, similar to the concept shown below? This is my HTML template: <ion-view cache-view="false" view-title="Historical HPP"> <ion-nav-bar class="nav-title-slide-ios7 ...

What is the best way to make an attribute in an interface mandatory only when another attribute is set to true

How can a relative be made required only when another attribute is true? For Example: interface ITesteProps { required: boolean content{!required && '?'}: string } I understand that this code is not valid. Is it possible to make the ...

Tips for customizing the MuiModal-root component with Material-UI in React:

I am currently working with the most recent version of Material-UI library for React and I am attempting to incorporate additional CSS into the MUI-Modal CSS class. (view screenshot) https://i.stack.imgur.com/ktVuf.png The modal I am using can be found a ...

Position a button and input element in alignment

I recently decided to challenge myself by recreating the Netflix registration page as a practice project. Using the flexbox model on the webpage, I encountered an issue with aligning the email input and the "Get Started" button. Despite trying different me ...

Encountering issues with link button redirection

I have a question regarding a Link element and CustomButton element with an onClick handler. < Link to = "/dashboard" style = { linkStyles } > < CustomButton text = "Login" onClick = { handleSubmit } /> < /Link> The code ...

Determine in React whether a JSX Element is a descendant of a specific class

I am currently working with TypeScript and need to determine if a JSX.Element instance is a subclass of another React component. For instance, if I have a Vehicle component and a Car component that extends it, then when given a JSX.Element generated from ...

Get the value of a CSS property in Python by parsing the rendered HTML

Currently, I am using Selenium and PhantomJS in conjunction with Python for the purpose of crawling rendered web pages. While it is a straightforward process to identify the presence of specific words within the HTML content (e.g. if "example" in html...), ...

The alignment feature in the center is malfunctioning

I'm currently utilizing jQuery Mobile and have the following HTML along with CSS: .ui-grid-a { padding: 0; margin: 0; margin-top: 15px; height: 380px; } .ui-block-a, .ui-block-b { padding: 0; margin: 0; height: 33.3%; } .ui-block-a a, .ui-block ...

Can a File Object be transmitted to an AWS Lambda function via API Gateway using JavaScript?

Can a JavaScript File Object be passed from a browser to AWS Lambda via API Gateway (ultimately to S3)? I am working with TypeScript and React. Environment Frontend TypeScript React AWS Amplify (for API module) Backend(AWS Lambda) Node.js Expecta ...

Automatically selecting checkboxes from an array using ReactJS

Hello there, I am a beginner working with react and I could really use some help with the issue below. Can you assist me? I am trying to figure out how to populate or check/uncheck checkboxes based on an Array's result. Specifically, I want to have ...

Incorporate concealed image into HTML

When I perform actions with a couple of images, I encounter delays because the browser needs to load the images. For example, if I use $('body').append(''); everything works smoothly without any delays. However, when I try using style= ...

Struggling to align my image in the center while applying a hover effect using CSS

Hey there, I'm having an issue centering my image when I add instructions for it to tilt on mouseover. If I take out the 'tilt pic' div, the image centers just fine. Can anyone help me identify what I might be doing wrong? Thanks in advance! ...

hardhat activities do not store information on the local network

I recently developed a small NFT marketplace using solidity and hardhat. I have all the NFT details stored in a JSON file, and I even created a hardhat task to automate the process. task("populate-market", "populate market with nfts").s ...

You cannot call this expression. The type 'String' does not have any call signatures. Error ts(2349)

Here is the User class I am working with: class User { private _email: string; public get email(): string { return this._email; } public set email(value: string) { this._email = value; } ...

Encountering an error when running "npm start" command in webpack-dev-server

After spending hours on this, I am desperate for help. Everything was working perfectly until I updated the app store this morning and had to restart. Now my npm server (as part of learning React.js) is failing to work. Here is the error log: 0 info it wo ...

jQuery selector unable to locate the specified class in the table row

After receiving an array of strings as a response from an SQL query, I am using JavaScript to dynamically add this data as rows to an HTML table: loadUsers: function() { . . . function displayUsersOnTable(response) { for(var i = ...

Chrome browser experiencing a disappearing vertical scroll bar issue on a Bootstrap Tab

<div class="tabs-wrap left relative nomargin" id="tabs"> <ul class="nav ultab" id="fram"> <li class="active"><a href="#history" data-toggle="tab" id="history1" >History< ...

What is the best way to access form data in an AWS Lambda function coded in NodeJs?

In the process of constructing an application, I am utilizing ReactJS for the front-end and AWS API Gateway/AWS Lambda in NodeJS for the back-end. The form data from my React app is being passed within the userAttributes attribute as illustrated below: ht ...

Switching a component in Mui App transforms the entire aesthetic

I'm currently working on a project using Mui and the Material Kit theme. While I initially tried to customize the default components provided by Material Kit using custom CSS, I found that I was unable to override the styles as expected. Consequently, ...

evt.target consistently returns the initial input within the list of inputs

My React file uploader allows users to attach multiple file attachments. Each time a user clicks on an input, I retrieve the data-index to identify the input position. renderFileUploader() { let file_attachment = this.state.file_attachment.map(fun ...