diff --git a/src/hooks/orderbook/useCenterOrderbook.ts b/src/hooks/orderbook/useCenterOrderbook.ts index 2247ec8..77c565e 100644 --- a/src/hooks/orderbook/useCenterOrderbook.ts +++ b/src/hooks/orderbook/useCenterOrderbook.ts @@ -10,10 +10,13 @@ type ElementProps = { * Assumed that the two sides are the same height */ export const useCenterOrderbook = ({ marketId, orderbookRef }: ElementProps) => { + const orderbookEl = orderbookRef.current; + const { clientHeight, scrollHeight } = orderbookEl ?? {}; + const shouldScroll = !!scrollHeight && !!clientHeight && scrollHeight > clientHeight; + useEffect(() => { - if (orderbookRef.current) { - const { clientHeight, scrollHeight } = orderbookRef.current; - orderbookRef.current.scrollTo({ top: (scrollHeight - clientHeight) / 2 }); + if (orderbookEl && shouldScroll) { + orderbookEl.scrollTo({ top: (scrollHeight - clientHeight) / 2 }); } - }, [orderbookRef.current, marketId]); + }, [clientHeight, shouldScroll, marketId]); };