Dynamically adjust the height of scrollable content (Ion Page, React)
P粉478835592
P粉478835592 2023-09-07 18:08:44
0
1
637

Suppose you have some text in your ionic React page that is very large, so you want it to scroll rather than fill the entire page, then the following styles will help a lot:

<div  style={{ overflow: 'auto', flex: '1 1 auto', height: '200px'}}>
Some Text
</>

But let's say, I have a page with some content and a footer, for example like this:

<IonPage>
      <IonContent>
        <h2>Search Detail Page</h2>
        <IonItem>
          Some Search Result Details
        </IonItem>

        <h1> Description</h1>
        <div  style={{ overflow: 'auto', flex: '1 1 auto'}}>
          <p>This is the content that matters to me. I want this to scroll when this description gets so long, that the footer would start covering it</p>
          <p>Now let's put a whole lot of text in this paragraph so we can demonstrate what happens if we have a long text. Repeat after me: Now let's put a lot of text in this paragraph so we can demonstrate what happens if we have a long text. Repeat after me: Now let's put a lot of text in this paragraph so we can demonstrate what happens if we have a long text. Repeat after me: Now let's put a lot of text in this paragraph so we can demonstrate what happens if we have a long text. Repeat after me: Now let's put a lot of text in this paragraph so we can demonstrate what happens if we have a long text. Repeat after me: Now let's put a lot of text in this paragraph so we can demonstrate what happens if we have a long text. Repeat after me: Now let's put a lot of text in this paragraph so we can demonstrate what happens if we have a long text. Repeat after me: Now let's put a lot of text in this paragraph so we can demonstrate what happens if we have a long text. Repeat after me: Now let's put a lot of text in this paragraph so we can demonstrate what happens if we have a long text. Repeat after me: Now let's put a lot of text in this paragraph so we can demonstrate what happens if we have a long text. Repeat after me: </p>
        </div>
      </IonContent>

      <IonFooter>
        <IonToolbar>
          Some shiny footer content
        </IonToolbar>
      </IonFooter>
    </IonPage>

On mobile, it looks like this:

Please note that the description content is not scrollable because the height of dev is not limited. The problem is, we need to define a "max-height" but we can't do that because the page size will vary between different devices and it depends on the footer height.

Any suggestions? This seems to be a common question but I can't find the answer.

I tried this:

  useEffect(() => {
    setTimeout(() => {
      const h2Height = document.querySelector('h2').clientHeight;
      const itemHeight = document.querySelector('ion-item').clientHeight;

      console.log('Header Height:', h2Height); // sadly, this is 0 at init time. But then works on reload
      console.log('Item Height:', itemHeight);


      if (descriptionRef.current) {
        descriptionRef.current.style.height = `calc(100% - ${h2Height + itemHeight}px - 2em)`;
      }
    }, 100); // we need this 100ms timeout, since otherwise the heights are not available and equals 0 at load time
  }, []);

//...
<h1> Description</h1>
<div ref={descriptionRef}>Some very long detailed description</div>

This only works when I reload the page because on the first load both h2Heigth and itemHeight show up as 0

P粉478835592
P粉478835592

reply all(1)
P粉343408929

I ended up adding this css class:

.scroll-content {
  max-height: calc(100% - 60px);
  overflow: auto;
  flex: 1 1 auto;

  p {
    margin-bottom: 3em;
  }
}

Then in my page:

<div className={'scroll-content'}>
  <p>{searchResult?.description}</p>
</div>

Description: calc(100% - 60px) is the height of the entire content minus all content except the scrolling content. (Obviously, footers don’t count)

This works on my target device. However, this is a rather unsatisfactory solution as it breaks as soon as I add content to the page unless I happen to remember to update the diff value

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template