Skip to content

Commit

Permalink
fix hidden wallet balance layout shift
Browse files Browse the repository at this point in the history
  • Loading branch information
huumn committed Apr 17, 2024
1 parent 59b3d1c commit a77f778
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions components/hidden-wallet-summary.js
@@ -1,24 +1,24 @@
import { useState, useRef, useEffect } from 'react'
import { useState, useMemo } from 'react'
import { abbrNum, numWithUnits } from '@/lib/format'
import { useMe } from './me'

export default function HiddenWalletSummary ({ abbreviate, fixedWidth }) {
const me = useMe()
const [hover, setHover] = useState(false)

// prevent layout shifts when hovering by fixing width to initial rendered width
const ref = useRef()
const [width, setWidth] = useState(undefined)
useEffect(() => {
setWidth(ref.current?.offsetWidth)
}, [])
const fixedWidthAbbrSats = useMemo(() => {
const abbr = abbrNum(me.privates?.sats).toString()
if (abbr.length >= 6) return abbr

// add leading spaces if abbr is shorter than 6 characters
return ' '.repeat(6 - abbr.length) + abbr
}, [me.privates?.sats])

return (
<span
ref={ref} style={{ width: fixedWidth ? width : undefined }}
className='text-monospace' align='right' onPointerEnter={() => setHover(true)} onPointerLeave={() => setHover(false)}
className='text-monospace' style={{ whiteSpace: 'pre-wrap' }} align='right' onPointerEnter={() => setHover(true)} onPointerLeave={() => setHover(false)}
>
{hover ? (abbreviate ? abbrNum(me.privates?.sats) : numWithUnits(me.privates?.sats, { abbreviate: false, format: true })) : '******'}
{hover ? (abbreviate ? fixedWidthAbbrSats : numWithUnits(me.privates?.sats, { abbreviate: false, format: true })) : '******'}
</span>
)
}

0 comments on commit a77f778

Please sign in to comment.