react-sway 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,109 @@
1
+ # react-sway
2
+
3
+ A React component for smooth, infinite, and interactive content scrolling. It duplicates content to create a seamless looping effect, controllable via touch, mouse drag, wheel, and keyboard.
4
+
5
+ ## What is `react-sway`?
6
+
7
+ React Sway takes your list of items and makes them scroll endlessly. It's designed to be easy to use and performant, with auto-scrolling that pauses when users interact.
8
+
9
+ It works by duplicating your content to create a seamless loop and uses CSS transforms for smooth animation. The duplicated content is wrapped in `<aside>` elements with `aria-hidden="true"` and `role="presentation"` to ensure good accessibility and helps search engines understand the content structure.
10
+
11
+ ### Core Features
12
+
13
+ * **Smooth Infinite Scroll:** Content loops continuously.
14
+ * **Auto-Scroll:** Scrolls automatically, with configurable speed and direction.
15
+ * **User Friendly Interactions:**
16
+ * Click and drag to scroll.
17
+ * Swipe on touch devices.
18
+ * Mouse wheel support with velocity capping.
19
+ * Keyboard controls: Spacebar to pause/resume, ArrowUp/ArrowDown to scroll, Home/End to jump.
20
+ * **Responsive:** Adjusts to window resizing with debounced recalculation.
21
+ * **Lazy Visibility Detection:** Add a `content-item` class to your child elements, and `react-sway` automatically uses an IntersectionObserver to add a `.visible` class when they enter the viewport. Useful for triggering CSS animations or deferred rendering. Configurable via `lazy`, `lazyRootMargin`, and `lazyThreshold` props.
22
+ * **Reduced Motion Support:** Respects `prefers-reduced-motion: reduce` by lowering auto-scroll speed and disabling momentum effects.
23
+
24
+ ## Installation
25
+
26
+ ```bash
27
+ npm install react-sway
28
+ # or
29
+ yarn add react-sway
30
+ # or
31
+ pnpm add react-sway
32
+ ```
33
+
34
+ ## Usage
35
+
36
+ ```tsx
37
+ import { ReactSway } from 'react-sway';
38
+ import './index.css'; // Your global styles
39
+
40
+ function SwayUsageExample() {
41
+ return (
42
+ <div className="scroller-container"> {/* Style this container to define ReactSway's area (e.g., full-page, or a specific height) */}
43
+ <ReactSway>
44
+ <div className="content-item" style={{ background: 'linear-gradient(135deg, #667eea 0%, #764ba2 100%)' }}>
45
+ <h2>Seamless Scrolling</h2>
46
+ <p>Experience buttery smooth infinite scrolling with no stutters or pauses.</p>
47
+ </div>
48
+
49
+ <div className="content-item" style={{ background: 'linear-gradient(135deg, #f093fb 0%, #f5576c 100%)' }}>
50
+ <h2>Touch & Mouse Support</h2>
51
+ <p>Interact naturally with touch gestures, mouse wheel, or click-and-drag.</p>
52
+ </div>
53
+
54
+ <div className="content-item" style={{ background: 'linear-gradient(135deg, #4facfe 0%, #00f2fe 100%)' }}>
55
+ <h2>Momentum Scrolling</h2>
56
+ <p>Flick to scroll with realistic physics-based momentum and friction.</p>
57
+ </div>
58
+
59
+ <div className="content-item" style={{ background: 'linear-gradient(135deg, #43e97b 0%, #38f9d7 100%)' }}>
60
+ <h2>Performance Optimized</h2>
61
+ <p>Using requestAnimationFrame for 60+ FPS scrolling on all devices.</p>
62
+ </div>
63
+
64
+ <div className="content-item" style={{ background: 'linear-gradient(135deg, #667eea 0%, #764ba2 100%)' }}>
65
+ <h2>Responsive Design</h2>
66
+ <p>Adapts perfectly to any screen size and device orientation.</p>
67
+ </div>
68
+
69
+ <div className="content-item" style={{ background: 'linear-gradient(135deg, #f093fb 0%, #f5576c 100%)' }}>
70
+ <h2>No Native Scroll</h2>
71
+ <p>Custom implementation avoids native scroll jank and inconsistencies.</p>
72
+ </div>
73
+
74
+ <div className="content-item" style={{ background: 'linear-gradient(135deg, #4facfe 0%, #00f2fe 100%)' }}>
75
+ <h2>Continuous Loop</h2>
76
+ <p>Content loops seamlessly without any visible seams or jumps.</p>
77
+ </div>
78
+ </ReactSway>
79
+ </div>
80
+ );
81
+ }
82
+
83
+ export default SwayUsageExample;
84
+ ```
85
+
86
+ ## Props
87
+
88
+ | Prop | Type | Default | Description |
89
+ |------|------|---------|-------------|
90
+ | `autoScroll` | `boolean` | `true` | Enable/disable auto-scrolling. |
91
+ | `children` | `ReactNode` | — | Content elements to render in the scroll container. |
92
+ | `direction` | `'down' \| 'up'` | `'up'` | Auto-scroll direction. |
93
+ | `draggable` | `boolean` | `true` | Enable mouse/touch drag interaction. |
94
+ | `friction` | `number` | `0.95` | Momentum decay coefficient (0–1, lower = more friction). |
95
+ | `keyboard` | `boolean` | `true` | Enable keyboard controls (Space, ArrowUp/Down, Home/End). |
96
+ | `lazy` | `boolean` | `true` | Enable lazy visibility detection via IntersectionObserver. |
97
+ | `lazyRootMargin` | `string` | `'100px'` | IntersectionObserver `rootMargin` for lazy visibility detection. |
98
+ | `lazyThreshold` | `number` | `0.01` | IntersectionObserver `threshold` for lazy visibility detection. |
99
+ | `onPause` | `() => void` | — | Fired when scrolling pauses. |
100
+ | `onResume` | `() => void` | — | Fired when scrolling resumes after pause. |
101
+ | `onScroll` | `(position: number) => void` | — | Fired on every position change with the current scroll position. |
102
+ | `pauseOnInteraction` | `boolean` | `true` | Pause auto-scroll during user interaction. |
103
+ | `resumeDelay` | `number` | `2000` | Milliseconds before auto-scroll resumes after interaction. |
104
+ | `speed` | `number` | `0.5` | Auto-scroll speed in pixels per frame at 60fps. |
105
+ | `wheelEnabled` | `boolean` | `true` | Enable mouse wheel scrolling. |
106
+
107
+ ## License
108
+
109
+ This package is licensed under the [MIT License](../../LICENSE).