react-native-marquee-animation 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +174 -0
  3. package/RNMarquee.podspec +27 -0
  4. package/THIRD_PARTY_NOTICES.md +8 -0
  5. package/android/build.gradle +58 -0
  6. package/android/settings.gradle +34 -0
  7. package/android/src/main/AndroidManifest.xml +1 -0
  8. package/android/src/main/java/com/marquee/MarqueeEvent.kt +44 -0
  9. package/android/src/main/java/com/marquee/MarqueeModels.kt +45 -0
  10. package/android/src/main/java/com/marquee/MarqueeMotion.kt +163 -0
  11. package/android/src/main/java/com/marquee/MarqueePackage.kt +16 -0
  12. package/android/src/main/java/com/marquee/MarqueeView.kt +376 -0
  13. package/android/src/main/java/com/marquee/MarqueeViewManager.kt +135 -0
  14. package/ios/MarqueeRenderer.swift +652 -0
  15. package/ios/RNMarqueeView.h +14 -0
  16. package/ios/RNMarqueeView.mm +118 -0
  17. package/lib/module/RNMarqueeViewNativeComponent.ts +32 -0
  18. package/lib/module/index.js +4 -0
  19. package/lib/module/index.js.map +1 -0
  20. package/lib/module/internal/events.js +10 -0
  21. package/lib/module/internal/events.js.map +1 -0
  22. package/lib/module/internal/geometry.js +10 -0
  23. package/lib/module/internal/geometry.js.map +1 -0
  24. package/lib/module/internal/options.js +29 -0
  25. package/lib/module/internal/options.js.map +1 -0
  26. package/lib/module/marquee.js +32 -0
  27. package/lib/module/marquee.js.map +1 -0
  28. package/lib/module/marquee.native.js +95 -0
  29. package/lib/module/marquee.native.js.map +1 -0
  30. package/lib/module/package.json +1 -0
  31. package/lib/module/types.js +4 -0
  32. package/lib/module/types.js.map +1 -0
  33. package/lib/module/use-reduced-motion.js +61 -0
  34. package/lib/module/use-reduced-motion.js.map +1 -0
  35. package/lib/module/view-props.js +5 -0
  36. package/lib/module/view-props.js.map +1 -0
  37. package/lib/typescript/package.json +1 -0
  38. package/lib/typescript/src/RNMarqueeViewNativeComponent.d.ts +28 -0
  39. package/lib/typescript/src/RNMarqueeViewNativeComponent.d.ts.map +1 -0
  40. package/lib/typescript/src/index.d.ts +3 -0
  41. package/lib/typescript/src/index.d.ts.map +1 -0
  42. package/lib/typescript/src/internal/events.d.ts +3 -0
  43. package/lib/typescript/src/internal/events.d.ts.map +1 -0
  44. package/lib/typescript/src/internal/geometry.d.ts +3 -0
  45. package/lib/typescript/src/internal/geometry.d.ts.map +1 -0
  46. package/lib/typescript/src/internal/options.d.ts +16 -0
  47. package/lib/typescript/src/internal/options.d.ts.map +1 -0
  48. package/lib/typescript/src/marquee.d.ts +469 -0
  49. package/lib/typescript/src/marquee.d.ts.map +1 -0
  50. package/lib/typescript/src/marquee.native.d.ts +470 -0
  51. package/lib/typescript/src/marquee.native.d.ts.map +1 -0
  52. package/lib/typescript/src/types.d.ts +46 -0
  53. package/lib/typescript/src/types.d.ts.map +1 -0
  54. package/lib/typescript/src/use-reduced-motion.d.ts +3 -0
  55. package/lib/typescript/src/use-reduced-motion.d.ts.map +1 -0
  56. package/lib/typescript/src/view-props.d.ts +4 -0
  57. package/lib/typescript/src/view-props.d.ts.map +1 -0
  58. package/package.json +166 -0
  59. package/src/RNMarqueeViewNativeComponent.ts +32 -0
  60. package/src/index.ts +12 -0
  61. package/src/internal/events.ts +12 -0
  62. package/src/internal/geometry.ts +14 -0
  63. package/src/internal/options.ts +48 -0
  64. package/src/marquee.native.tsx +144 -0
  65. package/src/marquee.tsx +35 -0
  66. package/src/types.ts +59 -0
  67. package/src/use-reduced-motion.ts +88 -0
  68. package/src/view-props.ts +24 -0
package/package.json ADDED
@@ -0,0 +1,166 @@
1
+ {
2
+ "name": "react-native-marquee-animation",
3
+ "version": "0.1.0",
4
+ "description": "Production-grade generic native marquee for React Native",
5
+ "main": "./lib/module/index.js",
6
+ "react-native": "./src/index.ts",
7
+ "types": "./lib/typescript/src/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "react-native-marquee-animation-source": "./src/index.ts",
11
+ "react-native": "./src/index.ts",
12
+ "types": "./lib/typescript/src/index.d.ts",
13
+ "default": "./lib/module/index.js"
14
+ },
15
+ "./package.json": "./package.json"
16
+ },
17
+ "files": [
18
+ "src",
19
+ "lib",
20
+ "android",
21
+ "ios",
22
+ "*.podspec",
23
+ "README.md",
24
+ "LICENSE",
25
+ "THIRD_PARTY_NOTICES.md",
26
+ "!ios/build",
27
+ "!ios/tests",
28
+ "!android/build",
29
+ "!android/gradle",
30
+ "!android/gradlew",
31
+ "!android/gradlew.bat",
32
+ "!android/local.properties",
33
+ "!android/src/test",
34
+ "!android/src/androidTest",
35
+ "!**/__tests__",
36
+ "!**/__fixtures__",
37
+ "!**/__mocks__",
38
+ "!**/.*"
39
+ ],
40
+ "scripts": {
41
+ "example": "bun --filter react-native-marquee-example",
42
+ "build": "bob build",
43
+ "clean": "del-cli lib",
44
+ "format": "prettier --write .",
45
+ "format:check": "prettier --check .",
46
+ "prepack": "bun run clean && bun run build",
47
+ "test": "bun test",
48
+ "test:android": "cd example/android && ./gradlew :react-native-marquee-animation:testDebugUnitTest",
49
+ "typecheck": "tsc --noEmit",
50
+ "lint": "eslint \"**/*.{js,mjs,ts,tsx}\"",
51
+ "check": "bun run format:check && bun run lint && bun run typecheck && bun run test && bun run build",
52
+ "release": "release-it",
53
+ "release:next": "release-it --preRelease=next"
54
+ },
55
+ "keywords": [
56
+ "react-native",
57
+ "react-native-component",
58
+ "marquee",
59
+ "ticker",
60
+ "continuous-scroll",
61
+ "native-animation",
62
+ "fabric",
63
+ "new-architecture",
64
+ "expo",
65
+ "ios",
66
+ "android"
67
+ ],
68
+ "repository": {
69
+ "type": "git",
70
+ "url": "git+https://github.com/invivek26/react-native-marquee-animation.git"
71
+ },
72
+ "author": "Vivek Indlebele Narasimha Prasad (https://github.com/invivek26)",
73
+ "license": "MIT",
74
+ "bugs": {
75
+ "url": "https://github.com/invivek26/react-native-marquee-animation/issues"
76
+ },
77
+ "homepage": "https://github.com/invivek26/react-native-marquee-animation#readme",
78
+ "publishConfig": {
79
+ "access": "public",
80
+ "provenance": true,
81
+ "registry": "https://registry.npmjs.org/"
82
+ },
83
+ "devDependencies": {
84
+ "@babel/eslint-parser": "^7.25.1",
85
+ "@eslint/compat": "^2.1.0",
86
+ "@eslint/eslintrc": "^3.3.5",
87
+ "@eslint/js": "^10.0.1",
88
+ "@react-native/babel-preset": "0.85.0",
89
+ "@react-native/eslint-config": "0.85.0",
90
+ "@react-native/eslint-plugin": "0.85.0",
91
+ "@typescript-eslint/eslint-plugin": "^8.36.0",
92
+ "@typescript-eslint/parser": "^8.36.0",
93
+ "@types/bun": "^1.3.11",
94
+ "@types/react": "^19.2.0",
95
+ "@types/react-dom": "^19.2.4",
96
+ "del-cli": "^7.0.0",
97
+ "eslint": "^9.39.4",
98
+ "eslint-config-prettier": "^10.1.8",
99
+ "eslint-plugin-eslint-comments": "^3.2.0",
100
+ "eslint-plugin-ft-flow": "^3.0.11",
101
+ "eslint-plugin-jest": "^29.0.1",
102
+ "eslint-plugin-prettier": "^5.5.6",
103
+ "eslint-plugin-react": "^7.37.5",
104
+ "eslint-plugin-react-hooks": "^7.0.1",
105
+ "eslint-plugin-react-native": "^5.0.0",
106
+ "prettier": "^3.8.3",
107
+ "react": "19.2.3",
108
+ "react-dom": "19.2.3",
109
+ "react-native": "0.85.3",
110
+ "react-native-builder-bob": "^0.43.0",
111
+ "release-it": "^19.2.4",
112
+ "turbo": "^2.9.16",
113
+ "typescript": "^6.0.3"
114
+ },
115
+ "peerDependencies": {
116
+ "react": ">=19.2.0 <20.0.0",
117
+ "react-native": ">=0.85.0 <0.86.0"
118
+ },
119
+ "workspaces": [
120
+ "example"
121
+ ],
122
+ "packageManager": "bun@1.3.11",
123
+ "engines": {
124
+ "node": ">=20.19.4"
125
+ },
126
+ "react-native-builder-bob": {
127
+ "source": "src",
128
+ "output": "lib",
129
+ "targets": [
130
+ [
131
+ "module",
132
+ {
133
+ "esm": true
134
+ }
135
+ ],
136
+ [
137
+ "typescript",
138
+ {
139
+ "project": "tsconfig.build.json"
140
+ }
141
+ ]
142
+ ]
143
+ },
144
+ "codegenConfig": {
145
+ "name": "RNMarqueeViewSpec",
146
+ "type": "components",
147
+ "jsSrcsDir": "src",
148
+ "android": {
149
+ "javaPackageName": "com.marquee"
150
+ },
151
+ "ios": {
152
+ "components": {
153
+ "RNMarqueeView": {
154
+ "className": "RNMarqueeView"
155
+ }
156
+ }
157
+ }
158
+ },
159
+ "prettier": {
160
+ "quoteProps": "consistent",
161
+ "singleQuote": true,
162
+ "tabWidth": 2,
163
+ "trailingComma": "es5",
164
+ "useTabs": false
165
+ }
166
+ }
@@ -0,0 +1,32 @@
1
+ import type { HostComponent, ViewProps } from 'react-native';
2
+ import { codegenNativeComponent, type CodegenTypes } from 'react-native';
3
+
4
+ type NativeAnimationStateEvent = Readonly<{
5
+ state: string;
6
+ }>;
7
+
8
+ type NativeContentLayoutEvent = Readonly<{
9
+ contentWidth: CodegenTypes.Float;
10
+ containerWidth: CodegenTypes.Float;
11
+ }>;
12
+
13
+ export interface NativeMarqueeViewProps extends ViewProps {
14
+ active: boolean;
15
+ reduceMotion: boolean;
16
+ speed: CodegenTypes.Double;
17
+ direction: string;
18
+ shortContentMode: string;
19
+ contentAlignment: string;
20
+ contentWidth: CodegenTypes.Float;
21
+ spacing: CodegenTypes.Float;
22
+ maxFlingVelocity: CodegenTypes.Float;
23
+ deceleration: CodegenTypes.Float;
24
+ pauseOnPress: boolean;
25
+ resumeDelayMs: CodegenTypes.Double;
26
+ onAnimationStateChange?: CodegenTypes.DirectEventHandler<NativeAnimationStateEvent>;
27
+ onContentLayout?: CodegenTypes.DirectEventHandler<NativeContentLayoutEvent>;
28
+ }
29
+
30
+ export default codegenNativeComponent<NativeMarqueeViewProps>(
31
+ 'RNMarqueeView'
32
+ ) as HostComponent<NativeMarqueeViewProps>;
package/src/index.ts ADDED
@@ -0,0 +1,12 @@
1
+ export { Marquee } from './marquee';
2
+ export type {
3
+ MarqueeAnimationState,
4
+ MarqueeAnimationStateEvent,
5
+ MarqueeContentAlignment,
6
+ MarqueeContentLayoutEvent,
7
+ MarqueeDirection,
8
+ MarqueeInteraction,
9
+ MarqueeProps,
10
+ MarqueeReduceMotion,
11
+ MarqueeShortContentMode,
12
+ } from './types';
@@ -0,0 +1,12 @@
1
+ import type { MarqueeAnimationState } from '../types';
2
+
3
+ const STATE_BY_NATIVE_VALUE: Readonly<Record<string, MarqueeAnimationState>> = {
4
+ idle: 'idle',
5
+ paused: 'paused',
6
+ reducedMotion: 'reduced-motion',
7
+ running: 'running',
8
+ };
9
+
10
+ export const resolveAnimationState = (
11
+ nativeState: string
12
+ ): MarqueeAnimationState | null => STATE_BY_NATIVE_VALUE[nativeState] ?? null;
@@ -0,0 +1,14 @@
1
+ export const CONTENT_WIDTH_EPSILON = 0.25;
2
+
3
+ export const resolveMeasuredContentWidth = (
4
+ currentWidth: number,
5
+ measuredWidth: number
6
+ ): number => {
7
+ if (!Number.isFinite(measuredWidth) || measuredWidth < 0) {
8
+ return currentWidth;
9
+ }
10
+
11
+ return Math.abs(currentWidth - measuredWidth) > CONTENT_WIDTH_EPSILON
12
+ ? measuredWidth
13
+ : currentWidth;
14
+ };
@@ -0,0 +1,48 @@
1
+ import type { MarqueeInteraction } from '../types';
2
+
3
+ export const DEFAULT_INTERACTION = {
4
+ deceleration: 0.9985,
5
+ maxFlingVelocity: 3_200,
6
+ pauseOnPress: true,
7
+ resumeDelay: 0,
8
+ } as const;
9
+
10
+ const requireNonnegative = (value: number, name: string): number => {
11
+ if (!Number.isFinite(value) || value < 0) {
12
+ throw new RangeError(`${name} must be a finite nonnegative number.`);
13
+ }
14
+ return value;
15
+ };
16
+
17
+ export const resolveInteraction = (interaction?: MarqueeInteraction) => {
18
+ const deceleration =
19
+ interaction?.deceleration ?? DEFAULT_INTERACTION.deceleration;
20
+ if (
21
+ !Number.isFinite(deceleration) ||
22
+ deceleration <= 0 ||
23
+ deceleration >= 1
24
+ ) {
25
+ throw new RangeError(
26
+ 'interaction.deceleration must be greater than 0 and below 1.'
27
+ );
28
+ }
29
+
30
+ return {
31
+ deceleration,
32
+ maxFlingVelocity: requireNonnegative(
33
+ interaction?.maxFlingVelocity ?? DEFAULT_INTERACTION.maxFlingVelocity,
34
+ 'interaction.maxFlingVelocity'
35
+ ),
36
+ pauseOnPress: interaction?.pauseOnPress ?? DEFAULT_INTERACTION.pauseOnPress,
37
+ resumeDelay: requireNonnegative(
38
+ interaction?.resumeDelay ?? DEFAULT_INTERACTION.resumeDelay,
39
+ 'interaction.resumeDelay'
40
+ ),
41
+ };
42
+ };
43
+
44
+ export const resolveSpeed = (speed: number): number =>
45
+ requireNonnegative(speed, 'speed');
46
+
47
+ export const resolveSpacing = (spacing: number): number =>
48
+ requireNonnegative(spacing, 'spacing');
@@ -0,0 +1,144 @@
1
+ import {
2
+ forwardRef,
3
+ useCallback,
4
+ useMemo,
5
+ useState,
6
+ type ComponentRef,
7
+ } from 'react';
8
+ import {
9
+ StyleSheet,
10
+ View,
11
+ type LayoutChangeEvent,
12
+ type NativeSyntheticEvent,
13
+ } from 'react-native';
14
+
15
+ import NativeMarqueeView from './RNMarqueeViewNativeComponent';
16
+ import { resolveAnimationState } from './internal/events';
17
+ import { resolveMeasuredContentWidth } from './internal/geometry';
18
+ import {
19
+ resolveInteraction,
20
+ resolveSpacing,
21
+ resolveSpeed,
22
+ } from './internal/options';
23
+ import type { MarqueeProps } from './types';
24
+ import { useMarqueeReducedMotion } from './use-reduced-motion';
25
+
26
+ type NativeAnimationStateEvent = NativeSyntheticEvent<
27
+ Readonly<{ state: string }>
28
+ >;
29
+ type NativeContentLayoutEvent = NativeSyntheticEvent<
30
+ Readonly<{ containerWidth: number; contentWidth: number }>
31
+ >;
32
+
33
+ const NATIVE_SHORT_CONTENT_MODES = {
34
+ repeat: 'repeat',
35
+ static: 'static',
36
+ } as const;
37
+
38
+ export const Marquee = forwardRef<
39
+ ComponentRef<typeof NativeMarqueeView>,
40
+ MarqueeProps
41
+ >((props, ref) => {
42
+ const {
43
+ accessibilityLabel,
44
+ active = true,
45
+ children,
46
+ contentAlignment = 'start',
47
+ contentContainerStyle,
48
+ direction = 'left',
49
+ interaction,
50
+ onAnimationStateChange,
51
+ onContentLayout,
52
+ reduceMotion: reduceMotionPolicy = 'system',
53
+ shortContentMode = 'static',
54
+ spacing: spacingProp = 0,
55
+ speed: speedProp = 25,
56
+ style,
57
+ ...viewProps
58
+ } = props;
59
+ const [contentWidth, setContentWidth] = useState(0);
60
+ const reduceMotion = useMarqueeReducedMotion(reduceMotionPolicy);
61
+ const resolvedInteraction = useMemo(
62
+ () => resolveInteraction(interaction),
63
+ [interaction]
64
+ );
65
+ const speed = resolveSpeed(speedProp);
66
+ const spacing = resolveSpacing(spacingProp);
67
+
68
+ const handleContentContainerLayout = useCallback(
69
+ (event: LayoutChangeEvent) => {
70
+ const nextWidth = event.nativeEvent.layout.width;
71
+ setContentWidth((currentWidth) =>
72
+ resolveMeasuredContentWidth(currentWidth, nextWidth)
73
+ );
74
+ },
75
+ []
76
+ );
77
+
78
+ const handleAnimationStateChange = useCallback(
79
+ (event: NativeAnimationStateEvent) => {
80
+ const state = resolveAnimationState(event.nativeEvent.state);
81
+ if (state) {
82
+ onAnimationStateChange?.({ state });
83
+ }
84
+ },
85
+ [onAnimationStateChange]
86
+ );
87
+
88
+ const handleContentLayout = useCallback(
89
+ (event: NativeContentLayoutEvent) => {
90
+ onContentLayout?.(event.nativeEvent);
91
+ },
92
+ [onContentLayout]
93
+ );
94
+
95
+ return (
96
+ <NativeMarqueeView
97
+ {...viewProps}
98
+ accessibilityLabel={accessibilityLabel}
99
+ accessibilityLiveRegion="none"
100
+ accessibilityRole={viewProps.accessibilityRole ?? 'text'}
101
+ accessible
102
+ active={active}
103
+ contentAlignment={contentAlignment}
104
+ contentWidth={contentWidth}
105
+ deceleration={resolvedInteraction.deceleration}
106
+ direction={direction}
107
+ importantForAccessibility="yes"
108
+ maxFlingVelocity={resolvedInteraction.maxFlingVelocity}
109
+ onAnimationStateChange={
110
+ onAnimationStateChange ? handleAnimationStateChange : undefined
111
+ }
112
+ onContentLayout={onContentLayout ? handleContentLayout : undefined}
113
+ pauseOnPress={resolvedInteraction.pauseOnPress}
114
+ reduceMotion={reduceMotion}
115
+ ref={ref}
116
+ resumeDelayMs={resolvedInteraction.resumeDelay}
117
+ shortContentMode={NATIVE_SHORT_CONTENT_MODES[shortContentMode]}
118
+ spacing={spacing}
119
+ speed={speed}
120
+ style={style}
121
+ >
122
+ <View
123
+ accessibilityElementsHidden
124
+ collapsable={false}
125
+ importantForAccessibility="no-hide-descendants"
126
+ onLayout={handleContentContainerLayout}
127
+ pointerEvents="none"
128
+ style={[styles.content, contentContainerStyle]}
129
+ >
130
+ {children}
131
+ </View>
132
+ </NativeMarqueeView>
133
+ );
134
+ });
135
+
136
+ Marquee.displayName = 'Marquee';
137
+
138
+ const styles = StyleSheet.create({
139
+ content: {
140
+ alignSelf: 'flex-start',
141
+ flexDirection: 'row',
142
+ flexShrink: 0,
143
+ },
144
+ });
@@ -0,0 +1,35 @@
1
+ import { forwardRef, type ComponentRef } from 'react';
2
+ import { View } from 'react-native';
3
+
4
+ import type { MarqueeProps } from './types';
5
+ import { getViewProps } from './view-props';
6
+
7
+ export const Marquee = forwardRef<ComponentRef<typeof View>, MarqueeProps>(
8
+ (props, ref) => {
9
+ const { accessibilityLabel, children, contentContainerStyle, style } =
10
+ props;
11
+ const viewProps = getViewProps(props);
12
+
13
+ return (
14
+ <View
15
+ {...viewProps}
16
+ accessibilityLabel={accessibilityLabel}
17
+ accessibilityRole={viewProps.accessibilityRole ?? 'text'}
18
+ accessible
19
+ ref={ref}
20
+ style={style}
21
+ >
22
+ <View
23
+ accessibilityElementsHidden
24
+ importantForAccessibility="no-hide-descendants"
25
+ pointerEvents="none"
26
+ style={contentContainerStyle}
27
+ >
28
+ {children}
29
+ </View>
30
+ </View>
31
+ );
32
+ }
33
+ );
34
+
35
+ Marquee.displayName = 'Marquee';
package/src/types.ts ADDED
@@ -0,0 +1,59 @@
1
+ import type { ReactNode } from 'react';
2
+ import type { StyleProp, ViewProps, ViewStyle } from 'react-native';
3
+
4
+ export type MarqueeDirection = 'left' | 'right';
5
+ export type MarqueeReduceMotion = 'system' | 'always' | 'never';
6
+ export type MarqueeShortContentMode = 'static' | 'repeat';
7
+ export type MarqueeContentAlignment = 'start' | 'center' | 'end';
8
+ export type MarqueeAnimationState =
9
+ 'idle' | 'running' | 'paused' | 'reduced-motion';
10
+
11
+ export type MarqueeAnimationStateEvent = Readonly<{
12
+ state: MarqueeAnimationState;
13
+ }>;
14
+
15
+ export type MarqueeContentLayoutEvent = Readonly<{
16
+ containerWidth: number;
17
+ contentWidth: number;
18
+ }>;
19
+
20
+ export type MarqueeInteraction = Readonly<{
21
+ maxFlingVelocity?: number;
22
+ /** Unitless velocity retention per millisecond, greater than 0 and below 1. */
23
+ deceleration?: number;
24
+ pauseOnPress?: boolean;
25
+ /** Delay in milliseconds before automatic motion resumes. */
26
+ resumeDelay?: number;
27
+ }>;
28
+
29
+ type InheritedViewProps = Omit<
30
+ ViewProps,
31
+ | 'accessibilityElementsHidden'
32
+ | 'accessibilityLabel'
33
+ | 'children'
34
+ | 'importantForAccessibility'
35
+ | 'style'
36
+ >;
37
+
38
+ export type MarqueeProps = InheritedViewProps &
39
+ Readonly<{
40
+ /** One noninteractive visual strip. React state is mounted exactly once. */
41
+ children: ReactNode;
42
+ /** A single semantic description for all visually repeated content. */
43
+ accessibilityLabel: string;
44
+ /** Keeps native motion active. Attachment and visibility are also enforced natively. */
45
+ active?: boolean;
46
+ contentAlignment?: MarqueeContentAlignment;
47
+ contentContainerStyle?: StyleProp<ViewStyle>;
48
+ direction?: MarqueeDirection;
49
+ interaction?: MarqueeInteraction;
50
+ onAnimationStateChange?: (event: MarqueeAnimationStateEvent) => void;
51
+ onContentLayout?: (event: MarqueeContentLayoutEvent) => void;
52
+ reduceMotion?: MarqueeReduceMotion;
53
+ shortContentMode?: MarqueeShortContentMode;
54
+ /** Empty distance between the end of one visual copy and the next. */
55
+ spacing?: number;
56
+ /** Constant native motion in logical pixels per second. */
57
+ speed?: number;
58
+ style?: StyleProp<ViewStyle>;
59
+ }>;
@@ -0,0 +1,88 @@
1
+ import { useSyncExternalStore } from 'react';
2
+ import { AccessibilityInfo } from 'react-native';
3
+ import type { MarqueeReduceMotion } from './types';
4
+
5
+ type Listener = () => void;
6
+
7
+ const listeners = new Set<Listener>();
8
+ let reduceMotion = true;
9
+ let listening = false;
10
+ let generation = 0;
11
+ let subscription: ReturnType<typeof AccessibilityInfo.addEventListener> | null =
12
+ null;
13
+
14
+ const notify = (): void => {
15
+ listeners.forEach((listener) => listener());
16
+ };
17
+
18
+ const setReduceMotion = (value: boolean): void => {
19
+ if (reduceMotion === value) {
20
+ return;
21
+ }
22
+ reduceMotion = value;
23
+ notify();
24
+ };
25
+
26
+ const startListening = (): void => {
27
+ if (listening) {
28
+ return;
29
+ }
30
+
31
+ listening = true;
32
+ generation += 1;
33
+ const currentGeneration = generation;
34
+ AccessibilityInfo.isReduceMotionEnabled()
35
+ .then((value) => {
36
+ if (listening && currentGeneration === generation) {
37
+ setReduceMotion(value);
38
+ }
39
+ })
40
+ .catch(() => undefined);
41
+ subscription = AccessibilityInfo.addEventListener(
42
+ 'reduceMotionChanged',
43
+ (value) => {
44
+ if (listening && currentGeneration === generation) {
45
+ setReduceMotion(value);
46
+ }
47
+ }
48
+ );
49
+ };
50
+
51
+ const subscribe = (listener: Listener): (() => void) => {
52
+ listeners.add(listener);
53
+ startListening();
54
+
55
+ return () => {
56
+ listeners.delete(listener);
57
+ if (listeners.size !== 0) {
58
+ return;
59
+ }
60
+
61
+ subscription?.remove();
62
+ subscription = null;
63
+ listening = false;
64
+ generation += 1;
65
+ reduceMotion = true;
66
+ };
67
+ };
68
+
69
+ const emptySubscribe = (): (() => void) => () => undefined;
70
+ const getSystemSnapshot = (): boolean => (listening ? reduceMotion : true);
71
+ const getReducedSnapshot = (): boolean => true;
72
+ const getUnreducedSnapshot = (): boolean => false;
73
+
74
+ export const useMarqueeReducedMotion = (
75
+ policy: MarqueeReduceMotion
76
+ ): boolean => {
77
+ const system = policy === 'system';
78
+
79
+ return useSyncExternalStore(
80
+ system ? subscribe : emptySubscribe,
81
+ system
82
+ ? getSystemSnapshot
83
+ : policy === 'always'
84
+ ? getReducedSnapshot
85
+ : getUnreducedSnapshot,
86
+ policy === 'never' ? getUnreducedSnapshot : getReducedSnapshot
87
+ );
88
+ };
@@ -0,0 +1,24 @@
1
+ import type { ViewProps } from 'react-native';
2
+ import type { MarqueeProps } from './types';
3
+
4
+ const COMPONENT_PROP_KEYS = new Set([
5
+ 'accessibilityLabel',
6
+ 'active',
7
+ 'children',
8
+ 'contentAlignment',
9
+ 'contentContainerStyle',
10
+ 'direction',
11
+ 'interaction',
12
+ 'onAnimationStateChange',
13
+ 'onContentLayout',
14
+ 'reduceMotion',
15
+ 'shortContentMode',
16
+ 'spacing',
17
+ 'speed',
18
+ 'style',
19
+ ]);
20
+
21
+ export const getViewProps = (props: MarqueeProps): ViewProps =>
22
+ Object.fromEntries(
23
+ Object.entries(props).filter(([key]) => !COMPONENT_PROP_KEYS.has(key))
24
+ );