react-native-glitter 1.0.5 → 1.0.6
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 +71 -4
- package/lib/typescript/src/index.d.ts +185 -8
- package/package.json +1 -12
- package/src/index.tsx +278 -35
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();
|
|
@@ -215,7 +243,10 @@ function GlitterComponent({ children, duration = 1500, delay = 400, color = 'rgb
|
|
|
215
243
|
: [],
|
|
216
244
|
},
|
|
217
245
|
], [layerWidth, lineHeight, isAnimated, transformOriginOffset, scaleY]);
|
|
218
|
-
return (_jsxs(View, { style: [styles.container, style], onLayout: onLayout, testID: testID, accessibilityLabel: accessibilityLabel, accessible: accessible, children: [children, active &&
|
|
246
|
+
return (_jsxs(View, { style: [styles.container, style], onLayout: onLayout, testID: testID, accessibilityLabel: accessibilityLabel, accessible: accessible, children: [children, active &&
|
|
247
|
+
!reduceMotionEnabled &&
|
|
248
|
+
containerWidth > 0 &&
|
|
249
|
+
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: segments.map((segment, vIndex) => (_jsx(View, { style: [
|
|
219
250
|
styles.segment,
|
|
220
251
|
{
|
|
221
252
|
height: lineHeight * segment.heightRatio,
|
|
@@ -249,5 +280,41 @@ const styles = StyleSheet.create({
|
|
|
249
280
|
},
|
|
250
281
|
});
|
|
251
282
|
const ForwardedGlitter = forwardRef(GlitterComponent);
|
|
283
|
+
/**
|
|
284
|
+
* A beautiful shimmer/glitter effect component for React Native.
|
|
285
|
+
* Wrap any component to add a sparkling diagonal shine animation.
|
|
286
|
+
*
|
|
287
|
+
* @example
|
|
288
|
+
* ```tsx
|
|
289
|
+
* // Basic usage
|
|
290
|
+
* <Glitter>
|
|
291
|
+
* <View style={styles.card}>
|
|
292
|
+
* <Text>This content will shimmer!</Text>
|
|
293
|
+
* </View>
|
|
294
|
+
* </Glitter>
|
|
295
|
+
*
|
|
296
|
+
* // With custom options
|
|
297
|
+
* <Glitter
|
|
298
|
+
* duration={2000}
|
|
299
|
+
* color="rgba(255, 215, 0, 0.5)"
|
|
300
|
+
* mode="expand"
|
|
301
|
+
* direction="right-to-left"
|
|
302
|
+
* >
|
|
303
|
+
* <View style={styles.premiumButton} />
|
|
304
|
+
* </Glitter>
|
|
305
|
+
*
|
|
306
|
+
* // With ref for programmatic control
|
|
307
|
+
* const glitterRef = useRef<GlitterRef>(null);
|
|
308
|
+
* <Glitter ref={glitterRef} active={false}>
|
|
309
|
+
* <View style={styles.box} />
|
|
310
|
+
* </Glitter>
|
|
311
|
+
* // Later: glitterRef.current?.start();
|
|
312
|
+
* ```
|
|
313
|
+
*
|
|
314
|
+
* @see {@link GlitterProps} for available props
|
|
315
|
+
* @see {@link GlitterRef} for ref methods
|
|
316
|
+
*/
|
|
252
317
|
export const Glitter = memo(ForwardedGlitter);
|
|
318
|
+
// Set display name for React DevTools
|
|
319
|
+
Glitter.displayName = 'Glitter';
|
|
253
320
|
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.6",
|
|
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(() => {
|
|
@@ -390,32 +592,35 @@ function GlitterComponent(
|
|
|
390
592
|
accessible={accessible}
|
|
391
593
|
>
|
|
392
594
|
{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
|
-
|
|
595
|
+
{active &&
|
|
596
|
+
!reduceMotionEnabled &&
|
|
597
|
+
containerWidth > 0 &&
|
|
598
|
+
containerHeight > 0 && (
|
|
599
|
+
<Animated.View style={shimmerContainerStyle} pointerEvents="none">
|
|
600
|
+
<View style={rotationWrapperStyle}>
|
|
601
|
+
{shimmerLayers.map((layer, layerIndex) => (
|
|
602
|
+
<Animated.View
|
|
603
|
+
key={layerIndex}
|
|
604
|
+
style={getShimmerLineStyle(layer.position)}
|
|
605
|
+
>
|
|
606
|
+
{segments.map((segment, vIndex) => (
|
|
607
|
+
<View
|
|
608
|
+
key={vIndex}
|
|
609
|
+
style={[
|
|
610
|
+
styles.segment,
|
|
611
|
+
{
|
|
612
|
+
height: lineHeight * segment.heightRatio,
|
|
613
|
+
backgroundColor: color,
|
|
614
|
+
opacity: layer.opacity * segment.opacity,
|
|
615
|
+
},
|
|
616
|
+
]}
|
|
617
|
+
/>
|
|
618
|
+
))}
|
|
619
|
+
</Animated.View>
|
|
620
|
+
))}
|
|
621
|
+
</View>
|
|
622
|
+
</Animated.View>
|
|
623
|
+
)}
|
|
419
624
|
</View>
|
|
420
625
|
);
|
|
421
626
|
}
|
|
@@ -446,6 +651,44 @@ const styles = StyleSheet.create({
|
|
|
446
651
|
});
|
|
447
652
|
|
|
448
653
|
const ForwardedGlitter = forwardRef(GlitterComponent);
|
|
654
|
+
|
|
655
|
+
/**
|
|
656
|
+
* A beautiful shimmer/glitter effect component for React Native.
|
|
657
|
+
* Wrap any component to add a sparkling diagonal shine animation.
|
|
658
|
+
*
|
|
659
|
+
* @example
|
|
660
|
+
* ```tsx
|
|
661
|
+
* // Basic usage
|
|
662
|
+
* <Glitter>
|
|
663
|
+
* <View style={styles.card}>
|
|
664
|
+
* <Text>This content will shimmer!</Text>
|
|
665
|
+
* </View>
|
|
666
|
+
* </Glitter>
|
|
667
|
+
*
|
|
668
|
+
* // With custom options
|
|
669
|
+
* <Glitter
|
|
670
|
+
* duration={2000}
|
|
671
|
+
* color="rgba(255, 215, 0, 0.5)"
|
|
672
|
+
* mode="expand"
|
|
673
|
+
* direction="right-to-left"
|
|
674
|
+
* >
|
|
675
|
+
* <View style={styles.premiumButton} />
|
|
676
|
+
* </Glitter>
|
|
677
|
+
*
|
|
678
|
+
* // With ref for programmatic control
|
|
679
|
+
* const glitterRef = useRef<GlitterRef>(null);
|
|
680
|
+
* <Glitter ref={glitterRef} active={false}>
|
|
681
|
+
* <View style={styles.box} />
|
|
682
|
+
* </Glitter>
|
|
683
|
+
* // Later: glitterRef.current?.start();
|
|
684
|
+
* ```
|
|
685
|
+
*
|
|
686
|
+
* @see {@link GlitterProps} for available props
|
|
687
|
+
* @see {@link GlitterRef} for ref methods
|
|
688
|
+
*/
|
|
449
689
|
export const Glitter = memo(ForwardedGlitter);
|
|
450
690
|
|
|
691
|
+
// Set display name for React DevTools
|
|
692
|
+
Glitter.displayName = 'Glitter';
|
|
693
|
+
|
|
451
694
|
export default Glitter;
|