The preferred size variable for the allocation allotment is malfunctioning

I am working on creating a draggable split panel for an editor, similar to the behavior of the Console panel in CodeSandbox:

  1. Clicking on Console expands the panel and changes the arrow to ArrowDown to close it.
  2. The border of the panel is draggable.
  3. If you click on Console when the panel is expanded, it will collapse and change the arrow to ArrowUp to expand it again.

I have found some code (https://codesandbox.io/s/reset-forked-ydhy97?file=/src/App.js:0-927) by https://github.com/johnwalley/allotment. However, I am facing an issue where the prop preferredSize does not update according to this.state.toExpand.

Can anyone help me understand why this is happening?

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

export default class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      toExpand: true
    };
  }

  render() {
    return (
      <div>
        <div className={styles.container}>
          <Allotment vertical>
            <Allotment.Pane>Main Area</Allotment.Pane>
            <Allotment.Pane preferredSize={this.state.toExpand ? "0%" : "50%"}>
              <div
                onClick={() => {
                  this.setState({ toExpand: !this.state.toExpand });
                }}
              >
                Console &nbsp;
                {this.state.toExpand ? "ArrowUp" : "ArrowDown"}
              </div>
            </Allotment.Pane>
          </Allotment>
        </div>
      </div>
    );
  }
}

Answer №1

The issue lies in the fact that the prop known as preferredSize fails to update according to this.state.toExpand.

This is not where the problem originates; the prop does indeed change. However, as per the documentation, it mentions:

Allotment will attempt to use this size when adding this pane (including on initial mount) as well as when a user double clicks a sash, or the reset method is called on the Allotment instance.

It is not set up to automatically reflect changes in the prop value. Nonetheless, if you double click on the border while having ArrowDown selected, it will reset to 50%.

To address this, you can establish a reference to the Allotment element by creating a reference in the constructor initially:

constructor(props) {
  super(props);

  this.allotment = React.createRef();

  this.state = {
    toExpand: true
  };
}

Then assign it as a prop:

<Allotment vertical ref={this.allotment}>

Subsequently, add a callback to the setState function so that upon changing the expand option, it triggers the reset function:

resetAllotment() {
  if (this.allotment.current) {
    this.allotment.current.reset();
  }
}
// ...
this.setState({ toExpand: !this.state.toExpand }, () => this.resetAllotment());

Additional note: It seems that the Allotment component may not have enough time to process the new prop alteration before the reset is called within the setState callback... which might seem counterintuitive. In such cases, you can implement a temporary setTimeout of 0ms:

resetAllotment() {
  setTimeout(() => this.allotment.current && this.allotment.current.reset(), 0);
}

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 set up a webpage so that the left side remains fixed while allowing the right side to scroll?

Currently, I am developing a blog and aiming to position the blog posts on the left side while having the admin panel on the right. Essentially, I want both user and developer interfaces to be fully functional on one page. The developer side should be scro ...

Are there any better methods for creating div backgrounds and borders using CSS?

Within my application, there exists a multitude of div elements that share identical backgrounds and borders but vary in size. Having to utilize the same background image for each individual div proves to be highly inefficient, particularly in terms of ba ...

Guidelines for positioning social media icons to the right of the collapsible navigation bar in Bootstrap 3

I am having trouble aligning my social media icon with the navbar menu to the right. There seems to be a gap when I try to do it. The social icon is currently in a separate row, while the navbar is also in a separate row. I just want to align them both to ...

JavaScript obtain scroll position of a child element

I have a setup that looks something like the following: <div> <div id="scrollID" style="height:100px;"> content here </div> </div> <script> document.getElementById("myDIV").addEventListener("touchstart", m ...

A step-by-step guide on customizing the content of a DrawerNavigator in

In my app, I have set up both DrawerNavigator and StackNavigator. There are two types of users - members and limited users. My goal is to modify the navigation based on the type of user logged in. Specifically, I want to remove MyProfile from the DrawerNa ...

Unable to Trigger onClick Event with react-bootstrap Navbar Dropdown in Combination with react-router-dom

In my React application with react-bootstrap, I have a Navbar component that displays a dropdown menu for different brands. However, I noticed that the second brand option (Tommy Hilfiger) is always the one active by default, as if it is automatically cl ...

How can you implement a transform transition toggle when a user clicks on an element

Check out this CodePen example for a cool overlay animation: https://codepen.io/pen/MoWLrZ Experience the unique animation of two overlays as you click for the first time. However, on the second click, watch as both overlays seamlessly return to their ori ...

Creating a semi-circular shaped rectangle using CSS

Is it possible to achieve the unique rectangle borders shown in the image using border-radius or any other CSS property? Here's what I'm trying to create: https://i.sstatic.net/YKPEW.png Currently, this is what I've been able to achieve us ...

Header escapes div and continues to move

I am currently working on building a website. One of the key features I want to include is a fixed top bar that remains visible at all times. In this bar, I have added buttons, an image, and a headline. However, I am facing an issue where the headline keep ...

Preventing the dispatch function from being overwritten by an empty state

Scenario: I am currently exploring the concept of React/Redux and aiming to understand the data flow. The process involves axios dispatching actions -> reducers setting state to props -> Component rendering. As a newcomer to the Frontend world, ...

Learn how you can store checkbox values in an array when a checkbox is clicked by utilizing AJAX

Creating checkboxes in HTML <input type="checkbox" name="options[cid]" value='1' onChange="chkdeptCount(this.value)" class="test"> <input type="checkbox" name="options[cid]" value='2' onChange="chkdeptCount(this. ...

What is the best way to set up a React handler that can handle multiple values effectively?

Struggling with a handler that is not behaving as expected. I need to update the 'quantity' value of multiple items, but currently they all get updated with the last value entered. How can I change multiple values and update items individually? H ...

Prevent row stretching in React.js Material-ui DataGrid

Does anyone know if it is feasible to adjust the width of a row? I have rows containing JSON data that would be best displayed within a <pre> tag for easy viewing by users. However, I am facing challenges with this and wondering if there is a way to ...

What is the functionality of the icon `<i>` in Twitter Bootstrap?

After adding an icon to a webpage, I noticed that it replaced the old, semi-deprecated <i> tag and instead used a customized image tag. It almost seems like <i ...> is just a simpler version of <img ...>. I'm curious about how this ...

Uninterrupted Carousel Experience in Bootstrap 4 (Seamless slide transition)

I'm having trouble figuring out how to make a carousel run continuously without pauses between slides. Here's an example of what I'm working with on codeply: .carousel-img-small { width: 80px; height: 80px; margin-right: 2%; } .car ...

Tips for altering the scrolling rate of a container

I am trying to adjust the scroll speed of specific divs among a group of 5 divs. I came across a solution that changes the scroll speed for the entire document: http://jsfiddle.net/36dp03ur/ However, what I really need is a scenario like this: <div i ...

Is there a method to successfully utilize "const {user, userId} = this.state" when user pertains to username and userId pertains to id?

for instance state = { "username": "abc", "id": "1", } const {username, id} = this.state the code above will retrieve the values successfully is there a method to enable the following code to also ...

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! ...

The opacity of each div within a container increases as you move from one div to the

I am striving to craft a series of divs that feature varying levels of opacity in their background colors, creating a gradient effect. In an attempt to achieve this, I utilized a variable linked to the ID of each individual div. Unfortunately, my efforts ...

Implementing defaultProps in conjunction with withStyles

Currently, I am in the process of developing a component using material-ui withStylers and defaultProps. However, I have encountered an issue where the props of the component are not being retrieved in the styles objects unless they are explicitly passed t ...