Fix orderbook centering

This commit is contained in:
jaredvu 2023-11-26 14:55:53 -08:00
parent 1407c9a460
commit ca9501e3cc
No known key found for this signature in database
GPG Key ID: B9FE2F3F0A5D523C

View File

@ -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]);
};