Skip to content

Commit

Permalink
feat: lazyload image
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <i@innei.in>
  • Loading branch information
Innei committed Jan 20, 2024
1 parent 9112c1b commit 59d1944
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 25 deletions.
24 changes: 15 additions & 9 deletions src/app/(app)/(home)/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useQuery } from '@tanstack/react-query'
import React, { createElement, forwardRef, useCallback, useRef } from 'react'
import clsx from 'clsx'
import { m, useInView } from 'framer-motion'
import Image from 'next/image'
import Link from 'next/link'
import type { LinkModel } from '@mx-space/api-client'
import type { PropsWithChildren } from 'react'
Expand Down Expand Up @@ -182,15 +183,20 @@ const Welcome = () => {
</ul>
</>

<img
src={avatar}
alt="Site Owner Avatar"
className={clsxm(
'aspect-square rounded-full border border-slate-200 dark:border-neutral-800',
'lg:h-[300px] lg:w-[300px]',
'h-[200px] w-[200px]',
)}
/>
<div
className={clsx('lg:h-[300px] lg:w-[300px]', 'h-[200px] w-[200px]')}
>
<Image
height={300}
width={300}
src={avatar!}
alt="Site Owner Avatar"
className={clsxm(
'aspect-square rounded-full border border-slate-200 dark:border-neutral-800',
'w-full',
)}
/>
</div>

<m.div
initial={{ opacity: 0.0001, y: 50 }}
Expand Down
39 changes: 23 additions & 16 deletions src/components/ui/markdown/renderers/image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import React, { memo, useRef } from 'react'
import clsx from 'clsx'
import mediumZoom from 'medium-zoom'
import Image from 'next/image'
import type { FC } from 'react'

import { LazyLoad } from '~/components/common/Lazyload'
import { addImageUrlResizeQuery } from '~/lib/image'
import { isVideoExt } from '~/lib/mine-type'
import { useMarkdownImageRecord } from '~/providers/article/MarkdownImageRecordProvider'
Expand Down Expand Up @@ -80,29 +82,34 @@ const GridZoomImage: FC<{ src: string }> = memo(({ src }) => {
const imageEl = useRef<HTMLImageElement>(null)
const wGreaterThanH = width && height ? width > height : true

const ImageComponent = height && width ? Image : 'img'

return (
<div
className="relative flex h-full w-full overflow-hidden rounded-md bg-cover bg-center center"
style={{
backgroundColor: accent,
}}
>
<img
alt=""
height={height}
width={width}
src={cropUrl}
ref={imageEl}
className={clsx(
'!mx-0 !my-0 max-w-max object-cover',
wGreaterThanH ? 'h-full' : 'w-full',
)}
data-zoom-src={src}
onClick={() => {
if (!imageEl.current) return
mediumZoom(imageEl.current).open()
}}
/>
<LazyLoad offset={30}>
<ImageComponent
loading="lazy"
alt=""
height={height}
width={width}
src={cropUrl}
ref={imageEl}
className={clsx(
'!mx-0 !my-0 max-w-max object-cover',
wGreaterThanH ? 'h-full' : 'w-full',
)}
data-zoom-src={src}
onClick={() => {
if (!imageEl.current) return
mediumZoom(imageEl.current).open()
}}
/>
</LazyLoad>
</div>
)
})
Expand Down

1 comment on commit 59d1944

@vercel
Copy link

@vercel vercel bot commented on 59d1944 Jan 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

shiro – ./

shiro-innei.vercel.app
springtide.vercel.app
shiro-git-main-innei.vercel.app
innei.in

Please sign in to comment.