From 774941557277a8eac6bd0aa10abbb140080df2ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Malio?= Date: Fri, 10 May 2024 19:04:03 +0200 Subject: [PATCH] Adding the scrollToPosition The objective is to set an alternative attribute to go to the position of the terminal. I added a new parameter to receive optionally and thus be able to set if we want it to go to where the terminal cursor is. --- src/index.tsx | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/index.tsx b/src/index.tsx index a615135..df40254 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -19,9 +19,10 @@ export interface Props { redBtnCallback?: () => void; yellowBtnCallback?: () => void; greenBtnCallback?: () => void; + scrollToPosition?: boolean; } -const Terminal = ({name, prompt, height = "600px", colorMode, onInput, children, startingInputValue = "", redBtnCallback, yellowBtnCallback, greenBtnCallback}: Props) => { +const Terminal = ({name, prompt, height = "600px", colorMode, onInput, children, startingInputValue = "", redBtnCallback, yellowBtnCallback, greenBtnCallback, scrollToPosition = true}: Props) => { const [currentLineInput, setCurrentLineInput] = useState(''); const [cursorPos, setCursorPos] = useState(0); @@ -61,7 +62,10 @@ const Terminal = ({name, prompt, height = "600px", colorMode, onInput, children, onInput(currentLineInput); setCursorPos(0); setCurrentLineInput(''); - setTimeout(() => scrollIntoViewRef?.current?.scrollIntoView({ behavior: "auto", block: "nearest" }), 500); + if (scrollToPosition) { + setTimeout(() => scrollIntoViewRef?.current?.scrollIntoView({ behavior: "auto", block: "nearest" }), 500); + } + } else if (["ArrowLeft", "ArrowRight", "ArrowDown", "ArrowUp", "Delete"].includes(event.key)) { const inputElement = event.currentTarget; let charsToRightOfCursor = "";