-
-
Notifications
You must be signed in to change notification settings - Fork 452
feat(Avatar): improve image loading status handling and add crossOrigin prop #1838
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
62f7723
fix(avatar): improve image loading status handling and add crossOrigi…
sadeghbarati 69f2dd3
Merge branch 'v2' into avatar-changes
sadeghbarati 0a034e1
fix: use watchEffect cleanup, watch cleanup feature is for vue 3.5+
sadeghbarati d76811d
fix: move `resolveLoadingStatus` fn inside watchEffect instead of usi…
sadeghbarati 1523e89
fix: test case
zernonia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
fix(avatar): improve image loading status handling and add crossOrigi…
…n prop
- Loading branch information
commit 62f772395ca2afbc99eed634bbbca7cd8ab44429
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,33 +1,73 @@ | ||
| import { type ImgHTMLAttributes, type Ref, onMounted, onUnmounted, ref, watch } from 'vue' | ||
| import type { ImgHTMLAttributes, Ref } from 'vue' | ||
| import { computed, onMounted, onUnmounted, ref, watch } from 'vue' | ||
|
|
||
| export type ImageLoadingStatus = 'idle' | 'loading' | 'loaded' | 'error' | ||
|
|
||
| export function useImageLoadingStatus(src: Ref<string>, referrerPolicy?: Ref<ImgHTMLAttributes['referrerpolicy']>) { | ||
| const loadingStatus = ref<ImageLoadingStatus>('idle') | ||
| function resolveLoadingStatus(image: HTMLImageElement | null, src?: string): ImageLoadingStatus { | ||
| if (!image) { | ||
| return 'idle' | ||
| } | ||
| if (!src) { | ||
| return 'error' | ||
| } | ||
| if (image.src !== src) { | ||
| image.src = src | ||
| } | ||
| return image.complete && image.naturalWidth > 0 ? 'loaded' : 'loading' | ||
| } | ||
|
|
||
| export function useImageLoadingStatus(src: Ref<string>, { referrerPolicy, crossOrigin }: { referrerPolicy?: Ref<ImgHTMLAttributes['referrerpolicy']>, crossOrigin?: Ref<ImgHTMLAttributes['crossorigin']> } = {}) { | ||
| const isMounted = ref(false) | ||
| const imageRef = ref<HTMLImageElement | null>(null) | ||
| const image = computed(() => { | ||
| if (!isMounted.value) { | ||
| return null | ||
| } | ||
| if (!imageRef.value) { | ||
| imageRef.value = new window.Image() | ||
| } | ||
| return imageRef.value | ||
| }) | ||
|
|
||
| const loadingStatus = ref<ImageLoadingStatus>(resolveLoadingStatus(image.value, src.value)) | ||
|
|
||
| const updateStatus = (status: ImageLoadingStatus) => () => { | ||
| if (isMounted.value) | ||
| loadingStatus.value = status | ||
| } | ||
|
|
||
| onMounted(() => { | ||
| watch( | ||
| [() => image.value, () => src.value], | ||
| ([image, src]) => { | ||
| loadingStatus.value = resolveLoadingStatus(image, src) | ||
| }, | ||
| { immediate: true }, | ||
| ) | ||
| }) | ||
|
|
||
| onMounted(() => { | ||
| isMounted.value = true | ||
|
|
||
| watch([() => src.value, () => referrerPolicy?.value], ([src, referrer]) => { | ||
| if (!src) { | ||
| loadingStatus.value = 'error' | ||
| } | ||
| else { | ||
| const image = new window.Image() | ||
| loadingStatus.value = 'loading' | ||
| image.onload = updateStatus('loaded') | ||
| image.onerror = updateStatus('error') | ||
| image.src = src | ||
| if (referrer) { | ||
| image.referrerPolicy = referrer | ||
| } | ||
| } | ||
| watch([() => src.value, () => referrerPolicy?.value, () => crossOrigin?.value], ([src, referrerPolicy], _, onCleanup) => { | ||
| if (!image.value) | ||
| return | ||
|
|
||
| const handleLoad = updateStatus('loaded') | ||
| const handleError = updateStatus('error') | ||
|
|
||
| image.value.addEventListener('load', handleLoad) | ||
|
Check failure on line 59 in packages/core/src/Avatar/utils.ts
|
||
| image.value.addEventListener('error', handleError) | ||
|
|
||
| if (referrerPolicy) | ||
| image.value.referrerPolicy = referrerPolicy | ||
| if (typeof crossOrigin === 'string') | ||
| image.value.crossOrigin = crossOrigin | ||
|
|
||
| onCleanup(() => { | ||
| image.value?.removeEventListener('load', handleLoad) | ||
| image.value?.removeEventListener('error', handleError) | ||
| }) | ||
| }, { immediate: true }) | ||
| }) | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
onCleanupinwatchis 3.5+ feature, I believe this might not a feasible solution for earlier version. Perhaps usewatchEffect?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi Zernonia 👋
Right, will change it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done