Hide top divider while scrolling to the top, hide bottom divider while scrolling to the end.

首发于[Dialog] Add Divider to Dialog · Issue #3155 · mui-org/material-ui · GitHub,故用英文行文。

Example

Here is an example on CodeSandbox.

dialog-example

Details

Create two variables to control dividers.

1
2
const [topDivider, setTopDivider] = useState(false);
const [bottomDivider, setBottomDivider] = useState(false);

Create a function to initilize status of dividers. Because Dialog have its own “lifecycle”. (You will see the explanation below)

1
2
3
4
5
6
const initializeDividers = () => {
setTopDivider(false);
setBottomDivider(false);

/* ... */
};

Detect if scrollbar exist by comparing scrollHeight with clientHeight.[1]

The id in getElementById is for DialogContent.

1
2
3
4
5
6
7
8
9
10
11
const initializeDividers = () => {
/* ... */

const dialogContent = document.getElementById('dialog-content');

if (dialogContent !== null) {
if (dialogContent.scrollHeight > dialogContent.clientHeight) {
setBottomDivider(true);
}
}
};

Adding initializeDividers to Dialog in onEnter. Because display of Dialog is control by open. (Now you know why we don’t use Effect Hook) You will get null if you are using React Effect Hook to get element.

1
2
3
4
5
6
7
<Dialog
open={/* variable from caller */}
onEnter={initializeDividers}
scroll="paper"
>

{/* ... */}

Add Dividers before & after DialogContent with its variable from State Hook. And don’t forget to set a id for DialogContent.

1
2
3
4
5
{topDivider ? <Divider /> : null}
<DialogContent id="dialog-content">
{/* ... */}
</DialogContent>
{bottomDivider ? <Divider /> : null}

Now let’s handle the scrolling.

You need to listen the onScroll event in DialogContent.

1
2
3
4
5
6
7
8
9
const handleScrolling = (e: React.UIEvent<HTMLDivElement>) => {
/* ... */
};

/* ... */

<DialogContent id="dialog-content" onScroll={handleScrolling}>
{/* ... */}
</DialogContent>

Assert the type of event.target to HTMLDivElement.[2] [3]

1
2
3
4
5
const handleScrolling = (e: React.UIEvent<HTMLDivElement>) => {
const target = e.target as HTMLDivElement;

/* ... */
};

Compare the scrollTop with the difference between scrollHeight and scrollHeight.[4] [5]

1
2
3
4
5
6
const handleScrolling = (e: React.UIEvent<HTMLDivElement>) => {
/* ... */

setTopDivider(target.scrollTop !== 0);
setBottomDivider(target.scrollTop !== target.scrollHeight - target.offsetHeight);
};

Credits & Reference


  1. Check whether HTML element has scrollbars using JavaScript - GeeksforGeeks ↩︎

  2. angular - Property ‘value’ does not exist on type ‘EventTarget’ - Stack Overflow ↩︎

  3. TypeScript: Documentation - Everyday Types ↩︎

  4. reactjs - is this possible to get scroll position in material-ui select list on Scroll? - Stack Overflow ↩︎

  5. browser - How to get scrollbar position with Javascript? - Stack Overflow ↩︎