scroll-system 1.0.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Joel Starck
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,458 @@
1
+ # scroll-system
2
+
3
+ [![npm version](https://img.shields.io/npm/v/scroll-system.svg)](https://www.npmjs.com/package/scroll-system)
4
+ [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
5
+
6
+ **The ultimate React scroll system for immersive, TikTok-style full-screen navigation.**
7
+
8
+ Built for high-performance marketing sites, portfolios, and web apps that require a rigid, app-like scroll experience. It enforces a deterministic state machine to prevent "scroll jail" and ensure users never get stuck between views.
9
+
10
+ ---
11
+
12
+ ## 🌟 Key Features
13
+
14
+ | Feature | Description |
15
+ |---------|-------------|
16
+ | **Snap Views** | 3 specialized view types: `FullView`, `ScrollLockedView`, `ControlledView` |
17
+ | **1:1 Touch Physics** | Native-feeling drag interaction on mobile (like TikTok/Reels) |
18
+ | **Deterministic Locking** | Smart state machine handles mixed content without bugs |
19
+ | **Accessibility** | Focus management, keyboard navigation, screen reader announcements |
20
+ | **Deep Linking** | URL hash synchronization (`#about`, `#contact`) |
21
+ | **Horizontal Support** | Works in both vertical and horizontal orientations |
22
+ | **Performance** | Lazy loading support and throttled event listeners |
23
+ | **Analytics** | Built-in view engagement tracking |
24
+
25
+ ---
26
+
27
+ ## 📦 Installation
28
+
29
+ ```bash
30
+ npm install scroll-system
31
+ # or
32
+ yarn add scroll-system
33
+ # or
34
+ pnpm add scroll-system
35
+ ```
36
+
37
+ ### Peer Dependencies
38
+ ```json
39
+ {
40
+ "react": ">=18.0.0",
41
+ "react-dom": ">=18.0.0",
42
+ "zustand": ">=4.0.0",
43
+ "tailwindcss": ">=3.0.0"
44
+ }
45
+ ```
46
+
47
+ ---
48
+
49
+ ## 🚀 Quick Start
50
+
51
+ Wrap your application in `ScrollContainer` and add your views. Each view **MUST** have a unique `id`.
52
+
53
+ ```tsx
54
+ import {
55
+ ScrollContainer,
56
+ FullView,
57
+ ScrollLockedView,
58
+ ControlledView
59
+ } from "scroll-system";
60
+
61
+ export default function App() {
62
+ const [hasAccepted, setHasAccepted] = useState(false);
63
+
64
+ return (
65
+ <div className="fixed inset-0 overflow-hidden">
66
+ <ScrollContainer
67
+ enableDragPhysics={true}
68
+ transitionDuration={600}
69
+ onViewChange={(from, to) => console.log(`View changed: ${from} → ${to}`)}
70
+ >
71
+
72
+ {/* Simple full-screen section */}
73
+ <FullView id="hero" className="bg-gradient-to-b from-blue-600 to-purple-700">
74
+ <h1>Welcome to My App</h1>
75
+ </FullView>
76
+
77
+ {/* Section with internal scroll */}
78
+ <ScrollLockedView id="features">
79
+ <div className="min-h-[200vh] p-8">
80
+ <h2>Features</h2>
81
+ <p>This content is taller than the viewport...</p>
82
+ <p>User must scroll to the bottom to continue.</p>
83
+ </div>
84
+ </ScrollLockedView>
85
+
86
+ {/* Logic gate - must accept to proceed */}
87
+ <ControlledView
88
+ id="terms"
89
+ canProceed={hasAccepted}
90
+ onActivate={() => console.log('Terms section visible')}
91
+ >
92
+ <h2>Terms of Service</h2>
93
+ <button onClick={() => setHasAccepted(true)}>
94
+ Accept Terms
95
+ </button>
96
+ </ControlledView>
97
+
98
+ {/* Final section */}
99
+ <FullView id="contact">
100
+ <h2>Contact Us</h2>
101
+ </FullView>
102
+
103
+ </ScrollContainer>
104
+ </div>
105
+ );
106
+ }
107
+ ```
108
+
109
+ ---
110
+
111
+ ## 🧩 Components
112
+
113
+ ### `ScrollContainer`
114
+
115
+ The root wrapper component. Manages viewport, event listeners, and global state.
116
+
117
+ | Prop | Type | Default | Description |
118
+ |------|------|---------|-------------|
119
+ | `children` | `ReactNode` | Required | View components |
120
+ | `orientation` | `"vertical"` \| `"horizontal"` | `"vertical"` | Scroll direction |
121
+ | `transitionDuration` | `number` | `700` | Animation duration in ms |
122
+ | `transitionEasing` | `string` | `"cubic-bezier(0.16, 1, 0.3, 1)"` | CSS easing function |
123
+ | `enableDragPhysics` | `boolean` | `false` | Enable 1:1 touch dragging |
124
+ | `enableHashSync` | `boolean` | `false` | Sync URL hash with active view |
125
+ | `hashPrefix` | `string` | `""` | Prefix for URL hash (e.g., `"section-"`) |
126
+ | `hashPushHistory` | `boolean` | `false` | Use `pushState` instead of `replaceState` |
127
+ | `enableFocusManagement` | `boolean` | `true` | Move focus to active view for a11y |
128
+ | `respectReducedMotion` | `boolean` | `true` | Disable animations if OS prefers |
129
+ | `onViewChange` | `(from: number, to: number) => void` | - | Callback when view changes |
130
+ | `onInitialized` | `() => void` | - | Callback when system initializes |
131
+
132
+ ---
133
+
134
+ ### `FullView`
135
+
136
+ Standard full-screen container. Always "unlocked" - any scroll gesture navigates away.
137
+
138
+ | Prop | Type | Default | Description |
139
+ |------|------|---------|-------------|
140
+ | `id` | `string` | Required | Unique identifier |
141
+ | `className` | `string` | `""` | CSS classes |
142
+ | `meta` | `Record<string, any>` | - | Custom metadata |
143
+ | `onActivate` | `() => void` | - | Called when view becomes active |
144
+ | `onDeactivate` | `() => void` | - | Called when view becomes inactive |
145
+ | `onEnterStart` | `() => void` | - | Called when enter transition starts |
146
+ | `onEnterEnd` | `() => void` | - | Called when enter transition ends |
147
+ | `onExitStart` | `() => void` | - | Called when exit transition starts |
148
+ | `onExitEnd` | `() => void` | - | Called when exit transition ends |
149
+
150
+ ---
151
+
152
+ ### `ScrollLockedView`
153
+
154
+ Smart container for long content. Automatically detects overflow and locks navigation until user scrolls to the bottom.
155
+
156
+ | Prop | Type | Default | Description |
157
+ |------|------|---------|-------------|
158
+ | `id` | `string` | Required | Unique identifier |
159
+ | `className` | `string` | `""` | CSS classes |
160
+ | `scrollDirection` | `"vertical"` \| `"horizontal"` | `"vertical"` | Internal scroll direction |
161
+ | `scrollEndThreshold` | `number` | `0.99` | Progress threshold to unlock (0-1) |
162
+ | `onScrollProgress` | `(progress: number) => void` | - | Called on internal scroll |
163
+ | `onActivate` | `() => void` | - | Called when view becomes active |
164
+ | `onDeactivate` | `() => void` | - | Called when view becomes inactive |
165
+ | `onEnterStart` | `() => void` | - | Called when enter transition starts |
166
+ | `onEnterEnd` | `() => void` | - | Called when enter transition ends |
167
+ | `onExitStart` | `() => void` | - | Called when exit transition starts |
168
+ | `onExitEnd` | `() => void` | - | Called when exit transition ends |
169
+
170
+ **Behavior:**
171
+ - If content fits viewport → Acts like `FullView`
172
+ - If content overflows → **LOCKS** navigation until user scrolls to bottom (99%)
173
+
174
+ ---
175
+
176
+ ### `ControlledView`
177
+
178
+ Logic gate for explicit user actions (forms, terms, payment, etc.).
179
+
180
+ | Prop | Type | Default | Description |
181
+ |------|------|---------|-------------|
182
+ | `id` | `string` | Required | Unique identifier |
183
+ | `className` | `string` | `""` | CSS classes |
184
+ | `canProceed` | `boolean` | `false` | Allow navigation to NEXT view |
185
+ | `allowGoBack` | `boolean` | `true` | Allow navigation to PREVIOUS view |
186
+ | `allowInternalScroll` | `boolean` | `false` | Enable internal scrolling |
187
+ | `scrollDirection` | `"vertical"` \| `"horizontal"` \| `"none"` | `"none"` | Internal scroll direction |
188
+ | `onActivate` | `() => void` | - | Called when view becomes active |
189
+ | `onDeactivate` | `() => void` | - | Called when view becomes inactive |
190
+ | `onEnterStart` | `() => void` | - | Called when enter transition starts |
191
+ | `onEnterEnd` | `() => void` | - | Called when enter transition ends |
192
+ | `onExitStart` | `() => void` | - | Called when exit transition starts |
193
+ | `onExitEnd` | `() => void` | - | Called when exit transition ends |
194
+
195
+ ---
196
+
197
+ ### `LazyView`
198
+
199
+ Performance optimization wrapper. Only renders children when view is within active range.
200
+
201
+ ```tsx
202
+ <FullView id="charts">
203
+ <LazyView viewId="charts" buffer={1}>
204
+ <ExpensiveChartComponent />
205
+ </LazyView>
206
+ </FullView>
207
+ ```
208
+
209
+ | Prop | Type | Default | Description |
210
+ |------|------|---------|-------------|
211
+ | `viewId` | `string` | Required | ID of the parent view |
212
+ | `buffer` | `number` | `1` | Render views within ±N of active |
213
+ | `placeholder` | `ReactNode` | `null` | Content to show when inactive |
214
+
215
+ ---
216
+
217
+ ### `ScrollDebugOverlay`
218
+
219
+ Development tool for visualizing system state.
220
+
221
+ ```tsx
222
+ <ScrollContainer>
223
+ {/* Views */}
224
+ <ScrollDebugOverlay position="bottom-left" />
225
+ </ScrollContainer>
226
+ ```
227
+
228
+ | Prop | Type | Default | Description |
229
+ |------|------|---------|-------------|
230
+ | `position` | `"top-left"` \| `"top-right"` \| `"bottom-left"` \| `"bottom-right"` | `"bottom-right"` | Overlay position |
231
+
232
+ Shows: `activeIndex`, `transitioning`, `navigation state`, `metrics`, and more.
233
+
234
+ ---
235
+
236
+ ## 🪝 Hooks
237
+
238
+ ### `useScrollSystem()`
239
+
240
+ Main API hook for programmatic control.
241
+
242
+ ```tsx
243
+ const {
244
+ // Navigation
245
+ goToNext, // () => void
246
+ goToPrev, // () => void
247
+ goTo, // (index: number | id: string) => void
248
+
249
+ // State
250
+ activeIndex, // number
251
+ activeId, // string | null
252
+ totalViews, // number
253
+
254
+ // Status Checks
255
+ isLocked, // () => boolean
256
+ getProgress, // () => number (0-1)
257
+ canGoNext, // () => boolean
258
+ canGoPrev, // () => boolean
259
+
260
+ // UI State
261
+ isDragging, // boolean
262
+ isTransitioning // boolean
263
+ } = useScrollSystem();
264
+ ```
265
+
266
+ ---
267
+
268
+ ### `useViewControl(viewId)`
269
+
270
+ Hook for programmatic view control from within a `ControlledView`.
271
+
272
+ ```tsx
273
+ const { unlock, lock, goNext, goPrev, goTo } = useViewControl("terms");
274
+
275
+ // Unlock navigation after form completion
276
+ const handleSubmit = () => {
277
+ saveData();
278
+ unlock();
279
+ goNext();
280
+ };
281
+ ```
282
+
283
+ ---
284
+
285
+ ### `useScrollAnalytics(options)`
286
+
287
+ Track user engagement for analytics.
288
+
289
+ ```tsx
290
+ useScrollAnalytics({
291
+ onViewEnter: ({ viewId, viewIndex, enterTime }) => {
292
+ analytics.track('Section Viewed', { viewId, index: viewIndex });
293
+ },
294
+ onViewExit: ({ viewId, viewIndex, duration }) => {
295
+ analytics.track('Section Time', { viewId, seconds: duration });
296
+ },
297
+ enabled: process.env.NODE_ENV === 'production'
298
+ });
299
+ ```
300
+
301
+ ---
302
+
303
+ ### `useViewProgress(viewId)`
304
+
305
+ Get scroll progress for a specific view.
306
+
307
+ ```tsx
308
+ const progress = useViewProgress("features"); // 0 to 1
309
+
310
+ return (
311
+ <div
312
+ className="fixed top-0 left-0 h-1 bg-blue-500"
313
+ style={{ width: `${progress * 100}%` }}
314
+ />
315
+ );
316
+ ```
317
+
318
+ ---
319
+
320
+ ## ⌨️ Keyboard Navigation
321
+
322
+ Built-in keyboard support for accessibility:
323
+
324
+ | Key | Action |
325
+ |-----|--------|
326
+ | `↓` / `PageDown` | Navigate to next view |
327
+ | `↑` / `PageUp` | Navigate to previous view |
328
+ | `Space` | Navigate to next view |
329
+ | `Shift + Space` | Navigate to previous view |
330
+ | `Home` | Jump to first view |
331
+ | `End` | Jump to last view |
332
+
333
+ ---
334
+
335
+ ## 🔗 Deep Linking
336
+
337
+ Enable URL hash synchronization:
338
+
339
+ ```tsx
340
+ <ScrollContainer
341
+ enableHashSync={true}
342
+ hashPrefix="" // Optional: "section-" → "#section-about"
343
+ hashPushHistory={false} // false = replaceState, true = pushState
344
+ >
345
+ <FullView id="about">...</FullView> {/* URL: #about */}
346
+ <FullView id="contact">...</FullView> {/* URL: #contact */}
347
+ </ScrollContainer>
348
+ ```
349
+
350
+ **Features:**
351
+ - URL updates when navigating
352
+ - Direct links work (`yoursite.com/#contact`)
353
+ - Browser back/forward buttons work
354
+
355
+ ---
356
+
357
+ ## 👆 Touch Physics
358
+
359
+ Enable 1:1 native-feeling touch interactions:
360
+
361
+ ```tsx
362
+ <ScrollContainer enableDragPhysics={true}>
363
+ ```
364
+
365
+ **Behavior:**
366
+ - View follows finger position in real-time
367
+ - Spring-back if released before threshold
368
+ - Velocity-aware: quick flicks trigger navigation
369
+ - Resistance at boundaries
370
+
371
+ ---
372
+
373
+ ## ♿ Accessibility
374
+
375
+ ### Features Included
376
+ - **Focus Management**: Automatically moves focus to active view
377
+ - **Screen Readers**: `aria-live` announcements for view changes
378
+ - **Reduced Motion**: Respects `prefers-reduced-motion` OS setting
379
+ - **Keyboard Navigation**: Full arrow key + space navigation
380
+
381
+ ### Recommended CSS
382
+ ```css
383
+ /* Hide scrollbar but keep functionality */
384
+ .scroll-container .no-scrollbar {
385
+ scrollbar-width: none;
386
+ -ms-overflow-style: none;
387
+ }
388
+ .scroll-container .no-scrollbar::-webkit-scrollbar {
389
+ display: none;
390
+ }
391
+ ```
392
+
393
+ ---
394
+
395
+ ## 📱 Mobile Optimization
396
+
397
+ For the best app-like experience on mobile:
398
+
399
+ ```css
400
+ html, body {
401
+ position: fixed;
402
+ width: 100%;
403
+ height: 100%;
404
+ overflow: hidden;
405
+ overscroll-behavior-y: none; /* Prevents pull-to-refresh */
406
+ }
407
+ ```
408
+
409
+ ### Fullscreen API (Optional)
410
+ ```tsx
411
+ const toggleFullscreen = () => {
412
+ if (!document.fullscreenElement) {
413
+ document.documentElement.requestFullscreen();
414
+ } else {
415
+ document.exitFullscreen();
416
+ }
417
+ };
418
+ ```
419
+
420
+ ---
421
+
422
+ ## � Troubleshooting
423
+
424
+ | Issue | Cause | Solution |
425
+ |-------|-------|----------|
426
+ | Views not changing | Duplicate IDs | Ensure every view has a unique `id` |
427
+ | ScrollLockedView not scrolling | Content fits viewport | Content must be taller than `100vh` |
428
+ | `useScrollSystem` undefined | Used outside container | Must be inside `ScrollContainer` |
429
+ | Touch not working | Drag physics disabled | Set `enableDragPhysics={true}` |
430
+ | Stuck between views | Transition conflict | Check for conflicting event handlers |
431
+
432
+ ---
433
+
434
+ ## 📖 TypeScript
435
+
436
+ Full TypeScript support with exported types:
437
+
438
+ ```tsx
439
+ import type {
440
+ ScrollContainerProps,
441
+ FullViewProps,
442
+ ScrollLockedViewProps,
443
+ ControlledViewProps,
444
+ ScrollSystemAPI,
445
+ ViewState,
446
+ UserIntention
447
+ } from "scroll-system";
448
+ ```
449
+
450
+ ---
451
+
452
+ ## 📄 License
453
+
454
+ MIT © **Joel Starck**
455
+
456
+ ---
457
+
458
+ Built with ❤️ for the React community.