react-native-skeleton-native 0.0.0-stage → 0.1.1
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 +21 -0
- package/README.md +104 -2
- package/android/build.gradle +89 -0
- package/android/src/main/AndroidManifest.xml +3 -0
- package/android/src/main/AndroidManifestNew.xml +2 -0
- package/android/src/main/java/com/skeletonnative/SkeletonEpoch.java +10 -0
- package/android/src/main/java/com/skeletonnative/SkeletonGroupManager.java +22 -0
- package/android/src/main/java/com/skeletonnative/SkeletonGroupView.java +19 -0
- package/android/src/main/java/com/skeletonnative/SkeletonHighlightView.java +66 -0
- package/android/src/main/java/com/skeletonnative/SkeletonMotion.java +79 -0
- package/android/src/main/java/com/skeletonnative/SkeletonNativePackage.java +28 -0
- package/android/src/main/java/com/skeletonnative/SkeletonPainter.java +418 -0
- package/android/src/main/java/com/skeletonnative/SkeletonResources.java +123 -0
- package/android/src/main/java/com/skeletonnative/SkeletonShimmerManager.java +72 -0
- package/android/src/main/java/com/skeletonnative/SkeletonShimmerView.java +74 -0
- package/android/src/main/java/com/skeletonnative/SkeletonView.java +81 -0
- package/android/src/main/java/com/skeletonnative/SkeletonViewManager.java +89 -0
- package/android/src/main/res/animator/skeleton_native_slide_1000.xml +6 -0
- package/android/src/main/res/animator/skeleton_native_slide_1100.xml +6 -0
- package/android/src/main/res/animator/skeleton_native_slide_1200.xml +6 -0
- package/android/src/main/res/animator/skeleton_native_slide_1300.xml +6 -0
- package/android/src/main/res/animator/skeleton_native_slide_1400.xml +6 -0
- package/android/src/main/res/animator/skeleton_native_slide_1500.xml +6 -0
- package/android/src/main/res/animator/skeleton_native_slide_1600.xml +6 -0
- package/android/src/main/res/animator/skeleton_native_slide_1700.xml +6 -0
- package/android/src/main/res/animator/skeleton_native_slide_1800.xml +6 -0
- package/android/src/main/res/animator/skeleton_native_slide_1900.xml +6 -0
- package/android/src/main/res/animator/skeleton_native_slide_2000.xml +6 -0
- package/android/src/main/res/animator/skeleton_native_slide_2100.xml +6 -0
- package/android/src/main/res/animator/skeleton_native_slide_2200.xml +6 -0
- package/android/src/main/res/animator/skeleton_native_slide_2300.xml +6 -0
- package/android/src/main/res/animator/skeleton_native_slide_2400.xml +6 -0
- package/android/src/main/res/animator/skeleton_native_slide_2500.xml +6 -0
- package/android/src/main/res/animator/skeleton_native_slide_2600.xml +6 -0
- package/android/src/main/res/animator/skeleton_native_slide_2700.xml +6 -0
- package/android/src/main/res/animator/skeleton_native_slide_2800.xml +6 -0
- package/android/src/main/res/animator/skeleton_native_slide_2900.xml +6 -0
- package/android/src/main/res/animator/skeleton_native_slide_300.xml +6 -0
- package/android/src/main/res/animator/skeleton_native_slide_3000.xml +6 -0
- package/android/src/main/res/animator/skeleton_native_slide_3100.xml +6 -0
- package/android/src/main/res/animator/skeleton_native_slide_3200.xml +6 -0
- package/android/src/main/res/animator/skeleton_native_slide_3300.xml +6 -0
- package/android/src/main/res/animator/skeleton_native_slide_3400.xml +6 -0
- package/android/src/main/res/animator/skeleton_native_slide_3500.xml +6 -0
- package/android/src/main/res/animator/skeleton_native_slide_3600.xml +6 -0
- package/android/src/main/res/animator/skeleton_native_slide_3700.xml +6 -0
- package/android/src/main/res/animator/skeleton_native_slide_3800.xml +6 -0
- package/android/src/main/res/animator/skeleton_native_slide_3900.xml +6 -0
- package/android/src/main/res/animator/skeleton_native_slide_400.xml +6 -0
- package/android/src/main/res/animator/skeleton_native_slide_4000.xml +6 -0
- package/android/src/main/res/animator/skeleton_native_slide_4100.xml +6 -0
- package/android/src/main/res/animator/skeleton_native_slide_4200.xml +6 -0
- package/android/src/main/res/animator/skeleton_native_slide_4300.xml +6 -0
- package/android/src/main/res/animator/skeleton_native_slide_4400.xml +6 -0
- package/android/src/main/res/animator/skeleton_native_slide_4500.xml +6 -0
- package/android/src/main/res/animator/skeleton_native_slide_4600.xml +6 -0
- package/android/src/main/res/animator/skeleton_native_slide_4700.xml +6 -0
- package/android/src/main/res/animator/skeleton_native_slide_4800.xml +6 -0
- package/android/src/main/res/animator/skeleton_native_slide_4900.xml +6 -0
- package/android/src/main/res/animator/skeleton_native_slide_500.xml +6 -0
- package/android/src/main/res/animator/skeleton_native_slide_5000.xml +6 -0
- package/android/src/main/res/animator/skeleton_native_slide_600.xml +6 -0
- package/android/src/main/res/animator/skeleton_native_slide_700.xml +6 -0
- package/android/src/main/res/animator/skeleton_native_slide_800.xml +6 -0
- package/android/src/main/res/animator/skeleton_native_slide_900.xml +6 -0
- package/android/src/main/res/drawable/skeleton_native_pulse.xml +22 -0
- package/android/src/main/res/drawable/skeleton_native_pulse_1000.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_pulse_1100.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_pulse_1200.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_pulse_1300.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_pulse_1400.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_pulse_1500.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_pulse_1600.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_pulse_1700.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_pulse_1800.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_pulse_1900.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_pulse_2000.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_pulse_2100.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_pulse_2200.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_pulse_2300.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_pulse_2400.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_pulse_2500.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_pulse_2600.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_pulse_2700.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_pulse_2800.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_pulse_2900.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_pulse_300.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_pulse_3000.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_pulse_3100.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_pulse_3200.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_pulse_3300.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_pulse_3400.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_pulse_3500.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_pulse_3600.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_pulse_3700.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_pulse_3800.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_pulse_3900.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_pulse_400.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_pulse_4000.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_pulse_4100.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_pulse_4200.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_pulse_4300.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_pulse_4400.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_pulse_4500.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_pulse_4600.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_pulse_4700.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_pulse_4800.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_pulse_4900.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_pulse_500.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_pulse_5000.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_pulse_600.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_pulse_700.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_pulse_800.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_pulse_900.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_shimmer.xml +22 -0
- package/android/src/main/res/drawable/skeleton_native_shimmer_1000.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_shimmer_1100.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_shimmer_1200.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_shimmer_1300.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_shimmer_1400.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_shimmer_1500.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_shimmer_1600.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_shimmer_1700.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_shimmer_1800.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_shimmer_1900.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_shimmer_2000.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_shimmer_2100.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_shimmer_2200.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_shimmer_2300.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_shimmer_2400.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_shimmer_2500.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_shimmer_2600.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_shimmer_2700.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_shimmer_2800.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_shimmer_2900.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_shimmer_300.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_shimmer_3000.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_shimmer_3100.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_shimmer_3200.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_shimmer_3300.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_shimmer_3400.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_shimmer_3500.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_shimmer_3600.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_shimmer_3700.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_shimmer_3800.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_shimmer_3900.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_shimmer_400.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_shimmer_4000.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_shimmer_4100.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_shimmer_4200.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_shimmer_4300.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_shimmer_4400.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_shimmer_4500.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_shimmer_4600.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_shimmer_4700.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_shimmer_4800.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_shimmer_4900.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_shimmer_500.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_shimmer_5000.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_shimmer_600.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_shimmer_700.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_shimmer_800.xml +5 -0
- package/android/src/main/res/drawable/skeleton_native_shimmer_900.xml +5 -0
- package/android/src/newarch/java/com/skeletonnative/SkeletonGroupManagerSpec.java +19 -0
- package/android/src/newarch/java/com/skeletonnative/SkeletonShimmerManagerSpec.java +19 -0
- package/android/src/newarch/java/com/skeletonnative/SkeletonViewManagerSpec.java +19 -0
- package/android/src/oldarch/java/com/skeletonnative/SkeletonGroupManagerSpec.java +6 -0
- package/android/src/oldarch/java/com/skeletonnative/SkeletonShimmerManagerSpec.java +21 -0
- package/android/src/oldarch/java/com/skeletonnative/SkeletonViewManagerSpec.java +25 -0
- package/ios/RNSkeletonAnimator.h +75 -0
- package/ios/RNSkeletonAnimator.mm +406 -0
- package/ios/RNSkeletonComponentViews.mm +256 -0
- package/ios/RNSkeletonGroupRegistry.h +14 -0
- package/ios/RNSkeletonGroupRegistry.mm +37 -0
- package/ios/RNSkeletonLegacyViews.h +22 -0
- package/ios/RNSkeletonLegacyViews.mm +99 -0
- package/ios/RNSkeletonView.h +29 -0
- package/ios/RNSkeletonView.mm +77 -0
- package/ios/RNSkeletonViewManagers.mm +65 -0
- package/lib/commonjs/Shimmer.js +55 -0
- package/lib/commonjs/Skeleton.js +113 -0
- package/lib/commonjs/SkeletonGroup.js +58 -0
- package/lib/commonjs/context.js +10 -0
- package/lib/commonjs/index.js +15 -0
- package/lib/commonjs/props.js +113 -0
- package/lib/commonjs/specs/SkeletonNativeGroupNativeComponent.js +8 -0
- package/lib/commonjs/specs/SkeletonNativeShimmerNativeComponent.js +8 -0
- package/lib/commonjs/specs/SkeletonNativeViewNativeComponent.js +8 -0
- package/lib/commonjs/types.js +3 -0
- package/lib/module/Shimmer.js +16 -0
- package/lib/module/Skeleton.js +73 -0
- package/lib/module/SkeletonGroup.js +19 -0
- package/lib/module/context.js +6 -0
- package/lib/module/index.js +5 -0
- package/lib/module/props.js +103 -0
- package/lib/module/specs/SkeletonNativeGroupNativeComponent.js +3 -0
- package/lib/module/specs/SkeletonNativeShimmerNativeComponent.js +3 -0
- package/lib/module/specs/SkeletonNativeViewNativeComponent.js +3 -0
- package/lib/module/types.js +2 -0
- package/lib/typescript/Shimmer.d.ts +13 -0
- package/lib/typescript/Skeleton.d.ts +17 -0
- package/lib/typescript/SkeletonGroup.d.ts +17 -0
- package/lib/typescript/context.d.ts +7 -0
- package/lib/typescript/index.d.ts +7 -0
- package/lib/typescript/props.d.ts +42 -0
- package/lib/typescript/specs/SkeletonNativeGroupNativeComponent.d.ts +5 -0
- package/lib/typescript/specs/SkeletonNativeShimmerNativeComponent.d.ts +13 -0
- package/lib/typescript/specs/SkeletonNativeViewNativeComponent.d.ts +15 -0
- package/lib/typescript/types.d.ts +60 -0
- package/package.json +83 -4
- package/react-native-skeleton-native.podspec +20 -0
- package/react-native.config.js +10 -0
- package/src/Shimmer.tsx +38 -0
- package/src/Skeleton.tsx +125 -0
- package/src/SkeletonGroup.tsx +46 -0
- package/src/context.ts +13 -0
- package/src/index.ts +15 -0
- package/src/props.ts +126 -0
- package/src/specs/SkeletonNativeGroupNativeComponent.ts +9 -0
- package/src/specs/SkeletonNativeShimmerNativeComponent.ts +17 -0
- package/src/specs/SkeletonNativeViewNativeComponent.ts +20 -0
- package/src/types.ts +66 -0
package/src/Skeleton.tsx
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { StyleSheet, View } from 'react-native';
|
|
3
|
+
import type { ViewProps } from 'react-native';
|
|
4
|
+
import { useSkeletonGroup } from './context';
|
|
5
|
+
import {
|
|
6
|
+
DEFAULT_LABEL,
|
|
7
|
+
normalizeDimension,
|
|
8
|
+
normalizeLines,
|
|
9
|
+
normalizeRadius,
|
|
10
|
+
resolveAnimationProps,
|
|
11
|
+
} from './props';
|
|
12
|
+
import type { NativeAnimationProps } from './props';
|
|
13
|
+
import SkeletonNativeView from './specs/SkeletonNativeViewNativeComponent';
|
|
14
|
+
import type { SkeletonCircleProps, SkeletonProps, SkeletonTextProps } from './types';
|
|
15
|
+
|
|
16
|
+
type A11yProps = Pick<
|
|
17
|
+
ViewProps,
|
|
18
|
+
'accessible' | 'accessibilityRole' | 'accessibilityState' | 'accessibilityLabel' | 'importantForAccessibility' | 'accessibilityElementsHidden'
|
|
19
|
+
>;
|
|
20
|
+
|
|
21
|
+
const BUSY = { busy: true } as const;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Accessibility props for one placeholder. Outside a group each placeholder is
|
|
25
|
+
* one "Loading" progress bar; inside a group the group is the only element and
|
|
26
|
+
* every placeholder is hidden from screen readers.
|
|
27
|
+
*/
|
|
28
|
+
export function accessibilityProps(inGroup: boolean, label: string | undefined): A11yProps {
|
|
29
|
+
if (inGroup) {
|
|
30
|
+
return { accessible: false, importantForAccessibility: 'no-hide-descendants', accessibilityElementsHidden: true };
|
|
31
|
+
}
|
|
32
|
+
return {
|
|
33
|
+
accessible: true,
|
|
34
|
+
accessibilityRole: 'progressbar',
|
|
35
|
+
accessibilityState: BUSY,
|
|
36
|
+
accessibilityLabel: label ?? DEFAULT_LABEL,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const HIDDEN: A11yProps = accessibilityProps(true, undefined);
|
|
41
|
+
|
|
42
|
+
function nativeProps(p: NativeAnimationProps) {
|
|
43
|
+
return {
|
|
44
|
+
baseColor: p.baseColor,
|
|
45
|
+
highlightColor: p.highlightColor,
|
|
46
|
+
duration: p.duration,
|
|
47
|
+
direction: p.direction,
|
|
48
|
+
angle: p.angle,
|
|
49
|
+
variant: p.variant,
|
|
50
|
+
animated: p.animated,
|
|
51
|
+
reduceMotion: p.reduceMotion,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function SkeletonBlock(props: SkeletonProps): React.ReactElement {
|
|
56
|
+
const group = useSkeletonGroup();
|
|
57
|
+
const anim = resolveAnimationProps(props, group ? group.props : null);
|
|
58
|
+
const width = normalizeDimension('width', props.width, '100%');
|
|
59
|
+
const height = normalizeDimension('height', props.height, 16);
|
|
60
|
+
const radius = normalizeRadius(props.radius, 4);
|
|
61
|
+
return (
|
|
62
|
+
<SkeletonNativeView
|
|
63
|
+
{...nativeProps(anim)}
|
|
64
|
+
{...accessibilityProps(group != null, props.accessibilityLabel)}
|
|
65
|
+
cornerRadius={radius}
|
|
66
|
+
testID={props.testID}
|
|
67
|
+
style={[{ width, height }, props.style]}
|
|
68
|
+
/>
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function SkeletonCircle(props: SkeletonCircleProps): React.ReactElement {
|
|
73
|
+
const size = typeof props.size === 'number' && Number.isFinite(props.size) && props.size >= 0 ? props.size : 40;
|
|
74
|
+
return <SkeletonBlock {...props} width={size} height={size} radius={size / 2} />;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function SkeletonText(props: SkeletonTextProps): React.ReactElement {
|
|
78
|
+
const group = useSkeletonGroup();
|
|
79
|
+
const anim = resolveAnimationProps(props, group ? group.props : null);
|
|
80
|
+
const lines = normalizeLines(props.lines);
|
|
81
|
+
const lastLineWidth = normalizeDimension('lastLineWidth', props.lastLineWidth, '60%');
|
|
82
|
+
const lineHeight = normalizeRadius(props.lineHeight, 12);
|
|
83
|
+
const gap = normalizeRadius(props.gap, 8);
|
|
84
|
+
const radius = normalizeRadius(props.radius, 4);
|
|
85
|
+
const rows: React.ReactElement[] = [];
|
|
86
|
+
for (let i = 0; i < lines; i++) {
|
|
87
|
+
const last = lines > 1 && i === lines - 1;
|
|
88
|
+
rows.push(
|
|
89
|
+
<SkeletonNativeView
|
|
90
|
+
key={i}
|
|
91
|
+
{...nativeProps(anim)}
|
|
92
|
+
{...HIDDEN}
|
|
93
|
+
cornerRadius={radius}
|
|
94
|
+
style={{ width: last ? lastLineWidth : '100%', height: lineHeight, marginTop: i === 0 ? 0 : gap }}
|
|
95
|
+
/>,
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
return (
|
|
99
|
+
<View
|
|
100
|
+
{...accessibilityProps(group != null, props.accessibilityLabel)}
|
|
101
|
+
testID={props.testID}
|
|
102
|
+
style={[styles.text, props.style]}
|
|
103
|
+
>
|
|
104
|
+
{rows}
|
|
105
|
+
</View>
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const styles = StyleSheet.create({
|
|
110
|
+
text: { width: '100%' },
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
type SkeletonComponent = React.NamedExoticComponent<SkeletonProps> & {
|
|
114
|
+
Circle: React.NamedExoticComponent<SkeletonCircleProps>;
|
|
115
|
+
Text: React.NamedExoticComponent<SkeletonTextProps>;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
const Circle = React.memo(SkeletonCircle);
|
|
119
|
+
Circle.displayName = 'Skeleton.Circle';
|
|
120
|
+
const Text = React.memo(SkeletonText);
|
|
121
|
+
Text.displayName = 'Skeleton.Text';
|
|
122
|
+
|
|
123
|
+
/** One placeholder block. `Skeleton.Circle` and `Skeleton.Text` are shortcuts. */
|
|
124
|
+
export const Skeleton = Object.assign(React.memo(SkeletonBlock), { Circle, Text }) as SkeletonComponent;
|
|
125
|
+
Skeleton.displayName = 'Skeleton';
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import type { StyleProp, ViewStyle } from 'react-native';
|
|
3
|
+
import { SkeletonGroupContext } from './context';
|
|
4
|
+
import type { GroupContextValue } from './context';
|
|
5
|
+
import { DEFAULT_LABEL } from './props';
|
|
6
|
+
import SkeletonNativeGroup from './specs/SkeletonNativeGroupNativeComponent';
|
|
7
|
+
import type { SkeletonAnimationProps } from './types';
|
|
8
|
+
|
|
9
|
+
export interface SkeletonGroupProps extends SkeletonAnimationProps {
|
|
10
|
+
children?: React.ReactNode;
|
|
11
|
+
style?: StyleProp<ViewStyle>;
|
|
12
|
+
/** Screen reader label for the whole group. Default `"Loading"`. */
|
|
13
|
+
accessibilityLabel?: string;
|
|
14
|
+
testID?: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const BUSY = { busy: true } as const;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Wraps many skeletons. Every skeleton and <Shimmer> inside shares one native
|
|
21
|
+
* animation epoch and the group's coordinate space, so one highlight sweeps
|
|
22
|
+
* across the whole group. Animation props set here are inherited.
|
|
23
|
+
* The group is one accessibility element; the blocks inside are hidden.
|
|
24
|
+
*/
|
|
25
|
+
export function SkeletonGroup(props: SkeletonGroupProps): React.ReactElement {
|
|
26
|
+
const { children, style, accessibilityLabel, testID } = props;
|
|
27
|
+
const { baseColor, highlightColor, duration, direction, angle, variant, animated, reduceMotion } = props;
|
|
28
|
+
const value = React.useMemo<GroupContextValue>(
|
|
29
|
+
() => ({ props: { baseColor, highlightColor, duration, direction, angle, variant, animated, reduceMotion } }),
|
|
30
|
+
[baseColor, highlightColor, duration, direction, angle, variant, animated, reduceMotion],
|
|
31
|
+
);
|
|
32
|
+
return (
|
|
33
|
+
<SkeletonGroupContext.Provider value={value}>
|
|
34
|
+
<SkeletonNativeGroup
|
|
35
|
+
accessible
|
|
36
|
+
accessibilityRole="progressbar"
|
|
37
|
+
accessibilityState={BUSY}
|
|
38
|
+
accessibilityLabel={accessibilityLabel ?? DEFAULT_LABEL}
|
|
39
|
+
testID={testID}
|
|
40
|
+
style={style}
|
|
41
|
+
>
|
|
42
|
+
{children}
|
|
43
|
+
</SkeletonNativeGroup>
|
|
44
|
+
</SkeletonGroupContext.Provider>
|
|
45
|
+
);
|
|
46
|
+
}
|
package/src/context.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { createContext, useContext } from 'react';
|
|
2
|
+
import type { SkeletonAnimationProps } from './types';
|
|
3
|
+
|
|
4
|
+
export interface GroupContextValue {
|
|
5
|
+
/** Props set on <SkeletonGroup>, inherited by every skeleton inside it. */
|
|
6
|
+
props: SkeletonAnimationProps;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export const SkeletonGroupContext = createContext<GroupContextValue | null>(null);
|
|
10
|
+
|
|
11
|
+
export function useSkeletonGroup(): GroupContextValue | null {
|
|
12
|
+
return useContext(SkeletonGroupContext);
|
|
13
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export { Skeleton } from './Skeleton';
|
|
2
|
+
export { SkeletonGroup } from './SkeletonGroup';
|
|
3
|
+
export type { SkeletonGroupProps } from './SkeletonGroup';
|
|
4
|
+
export { Shimmer } from './Shimmer';
|
|
5
|
+
export type { ShimmerProps } from './Shimmer';
|
|
6
|
+
export { DEFAULT_ANGLE, DEFAULT_COLORS, DEFAULT_DURATION_MS, DEFAULT_LABEL } from './props';
|
|
7
|
+
export type {
|
|
8
|
+
ReduceMotionMode,
|
|
9
|
+
SkeletonAnimationProps,
|
|
10
|
+
SkeletonCircleProps,
|
|
11
|
+
SkeletonDirection,
|
|
12
|
+
SkeletonProps,
|
|
13
|
+
SkeletonTextProps,
|
|
14
|
+
SkeletonVariant,
|
|
15
|
+
} from './types';
|
package/src/props.ts
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { I18nManager } from 'react-native';
|
|
2
|
+
import type { ColorValue } from 'react-native';
|
|
3
|
+
import type { ReduceMotionMode, SkeletonAnimationProps, SkeletonDirection, SkeletonVariant } from './types';
|
|
4
|
+
|
|
5
|
+
export const DEFAULT_DURATION_MS = 1400;
|
|
6
|
+
export const MIN_DURATION_MS = 100;
|
|
7
|
+
export const MAX_DURATION_MS = 60000;
|
|
8
|
+
export const DEFAULT_ANGLE = 20;
|
|
9
|
+
export const MAX_ANGLE = 45;
|
|
10
|
+
export const DEFAULT_LABEL = 'Loading';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The colours the native views use when `baseColor` / `highlightColor` are not
|
|
14
|
+
* given. They are applied natively, so they follow the system theme without a
|
|
15
|
+
* React re-render.
|
|
16
|
+
*/
|
|
17
|
+
export const DEFAULT_COLORS = {
|
|
18
|
+
light: { base: '#E1E4E8', highlight: '#F5F7F9' },
|
|
19
|
+
dark: { base: '#2B2E33', highlight: '#3D4148' },
|
|
20
|
+
} as const;
|
|
21
|
+
|
|
22
|
+
const VARIANTS: readonly SkeletonVariant[] = ['shimmer', 'pulse', 'none'];
|
|
23
|
+
const DIRECTIONS: readonly SkeletonDirection[] = ['ltr', 'rtl', 'auto'];
|
|
24
|
+
const REDUCE_MOTION: readonly ReduceMotionMode[] = ['system', 'always', 'never'];
|
|
25
|
+
|
|
26
|
+
declare const __DEV__: boolean | undefined;
|
|
27
|
+
|
|
28
|
+
function warn(message: string): void {
|
|
29
|
+
if (typeof __DEV__ !== 'undefined' && __DEV__) {
|
|
30
|
+
console.warn(`react-native-skeleton-native: ${message}`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface NativeAnimationProps {
|
|
35
|
+
baseColor?: ColorValue;
|
|
36
|
+
highlightColor?: ColorValue;
|
|
37
|
+
duration: number;
|
|
38
|
+
direction: 'ltr' | 'rtl';
|
|
39
|
+
angle: number;
|
|
40
|
+
variant: SkeletonVariant;
|
|
41
|
+
animated: boolean;
|
|
42
|
+
reduceMotion: ReduceMotionMode;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function normalizeDuration(value: number | undefined): number {
|
|
46
|
+
if (value === undefined) return DEFAULT_DURATION_MS;
|
|
47
|
+
if (typeof value !== 'number' || !Number.isFinite(value) || value <= 0) {
|
|
48
|
+
warn(`duration must be a positive number of ms, got ${String(value)}; using ${DEFAULT_DURATION_MS}.`);
|
|
49
|
+
return DEFAULT_DURATION_MS;
|
|
50
|
+
}
|
|
51
|
+
return Math.round(Math.min(MAX_DURATION_MS, Math.max(MIN_DURATION_MS, value)));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function normalizeAngle(value: number | undefined): number {
|
|
55
|
+
if (value === undefined) return DEFAULT_ANGLE;
|
|
56
|
+
if (typeof value !== 'number' || !Number.isFinite(value)) {
|
|
57
|
+
warn(`angle must be a number of degrees, got ${String(value)}; using ${DEFAULT_ANGLE}.`);
|
|
58
|
+
return DEFAULT_ANGLE;
|
|
59
|
+
}
|
|
60
|
+
return Math.min(MAX_ANGLE, Math.max(-MAX_ANGLE, value));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function oneOf<T extends string>(name: string, value: T | undefined, allowed: readonly T[], fallback: T): T {
|
|
64
|
+
if (value === undefined) return fallback;
|
|
65
|
+
if (allowed.indexOf(value) === -1) {
|
|
66
|
+
warn(`${name} must be one of ${allowed.join(', ')}, got ${String(value)}; using ${fallback}.`);
|
|
67
|
+
return fallback;
|
|
68
|
+
}
|
|
69
|
+
return value;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function resolveDirection(value: SkeletonDirection | undefined, isRTL: boolean = I18nManager.isRTL): 'ltr' | 'rtl' {
|
|
73
|
+
const d = oneOf('direction', value, DIRECTIONS, 'auto');
|
|
74
|
+
if (d === 'auto') return isRTL ? 'rtl' : 'ltr';
|
|
75
|
+
return d;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Merge own props over group props, validate, and fill defaults. */
|
|
79
|
+
export function resolveAnimationProps(
|
|
80
|
+
own: SkeletonAnimationProps,
|
|
81
|
+
inherited: SkeletonAnimationProps | null,
|
|
82
|
+
): NativeAnimationProps {
|
|
83
|
+
const pick = <K extends keyof SkeletonAnimationProps>(key: K): SkeletonAnimationProps[K] =>
|
|
84
|
+
own[key] !== undefined ? own[key] : inherited ? inherited[key] : undefined;
|
|
85
|
+
return {
|
|
86
|
+
baseColor: pick('baseColor'),
|
|
87
|
+
highlightColor: pick('highlightColor'),
|
|
88
|
+
duration: normalizeDuration(pick('duration')),
|
|
89
|
+
direction: resolveDirection(pick('direction')),
|
|
90
|
+
angle: normalizeAngle(pick('angle')),
|
|
91
|
+
variant: oneOf('variant', pick('variant'), VARIANTS, 'shimmer'),
|
|
92
|
+
animated: pick('animated') !== false,
|
|
93
|
+
reduceMotion: oneOf('reduceMotion', pick('reduceMotion'), REDUCE_MOTION, 'system'),
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export type Dimension = number | `${number}%`;
|
|
98
|
+
|
|
99
|
+
export function normalizeDimension(name: string, value: Dimension | undefined, fallback: Dimension): Dimension {
|
|
100
|
+
if (value === undefined) return fallback;
|
|
101
|
+
if (typeof value === 'number') {
|
|
102
|
+
if (Number.isFinite(value) && value >= 0) return value;
|
|
103
|
+
} else if (typeof value === 'string' && /^\d+(\.\d+)?%$/.test(value)) {
|
|
104
|
+
return value;
|
|
105
|
+
}
|
|
106
|
+
warn(`${name} must be a non-negative number or a percentage string, got ${String(value)}; using ${String(fallback)}.`);
|
|
107
|
+
return fallback;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function normalizeRadius(value: number | undefined, fallback: number): number {
|
|
111
|
+
if (value === undefined) return fallback;
|
|
112
|
+
if (typeof value !== 'number' || !Number.isFinite(value) || value < 0) {
|
|
113
|
+
warn(`radius must be a non-negative number, got ${String(value)}; using ${fallback}.`);
|
|
114
|
+
return fallback;
|
|
115
|
+
}
|
|
116
|
+
return value;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export function normalizeLines(value: number | undefined): number {
|
|
120
|
+
if (value === undefined) return 3;
|
|
121
|
+
if (typeof value !== 'number' || !Number.isFinite(value) || value < 1) {
|
|
122
|
+
warn(`lines must be a number of at least 1, got ${String(value)}; using 1.`);
|
|
123
|
+
return 1;
|
|
124
|
+
}
|
|
125
|
+
return Math.min(100, Math.floor(value));
|
|
126
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// Fabric component spec for the group container. It has no props of its own:
|
|
2
|
+
// the native view holds the shared animation epoch and the coordinate space
|
|
3
|
+
// the highlight sweeps across. Children are ordinary React Native views.
|
|
4
|
+
import type { ViewProps } from 'react-native';
|
|
5
|
+
import codegenNativeComponent from 'react-native/Libraries/Utilities/codegenNativeComponent';
|
|
6
|
+
|
|
7
|
+
export interface NativeProps extends ViewProps {}
|
|
8
|
+
|
|
9
|
+
export default codegenNativeComponent<NativeProps>('SkeletonNativeGroup');
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// Fabric component spec for <Shimmer>: a container that draws the highlight
|
|
2
|
+
// over its children, masked to the pixels they draw.
|
|
3
|
+
import type { ColorValue, ViewProps } from 'react-native';
|
|
4
|
+
import type { Float, Int32, WithDefault } from 'react-native/Libraries/Types/CodegenTypes';
|
|
5
|
+
import codegenNativeComponent from 'react-native/Libraries/Utilities/codegenNativeComponent';
|
|
6
|
+
|
|
7
|
+
export interface NativeProps extends ViewProps {
|
|
8
|
+
highlightColor?: ColorValue;
|
|
9
|
+
duration?: WithDefault<Int32, 1400>;
|
|
10
|
+
direction?: WithDefault<'ltr' | 'rtl', 'ltr'>;
|
|
11
|
+
angle?: WithDefault<Float, 20>;
|
|
12
|
+
variant?: WithDefault<'shimmer' | 'pulse' | 'none', 'shimmer'>;
|
|
13
|
+
animated?: WithDefault<boolean, true>;
|
|
14
|
+
reduceMotion?: WithDefault<'system' | 'always' | 'never', 'system'>;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export default codegenNativeComponent<NativeProps>('SkeletonNativeShimmer');
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// Fabric component spec for one placeholder block. Codegen reads this file
|
|
2
|
+
// (`codegenConfig` in package.json); on the old architecture
|
|
3
|
+
// codegenNativeComponent falls back to requireNativeComponent.
|
|
4
|
+
import type { ColorValue, ViewProps } from 'react-native';
|
|
5
|
+
import type { Float, Int32, WithDefault } from 'react-native/Libraries/Types/CodegenTypes';
|
|
6
|
+
import codegenNativeComponent from 'react-native/Libraries/Utilities/codegenNativeComponent';
|
|
7
|
+
|
|
8
|
+
export interface NativeProps extends ViewProps {
|
|
9
|
+
baseColor?: ColorValue;
|
|
10
|
+
highlightColor?: ColorValue;
|
|
11
|
+
duration?: WithDefault<Int32, 1400>;
|
|
12
|
+
direction?: WithDefault<'ltr' | 'rtl', 'ltr'>;
|
|
13
|
+
angle?: WithDefault<Float, 20>;
|
|
14
|
+
variant?: WithDefault<'shimmer' | 'pulse' | 'none', 'shimmer'>;
|
|
15
|
+
animated?: WithDefault<boolean, true>;
|
|
16
|
+
reduceMotion?: WithDefault<'system' | 'always' | 'never', 'system'>;
|
|
17
|
+
cornerRadius?: WithDefault<Float, 0>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export default codegenNativeComponent<NativeProps>('SkeletonNativeView');
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { ColorValue, StyleProp, ViewStyle } from 'react-native';
|
|
2
|
+
|
|
3
|
+
export type SkeletonVariant = 'shimmer' | 'pulse' | 'none';
|
|
4
|
+
export type SkeletonDirection = 'ltr' | 'rtl' | 'auto';
|
|
5
|
+
/**
|
|
6
|
+
* `system` follows the OS setting (iOS Reduce Motion, Android "Remove
|
|
7
|
+
* animations" / animator duration scale 0). `always` behaves as if it were
|
|
8
|
+
* on, `never` ignores it.
|
|
9
|
+
*/
|
|
10
|
+
export type ReduceMotionMode = 'system' | 'always' | 'never';
|
|
11
|
+
|
|
12
|
+
/** Animation and colour props shared by every component. */
|
|
13
|
+
export interface SkeletonAnimationProps {
|
|
14
|
+
/** Block colour. Defaults to a grey that follows the system light/dark theme, natively. */
|
|
15
|
+
baseColor?: ColorValue;
|
|
16
|
+
/** Colour at the centre of the moving highlight. Defaults follow the system theme. */
|
|
17
|
+
highlightColor?: ColorValue;
|
|
18
|
+
/** Time for one sweep (or one pulse), in ms. Default 1400. */
|
|
19
|
+
duration?: number;
|
|
20
|
+
/** Sweep direction. `auto` follows the app's layout direction (`I18nManager.isRTL`). Default `auto`. */
|
|
21
|
+
direction?: SkeletonDirection;
|
|
22
|
+
/** Lean of the highlight band from vertical, in degrees, clamped to -45..45. Default 20. */
|
|
23
|
+
angle?: number;
|
|
24
|
+
/** Default `shimmer`. */
|
|
25
|
+
variant?: SkeletonVariant;
|
|
26
|
+
/** `false` pauses the animation and shows the static block. Default `true`. */
|
|
27
|
+
animated?: boolean;
|
|
28
|
+
/** Default `system`. */
|
|
29
|
+
reduceMotion?: ReduceMotionMode;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface SkeletonAccessibilityProps {
|
|
33
|
+
/** Screen reader label. Default `"Loading"`. */
|
|
34
|
+
accessibilityLabel?: string;
|
|
35
|
+
testID?: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface SkeletonProps extends SkeletonAnimationProps, SkeletonAccessibilityProps {
|
|
39
|
+
/** Number (dp/pt) or percentage string. Default `'100%'`. */
|
|
40
|
+
width?: number | `${number}%`;
|
|
41
|
+
/** Default 16. */
|
|
42
|
+
height?: number | `${number}%`;
|
|
43
|
+
/** Corner radius. Default 4. */
|
|
44
|
+
radius?: number;
|
|
45
|
+
style?: StyleProp<ViewStyle>;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface SkeletonCircleProps extends SkeletonAnimationProps, SkeletonAccessibilityProps {
|
|
49
|
+
/** Diameter. Default 40. */
|
|
50
|
+
size?: number;
|
|
51
|
+
style?: StyleProp<ViewStyle>;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface SkeletonTextProps extends SkeletonAnimationProps, SkeletonAccessibilityProps {
|
|
55
|
+
/** Number of lines, at least 1. Default 3. */
|
|
56
|
+
lines?: number;
|
|
57
|
+
/** Width of the last line when there is more than one: number or percentage. Default `'60%'`. */
|
|
58
|
+
lastLineWidth?: number | `${number}%`;
|
|
59
|
+
/** Height of one line. Default 12. */
|
|
60
|
+
lineHeight?: number;
|
|
61
|
+
/** Space between lines. Default 8. */
|
|
62
|
+
gap?: number;
|
|
63
|
+
/** Corner radius of each line. Default 4. */
|
|
64
|
+
radius?: number;
|
|
65
|
+
style?: StyleProp<ViewStyle>;
|
|
66
|
+
}
|