From ca9501e3cc3fe40d1f04490b83b0a03d034d1511 Mon Sep 17 00:00:00 2001 From: jaredvu Date: Sun, 26 Nov 2023 14:55:53 -0800 Subject: [PATCH] Fix orderbook centering --- src/hooks/orderbook/useCenterOrderbook.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) 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]); };