twobin / react-lazyload

Lazy load your component, image or anything matters the performance.
MIT License
5.85k stars 487 forks source link

LazyLoadWrapper height is wrong #330

Closed ziimakc closed 3 years ago

ziimakc commented 3 years ago

Why is that LazyLoadWrapper has height + 5px thats create unnecessary margin?

height is wrong height is wrong

// height props is 100
function AvatarPlaceholder({ width, height }: { width: number; height: number }) {
  return (
    <AvatarPlaceholderStyled height={height} width={width}>
      123
    </AvatarPlaceholderStyled>
  )
}

const Avatar = forwardRef<HTMLImageElement, Props>(({ url, width, height, ...rest }, ref) => {
  return (
    <LazyLoad height={height} once offset={200} placeholder={<AvatarPlaceholder height={height} width={width} />}>
      <AvatarStyled src={url || 'https://img.webnovel.com/bookcover/12333953306291305/600/600.jpg'} {...rest} ref={ref} />
    </LazyLoad>
  )
})

const AvatarStyled = styled.img`
  object-fit: fill;
`
const AvatarPlaceholderStyled = styled.div<{ width: number; height: number }>`
  object-fit: fill;
  width: ${({ width }) => width}px;
  height: ${({ height }) => height}px;
`
ameerthehacker commented 3 years ago

@ZiiMakc can you share a minimal example in codesandbox

ziimakc commented 3 years ago

@ameerthehacker here it is: https://codesandbox.io/s/gracious-http-91cnz?file=/src/App.tsx

himanshunegi378 commented 3 years ago

You will find your answer here https://stackoverflow.com/a/5804278