react-native-skelo 0.1.0-alpha.0 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +147 -75
- package/lib/commonjs/animations/PulseAnimation.js +17 -42
- package/lib/commonjs/animations/PulseAnimation.js.map +1 -1
- package/lib/commonjs/animations/ShimmerAnimation.js +54 -65
- package/lib/commonjs/animations/ShimmerAnimation.js.map +1 -1
- package/lib/commonjs/components/Skeleton.js +37 -23
- package/lib/commonjs/components/Skeleton.js.map +1 -1
- package/lib/commonjs/components/SkeletonProvider.js +38 -0
- package/lib/commonjs/components/SkeletonProvider.js.map +1 -0
- package/lib/commonjs/components/withSkeleton.js +1 -0
- package/lib/commonjs/components/withSkeleton.js.map +1 -1
- package/lib/commonjs/core/deepRegistry.js +18 -0
- package/lib/commonjs/core/deepRegistry.js.map +1 -0
- package/lib/commonjs/core/parser/expandLists.js +67 -0
- package/lib/commonjs/core/parser/expandLists.js.map +1 -0
- package/lib/commonjs/deep.js +18 -0
- package/lib/commonjs/deep.js.map +1 -0
- package/lib/commonjs/index.js +8 -0
- package/lib/commonjs/index.js.map +1 -1
- package/lib/module/animations/PulseAnimation.js +18 -43
- package/lib/module/animations/PulseAnimation.js.map +1 -1
- package/lib/module/animations/ShimmerAnimation.js +55 -66
- package/lib/module/animations/ShimmerAnimation.js.map +1 -1
- package/lib/module/components/Skeleton.js +37 -23
- package/lib/module/components/Skeleton.js.map +1 -1
- package/lib/module/components/SkeletonProvider.js +30 -0
- package/lib/module/components/SkeletonProvider.js.map +1 -0
- package/lib/module/components/withSkeleton.js +1 -0
- package/lib/module/components/withSkeleton.js.map +1 -1
- package/lib/module/core/deepRegistry.js +11 -0
- package/lib/module/core/deepRegistry.js.map +1 -0
- package/lib/module/core/parser/expandLists.js +61 -0
- package/lib/module/core/parser/expandLists.js.map +1 -0
- package/lib/module/deep.js +11 -0
- package/lib/module/deep.js.map +1 -0
- package/lib/module/index.js +5 -0
- package/lib/module/index.js.map +1 -1
- package/lib/typescript/animations/PulseAnimation.d.ts +0 -44
- package/lib/typescript/animations/PulseAnimation.d.ts.map +1 -1
- package/lib/typescript/animations/ShimmerAnimation.d.ts +0 -44
- package/lib/typescript/animations/ShimmerAnimation.d.ts.map +1 -1
- package/lib/typescript/components/Skeleton.d.ts +1 -1
- package/lib/typescript/components/Skeleton.d.ts.map +1 -1
- package/lib/typescript/components/SkeletonProvider.d.ts +10 -0
- package/lib/typescript/components/SkeletonProvider.d.ts.map +1 -0
- package/lib/typescript/components/withSkeleton.d.ts.map +1 -1
- package/lib/typescript/core/deepRegistry.d.ts +7 -0
- package/lib/typescript/core/deepRegistry.d.ts.map +1 -0
- package/lib/typescript/core/parser/expandLists.d.ts +3 -0
- package/lib/typescript/core/parser/expandLists.d.ts.map +1 -0
- package/lib/typescript/deep.d.ts +2 -0
- package/lib/typescript/deep.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +3 -0
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/types/skeleton.d.ts +8 -0
- package/lib/typescript/types/skeleton.d.ts.map +1 -1
- package/package.json +23 -12
- package/src/animations/PulseAnimation.tsx +17 -89
- package/src/animations/ShimmerAnimation.tsx +39 -118
- package/src/components/Skeleton.tsx +39 -23
- package/src/components/SkeletonProvider.tsx +40 -0
- package/src/components/withSkeleton.tsx +1 -0
- package/src/core/deepRegistry.ts +16 -0
- package/src/core/parser/expandLists.tsx +77 -0
- package/src/deep.ts +12 -0
- package/src/index.ts +6 -0
- package/src/types/skeleton.ts +9 -0
|
@@ -1,77 +1,19 @@
|
|
|
1
|
-
import React, { useEffect } from 'react';
|
|
1
|
+
import React, { useEffect, useRef } from 'react';
|
|
2
2
|
import type { DimensionValue, StyleProp, ViewStyle } from 'react-native';
|
|
3
|
-
import { StyleSheet } from 'react-native';
|
|
4
|
-
import Animated, {
|
|
5
|
-
useSharedValue,
|
|
6
|
-
useAnimatedStyle,
|
|
7
|
-
withRepeat,
|
|
8
|
-
withSequence,
|
|
9
|
-
withTiming,
|
|
10
|
-
Easing,
|
|
11
|
-
} from 'react-native-reanimated';
|
|
3
|
+
import { Animated, Easing, StyleSheet } from 'react-native';
|
|
12
4
|
|
|
13
5
|
interface PulseProps {
|
|
14
|
-
/**
|
|
15
|
-
* Width of the pulse container
|
|
16
|
-
*/
|
|
17
6
|
width?: DimensionValue;
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Height of the pulse container
|
|
21
|
-
*/
|
|
22
7
|
height?: DimensionValue;
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Base color of the skeleton
|
|
26
|
-
* @default '#E1E9EE'
|
|
27
|
-
*/
|
|
28
8
|
baseColor?: string;
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Highlight color for pulse
|
|
32
|
-
* @default '#F2F8FC'
|
|
33
|
-
*/
|
|
34
9
|
highlightColor?: string;
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Animation duration in milliseconds
|
|
38
|
-
* @default 1000
|
|
39
|
-
*/
|
|
40
10
|
duration?: number;
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Border radius
|
|
44
|
-
*/
|
|
45
11
|
borderRadius?: number;
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Additional styles
|
|
49
|
-
*/
|
|
50
12
|
style?: StyleProp<ViewStyle>;
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Child content (rendered inside pulse)
|
|
54
|
-
*/
|
|
55
13
|
children?: React.ReactNode;
|
|
56
14
|
}
|
|
57
15
|
|
|
58
|
-
|
|
59
|
-
* Pulse animation component using Reanimated 3
|
|
60
|
-
*
|
|
61
|
-
* Creates a subtle breathing/pulsing effect using:
|
|
62
|
-
* - Shared values for performance
|
|
63
|
-
* - Worklets for 60 FPS on UI thread
|
|
64
|
-
* - Opacity animation for pulse effect
|
|
65
|
-
*
|
|
66
|
-
* @example
|
|
67
|
-
* ```tsx
|
|
68
|
-
* <PulseAnimation
|
|
69
|
-
* width={200}
|
|
70
|
-
* height={20}
|
|
71
|
-
* borderRadius={4}
|
|
72
|
-
* />
|
|
73
|
-
* ```
|
|
74
|
-
*/
|
|
16
|
+
// Breathing opacity loop using React Native's built-in Animated (native driver).
|
|
75
17
|
export function PulseAnimation({
|
|
76
18
|
width,
|
|
77
19
|
height,
|
|
@@ -81,48 +23,34 @@ export function PulseAnimation({
|
|
|
81
23
|
style,
|
|
82
24
|
children,
|
|
83
25
|
}: PulseProps) {
|
|
84
|
-
|
|
85
|
-
const opacity = useSharedValue(1);
|
|
26
|
+
const opacity = useRef(new Animated.Value(1)).current;
|
|
86
27
|
|
|
87
28
|
useEffect(() => {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
withTiming(0.5, {
|
|
29
|
+
const loop = Animated.loop(
|
|
30
|
+
Animated.sequence([
|
|
31
|
+
Animated.timing(opacity, {
|
|
32
|
+
toValue: 0.5,
|
|
93
33
|
duration: duration / 2,
|
|
94
34
|
easing: Easing.inOut(Easing.ease),
|
|
35
|
+
useNativeDriver: true,
|
|
95
36
|
}),
|
|
96
|
-
|
|
37
|
+
Animated.timing(opacity, {
|
|
38
|
+
toValue: 1,
|
|
97
39
|
duration: duration / 2,
|
|
98
40
|
easing: Easing.inOut(Easing.ease),
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
false // Don't reverse
|
|
41
|
+
useNativeDriver: true,
|
|
42
|
+
}),
|
|
43
|
+
])
|
|
103
44
|
);
|
|
45
|
+
loop.start();
|
|
46
|
+
return () => loop.stop();
|
|
104
47
|
}, [duration, opacity]);
|
|
105
48
|
|
|
106
|
-
// Animated style for pulse effect
|
|
107
|
-
const animatedStyle = useAnimatedStyle(() => {
|
|
108
|
-
'worklet';
|
|
109
|
-
|
|
110
|
-
return {
|
|
111
|
-
opacity: opacity.value,
|
|
112
|
-
};
|
|
113
|
-
}, []);
|
|
114
|
-
|
|
115
49
|
return (
|
|
116
50
|
<Animated.View
|
|
117
51
|
style={[
|
|
118
52
|
styles.container,
|
|
119
|
-
{
|
|
120
|
-
width,
|
|
121
|
-
height,
|
|
122
|
-
borderRadius,
|
|
123
|
-
backgroundColor: baseColor,
|
|
124
|
-
},
|
|
125
|
-
animatedStyle,
|
|
53
|
+
{ width, height, borderRadius, backgroundColor: baseColor, opacity },
|
|
126
54
|
style,
|
|
127
55
|
]}
|
|
128
56
|
>
|
|
@@ -1,78 +1,21 @@
|
|
|
1
|
-
import React, { useEffect } from 'react';
|
|
2
|
-
import type { DimensionValue, StyleProp, ViewStyle } from 'react-native';
|
|
3
|
-
import { StyleSheet, View } from 'react-native';
|
|
4
|
-
import Animated, {
|
|
5
|
-
useSharedValue,
|
|
6
|
-
useAnimatedStyle,
|
|
7
|
-
withRepeat,
|
|
8
|
-
withTiming,
|
|
9
|
-
interpolate,
|
|
10
|
-
Easing,
|
|
11
|
-
} from 'react-native-reanimated';
|
|
12
|
-
import { LinearGradient } from 'react-native-linear-gradient';
|
|
1
|
+
import React, { useEffect, useRef, useState } from 'react';
|
|
2
|
+
import type { DimensionValue, LayoutChangeEvent, StyleProp, ViewStyle } from 'react-native';
|
|
3
|
+
import { Animated, Easing, StyleSheet, View } from 'react-native';
|
|
13
4
|
|
|
14
5
|
interface ShimmerProps {
|
|
15
|
-
/**
|
|
16
|
-
* Width of the shimmer container
|
|
17
|
-
*/
|
|
18
6
|
width?: DimensionValue;
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Height of the shimmer container
|
|
22
|
-
*/
|
|
23
7
|
height?: DimensionValue;
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Base color of the skeleton
|
|
27
|
-
* @default '#E1E9EE'
|
|
28
|
-
*/
|
|
29
8
|
baseColor?: string;
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Highlight color that shimmers across
|
|
33
|
-
* @default '#F2F8FC'
|
|
34
|
-
*/
|
|
35
9
|
highlightColor?: string;
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Animation duration in milliseconds
|
|
39
|
-
* @default 1500
|
|
40
|
-
*/
|
|
41
10
|
duration?: number;
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Border radius
|
|
45
|
-
*/
|
|
46
11
|
borderRadius?: number;
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Additional styles
|
|
50
|
-
*/
|
|
51
12
|
style?: StyleProp<ViewStyle>;
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Child content (rendered on top of shimmer)
|
|
55
|
-
*/
|
|
56
13
|
children?: React.ReactNode;
|
|
57
14
|
}
|
|
58
15
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
* Creates a smooth left-to-right shimmer effect using:
|
|
63
|
-
* - Shared values for performance
|
|
64
|
-
* - Worklets for 60 FPS on UI thread
|
|
65
|
-
* - Linear gradient for shimmer effect
|
|
66
|
-
*
|
|
67
|
-
* @example
|
|
68
|
-
* ```tsx
|
|
69
|
-
* <ShimmerAnimation
|
|
70
|
-
* width={200}
|
|
71
|
-
* height={20}
|
|
72
|
-
* borderRadius={4}
|
|
73
|
-
* />
|
|
74
|
-
* ```
|
|
75
|
-
*/
|
|
16
|
+
// Zero-dependency shimmer: a highlight band (dim/bright/dim segments to fake a
|
|
17
|
+
// soft gradient) sweeps across the base, driven by React Native's built-in
|
|
18
|
+
// Animated native driver. Plain Views only — guaranteed to render and animate.
|
|
76
19
|
export function ShimmerAnimation({
|
|
77
20
|
width,
|
|
78
21
|
height,
|
|
@@ -83,77 +26,49 @@ export function ShimmerAnimation({
|
|
|
83
26
|
style,
|
|
84
27
|
children,
|
|
85
28
|
}: ShimmerProps) {
|
|
86
|
-
|
|
87
|
-
const
|
|
29
|
+
const progress = useRef(new Animated.Value(0)).current;
|
|
30
|
+
const [measuredWidth, setMeasuredWidth] = useState(0);
|
|
88
31
|
|
|
89
32
|
useEffect(() => {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
33
|
+
const loop = Animated.loop(
|
|
34
|
+
Animated.timing(progress, {
|
|
35
|
+
toValue: 1,
|
|
93
36
|
duration,
|
|
94
37
|
easing: Easing.linear,
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
false // Don't reverse
|
|
38
|
+
useNativeDriver: true,
|
|
39
|
+
})
|
|
98
40
|
);
|
|
41
|
+
loop.start();
|
|
42
|
+
return () => loop.stop();
|
|
99
43
|
}, [duration, progress]);
|
|
100
44
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
45
|
+
const onLayout = (event: LayoutChangeEvent) => {
|
|
46
|
+
const w = event.nativeEvent.layout.width;
|
|
47
|
+
setMeasuredWidth(prev => (prev === w ? prev : w));
|
|
48
|
+
};
|
|
104
49
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
const translateX = interpolate(progress.value, [0, 1], [-1, 1]);
|
|
50
|
+
const containerWidth = typeof width === 'number' ? width : measuredWidth || 200;
|
|
51
|
+
const bandWidth = Math.max(containerWidth * 0.6, 80);
|
|
108
52
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
}
|
|
53
|
+
const translateX = progress.interpolate({
|
|
54
|
+
inputRange: [0, 1],
|
|
55
|
+
outputRange: [-bandWidth, containerWidth],
|
|
56
|
+
});
|
|
113
57
|
|
|
114
58
|
return (
|
|
115
59
|
<View
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
{
|
|
119
|
-
width,
|
|
120
|
-
height,
|
|
121
|
-
borderRadius,
|
|
122
|
-
backgroundColor: baseColor,
|
|
123
|
-
},
|
|
124
|
-
style,
|
|
125
|
-
]}
|
|
60
|
+
onLayout={onLayout}
|
|
61
|
+
style={[styles.container, { width, height, borderRadius, backgroundColor: baseColor }, style]}
|
|
126
62
|
>
|
|
127
|
-
{/* Shimmer gradient overlay */}
|
|
128
63
|
<Animated.View
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
{
|
|
132
|
-
overflow: 'hidden',
|
|
133
|
-
borderRadius,
|
|
134
|
-
},
|
|
135
|
-
]}
|
|
64
|
+
pointerEvents="none"
|
|
65
|
+
style={[styles.band, { width: bandWidth, transform: [{ translateX }] }]}
|
|
136
66
|
>
|
|
137
|
-
<
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
{
|
|
141
|
-
width: typeof width === 'number' ? width : '100%',
|
|
142
|
-
height: '100%',
|
|
143
|
-
},
|
|
144
|
-
animatedStyle,
|
|
145
|
-
]}
|
|
146
|
-
>
|
|
147
|
-
<LinearGradient
|
|
148
|
-
colors={[baseColor, highlightColor, highlightColor, baseColor]}
|
|
149
|
-
start={{ x: 0, y: 0 }}
|
|
150
|
-
end={{ x: 1, y: 0 }}
|
|
151
|
-
style={StyleSheet.absoluteFill}
|
|
152
|
-
/>
|
|
153
|
-
</Animated.View>
|
|
67
|
+
<View style={[styles.segment, { backgroundColor: highlightColor, opacity: 0.25 }]} />
|
|
68
|
+
<View style={[styles.segment, { backgroundColor: highlightColor, opacity: 0.9 }]} />
|
|
69
|
+
<View style={[styles.segment, { backgroundColor: highlightColor, opacity: 0.25 }]} />
|
|
154
70
|
</Animated.View>
|
|
155
71
|
|
|
156
|
-
{/* Child content */}
|
|
157
72
|
{children}
|
|
158
73
|
</View>
|
|
159
74
|
);
|
|
@@ -163,7 +78,13 @@ const styles = StyleSheet.create({
|
|
|
163
78
|
container: {
|
|
164
79
|
overflow: 'hidden',
|
|
165
80
|
},
|
|
166
|
-
|
|
81
|
+
band: {
|
|
167
82
|
position: 'absolute',
|
|
83
|
+
top: 0,
|
|
84
|
+
bottom: 0,
|
|
85
|
+
flexDirection: 'row',
|
|
86
|
+
},
|
|
87
|
+
segment: {
|
|
88
|
+
flex: 1,
|
|
168
89
|
},
|
|
169
90
|
});
|
|
@@ -4,8 +4,9 @@ import { Parser } from '../core/parser';
|
|
|
4
4
|
import { SkeletonRenderer } from './SkeletonRenderer';
|
|
5
5
|
import { SkeletonIgnore } from './SkeletonIgnore';
|
|
6
6
|
import { StyleSkeleton } from '../core/generator/StyleSkeleton';
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
7
|
+
import { getDeepExpander } from '../core/deepRegistry';
|
|
8
|
+
import { expandListPlaceholders } from '../core/parser/expandLists';
|
|
9
|
+
import { useSkeletonTheme } from './SkeletonProvider';
|
|
9
10
|
|
|
10
11
|
/**
|
|
11
12
|
* Main Skeleton component
|
|
@@ -35,21 +36,24 @@ import { DEFAULT_CONFIG } from '../constants/defaults';
|
|
|
35
36
|
* </Skeleton>
|
|
36
37
|
* ```
|
|
37
38
|
*/
|
|
38
|
-
export function Skeleton({
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
39
|
+
export function Skeleton(props: SkeletonProps) {
|
|
40
|
+
const theme = useSkeletonTheme();
|
|
41
|
+
const {
|
|
42
|
+
loading,
|
|
43
|
+
children,
|
|
44
|
+
styles,
|
|
45
|
+
excludeStyles,
|
|
46
|
+
deep,
|
|
47
|
+
count = 6,
|
|
48
|
+
animation = theme.animation,
|
|
49
|
+
duration = theme.duration,
|
|
50
|
+
baseColor = theme.baseColor,
|
|
51
|
+
highlightColor = theme.highlightColor,
|
|
52
|
+
borderRadius = theme.borderRadius,
|
|
53
|
+
accessible = true,
|
|
54
|
+
accessibilityLabel = 'Loading content',
|
|
55
|
+
debug = theme.debug,
|
|
56
|
+
} = props;
|
|
53
57
|
// Parse component tree and generate skeleton.
|
|
54
58
|
// Hooks must run unconditionally (rules of hooks), but parsing stays lazy:
|
|
55
59
|
// we skip the work entirely when not loading or in styles-driven mode.
|
|
@@ -63,19 +67,31 @@ export function Skeleton({
|
|
|
63
67
|
}
|
|
64
68
|
|
|
65
69
|
try {
|
|
66
|
-
//
|
|
67
|
-
//
|
|
68
|
-
|
|
69
|
-
|
|
70
|
+
// Turn FlatList/SectionList into placeholder rows so lists show a
|
|
71
|
+
// skeleton even though their data (and rows) don't exist yet.
|
|
72
|
+
const prepared = expandListPlaceholders(children, count);
|
|
73
|
+
|
|
74
|
+
// Deep mode: expand opaque components into their real host tree via the
|
|
75
|
+
// opt-in expander (registered by importing 'react-native-skelo/deep').
|
|
76
|
+
// When `deep` is not set explicitly, it's used automatically whenever the
|
|
77
|
+
// expander is available. Falls back to static parsing otherwise.
|
|
78
|
+
const expand = getDeepExpander();
|
|
79
|
+
const wantDeep = deep === undefined ? expand !== null : deep;
|
|
80
|
+
if (wantDeep && expand) {
|
|
81
|
+
const expanded = expand(<>{prepared}</>);
|
|
70
82
|
if (expanded && expanded.length > 0) {
|
|
71
83
|
return expanded;
|
|
72
84
|
}
|
|
73
85
|
if (__DEV__) {
|
|
74
86
|
console.warn('[Skelo] deep expansion returned nothing; falling back to static parsing.');
|
|
75
87
|
}
|
|
88
|
+
} else if (deep === true && !expand && __DEV__) {
|
|
89
|
+
console.warn(
|
|
90
|
+
"[Skelo] `deep` requires react-reconciler. Install it and add `import 'react-native-skelo/deep';` in your app entry."
|
|
91
|
+
);
|
|
76
92
|
}
|
|
77
93
|
|
|
78
|
-
const parsed = Parser.parse(
|
|
94
|
+
const parsed = Parser.parse(prepared);
|
|
79
95
|
|
|
80
96
|
if (__DEV__ && debug) {
|
|
81
97
|
// Log a lightweight summary: the raw nodes hold React elements
|
|
@@ -96,7 +112,7 @@ export function Skeleton({
|
|
|
96
112
|
}
|
|
97
113
|
return [];
|
|
98
114
|
}
|
|
99
|
-
}, [loading, styles, deep, children, debug]);
|
|
115
|
+
}, [loading, styles, deep, count, children, debug]);
|
|
100
116
|
|
|
101
117
|
// Create skeleton config
|
|
102
118
|
const config = useMemo(
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import React, { createContext, useContext, useMemo } from 'react';
|
|
2
|
+
import type { ReactNode } from 'react';
|
|
3
|
+
import type { SkeletonConfig } from '../types';
|
|
4
|
+
import { DEFAULT_CONFIG } from '../constants/defaults';
|
|
5
|
+
|
|
6
|
+
const SkeletonThemeContext = createContext<Required<SkeletonConfig>>(DEFAULT_CONFIG);
|
|
7
|
+
|
|
8
|
+
export interface SkeletonProviderProps extends SkeletonConfig {
|
|
9
|
+
children: ReactNode;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** App-wide skeleton defaults. Nested providers inherit unspecified values. */
|
|
13
|
+
export function SkeletonProvider({
|
|
14
|
+
children,
|
|
15
|
+
animation,
|
|
16
|
+
duration,
|
|
17
|
+
baseColor,
|
|
18
|
+
highlightColor,
|
|
19
|
+
borderRadius,
|
|
20
|
+
debug,
|
|
21
|
+
}: SkeletonProviderProps) {
|
|
22
|
+
const parent = useContext(SkeletonThemeContext);
|
|
23
|
+
const value = useMemo(
|
|
24
|
+
() => ({
|
|
25
|
+
animation: animation ?? parent.animation,
|
|
26
|
+
duration: duration ?? parent.duration,
|
|
27
|
+
baseColor: baseColor ?? parent.baseColor,
|
|
28
|
+
highlightColor: highlightColor ?? parent.highlightColor,
|
|
29
|
+
borderRadius: borderRadius ?? parent.borderRadius,
|
|
30
|
+
debug: debug ?? parent.debug,
|
|
31
|
+
}),
|
|
32
|
+
[parent, animation, duration, baseColor, highlightColor, borderRadius, debug]
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
return <SkeletonThemeContext.Provider value={value}>{children}</SkeletonThemeContext.Provider>;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function useSkeletonTheme() {
|
|
39
|
+
return useContext(SkeletonThemeContext);
|
|
40
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { ReactElement } from 'react';
|
|
2
|
+
import type { ComponentNode } from '../types';
|
|
3
|
+
|
|
4
|
+
// Deep expansion is opt-in so the core bundle never depends on react-reconciler.
|
|
5
|
+
// `react-native-skelo/deep` registers the expander; Skeleton reads it here.
|
|
6
|
+
type DeepExpander = (element: ReactElement) => ComponentNode[] | null;
|
|
7
|
+
|
|
8
|
+
let expander: DeepExpander | null = null;
|
|
9
|
+
|
|
10
|
+
export function setDeepExpander(fn: DeepExpander): void {
|
|
11
|
+
expander = fn;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function getDeepExpander(): DeepExpander | null {
|
|
15
|
+
return expander;
|
|
16
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import React, { isValidElement } from 'react';
|
|
2
|
+
import type { ReactElement, ReactNode } from 'react';
|
|
3
|
+
import { View } from 'react-native';
|
|
4
|
+
import { getComponentName } from '../../utils/componentUtils';
|
|
5
|
+
|
|
6
|
+
// List components whose rows come from `renderItem` (not JSX children).
|
|
7
|
+
const LIST_TYPES = new Set(['FlatList', 'SectionList', 'VirtualizedList', 'FlashList']);
|
|
8
|
+
|
|
9
|
+
const NOOP_SEPARATORS = {
|
|
10
|
+
highlight: () => {},
|
|
11
|
+
unhighlight: () => {},
|
|
12
|
+
updateProps: () => {},
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
// Renders `count` sample rows from a list's `renderItem` so there's something
|
|
16
|
+
// to skeletonize while the real data is still loading (the rows don't exist in
|
|
17
|
+
// the tree yet). Rows that throw on placeholder data are skipped.
|
|
18
|
+
function renderSampleRows(list: ReactElement, count: number): ReactNode {
|
|
19
|
+
const props = (list.props || {}) as {
|
|
20
|
+
renderItem?: (info: {
|
|
21
|
+
item: unknown;
|
|
22
|
+
index: number;
|
|
23
|
+
section?: unknown;
|
|
24
|
+
separators: typeof NOOP_SEPARATORS;
|
|
25
|
+
}) => ReactNode;
|
|
26
|
+
data?: unknown[];
|
|
27
|
+
contentContainerStyle?: unknown;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const data = Array.isArray(props.data) ? props.data : [];
|
|
31
|
+
const sample = data.length > 0 ? data[0] : {};
|
|
32
|
+
|
|
33
|
+
const rows: ReactNode[] = [];
|
|
34
|
+
for (let i = 0; i < count; i++) {
|
|
35
|
+
try {
|
|
36
|
+
const row = props.renderItem?.({
|
|
37
|
+
item: sample,
|
|
38
|
+
index: i,
|
|
39
|
+
section: {},
|
|
40
|
+
separators: NOOP_SEPARATORS,
|
|
41
|
+
});
|
|
42
|
+
if (row) {
|
|
43
|
+
rows.push(<React.Fragment key={i}>{row}</React.Fragment>);
|
|
44
|
+
}
|
|
45
|
+
} catch {
|
|
46
|
+
// Row couldn't render with placeholder data — skip it.
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return <View style={props.contentContainerStyle as never}>{rows}</View>;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Walks a child tree and replaces FlatList/SectionList with sample rows so a
|
|
54
|
+
// plain `<Skeleton><FlatList/></Skeleton>` produces a skeleton while loading.
|
|
55
|
+
export function expandListPlaceholders(children: ReactNode, count: number): ReactNode {
|
|
56
|
+
return React.Children.map(children, child => {
|
|
57
|
+
if (!isValidElement(child)) {
|
|
58
|
+
return child;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const name = getComponentName(child);
|
|
62
|
+
const props = (child.props || {}) as { children?: ReactNode };
|
|
63
|
+
|
|
64
|
+
if (
|
|
65
|
+
LIST_TYPES.has(name) &&
|
|
66
|
+
typeof (props as { renderItem?: unknown }).renderItem === 'function'
|
|
67
|
+
) {
|
|
68
|
+
return renderSampleRows(child, count);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (props.children) {
|
|
72
|
+
return React.cloneElement(child, undefined, expandListPlaceholders(props.children, count));
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return child;
|
|
76
|
+
});
|
|
77
|
+
}
|
package/src/deep.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// Opt-in deep mode. Import once in your app entry to enable `<Skeleton deep>`:
|
|
2
|
+
//
|
|
3
|
+
// import 'react-native-skelo/deep';
|
|
4
|
+
//
|
|
5
|
+
// This is the only module that pulls in react-reconciler, so apps that don't
|
|
6
|
+
// use deep mode never bundle it. Requires `react-reconciler` to be installed.
|
|
7
|
+
import { setDeepExpander } from './core/deepRegistry';
|
|
8
|
+
import { expandToComponentNodes } from './core/parser/expandOffline';
|
|
9
|
+
|
|
10
|
+
setDeepExpander(expandToComponentNodes);
|
|
11
|
+
|
|
12
|
+
export const deepEnabled = true;
|
package/src/index.ts
CHANGED
|
@@ -6,7 +6,13 @@
|
|
|
6
6
|
* @packageDocumentation
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
+
// Auto-register deep expansion (react-reconciler ships as a dependency), so
|
|
10
|
+
// `<Skeleton deep>` / opaque components work with zero setup.
|
|
11
|
+
import './deep';
|
|
12
|
+
|
|
9
13
|
// Main component
|
|
14
|
+
export { SkeletonProvider } from './components/SkeletonProvider';
|
|
15
|
+
export type { SkeletonProviderProps } from './components/SkeletonProvider';
|
|
10
16
|
export { Skeleton } from './components/Skeleton';
|
|
11
17
|
export { SkeletonIgnore } from './components/SkeletonIgnore';
|
|
12
18
|
export { withSkeleton } from './components/withSkeleton';
|
package/src/types/skeleton.ts
CHANGED
|
@@ -79,6 +79,15 @@ export interface SkeletonProps {
|
|
|
79
79
|
*/
|
|
80
80
|
excludeStyles?: string[];
|
|
81
81
|
|
|
82
|
+
/**
|
|
83
|
+
* Number of placeholder rows to render for a `FlatList` / `SectionList`
|
|
84
|
+
* while its data is still loading (its rows don't exist yet, so Skelo calls
|
|
85
|
+
* `renderItem` with a placeholder and repeats it `count` times).
|
|
86
|
+
*
|
|
87
|
+
* @default 6
|
|
88
|
+
*/
|
|
89
|
+
count?: number;
|
|
90
|
+
|
|
82
91
|
/**
|
|
83
92
|
* Deep mode: expand opaque (custom) child components into their real
|
|
84
93
|
* host-element tree via offline rendering, producing a structured skeleton
|