diff --git a/demo/DemoWindow.tsx b/demo/DemoWindow.tsx new file mode 100644 index 0000000..0fc8c03 --- /dev/null +++ b/demo/DemoWindow.tsx @@ -0,0 +1,93 @@ +import {useEffect, useState} from 'react'; +import { useFakeMessages } from './useFakeMessages'; +import {useStickToBottom, ScrollToBottom} from "../src"; + +function ScrollToBottomButton({isAtBottom, scrollToBottom}: {isAtBottom: boolean, scrollToBottom: ScrollToBottom}) { + return ( + !isAtBottom && ( + + + + ); +} + +function Messages({ animation, speed }: { animation: ScrollBehavior; speed: number }) { + const messages = useFakeMessages(speed); + + return ( +
+

{animation}:

+ +
+ +
+
+ ); +} + +export function DemoWindow() { + const [speed, setSpeed] = useState(0.2); + + return ( +
+ setSpeed(+e.target.value)} + min={0} + max={1} + step={0.01} + > + +
+ +
+
+ ); +} + +function Message({ children }: { children: React.ReactNode }) { + return
{children}
; +} diff --git a/src/useStickToBottom.ts b/src/useStickToBottom.ts index b6dbdf8..168c4ec 100644 --- a/src/useStickToBottom.ts +++ b/src/useStickToBottom.ts @@ -398,7 +398,7 @@ export const useStickToBottom = ( const handleScroll = useCallback( ({ target }: Event) => { - if (target !== scrollRef.current) { + if (target !== document && target !== scrollRef.current) { return; } @@ -469,7 +469,7 @@ export const useStickToBottom = ( ({ target, deltaY }: WheelEvent) => { let element = target as HTMLElement; - while (!["scroll", "auto"].includes(getComputedStyle(element).overflow)) { + while (element !== document.documentElement && !["scroll", "auto"].includes(getComputedStyle(element).overflow)) { if (!element.parentElement) { return; } @@ -595,7 +595,7 @@ export interface StickToBottomInstance { contentRef: React.MutableRefObject & React.RefCallback; scrollRef: React.MutableRefObject & - React.RefCallback; + React.RefCallback; scrollToBottom: ScrollToBottom; stopScroll: StopScroll; isAtBottom: boolean; @@ -610,7 +610,7 @@ function useRefCallback any>( ) { // biome-ignore lint/correctness/useExhaustiveDependencies: not needed const result = useCallback((ref: HTMLElement | null) => { - result.current = ref; + result.current = ref instanceof Window ? document.documentElement : ref; return callback(ref); }, deps) as any as MutableRefObject & RefCallback;