-
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented autoScroll on generation for React
- Loading branch information
Showing
14 changed files
with
222 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
packages/react/core/src/exports/hooks/useAutoScrollHandler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import {MutableRefObject, useEffect, useRef, useState} from 'react'; | ||
import {createAutoScrollHandler} from '../../../../../shared/src/interactions/autoScroll/autoScrollHandler'; | ||
import {AutoScrollHandler} from '../../../../../shared/src/interactions/autoScroll/type'; | ||
|
||
const defaultAutoScrollOption = true; | ||
|
||
export const useAutoScrollHandler = ( | ||
conversationContainerRef: MutableRefObject<HTMLDivElement | null>, | ||
autoScroll?: boolean, | ||
) => { | ||
const [autoScrollHandler, setAutoScrollHandler] = useState<AutoScrollHandler>(); | ||
const [conversationContainer, setConversationContainer] = useState<HTMLDivElement>(); | ||
|
||
const autoScrollHandlerRef = useRef(autoScrollHandler); | ||
const autoScrollPropRef = useRef(autoScroll); | ||
|
||
useEffect(() => { | ||
const currentConversationContainer = conversationContainerRef.current || undefined; | ||
if (currentConversationContainer !== conversationContainer) { | ||
setConversationContainer(currentConversationContainer); | ||
} | ||
}); // No dependencies - If statement inside the effect will handle the update | ||
|
||
useEffect(() => { | ||
if (!conversationContainer) { | ||
if (autoScrollHandlerRef.current) { | ||
autoScrollHandlerRef.current.destroy(); | ||
setAutoScrollHandler(undefined); | ||
autoScrollHandlerRef.current = undefined; | ||
} | ||
|
||
return; | ||
} | ||
|
||
if (autoScrollHandlerRef.current) { | ||
autoScrollHandlerRef.current.updateConversationContainer(conversationContainer); | ||
} else { | ||
autoScrollHandlerRef.current = createAutoScrollHandler( | ||
conversationContainer, | ||
autoScrollPropRef.current ?? defaultAutoScrollOption, | ||
); | ||
setAutoScrollHandler(autoScrollHandlerRef.current); | ||
} | ||
}, [conversationContainer]); | ||
|
||
useEffect(() => { | ||
autoScrollPropRef.current = autoScroll; | ||
if (autoScrollHandlerRef.current) { | ||
autoScrollHandlerRef.current.updateProps({ | ||
autoScroll: autoScroll ?? defaultAutoScrollOption, | ||
}); | ||
} | ||
}, [autoScroll]); | ||
|
||
return autoScrollHandler; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.