Skip to content

Commit

Permalink
fix(image): fallback-src prop and lazy loading dosen't work (tusen-…
Browse files Browse the repository at this point in the history
…ai#4485)

* fix(image): `fallback-src` prop and lazy loading dosen't work, close tusen-ai#4480

* Update src/image/demos/zhCN/lazy.demo.vue

---------

Co-authored-by: 07akioni <07akioni2@gmail.com>
  • Loading branch information
jizai1125 and 07akioni committed May 11, 2023
1 parent a4ae4e2 commit 31fb2f1
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 25 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
- 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)
- Fix `n-upload` warning cause by extraneous non-props attributes were passed to vue component `TransitionGroup` but could not be automatically inherited, closes [#4447](https://github.com/tusen-ai/naive-ui/issues/4447).
- Fix `n-menu` `show` `default` attribute spelling problem, closes [#4750](https://github.com/tusen-ai/naive-ui/issues/4750).
- Fix `n-icon-wrapper`'s theme error, closes [#4768](https://github.com/tusen-ai/naive-ui/issues/4768).


### Feats

- `n-dynamic-input` adds `action` slot, closes [#3981](https://github.com/tusen-ai/naive-ui/issues/3981).
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,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)
- 修复 `n-upload` 内部使用 vue `TransitionGroup` 组件时, 传递了未声明的 prop 且未被自动继承导致警告, 关闭[#4447](https://github.com/tusen-ai/naive-ui/issues/4447)
- 修复 `n-menu` `show` `default` 属性拼写问题,关闭 [#4750](https://github.com/tusen-ai/naive-ui/issues/4750)
- 修复 `n-icon-wrapper` 的主题异常,关闭 [#4768](https://github.com/tusen-ai/naive-ui/issues/4768)
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) 的属性值;
另一种方式是配合 `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

0 comments on commit 31fb2f1

Please sign in to comment.