Skip to content

Commit

Permalink
feat: fadeIn ability and stronger loading initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
marcuzgabriel-topdk committed Oct 4, 2023
1 parent 5ae98bd commit 9b8ee53
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 53 deletions.
9 changes: 9 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
MIT License

Copyright (c) 2023 Marcuz Gabriel Larsen

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
9 changes: 9 additions & 0 deletions packages/npm/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
MIT License

Copyright (c) 2023 Marcuz Gabriel Larsen

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2 changes: 1 addition & 1 deletion packages/npm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"author": "Marcuz Gabriel Larsen <marcuzgabriel@gmail.com>",
"license": "MIT",
"name": "react-native-reanimated-skeleton",
"version": "1.2.2",
"version": "1.3.2",
"peerDependencies": {
"react": "*",
"react-native": "*",
Expand Down
46 changes: 34 additions & 12 deletions packages/npm/src/Skeleton.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import React, { memo, useMemo } from 'react';
import { StyleSheet, View } from 'react-native';
import {
import Animated, {
useSharedValue,
withRepeat,
withTiming,
useAnimatedReaction,
useDerivedValue,
useAnimatedStyle,
} from 'react-native-reanimated';
import { DEFAULT_CONFIG } from './constants';
import { useLayout, useGetBones } from './hooks';
import type { ISkeletonProps } from './constants';

const FADE_IN_DURATION = 250;

/* NOTE: Exporting props so it can be reused */
export { ISkeletonProps };

Expand All @@ -23,6 +27,7 @@ export { ISkeletonProps };
* @animationDirection is the direction of the animation.
* @isLoading is the state of the animation visibility.
* @boneColor is the color of the bone.
* @hasFadeIn is the state of the fade in animation.
* @highlightColor is the color of the highlight.
* @children is the children of the component / the content that should be visible after loading.
*/
Expand All @@ -36,10 +41,11 @@ const Skeleton: React.FC<ISkeletonProps> = ({
isLoading = DEFAULT_CONFIG.LOADING,
boneColor = DEFAULT_CONFIG.BONE_COLOR,
highlightColor = DEFAULT_CONFIG.HIGHLIGHT_COLOR,
hasFadeIn,
children,
}) => {
const animationValue = useSharedValue(0);
const loadingValue = useSharedValue(isLoading ? 1 : 0);
const loadingValue = useSharedValue(0);
const shiverValue = useSharedValue(animationType === 'shiver' ? 1 : 0);
const [componentSize, onLayout] = useLayout();
const generalStyles = useMemo(
Expand All @@ -62,9 +68,9 @@ const Skeleton: React.FC<ISkeletonProps> = ({
const getBones = useGetBones(componentSize);

useAnimatedReaction(
() => ({ loadingValue }),
() => ({ isLoading, loadingValue }),
() => {
if (loadingValue.value === 1) {
if (isLoading && loadingValue.value !== 1) {
animationValue.value =
shiverValue.value === 1
? withRepeat(withTiming(1, { duration, easing }), -1, false)
Expand All @@ -75,18 +81,34 @@ const Skeleton: React.FC<ISkeletonProps> = ({
);
}
},
[loadingValue, shiverValue],
[isLoading, shiverValue],
);

const opacity = useDerivedValue(() => {
if (!isLoading) {
return withTiming(1, { duration: FADE_IN_DURATION });
}

return 0;
});

const animatedStyle = useAnimatedStyle(() => ({
opacity: opacity.value,
}));

return (
<View style={containerStyle} onLayout={onLayout}>
{isLoading
? getBones({
bonesLayout: layout,
children,
generalStyles,
})
: children}
{isLoading ? (
getBones({
bonesLayout: layout,
children,
generalStyles,
})
) : (
<Animated.View style={hasFadeIn ? animatedStyle : {}}>
{children}
</Animated.View>
)}
</View>
);
};
Expand Down
1 change: 1 addition & 0 deletions packages/npm/src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface ISkeletonProps {
animationType?: _animationType;
animationDirection?: _animationDirection;
boneColor?: string | undefined;
hasFadeIn?: boolean;
highlightColor?: string | undefined;
easing?: { factory: () => EasingFn };
children?: React.ReactNode;
Expand Down
91 changes: 51 additions & 40 deletions packages/npm/src/example/index.tsx
Original file line number Diff line number Diff line change
@@ -1,46 +1,57 @@
import React from 'react';
import React, { useState, useEffect } from 'react';
import { Text, StyleSheet, SafeAreaView } from 'react-native';
import Skeleton from '../Skeleton';

const SkeletonExample: React.FC = () => (
<SafeAreaView>
<Skeleton
isLoading={true}
containerStyle={styles.container}
layout={[
{
width: 325,
height: 325,
borderRadius: 34,
marginBottom: 16,
},
{
flexDirection: 'column',
justifyContent: 'center',
alignItems: 'center',
children: [
{
width: 119,
height: 19,
borderRadius: 16,
marginBottom: 8,
},
{
width: 234,
height: 42,
borderRadius: 16,
},
],
},
]}
>
<>
<Text style={styles.normalText}>Your content</Text>
<Text style={styles.bigText}>Other content</Text>
</>
</Skeleton>
</SafeAreaView>
);
const SkeletonExample: React.FC = () => {
const [isLoading, setIsLoading] = useState(true);

useEffect(() => {
setTimeout(() => {
setIsLoading(false);
}, 1500);
}, []);

return (
<SafeAreaView>
<Skeleton
isLoading={isLoading}
hasFadeIn
containerStyle={styles.container}
layout={[
{
width: 325,
height: 325,
borderRadius: 34,
marginBottom: 16,
},
{
flexDirection: 'column',
justifyContent: 'center',
alignItems: 'center',
children: [
{
width: 119,
height: 19,
borderRadius: 16,
marginBottom: 8,
},
{
width: 234,
height: 42,
borderRadius: 16,
},
],
},
]}
>
<>
<Text style={styles.normalText}>Your content</Text>
<Text style={styles.bigText}>Other content</Text>
</>
</Skeleton>
</SafeAreaView>
);
};

export default SkeletonExample;

Expand Down

0 comments on commit 9b8ee53

Please sign in to comment.