Skip to content
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

fix(image): fallback-src prop and lazy loading dosen't work #4485

Merged
merged 3 commits into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 1 addition & 0 deletions CHANGELOG.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Fix `n-dynamic-input` doesn't return correct `index` in `on-create` callback.
- Fix `trTR` i18n, closes [#4231](https://github.com/tusen-ai/naive-ui/issues/4231).
- Fix `n-input`'s show password icon is offset when use both `password` and `disabled`, closes [#4364](https://github.com/tusen-ai/naive-ui/issues/4364).
- Fix `n-image` set `fallback-src` prop and lazy loading dosen't work, closes[#4480](https://github.com/tusen-ai/naive-ui/issues/4480)

### Feats

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- 修复 `n-dynamic-input` 在点击添加按钮后 `on-create` 返回的 `index` 不正确
- 修复 `trTR` 国际化,关闭 [#4231](https://github.com/tusen-ai/naive-ui/issues/4231)
- 修复 `n-input` 同时使用 `password` 和 `disabled` 时,显示密码图标偏移的问题,关闭 [#4364](https://github.com/tusen-ai/naive-ui/issues/4364)
- 修复 `n-image` 设置 `fallback-src` 属性和懒加载无效,closes[#4480](https://github.com/tusen-ai/naive-ui/issues/4480)

### Feats

Expand Down
3 changes: 2 additions & 1 deletion src/image/demos/enUS/index.demo-entry.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ previewed-img-props.vue
| fallback-src | `string` | `undefined` | URL to show when the image fails to load. | |
| height | `string \| number` | `undefined` | Image height. | |
| img-props | `object` | `undefined` | The props of the img element inside the component. | |
| lazy | `boolean` | `false` | Whether to show after it enters viewport configured by `intersection-observer-options` | 2.30.5 |
| lazy | `boolean` | `false` | Load image after it enters viewport. When used alone, it will be assigned the property value of [HTMLImageElement.loading](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/loading). Alternatively, it can be used in conjunction with the `intersection-observer-options` configuration to achieve lazy loading. |
| 2.30.5 |
| intersection-observer-options | `{ root?: Element \| Document \| string \| null, rootMargin?: string, threshold?: number \| number[]; }` | `undefined` | Intersection observer's config to be applied when `lazy=true`. | 2.30.5 |
| object-fit | `'fill' \| 'contain' \| 'cover' \| 'none' \| 'scale-down'` | `fill` | Object-fit type of the image in the container. | |
| preview-src | `string` | `undefined` | Source of preview image. | |
Expand Down
18 changes: 17 additions & 1 deletion src/image/demos/enUS/lazy.demo.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
<markdown>
# Lazy load

You can use `lazy` to let image load after it enters viewport.
Load the images only after they enter the viewport. There are two ways to use it: one is to use the `lazy` property alone, which will be set to the value of the [HTMLImageElement.loading](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/loading) property, and the other is to use `lazy` together with the `intersection-observer-options` configuration to implement lazy loading using the [IntersectionObserver](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver) API.
</markdown>

<template>
<n-p>use<n-text code>
lazy
</n-text> along</n-p>
<n-image
lazy
width="100"
src="https://07akioni.oss-cn-beijing.aliyuncs.com/07akioni.jpeg"
/>
<n-p>
use <n-text code>
lazy
</n-text> with
<n-text code>
intersection-observer-options
</n-text>
</n-p>
<div
id="image-scroll-container"
style="
Expand Down
2 changes: 1 addition & 1 deletion src/image/demos/zhCN/index.demo-entry.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ previewed-img-props.vue
| fallback-src | `string` | `undefined` | 图片加载失败时显示的地址 | |
| height | `string \| number` | `undefined` | 图片高度 | |
| img-props | `object` | `undefined` | 组件中 img 元素的属性 | |
| lazy | `boolean` | `false` | 是否在进入 `intersection-observer-options` 配置的视口之后再开始加载 | 2.30.5 |
| lazy | `boolean` | `false` | 是否让图片进入视口再加载,单独使用将设置为[HTMLImageElement.loading](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/loading) 的属性值;也可配合 `intersection-observer-options` 配置实现懒加载 | 2.30.5 |
| intersection-observer-options | `{ root?: Element \| Document \| string \| null, rootMargin?: string, threshold?: number \| number[]; }` | `undefined` | `lazy=true` 时 intersection observer 观测的配置 | 2.30.5 |
| object-fit | `'fill' \| 'contain' \| 'cover' \| 'none' \| 'scale-down'` | `'fill'` | 图片在容器内的的适应类型 | |
| preview-src | `string` | `undefined` | 预览图片的图片地址 | |
Expand Down
19 changes: 18 additions & 1 deletion src/image/demos/zhCN/lazy.demo.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
<markdown>
# 懒加载

你可以使用 `lazy` 属性让图片进入视口再加载。
让图片进入视口再加载,两种使用方式:一种是单独使用 `lazy`属性,则将设置为原生[HTMLImageElement.loading](https://developer.mozilla.org/zh-CN/docs/Web/API/HTMLImageElement/loading) 的属性值;
07akioni marked this conversation as resolved.
Show resolved Hide resolved
另一种方式是配合 `intersection-observer-options` 配置,将采用 [IntersectionObserver](https://developer.mozilla.org/zh-CN/docs/Web/API/IntersectionObserver) API 实现懒加载。
</markdown>

<template>
<n-p>单独设置 <n-text code>
lazy
</n-text> 属性</n-p>
<n-image
lazy
width="100"
src="https://07akioni.oss-cn-beijing.aliyuncs.com/07akioni.jpeg"
/>
<n-p>
<n-text code>
lazy
</n-text> 属性配合
<n-text code>
intersection-observer-options
</n-text>
</n-p>
<div
id="image-scroll-container"
style="
Expand Down
41 changes: 20 additions & 21 deletions src/image/src/Image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,25 +89,23 @@ export default defineComponent({
})

onMounted(() => {
if (isImageSupportNativeLazy) {
return
}
let unobserve: (() => void) | undefined
const stopWatchHandle = watchEffect(() => {
unobserve?.()
unobserve = undefined
if (props.lazy) {
// Use IntersectionObserver if lazy and intersectionObserverOptions is set
if (props.lazy && props.intersectionObserverOptions) {
let unobserve: (() => void) | undefined
const stopWatchHandle = watchEffect(() => {
unobserve?.()
unobserve = undefined
unobserve = observeIntersection(
imageRef.value,
props.intersectionObserverOptions,
shouldStartLoadingRef
)
}
})
onBeforeUnmount(() => {
stopWatchHandle()
unobserve?.()
})
})
onBeforeUnmount(() => {
stopWatchHandle()
unobserve?.()
})
}
})

watchEffect(() => {
Expand Down Expand Up @@ -154,19 +152,20 @@ export default defineComponent({
const { mergedClsPrefix, imgProps = {}, loaded, $attrs, lazy } = this

const placeholderNode = this.$slots.placeholder?.()
const loadSrc: string = this.src || imgProps.src || ''
const loadSrc = this.src || imgProps.src

const imgNode = h('img', {
...imgProps,
ref: 'imageRef',
width: this.width || imgProps.width,
height: this.height || imgProps.height,
src: isImageSupportNativeLazy
? loadSrc
: this.showError
? this.fallbackSrc
: this.shouldStartLoading
src: this.showError
? this.fallbackSrc
: lazy && this.intersectionObserverOptions
? this.shouldStartLoading
? loadSrc
: undefined,
: undefined
: loadSrc,
alt: this.alt || imgProps.alt,
'aria-label': this.alt || imgProps.alt,
onClick: this.mergedOnClick,
Expand Down