Repository navigation
feat: show skeleton and grey background while avatar loads #7745
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
Rohit3523
wants to merge
27
commits into
develop
Choose a base branch
from
feat/avatar-loading-skeleton
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,510
−14
Open
Changes from 7 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
d9ee6db
feat: show skeleton and grey background while avatar loads
Rohit3523 cd926ea
Merge branch 'develop' into feat/avatar-loading-skeleton
Rohit3523 4718ddf
change absoluteFillObject to absoluteFill
Rohit3523 b39b23f
Merge branch 'develop' into feat/avatar-loading-skeleton
Rohit3523 00e3988
fix: update avatar snapshots and tidy skeleton code
Rohit3523 8ad2305
Merge branch 'develop' into feat/avatar-loading-skeleton
Rohit3523 0d75cf4
Merge branch 'develop' into feat/avatar-loading-skeleton
Rohit3523 4c41741
refactor: move avatar skeleton delay into Skeleton and test uri chang…
Rohit3523 defc501
Merge branch 'develop' into feat/avatar-loading-skeleton
Rohit3523 4cac1e8
test: pin avatar skeleton delay and shared pulse
Rohit3523 8f129d4
refactor: move the avatar skeleton into its own file
Rohit3523 0f35a00
fix: clear the previous avatar while the new one loads
Rohit3523 383edca
refactor: name the avatar skeleton settle handler and pulse constants
Rohit3523 ff3f5c4
test: move the avatar story snapshots into __tests__
Rohit3523 9a38ceb
refactor: drop the avatar image wrapper view and the Loading story
Rohit3523 9511cdc
refactor: give each avatar skeleton its own full-range pulse
Rohit3523 2fafac7
test: pin the avatar skeleton staying when the uri changes mid-load
Rohit3523 27d3310
refactor: use StyleSheet.absoluteFill directly in the avatar skeleton
Rohit3523 70bb818
fix: hide the avatar skeleton when the uri is removed after a load
Rohit3523 e11bc13
fix: fade the avatar skeleton in and share one pulse across avatars
Rohit3523 b060962
Merge branch 'develop' into feat/avatar-loading-skeleton
Rohit3523 7572ebd
refactor: use react-native-skeleton-placeholder for the avatar skelet…
Rohit3523 5b45228
fix: keep the avatar skeleton static when reduce motion is on
Rohit3523 e601e61
refactor: use one skeleton colour pair and drop reduce motion
Rohit3523 88b7af0
refactor: share the avatar skeleton delay with the tests and rename t…
Rohit3523 54ef9ee
Merge remote-tracking branch 'origin/develop' into feat/avatar-loadin…
Rohit3523 cbbae02
refactor: pass size and borderRadius to the avatar skeleton and drop …
Rohit3523 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| import { useEffect, useState } from 'react'; | ||
| import { StyleSheet, View, type ImageStyle, type ViewStyle } from 'react-native'; | ||
| import { Image } from 'expo-image'; | ||
| import { settings as RocketChatSettings } from '@rocket.chat/sdk'; | ||
| import Animated, { makeMutable, useAnimatedStyle, withRepeat, withTiming } from 'react-native-reanimated'; | ||
|
|
||
| import { headers } from '~/lib/methods/helpers/fetch'; | ||
| import { useTheme } from '~/theme'; | ||
|
|
||
| interface IAvatarImage { | ||
| uri?: string; | ||
| style: ImageStyle; | ||
| } | ||
|
|
||
| const styles = StyleSheet.create({ | ||
| skeleton: { | ||
| ...StyleSheet.absoluteFill | ||
| } | ||
| }); | ||
|
|
||
| const SKELETON_DELAY = 150; | ||
|
|
||
| const pulse = makeMutable(1); | ||
| let activeSkeletons = 0; | ||
|
|
||
| const startPulse = () => { | ||
| if (activeSkeletons++ === 0) { | ||
| pulse.value = withRepeat(withTiming(0.4, { duration: 800 }), -1, true); | ||
| } | ||
| }; | ||
|
|
||
| const stopPulse = () => { | ||
| if (--activeSkeletons === 0) { | ||
| pulse.value = 1; | ||
| } | ||
| }; | ||
|
|
||
| const Skeleton = ({ borderRadius }: { borderRadius: ViewStyle['borderRadius'] }) => { | ||
| const { colors } = useTheme(); | ||
|
|
||
| useEffect(() => { | ||
| startPulse(); | ||
| return stopPulse; | ||
| }, []); | ||
|
|
||
| const animatedStyle = useAnimatedStyle(() => ({ opacity: pulse.value })); | ||
|
|
||
| return ( | ||
| <Animated.View | ||
| pointerEvents='none' | ||
| testID='avatar-skeleton' | ||
| style={[styles.skeleton, { backgroundColor: colors.surfaceSelected, borderRadius }, animatedStyle]} | ||
| /> | ||
| ); | ||
| }; | ||
|
|
||
| const AvatarImage = ({ uri, style }: IAvatarImage) => { | ||
| const { colors } = useTheme(); | ||
| const [loadedUri, setLoadedUri] = useState<string | undefined>(); | ||
| const [delayedUri, setDelayedUri] = useState<string | undefined>(); | ||
| const showSkeleton = loadedUri !== uri && delayedUri === uri; | ||
|
|
||
| useEffect(() => { | ||
| const timer = setTimeout(() => setDelayedUri(uri), SKELETON_DELAY); | ||
| return () => clearTimeout(timer); | ||
| }, [uri]); | ||
|
|
||
| const onEnd = () => setLoadedUri(uri); | ||
|
|
||
| return ( | ||
| <View style={[style, { backgroundColor: colors.surfaceNeutral }]}> | ||
| <Image | ||
| style={style} | ||
| source={{ | ||
| uri, | ||
| headers: RocketChatSettings.customHeaders ?? headers | ||
| }} | ||
| priority='high' | ||
| onLoad={onEnd} | ||
| onError={onEnd} | ||
| /> | ||
| {showSkeleton ? <Skeleton borderRadius={style.borderRadius} /> : null} | ||
| </View> | ||
| ); | ||
| }; | ||
|
|
||
| export default AvatarImage; | ||
1,650 changes: 990 additions & 660 deletions
1,650
app/containers/Avatar/__snapshots__/Avatar.test.tsx.snap
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import { act, fireEvent, render } from '@testing-library/react-native'; | ||
| import { Image } from 'expo-image'; | ||
|
|
||
| import AvatarImage from '../AvatarImage'; | ||
|
|
||
| const style = { width: 25, height: 25, borderRadius: 4 }; | ||
| const showDelay = () => act(() => jest.advanceTimersByTime(150)); | ||
|
|
||
| beforeEach(() => jest.useFakeTimers()); | ||
| afterEach(() => jest.useRealTimers()); | ||
|
|
||
| describe('AvatarImage', () => { | ||
| it('shows the skeleton until the image loads', () => { | ||
| const { getByTestId, queryByTestId, UNSAFE_getByType } = render(<AvatarImage uri='https://a/1.png' style={style} />); | ||
| expect(queryByTestId('avatar-skeleton')).toBeNull(); | ||
| showDelay(); | ||
| expect(getByTestId('avatar-skeleton')).toBeTruthy(); | ||
| fireEvent(UNSAFE_getByType(Image), 'load'); | ||
| expect(queryByTestId('avatar-skeleton')).toBeNull(); | ||
| }); | ||
|
|
||
| it('hides the skeleton when the image fails to load', () => { | ||
| const { queryByTestId, UNSAFE_getByType } = render(<AvatarImage uri='https://a/1.png' style={style} />); | ||
| showDelay(); | ||
| fireEvent(UNSAFE_getByType(Image), 'error'); | ||
| expect(queryByTestId('avatar-skeleton')).toBeNull(); | ||
| }); | ||
|
|
||
| it('shows the skeleton again when the uri changes', () => { | ||
| const { getByTestId, queryByTestId, rerender, UNSAFE_getByType } = render( | ||
| <AvatarImage uri='https://a/1.png' style={style} /> | ||
| ); | ||
| fireEvent(UNSAFE_getByType(Image), 'load'); | ||
| expect(queryByTestId('avatar-skeleton')).toBeNull(); | ||
| rerender(<AvatarImage uri='https://a/2.png' style={style} />); | ||
| showDelay(); | ||
| expect(getByTestId('avatar-skeleton')).toBeTruthy(); | ||
| }); | ||
|
|
||
| it('never shows the skeleton when the image loads before the delay', () => { | ||
| const { queryByTestId, UNSAFE_getByType } = render(<AvatarImage uri='https://a/1.png' style={style} />); | ||
| fireEvent(UNSAFE_getByType(Image), 'load'); | ||
| showDelay(); | ||
| expect(queryByTestId('avatar-skeleton')).toBeNull(); | ||
| }); | ||
| }); |
Oops, something went wrong.
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.