From 490edd6833cb73e29135507e113c8c5ea8e23f13 Mon Sep 17 00:00:00 2001 From: keyan Date: Tue, 12 Sep 2023 14:50:06 -0500 Subject: [PATCH] prevent horizontal layout shift when balance is hidden --- components/header.js | 14 +++----------- components/hidden-wallet-summary.js | 9 ++++++--- 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/components/header.js b/components/header.js index fd7ed2b8..ba50d3c5 100644 --- a/components/header.js +++ b/components/header.js @@ -10,7 +10,7 @@ import Price from './price' import { useMe } from './me' import Head from 'next/head' import { signOut } from 'next-auth/react' -import { useCallback, useEffect, useState } from 'react' +import { useCallback, useEffect } from 'react' import { randInRange } from '../lib/rand' import { abbrNum } from '../lib/format' import NoteIcon from '../svgs/notification-4-fill.svg' @@ -26,16 +26,8 @@ import AnonIcon from '../svgs/spy-fill.svg' import Hat from './hat' import HiddenWalletSummary from './hidden-wallet-summary' -function WalletSummary ({ me, hideBalance }) { - const [show, setShow] = useState(false) - - useEffect(() => { - // fix warning about useLayoutEffect usage during SSR - // see https://reactjs.org/link/uselayouteffect-ssr - setShow(true) - }, []) - - if (!me || !show) return null +function WalletSummary ({ me }) { + if (!me) return null if (me.hideWalletBalance) { return } diff --git a/components/hidden-wallet-summary.js b/components/hidden-wallet-summary.js index 4001c01c..019af550 100644 --- a/components/hidden-wallet-summary.js +++ b/components/hidden-wallet-summary.js @@ -1,4 +1,4 @@ -import { useState, useRef, useLayoutEffect } from 'react' +import { useState, useRef, useEffect } from 'react' import { abbrNum, numWithUnits } from '../lib/format' import { useMe } from './me' @@ -9,12 +9,15 @@ export default function HiddenWalletSummary ({ abbreviate, fixedWidth }) { // prevent layout shifts when hovering by fixing width to initial rendered width const ref = useRef() const [width, setWidth] = useState(undefined) - useLayoutEffect(() => { + useEffect(() => { setWidth(ref.current?.offsetWidth) }, []) return ( - setHover(true)} onPointerLeave={() => setHover(false)}> + setHover(true)} onPointerLeave={() => setHover(false)} + > {hover ? (abbreviate ? abbrNum(me.sats) : numWithUnits(me.sats, { abbreviate: false })) : '*****'} )