react-native-glitter 1.0.5 → 1.0.7
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/README.md +1 -0
- package/lib/module/index.js +83 -8
- package/lib/typescript/src/index.d.ts +185 -8
- package/package.json +1 -12
- package/src/index.tsx +292 -36
package/README.md
CHANGED
|
@@ -170,6 +170,7 @@ function ControlledGlitter() {
|
|
|
170
170
|
| `testID` | `string` | - | Test ID for e2e testing |
|
|
171
171
|
| `accessibilityLabel` | `string` | - | Accessibility label for screen readers |
|
|
172
172
|
| `accessible` | `boolean` | `true` | Whether the component is accessible |
|
|
173
|
+
| `respectReduceMotion` | `boolean` | `true` | Whether to respect the system's "Reduce Motion" setting |
|
|
173
174
|
|
|
174
175
|
## Examples
|
|
175
176
|
|
package/lib/module/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
2
|
import { useEffect, useRef, useState, useCallback, useMemo, memo, useImperativeHandle, forwardRef, } from 'react';
|
|
3
|
-
import { View, Animated, StyleSheet, Easing, } from 'react-native';
|
|
3
|
+
import { View, Animated, StyleSheet, Easing, AccessibilityInfo, } from 'react-native';
|
|
4
4
|
function generateGlitterOpacities(count, peak = 1) {
|
|
5
5
|
const opacities = [];
|
|
6
6
|
const center = (count - 1) / 2;
|
|
@@ -52,10 +52,37 @@ const HEIGHT_MULTIPLIER = 1.5;
|
|
|
52
52
|
const NORMAL_FADE_RATIO = (HEIGHT_MULTIPLIER - 1) / HEIGHT_MULTIPLIER / 2;
|
|
53
53
|
const ANIMATED_SEGMENTS = generateVerticalSegments(0.25);
|
|
54
54
|
const NORMAL_SEGMENTS = generateVerticalSegments(NORMAL_FADE_RATIO);
|
|
55
|
-
function GlitterComponent({ children, duration = 1500, delay = 400, color = 'rgba(255, 255, 255, 0.8)', angle = 20, shimmerWidth = 60, active = true, style, easing, mode = 'normal', position = 'center', direction = 'left-to-right', iterations = -1, onAnimationStart, onAnimationComplete, testID, accessibilityLabel, accessible = true, }, ref) {
|
|
55
|
+
function GlitterComponent({ children, duration = 1500, delay = 400, color = 'rgba(255, 255, 255, 0.8)', angle = 20, shimmerWidth = 60, active = true, style, easing, mode = 'normal', position = 'center', direction = 'left-to-right', iterations = -1, onAnimationStart, onAnimationComplete, testID, accessibilityLabel, accessible = true, respectReduceMotion = true, }, ref) {
|
|
56
56
|
const animatedValue = useRef(new Animated.Value(0)).current;
|
|
57
57
|
const [containerWidth, setContainerWidth] = useState(0);
|
|
58
58
|
const [containerHeight, setContainerHeight] = useState(0);
|
|
59
|
+
const [reduceMotionEnabled, setReduceMotionEnabled] = useState(false);
|
|
60
|
+
// Detect system reduce motion preference
|
|
61
|
+
useEffect(() => {
|
|
62
|
+
if (!respectReduceMotion) {
|
|
63
|
+
setReduceMotionEnabled(false);
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
let isMounted = true;
|
|
67
|
+
AccessibilityInfo.isReduceMotionEnabled()
|
|
68
|
+
.then((enabled) => {
|
|
69
|
+
if (isMounted) {
|
|
70
|
+
setReduceMotionEnabled(enabled);
|
|
71
|
+
}
|
|
72
|
+
})
|
|
73
|
+
.catch(() => {
|
|
74
|
+
// Ignore errors (e.g., on web where this might not be supported)
|
|
75
|
+
});
|
|
76
|
+
const subscription = AccessibilityInfo.addEventListener('reduceMotionChanged', (enabled) => {
|
|
77
|
+
if (isMounted) {
|
|
78
|
+
setReduceMotionEnabled(enabled);
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
return () => {
|
|
82
|
+
isMounted = false;
|
|
83
|
+
subscription.remove();
|
|
84
|
+
};
|
|
85
|
+
}, [respectReduceMotion]);
|
|
59
86
|
const animationRef = useRef(null);
|
|
60
87
|
const currentIterationRef = useRef(null);
|
|
61
88
|
const iterationCount = useRef(0);
|
|
@@ -86,7 +113,7 @@ function GlitterComponent({ children, duration = 1500, delay = 400, color = 'rgb
|
|
|
86
113
|
isAnimating: () => isAnimatingRef.current,
|
|
87
114
|
}), [stopAnimation, restartAnimation, containerWidth]);
|
|
88
115
|
const startAnimation = useCallback(() => {
|
|
89
|
-
if (!active || containerWidth === 0)
|
|
116
|
+
if (!active || containerWidth === 0 || reduceMotionEnabled)
|
|
90
117
|
return;
|
|
91
118
|
stopAnimation();
|
|
92
119
|
animatedValue.setValue(0);
|
|
@@ -139,6 +166,7 @@ function GlitterComponent({ children, duration = 1500, delay = 400, color = 'rgb
|
|
|
139
166
|
onAnimationStart,
|
|
140
167
|
onAnimationComplete,
|
|
141
168
|
stopAnimation,
|
|
169
|
+
reduceMotionEnabled,
|
|
142
170
|
]);
|
|
143
171
|
useEffect(() => {
|
|
144
172
|
startAnimation();
|
|
@@ -180,7 +208,9 @@ function GlitterComponent({ children, duration = 1500, delay = 400, color = 'rgb
|
|
|
180
208
|
}
|
|
181
209
|
return position === 'top' ? -halfHeight : halfHeight;
|
|
182
210
|
}, [mode, position, halfHeight]);
|
|
183
|
-
|
|
211
|
+
// Optimized: reduced layer count for better performance
|
|
212
|
+
// shimmerWidth=60 → 12 layers (was 20), shimmerWidth=100 → 14 layers (was 33)
|
|
213
|
+
const layerCount = useMemo(() => Math.max(7, Math.round(shimmerWidth / 5)), [shimmerWidth]);
|
|
184
214
|
const layerWidth = useMemo(() => shimmerWidth / layerCount, [shimmerWidth, layerCount]);
|
|
185
215
|
const horizontalOpacities = useMemo(() => generateGlitterOpacities(layerCount, 1), [layerCount]);
|
|
186
216
|
const shimmerLayers = useMemo(() => horizontalOpacities.map((opacity, index) => ({
|
|
@@ -189,6 +219,12 @@ function GlitterComponent({ children, duration = 1500, delay = 400, color = 'rgb
|
|
|
189
219
|
})), [horizontalOpacities, layerWidth, shimmerWidth]);
|
|
190
220
|
const isAnimated = mode !== 'normal';
|
|
191
221
|
const segments = isAnimated ? ANIMATED_SEGMENTS : NORMAL_SEGMENTS;
|
|
222
|
+
// Memoize segment styles to avoid creating new objects on every render
|
|
223
|
+
const segmentStyles = useMemo(() => segments.map((segment) => ({
|
|
224
|
+
height: lineHeight * segment.heightRatio,
|
|
225
|
+
backgroundColor: color,
|
|
226
|
+
baseOpacity: segment.opacity,
|
|
227
|
+
})), [segments, lineHeight, color]);
|
|
192
228
|
const shimmerContainerStyle = useMemo(() => [styles.shimmerContainer, { transform: [{ translateX }] }], [translateX]);
|
|
193
229
|
const rotationWrapperStyle = useMemo(() => [
|
|
194
230
|
styles.rotationWrapper,
|
|
@@ -215,12 +251,15 @@ function GlitterComponent({ children, duration = 1500, delay = 400, color = 'rgb
|
|
|
215
251
|
: [],
|
|
216
252
|
},
|
|
217
253
|
], [layerWidth, lineHeight, isAnimated, transformOriginOffset, scaleY]);
|
|
218
|
-
return (_jsxs(View, { style: [styles.container, style], onLayout: onLayout, testID: testID, accessibilityLabel: accessibilityLabel, accessible: accessible, children: [children, active &&
|
|
254
|
+
return (_jsxs(View, { style: [styles.container, style], onLayout: onLayout, testID: testID, accessibilityLabel: accessibilityLabel, accessible: accessible, children: [children, active &&
|
|
255
|
+
!reduceMotionEnabled &&
|
|
256
|
+
containerWidth > 0 &&
|
|
257
|
+
containerHeight > 0 && (_jsx(Animated.View, { style: shimmerContainerStyle, pointerEvents: "none", children: _jsx(View, { style: rotationWrapperStyle, children: shimmerLayers.map((layer, layerIndex) => (_jsx(Animated.View, { style: getShimmerLineStyle(layer.position), children: segmentStyles.map((segmentStyle, vIndex) => (_jsx(View, { style: [
|
|
219
258
|
styles.segment,
|
|
220
259
|
{
|
|
221
|
-
height:
|
|
222
|
-
backgroundColor:
|
|
223
|
-
opacity: layer.opacity *
|
|
260
|
+
height: segmentStyle.height,
|
|
261
|
+
backgroundColor: segmentStyle.backgroundColor,
|
|
262
|
+
opacity: layer.opacity * segmentStyle.baseOpacity,
|
|
224
263
|
},
|
|
225
264
|
] }, vIndex))) }, layerIndex))) }) }))] }));
|
|
226
265
|
}
|
|
@@ -249,5 +288,41 @@ const styles = StyleSheet.create({
|
|
|
249
288
|
},
|
|
250
289
|
});
|
|
251
290
|
const ForwardedGlitter = forwardRef(GlitterComponent);
|
|
291
|
+
/**
|
|
292
|
+
* A beautiful shimmer/glitter effect component for React Native.
|
|
293
|
+
* Wrap any component to add a sparkling diagonal shine animation.
|
|
294
|
+
*
|
|
295
|
+
* @example
|
|
296
|
+
* ```tsx
|
|
297
|
+
* // Basic usage
|
|
298
|
+
* <Glitter>
|
|
299
|
+
* <View style={styles.card}>
|
|
300
|
+
* <Text>This content will shimmer!</Text>
|
|
301
|
+
* </View>
|
|
302
|
+
* </Glitter>
|
|
303
|
+
*
|
|
304
|
+
* // With custom options
|
|
305
|
+
* <Glitter
|
|
306
|
+
* duration={2000}
|
|
307
|
+
* color="rgba(255, 215, 0, 0.5)"
|
|
308
|
+
* mode="expand"
|
|
309
|
+
* direction="right-to-left"
|
|
310
|
+
* >
|
|
311
|
+
* <View style={styles.premiumButton} />
|
|
312
|
+
* </Glitter>
|
|
313
|
+
*
|
|
314
|
+
* // With ref for programmatic control
|
|
315
|
+
* const glitterRef = useRef<GlitterRef>(null);
|
|
316
|
+
* <Glitter ref={glitterRef} active={false}>
|
|
317
|
+
* <View style={styles.box} />
|
|
318
|
+
* </Glitter>
|
|
319
|
+
* // Later: glitterRef.current?.start();
|
|
320
|
+
* ```
|
|
321
|
+
*
|
|
322
|
+
* @see {@link GlitterProps} for available props
|
|
323
|
+
* @see {@link GlitterRef} for ref methods
|
|
324
|
+
*/
|
|
252
325
|
export const Glitter = memo(ForwardedGlitter);
|
|
326
|
+
// Set display name for React DevTools
|
|
327
|
+
Glitter.displayName = 'Glitter';
|
|
253
328
|
export default Glitter;
|
|
@@ -1,41 +1,218 @@
|
|
|
1
1
|
import { type ReactNode } from 'react';
|
|
2
2
|
import { type StyleProp, type ViewStyle } from 'react-native';
|
|
3
|
+
/**
|
|
4
|
+
* Animation mode for the shimmer effect.
|
|
5
|
+
* - `normal`: Constant size shimmer line
|
|
6
|
+
* - `expand`: Shimmer line starts small and grows
|
|
7
|
+
* - `shrink`: Shimmer line starts full size and shrinks
|
|
8
|
+
*/
|
|
3
9
|
export type GlitterMode = 'normal' | 'expand' | 'shrink';
|
|
10
|
+
/**
|
|
11
|
+
* Position where the shimmer line shrinks to or expands from.
|
|
12
|
+
* Only applies when mode is 'expand' or 'shrink'.
|
|
13
|
+
* - `top`: Shrinks to/expands from the top
|
|
14
|
+
* - `center`: Shrinks to/expands from the center
|
|
15
|
+
* - `bottom`: Shrinks to/expands from the bottom
|
|
16
|
+
*/
|
|
4
17
|
export type GlitterPosition = 'top' | 'center' | 'bottom';
|
|
18
|
+
/**
|
|
19
|
+
* Direction of the shimmer animation movement.
|
|
20
|
+
* - `left-to-right`: Shimmer moves from left to right
|
|
21
|
+
* - `right-to-left`: Shimmer moves from right to left
|
|
22
|
+
*/
|
|
5
23
|
export type GlitterDirection = 'left-to-right' | 'right-to-left';
|
|
24
|
+
/**
|
|
25
|
+
* Props for the Glitter component.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```tsx
|
|
29
|
+
* <Glitter
|
|
30
|
+
* duration={1500}
|
|
31
|
+
* color="rgba(255, 255, 255, 0.8)"
|
|
32
|
+
* mode="expand"
|
|
33
|
+
* >
|
|
34
|
+
* <View style={styles.card} />
|
|
35
|
+
* </Glitter>
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
6
38
|
export interface GlitterProps {
|
|
39
|
+
/**
|
|
40
|
+
* The content to apply the shimmer effect to.
|
|
41
|
+
* Can be any valid React node.
|
|
42
|
+
*/
|
|
7
43
|
children: ReactNode;
|
|
44
|
+
/**
|
|
45
|
+
* Duration of one shimmer animation cycle in milliseconds.
|
|
46
|
+
* @default 1500
|
|
47
|
+
*/
|
|
8
48
|
duration?: number;
|
|
49
|
+
/**
|
|
50
|
+
* Delay between animation cycles in milliseconds.
|
|
51
|
+
* @default 400
|
|
52
|
+
*/
|
|
9
53
|
delay?: number;
|
|
54
|
+
/**
|
|
55
|
+
* Color of the shimmer effect.
|
|
56
|
+
* Supports any valid React Native color format (rgba, hex, rgb, named colors).
|
|
57
|
+
* @default 'rgba(255, 255, 255, 0.8)'
|
|
58
|
+
* @example 'rgba(255, 215, 0, 0.5)' // Gold shimmer
|
|
59
|
+
*/
|
|
10
60
|
color?: string;
|
|
61
|
+
/**
|
|
62
|
+
* Angle of the shimmer in degrees.
|
|
63
|
+
* 0 = horizontal, 45 = diagonal.
|
|
64
|
+
* @default 20
|
|
65
|
+
*/
|
|
11
66
|
angle?: number;
|
|
67
|
+
/**
|
|
68
|
+
* Width of the shimmer band in pixels.
|
|
69
|
+
* @default 60
|
|
70
|
+
*/
|
|
12
71
|
shimmerWidth?: number;
|
|
72
|
+
/**
|
|
73
|
+
* Whether the animation is active.
|
|
74
|
+
* Set to false to pause the animation.
|
|
75
|
+
* @default true
|
|
76
|
+
*/
|
|
13
77
|
active?: boolean;
|
|
78
|
+
/**
|
|
79
|
+
* Additional styles for the container View.
|
|
80
|
+
*/
|
|
14
81
|
style?: StyleProp<ViewStyle>;
|
|
82
|
+
/**
|
|
83
|
+
* Custom easing function for the animation.
|
|
84
|
+
* If not provided, uses a smooth bezier curve (0.4, 0, 0.2, 1).
|
|
85
|
+
* @param value - Input value between 0 and 1
|
|
86
|
+
* @returns Output value between 0 and 1
|
|
87
|
+
* @example (value) => value * value // Ease in quad
|
|
88
|
+
*/
|
|
15
89
|
easing?: (value: number) => number;
|
|
90
|
+
/**
|
|
91
|
+
* Animation mode for the shimmer line.
|
|
92
|
+
* @default 'normal'
|
|
93
|
+
*/
|
|
16
94
|
mode?: GlitterMode;
|
|
95
|
+
/**
|
|
96
|
+
* Position where the line shrinks/expands.
|
|
97
|
+
* Only applies when mode is 'expand' or 'shrink'.
|
|
98
|
+
* @default 'center'
|
|
99
|
+
*/
|
|
17
100
|
position?: GlitterPosition;
|
|
101
|
+
/**
|
|
102
|
+
* Direction of the shimmer animation.
|
|
103
|
+
* @default 'left-to-right'
|
|
104
|
+
*/
|
|
18
105
|
direction?: GlitterDirection;
|
|
106
|
+
/**
|
|
107
|
+
* Number of animation cycles.
|
|
108
|
+
* Set to -1 for infinite loop.
|
|
109
|
+
* @default -1
|
|
110
|
+
*/
|
|
19
111
|
iterations?: number;
|
|
112
|
+
/**
|
|
113
|
+
* Callback fired when the animation starts.
|
|
114
|
+
* Called once at the beginning of the animation sequence.
|
|
115
|
+
*/
|
|
20
116
|
onAnimationStart?: () => void;
|
|
117
|
+
/**
|
|
118
|
+
* Callback fired when all iterations complete.
|
|
119
|
+
* Only called when iterations is a positive number.
|
|
120
|
+
*/
|
|
21
121
|
onAnimationComplete?: () => void;
|
|
22
|
-
/**
|
|
122
|
+
/**
|
|
123
|
+
* Test ID for e2e testing frameworks like Detox.
|
|
124
|
+
*/
|
|
23
125
|
testID?: string;
|
|
24
|
-
/**
|
|
126
|
+
/**
|
|
127
|
+
* Accessibility label for screen readers.
|
|
128
|
+
* Describes the shimmer effect to visually impaired users.
|
|
129
|
+
*/
|
|
25
130
|
accessibilityLabel?: string;
|
|
26
|
-
/**
|
|
131
|
+
/**
|
|
132
|
+
* Whether the component is accessible.
|
|
133
|
+
* @default true
|
|
134
|
+
*/
|
|
27
135
|
accessible?: boolean;
|
|
136
|
+
/**
|
|
137
|
+
* Whether to respect the system's "Reduce Motion" accessibility setting.
|
|
138
|
+
* When enabled and the user has reduced motion enabled, the shimmer animation will be disabled.
|
|
139
|
+
* @default true
|
|
140
|
+
*/
|
|
141
|
+
respectReduceMotion?: boolean;
|
|
28
142
|
}
|
|
29
|
-
/**
|
|
143
|
+
/**
|
|
144
|
+
* Ref methods exposed by the Glitter component for programmatic control.
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* ```tsx
|
|
148
|
+
* const glitterRef = useRef<GlitterRef>(null);
|
|
149
|
+
*
|
|
150
|
+
* // Control animation programmatically
|
|
151
|
+
* glitterRef.current?.start();
|
|
152
|
+
* glitterRef.current?.stop();
|
|
153
|
+
* glitterRef.current?.restart();
|
|
154
|
+
*
|
|
155
|
+
* // Check animation status
|
|
156
|
+
* if (glitterRef.current?.isAnimating()) {
|
|
157
|
+
* console.log('Animation is running');
|
|
158
|
+
* }
|
|
159
|
+
* ```
|
|
160
|
+
*/
|
|
30
161
|
export interface GlitterRef {
|
|
31
|
-
/**
|
|
162
|
+
/**
|
|
163
|
+
* Start the shimmer animation.
|
|
164
|
+
* Has no effect if already animating or container has no size.
|
|
165
|
+
*/
|
|
32
166
|
start: () => void;
|
|
33
|
-
/**
|
|
167
|
+
/**
|
|
168
|
+
* Stop the shimmer animation immediately.
|
|
169
|
+
* Cleans up all animation references.
|
|
170
|
+
*/
|
|
34
171
|
stop: () => void;
|
|
35
|
-
/**
|
|
172
|
+
/**
|
|
173
|
+
* Restart the shimmer animation from the beginning.
|
|
174
|
+
* Stops current animation and starts fresh.
|
|
175
|
+
*/
|
|
36
176
|
restart: () => void;
|
|
37
|
-
/**
|
|
177
|
+
/**
|
|
178
|
+
* Check if animation is currently running.
|
|
179
|
+
* @returns true if animation is active, false otherwise
|
|
180
|
+
*/
|
|
38
181
|
isAnimating: () => boolean;
|
|
39
182
|
}
|
|
183
|
+
/**
|
|
184
|
+
* A beautiful shimmer/glitter effect component for React Native.
|
|
185
|
+
* Wrap any component to add a sparkling diagonal shine animation.
|
|
186
|
+
*
|
|
187
|
+
* @example
|
|
188
|
+
* ```tsx
|
|
189
|
+
* // Basic usage
|
|
190
|
+
* <Glitter>
|
|
191
|
+
* <View style={styles.card}>
|
|
192
|
+
* <Text>This content will shimmer!</Text>
|
|
193
|
+
* </View>
|
|
194
|
+
* </Glitter>
|
|
195
|
+
*
|
|
196
|
+
* // With custom options
|
|
197
|
+
* <Glitter
|
|
198
|
+
* duration={2000}
|
|
199
|
+
* color="rgba(255, 215, 0, 0.5)"
|
|
200
|
+
* mode="expand"
|
|
201
|
+
* direction="right-to-left"
|
|
202
|
+
* >
|
|
203
|
+
* <View style={styles.premiumButton} />
|
|
204
|
+
* </Glitter>
|
|
205
|
+
*
|
|
206
|
+
* // With ref for programmatic control
|
|
207
|
+
* const glitterRef = useRef<GlitterRef>(null);
|
|
208
|
+
* <Glitter ref={glitterRef} active={false}>
|
|
209
|
+
* <View style={styles.box} />
|
|
210
|
+
* </Glitter>
|
|
211
|
+
* // Later: glitterRef.current?.start();
|
|
212
|
+
* ```
|
|
213
|
+
*
|
|
214
|
+
* @see {@link GlitterProps} for available props
|
|
215
|
+
* @see {@link GlitterRef} for ref methods
|
|
216
|
+
*/
|
|
40
217
|
export declare const Glitter: import("react").NamedExoticComponent<GlitterProps & import("react").RefAttributes<GlitterRef>>;
|
|
41
218
|
export default Glitter;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-glitter",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.7",
|
|
4
4
|
"description": "A beautiful shimmer/glitter effect component for React Native. Add a sparkling diagonal shine animation to any component!",
|
|
5
5
|
"main": "./lib/module/index.js",
|
|
6
6
|
"types": "./lib/typescript/src/index.d.ts",
|
|
@@ -15,17 +15,6 @@
|
|
|
15
15
|
"files": [
|
|
16
16
|
"src",
|
|
17
17
|
"lib",
|
|
18
|
-
"android",
|
|
19
|
-
"ios",
|
|
20
|
-
"cpp",
|
|
21
|
-
"*.podspec",
|
|
22
|
-
"react-native.config.js",
|
|
23
|
-
"!ios/build",
|
|
24
|
-
"!android/build",
|
|
25
|
-
"!android/gradle",
|
|
26
|
-
"!android/gradlew",
|
|
27
|
-
"!android/gradlew.bat",
|
|
28
|
-
"!android/local.properties",
|
|
29
18
|
"!**/__tests__",
|
|
30
19
|
"!**/__fixtures__",
|
|
31
20
|
"!**/__mocks__",
|
package/src/index.tsx
CHANGED
|
@@ -16,50 +16,215 @@ import {
|
|
|
16
16
|
Animated,
|
|
17
17
|
StyleSheet,
|
|
18
18
|
Easing,
|
|
19
|
+
AccessibilityInfo,
|
|
19
20
|
type LayoutChangeEvent,
|
|
20
21
|
type StyleProp,
|
|
21
22
|
type ViewStyle,
|
|
22
23
|
} from 'react-native';
|
|
23
24
|
|
|
25
|
+
/**
|
|
26
|
+
* Animation mode for the shimmer effect.
|
|
27
|
+
* - `normal`: Constant size shimmer line
|
|
28
|
+
* - `expand`: Shimmer line starts small and grows
|
|
29
|
+
* - `shrink`: Shimmer line starts full size and shrinks
|
|
30
|
+
*/
|
|
24
31
|
export type GlitterMode = 'normal' | 'expand' | 'shrink';
|
|
25
32
|
|
|
33
|
+
/**
|
|
34
|
+
* Position where the shimmer line shrinks to or expands from.
|
|
35
|
+
* Only applies when mode is 'expand' or 'shrink'.
|
|
36
|
+
* - `top`: Shrinks to/expands from the top
|
|
37
|
+
* - `center`: Shrinks to/expands from the center
|
|
38
|
+
* - `bottom`: Shrinks to/expands from the bottom
|
|
39
|
+
*/
|
|
26
40
|
export type GlitterPosition = 'top' | 'center' | 'bottom';
|
|
27
41
|
|
|
42
|
+
/**
|
|
43
|
+
* Direction of the shimmer animation movement.
|
|
44
|
+
* - `left-to-right`: Shimmer moves from left to right
|
|
45
|
+
* - `right-to-left`: Shimmer moves from right to left
|
|
46
|
+
*/
|
|
28
47
|
export type GlitterDirection = 'left-to-right' | 'right-to-left';
|
|
29
48
|
|
|
49
|
+
/**
|
|
50
|
+
* Props for the Glitter component.
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```tsx
|
|
54
|
+
* <Glitter
|
|
55
|
+
* duration={1500}
|
|
56
|
+
* color="rgba(255, 255, 255, 0.8)"
|
|
57
|
+
* mode="expand"
|
|
58
|
+
* >
|
|
59
|
+
* <View style={styles.card} />
|
|
60
|
+
* </Glitter>
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
30
63
|
export interface GlitterProps {
|
|
64
|
+
/**
|
|
65
|
+
* The content to apply the shimmer effect to.
|
|
66
|
+
* Can be any valid React node.
|
|
67
|
+
*/
|
|
31
68
|
children: ReactNode;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Duration of one shimmer animation cycle in milliseconds.
|
|
72
|
+
* @default 1500
|
|
73
|
+
*/
|
|
32
74
|
duration?: number;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Delay between animation cycles in milliseconds.
|
|
78
|
+
* @default 400
|
|
79
|
+
*/
|
|
33
80
|
delay?: number;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Color of the shimmer effect.
|
|
84
|
+
* Supports any valid React Native color format (rgba, hex, rgb, named colors).
|
|
85
|
+
* @default 'rgba(255, 255, 255, 0.8)'
|
|
86
|
+
* @example 'rgba(255, 215, 0, 0.5)' // Gold shimmer
|
|
87
|
+
*/
|
|
34
88
|
color?: string;
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Angle of the shimmer in degrees.
|
|
92
|
+
* 0 = horizontal, 45 = diagonal.
|
|
93
|
+
* @default 20
|
|
94
|
+
*/
|
|
35
95
|
angle?: number;
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Width of the shimmer band in pixels.
|
|
99
|
+
* @default 60
|
|
100
|
+
*/
|
|
36
101
|
shimmerWidth?: number;
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Whether the animation is active.
|
|
105
|
+
* Set to false to pause the animation.
|
|
106
|
+
* @default true
|
|
107
|
+
*/
|
|
37
108
|
active?: boolean;
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Additional styles for the container View.
|
|
112
|
+
*/
|
|
38
113
|
style?: StyleProp<ViewStyle>;
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Custom easing function for the animation.
|
|
117
|
+
* If not provided, uses a smooth bezier curve (0.4, 0, 0.2, 1).
|
|
118
|
+
* @param value - Input value between 0 and 1
|
|
119
|
+
* @returns Output value between 0 and 1
|
|
120
|
+
* @example (value) => value * value // Ease in quad
|
|
121
|
+
*/
|
|
39
122
|
easing?: (value: number) => number;
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Animation mode for the shimmer line.
|
|
126
|
+
* @default 'normal'
|
|
127
|
+
*/
|
|
40
128
|
mode?: GlitterMode;
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Position where the line shrinks/expands.
|
|
132
|
+
* Only applies when mode is 'expand' or 'shrink'.
|
|
133
|
+
* @default 'center'
|
|
134
|
+
*/
|
|
41
135
|
position?: GlitterPosition;
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Direction of the shimmer animation.
|
|
139
|
+
* @default 'left-to-right'
|
|
140
|
+
*/
|
|
42
141
|
direction?: GlitterDirection;
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Number of animation cycles.
|
|
145
|
+
* Set to -1 for infinite loop.
|
|
146
|
+
* @default -1
|
|
147
|
+
*/
|
|
43
148
|
iterations?: number;
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Callback fired when the animation starts.
|
|
152
|
+
* Called once at the beginning of the animation sequence.
|
|
153
|
+
*/
|
|
44
154
|
onAnimationStart?: () => void;
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Callback fired when all iterations complete.
|
|
158
|
+
* Only called when iterations is a positive number.
|
|
159
|
+
*/
|
|
45
160
|
onAnimationComplete?: () => void;
|
|
46
|
-
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Test ID for e2e testing frameworks like Detox.
|
|
164
|
+
*/
|
|
47
165
|
testID?: string;
|
|
48
|
-
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Accessibility label for screen readers.
|
|
169
|
+
* Describes the shimmer effect to visually impaired users.
|
|
170
|
+
*/
|
|
49
171
|
accessibilityLabel?: string;
|
|
50
|
-
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Whether the component is accessible.
|
|
175
|
+
* @default true
|
|
176
|
+
*/
|
|
51
177
|
accessible?: boolean;
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Whether to respect the system's "Reduce Motion" accessibility setting.
|
|
181
|
+
* When enabled and the user has reduced motion enabled, the shimmer animation will be disabled.
|
|
182
|
+
* @default true
|
|
183
|
+
*/
|
|
184
|
+
respectReduceMotion?: boolean;
|
|
52
185
|
}
|
|
53
186
|
|
|
54
|
-
/**
|
|
187
|
+
/**
|
|
188
|
+
* Ref methods exposed by the Glitter component for programmatic control.
|
|
189
|
+
*
|
|
190
|
+
* @example
|
|
191
|
+
* ```tsx
|
|
192
|
+
* const glitterRef = useRef<GlitterRef>(null);
|
|
193
|
+
*
|
|
194
|
+
* // Control animation programmatically
|
|
195
|
+
* glitterRef.current?.start();
|
|
196
|
+
* glitterRef.current?.stop();
|
|
197
|
+
* glitterRef.current?.restart();
|
|
198
|
+
*
|
|
199
|
+
* // Check animation status
|
|
200
|
+
* if (glitterRef.current?.isAnimating()) {
|
|
201
|
+
* console.log('Animation is running');
|
|
202
|
+
* }
|
|
203
|
+
* ```
|
|
204
|
+
*/
|
|
55
205
|
export interface GlitterRef {
|
|
56
|
-
/**
|
|
206
|
+
/**
|
|
207
|
+
* Start the shimmer animation.
|
|
208
|
+
* Has no effect if already animating or container has no size.
|
|
209
|
+
*/
|
|
57
210
|
start: () => void;
|
|
58
|
-
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Stop the shimmer animation immediately.
|
|
214
|
+
* Cleans up all animation references.
|
|
215
|
+
*/
|
|
59
216
|
stop: () => void;
|
|
60
|
-
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Restart the shimmer animation from the beginning.
|
|
220
|
+
* Stops current animation and starts fresh.
|
|
221
|
+
*/
|
|
61
222
|
restart: () => void;
|
|
62
|
-
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Check if animation is currently running.
|
|
226
|
+
* @returns true if animation is active, false otherwise
|
|
227
|
+
*/
|
|
63
228
|
isAnimating: () => boolean;
|
|
64
229
|
}
|
|
65
230
|
|
|
@@ -149,12 +314,48 @@ function GlitterComponent(
|
|
|
149
314
|
testID,
|
|
150
315
|
accessibilityLabel,
|
|
151
316
|
accessible = true,
|
|
317
|
+
respectReduceMotion = true,
|
|
152
318
|
}: GlitterProps,
|
|
153
319
|
ref: ForwardedRef<GlitterRef>
|
|
154
320
|
): ReactElement {
|
|
155
321
|
const animatedValue = useRef(new Animated.Value(0)).current;
|
|
156
322
|
const [containerWidth, setContainerWidth] = useState(0);
|
|
157
323
|
const [containerHeight, setContainerHeight] = useState(0);
|
|
324
|
+
const [reduceMotionEnabled, setReduceMotionEnabled] = useState(false);
|
|
325
|
+
|
|
326
|
+
// Detect system reduce motion preference
|
|
327
|
+
useEffect(() => {
|
|
328
|
+
if (!respectReduceMotion) {
|
|
329
|
+
setReduceMotionEnabled(false);
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
let isMounted = true;
|
|
334
|
+
|
|
335
|
+
AccessibilityInfo.isReduceMotionEnabled()
|
|
336
|
+
.then((enabled) => {
|
|
337
|
+
if (isMounted) {
|
|
338
|
+
setReduceMotionEnabled(enabled);
|
|
339
|
+
}
|
|
340
|
+
})
|
|
341
|
+
.catch(() => {
|
|
342
|
+
// Ignore errors (e.g., on web where this might not be supported)
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
const subscription = AccessibilityInfo.addEventListener(
|
|
346
|
+
'reduceMotionChanged',
|
|
347
|
+
(enabled) => {
|
|
348
|
+
if (isMounted) {
|
|
349
|
+
setReduceMotionEnabled(enabled);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
);
|
|
353
|
+
|
|
354
|
+
return () => {
|
|
355
|
+
isMounted = false;
|
|
356
|
+
subscription.remove();
|
|
357
|
+
};
|
|
358
|
+
}, [respectReduceMotion]);
|
|
158
359
|
const animationRef = useRef<ReturnType<typeof Animated.loop> | null>(null);
|
|
159
360
|
const currentIterationRef = useRef<ReturnType<
|
|
160
361
|
typeof Animated.sequence
|
|
@@ -196,7 +397,7 @@ function GlitterComponent(
|
|
|
196
397
|
);
|
|
197
398
|
|
|
198
399
|
const startAnimation = useCallback(() => {
|
|
199
|
-
if (!active || containerWidth === 0) return;
|
|
400
|
+
if (!active || containerWidth === 0 || reduceMotionEnabled) return;
|
|
200
401
|
|
|
201
402
|
stopAnimation();
|
|
202
403
|
animatedValue.setValue(0);
|
|
@@ -254,6 +455,7 @@ function GlitterComponent(
|
|
|
254
455
|
onAnimationStart,
|
|
255
456
|
onAnimationComplete,
|
|
256
457
|
stopAnimation,
|
|
458
|
+
reduceMotionEnabled,
|
|
257
459
|
]);
|
|
258
460
|
|
|
259
461
|
useEffect(() => {
|
|
@@ -316,8 +518,10 @@ function GlitterComponent(
|
|
|
316
518
|
return position === 'top' ? -halfHeight : halfHeight;
|
|
317
519
|
}, [mode, position, halfHeight]);
|
|
318
520
|
|
|
521
|
+
// Optimized: reduced layer count for better performance
|
|
522
|
+
// shimmerWidth=60 → 12 layers (was 20), shimmerWidth=100 → 14 layers (was 33)
|
|
319
523
|
const layerCount = useMemo(
|
|
320
|
-
() => Math.max(
|
|
524
|
+
() => Math.max(7, Math.round(shimmerWidth / 5)),
|
|
321
525
|
[shimmerWidth]
|
|
322
526
|
);
|
|
323
527
|
|
|
@@ -343,6 +547,17 @@ function GlitterComponent(
|
|
|
343
547
|
const isAnimated = mode !== 'normal';
|
|
344
548
|
const segments = isAnimated ? ANIMATED_SEGMENTS : NORMAL_SEGMENTS;
|
|
345
549
|
|
|
550
|
+
// Memoize segment styles to avoid creating new objects on every render
|
|
551
|
+
const segmentStyles = useMemo(
|
|
552
|
+
() =>
|
|
553
|
+
segments.map((segment) => ({
|
|
554
|
+
height: lineHeight * segment.heightRatio,
|
|
555
|
+
backgroundColor: color,
|
|
556
|
+
baseOpacity: segment.opacity,
|
|
557
|
+
})),
|
|
558
|
+
[segments, lineHeight, color]
|
|
559
|
+
);
|
|
560
|
+
|
|
346
561
|
const shimmerContainerStyle = useMemo(
|
|
347
562
|
() => [styles.shimmerContainer, { transform: [{ translateX }] }],
|
|
348
563
|
[translateX]
|
|
@@ -390,32 +605,35 @@ function GlitterComponent(
|
|
|
390
605
|
accessible={accessible}
|
|
391
606
|
>
|
|
392
607
|
{children}
|
|
393
|
-
{active &&
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
{
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
608
|
+
{active &&
|
|
609
|
+
!reduceMotionEnabled &&
|
|
610
|
+
containerWidth > 0 &&
|
|
611
|
+
containerHeight > 0 && (
|
|
612
|
+
<Animated.View style={shimmerContainerStyle} pointerEvents="none">
|
|
613
|
+
<View style={rotationWrapperStyle}>
|
|
614
|
+
{shimmerLayers.map((layer, layerIndex) => (
|
|
615
|
+
<Animated.View
|
|
616
|
+
key={layerIndex}
|
|
617
|
+
style={getShimmerLineStyle(layer.position)}
|
|
618
|
+
>
|
|
619
|
+
{segmentStyles.map((segmentStyle, vIndex) => (
|
|
620
|
+
<View
|
|
621
|
+
key={vIndex}
|
|
622
|
+
style={[
|
|
623
|
+
styles.segment,
|
|
624
|
+
{
|
|
625
|
+
height: segmentStyle.height,
|
|
626
|
+
backgroundColor: segmentStyle.backgroundColor,
|
|
627
|
+
opacity: layer.opacity * segmentStyle.baseOpacity,
|
|
628
|
+
},
|
|
629
|
+
]}
|
|
630
|
+
/>
|
|
631
|
+
))}
|
|
632
|
+
</Animated.View>
|
|
633
|
+
))}
|
|
634
|
+
</View>
|
|
635
|
+
</Animated.View>
|
|
636
|
+
)}
|
|
419
637
|
</View>
|
|
420
638
|
);
|
|
421
639
|
}
|
|
@@ -446,6 +664,44 @@ const styles = StyleSheet.create({
|
|
|
446
664
|
});
|
|
447
665
|
|
|
448
666
|
const ForwardedGlitter = forwardRef(GlitterComponent);
|
|
667
|
+
|
|
668
|
+
/**
|
|
669
|
+
* A beautiful shimmer/glitter effect component for React Native.
|
|
670
|
+
* Wrap any component to add a sparkling diagonal shine animation.
|
|
671
|
+
*
|
|
672
|
+
* @example
|
|
673
|
+
* ```tsx
|
|
674
|
+
* // Basic usage
|
|
675
|
+
* <Glitter>
|
|
676
|
+
* <View style={styles.card}>
|
|
677
|
+
* <Text>This content will shimmer!</Text>
|
|
678
|
+
* </View>
|
|
679
|
+
* </Glitter>
|
|
680
|
+
*
|
|
681
|
+
* // With custom options
|
|
682
|
+
* <Glitter
|
|
683
|
+
* duration={2000}
|
|
684
|
+
* color="rgba(255, 215, 0, 0.5)"
|
|
685
|
+
* mode="expand"
|
|
686
|
+
* direction="right-to-left"
|
|
687
|
+
* >
|
|
688
|
+
* <View style={styles.premiumButton} />
|
|
689
|
+
* </Glitter>
|
|
690
|
+
*
|
|
691
|
+
* // With ref for programmatic control
|
|
692
|
+
* const glitterRef = useRef<GlitterRef>(null);
|
|
693
|
+
* <Glitter ref={glitterRef} active={false}>
|
|
694
|
+
* <View style={styles.box} />
|
|
695
|
+
* </Glitter>
|
|
696
|
+
* // Later: glitterRef.current?.start();
|
|
697
|
+
* ```
|
|
698
|
+
*
|
|
699
|
+
* @see {@link GlitterProps} for available props
|
|
700
|
+
* @see {@link GlitterRef} for ref methods
|
|
701
|
+
*/
|
|
449
702
|
export const Glitter = memo(ForwardedGlitter);
|
|
450
703
|
|
|
704
|
+
// Set display name for React DevTools
|
|
705
|
+
Glitter.displayName = 'Glitter';
|
|
706
|
+
|
|
451
707
|
export default Glitter;
|