Skip to content

Commit 00a866a

Browse files
committed
feat: Refactored scroll area and improved it to reset the scroll position if it becomes fixed
1 parent ce1dae5 commit 00a866a

1 file changed

Lines changed: 20 additions & 2 deletions

File tree

src/ScrollArea.tsx

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import { Box, measureElement, useFocusManager, useInput } from 'ink';
2-
import React, { ReactNode } from 'react';
2+
import React, { ReactNode, useEffect, useState } from 'react';
33

44
const reducer = (state, action) => {
55
switch (action.type) {
6+
case 'RESET':
7+
return {
8+
...state,
9+
scrollTop: 0,
10+
}
611
case 'SET_INNER_HEIGHT':
712
return {
813
...state,
@@ -35,10 +40,23 @@ export function ScrollArea({height, isStart, children}) {
3540
scrollTop: 0,
3641
});
3742
const focusManager = useFocusManager();
43+
const [canScroll, setCanScroll] = useState(true);
44+
3845
focusManager.enableFocus();
3946

4047
const innerRef = React.useRef();
4148

49+
useEffect(() => {
50+
if (isStart || (children.length + 3) * 3 < (height - 6)) {
51+
setCanScroll(false);
52+
dispatch({
53+
type: 'RESET'
54+
})
55+
} else {
56+
setCanScroll(true);
57+
}
58+
}, [height, isStart, children.length]);
59+
4260
React.useEffect(() => {
4361
const dimensions = measureElement(innerRef.current);
4462

@@ -49,7 +67,7 @@ export function ScrollArea({height, isStart, children}) {
4967
}, []);
5068

5169
useInput((_input, key) => {
52-
if (isStart || (children.length + 2) * 4 < height) {
70+
if (!canScroll) {
5371
return;
5472
}
5573

0 commit comments

Comments
 (0)