Skip to content

Remove react-lazy-images #638

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/react-notion-x/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
"react-fast-compare": "^3.2.0",
"react-hotkeys-hook": "^4.5.1",
"react-image": "^4.0.3",
"react-lazy-images": "^1.1.0",
"react-modal": "^3.16.3"
},
"devDependencies": {
Expand Down
120 changes: 65 additions & 55 deletions packages/react-notion-x/src/components/lazy-image.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { normalizeUrl } from 'notion-utils'
import * as React from 'react'
import { ImageState, LazyImageFull } from 'react-lazy-images'

import { useNotionContext } from '../context'
import { cs } from '../utils'
Expand Down Expand Up @@ -59,6 +58,23 @@ export function LazyImage({
[zoomable, attachZoom]
)

const [isLazyLoaded, setIsLazyLoaded] = React.useState(false)
const onLoadLazy = React.useCallback(() => {
setIsLazyLoaded(true)
}, [])

const lazyImageRef = React.useCallback(
(image: HTMLImageElement) => {
attachZoomRef?.(image)

// if the image is cached, we can trigger the onLoadLazy immediately
if (image.complete) {
onLoadLazy()
}
},
[attachZoomRef, onLoadLazy]
)

if (previewImage) {
const aspectRatio = previewImage.originalHeight / previewImage.originalWidth

Expand All @@ -81,60 +97,54 @@ export function LazyImage({
)
}

return (
// @ts-expect-error LazyImage types are out-of-date.
<LazyImageFull src={src!} {...rest} experimentalDecode={true}>
{({ imageState, ref }) => {
const isLoaded = imageState === ImageState.LoadSuccess
const wrapperStyle: React.CSSProperties = {
width: '100%'
}
const imgStyle: React.CSSProperties = {}

if (height) {
wrapperStyle.height = height
} else {
imgStyle.position = 'absolute'
wrapperStyle.paddingBottom = `${aspectRatio * 100}%`
}

return (
<div
className={cs(
'lazy-image-wrapper',
isLoaded && 'lazy-image-loaded',
className
)}
style={wrapperStyle}
>
<img
className='lazy-image-preview'
src={previewImage.dataURIBase64}
alt={alt}
ref={ref}
style={style}
decoding='async'
/>

<img
className='lazy-image-real'
src={src}
alt={alt}
ref={attachZoomRef}
style={{
...style,
...imgStyle
}}
width={previewImage.originalWidth}
height={previewImage.originalHeight}
decoding='async'
loading='lazy'
/>
</div>
)
}}
</LazyImageFull>
)
return (() => {
const wrapperStyle: React.CSSProperties = {
width: '100%'
}
const imgStyle: React.CSSProperties = {}

if (height) {
wrapperStyle.height = height
} else {
imgStyle.position = 'absolute'
wrapperStyle.paddingBottom = `${aspectRatio * 100}%`
}

return (
<div
className={cs(
'lazy-image-wrapper',
isLazyLoaded && 'lazy-image-loaded',
className
)}
style={wrapperStyle}
>
<img
className='lazy-image-preview'
src={previewImage.dataURIBase64}
alt={alt}
style={style}
decoding='async'
/>

<img
className='lazy-image-real'
src={src}
alt={alt}
ref={lazyImageRef}
style={{
...style,
...imgStyle
}}
width={previewImage.originalWidth}
height={previewImage.originalHeight}
decoding='async'
loading='lazy'
onLoad={onLoadLazy}
/>
</div>
)
})()
} else {
// TODO: GracefulImage doesn't seem to support refs, but we'd like to prevent
// invalid images from loading as error states
Expand Down
39 changes: 0 additions & 39 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.