@capgo/capacitor-transitions 8.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 Capgo
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,583 @@
1
+ # @capgo/capacitor-transitions
2
+
3
+ Framework-agnostic page transitions for Capacitor apps. iOS-style navigation without opinions.
4
+
5
+ ## Features
6
+
7
+ - **Framework Agnostic** - Works with React, Vue, Angular, Svelte, Solid, and any other framework
8
+ - **iOS & Android Animations** - Platform-appropriate transitions out of the box
9
+ - **Web Animations API** - Smooth, GPU-accelerated animations
10
+ - **View Transitions API** - Optional progressive enhancement for supporting browsers
11
+ - **No Design Opinions** - Just transition logic, you bring your own styles
12
+ - **Coordinated Transitions** - Header, content, and footer animate together
13
+ - **Page Caching** - Keep pages in DOM for instant back navigation
14
+ - **Ionic-style iOS Swipe Back** - Optional edge gesture with Capacitor native iOS auto-enable
15
+ - **Lifecycle Hooks** - willEnter, didEnter, willLeave, didLeave events
16
+
17
+ ## Compatibility
18
+
19
+ | Plugin version | Capacitor compatibility | Maintained |
20
+ | -------------- | ----------------------- | ---------- |
21
+ | v8.\*.\* | v8.\*.\* | ✅ |
22
+ | v7.\*.\* | v7.\*.\* | On demand |
23
+ | v6.\*.\* | v6.\*.\* | ❌ |
24
+ | v5.\*.\* | v5.\*.\* | ❌ |
25
+
26
+ > **Note:** The major version of this plugin follows the major version of Capacitor. Use the version that matches your Capacitor installation (e.g., plugin v8 for Capacitor 8). Only the latest major version is actively maintained.
27
+
28
+ ## Installation
29
+
30
+ ```bash
31
+ npm install @capgo/capacitor-transitions
32
+ ```
33
+
34
+ ## Quick Start
35
+
36
+ ### Vanilla JavaScript / Web Components
37
+
38
+ ```html
39
+ <cap-router-outlet platform="auto">
40
+ <cap-page>
41
+ <cap-header slot="header">
42
+ <h1>My Page</h1>
43
+ </cap-header>
44
+ <cap-content slot="content">
45
+ <p>Page content here</p>
46
+ </cap-content>
47
+ <cap-footer slot="footer">
48
+ <nav>Tab bar</nav>
49
+ </cap-footer>
50
+ </cap-page>
51
+ </cap-router-outlet>
52
+ ```
53
+
54
+ ```javascript
55
+ import '@capgo/capacitor-transitions';
56
+
57
+ // Navigate programmatically
58
+ const outlet = document.querySelector('cap-router-outlet');
59
+ outlet.push(newPageElement);
60
+ outlet.pop();
61
+ outlet.setRoot(newRootElement);
62
+ ```
63
+
64
+ ### React
65
+
66
+ ```tsx
67
+ import { useRef, useEffect } from 'react';
68
+ import { useNavigate } from 'react-router-dom';
69
+ import { initTransitions, setDirection, setupPage, setupRouterOutlet } from '@capgo/capacitor-transitions/react';
70
+ import '@capgo/capacitor-transitions';
71
+
72
+ // Initialize once at app startup
73
+ initTransitions({ platform: 'auto' });
74
+
75
+ function App() {
76
+ const outletRef = useRef<HTMLElement>(null);
77
+
78
+ useEffect(() => {
79
+ if (outletRef.current) {
80
+ setupRouterOutlet(outletRef.current, { platform: 'auto', swipeGesture: 'auto' });
81
+ }
82
+ }, []);
83
+
84
+ return (
85
+ <cap-router-outlet ref={outletRef}>
86
+ <Routes>
87
+ <Route path="/" element={<HomePage />} />
88
+ <Route path="/details/:id" element={<DetailsPage />} />
89
+ </Routes>
90
+ </cap-router-outlet>
91
+ );
92
+ }
93
+
94
+ function HomePage() {
95
+ const navigate = useNavigate();
96
+ const pageRef = useRef<HTMLElement>(null);
97
+
98
+ useEffect(() => {
99
+ if (pageRef.current) {
100
+ return setupPage(pageRef.current, {
101
+ onDidEnter: () => console.log('entered'),
102
+ });
103
+ }
104
+ }, []);
105
+
106
+ const goToDetails = (id: number) => {
107
+ setDirection('forward');
108
+ navigate(`/details/${id}`);
109
+ };
110
+
111
+ return (
112
+ <cap-page ref={pageRef}>
113
+ <cap-header slot="header">
114
+ <h1>Home</h1>
115
+ </cap-header>
116
+ <cap-content slot="content">
117
+ <button onClick={() => goToDetails(1)}>Go to Details</button>
118
+ </cap-content>
119
+ <cap-footer slot="footer">
120
+ <nav>Tab bar</nav>
121
+ </cap-footer>
122
+ </cap-page>
123
+ );
124
+ }
125
+ ```
126
+
127
+ #### React JSX TypeScript setup
128
+
129
+ Importing from `@capgo/capacitor-transitions/react` registers the web components and includes React JSX typings for `cap-router-outlet`, `cap-page`, `cap-header`, `cap-content`, and `cap-footer`. In most React projects, the import in the example above is enough.
130
+
131
+ If TypeScript still shows `Property 'cap-router-outlet' does not exist on type 'JSX.IntrinsicElements'`, add a project declaration file:
132
+
133
+ ```ts
134
+ // src/capgo-transitions.d.ts
135
+ import '@capgo/capacitor-transitions/react';
136
+ ```
137
+
138
+ Then make sure TypeScript includes it:
139
+
140
+ ```json
141
+ {
142
+ "include": ["src", "src/capgo-transitions.d.ts"]
143
+ }
144
+ ```
145
+
146
+ For Vite, Create React App, and most webpack React apps, putting `capgo-transitions.d.ts` inside `src/` is enough. For Next.js, put it in `src/` or the project root and keep it included in `tsconfig.json`. For custom TypeScript or webpack setups with a separate `types/` folder, add that folder to `tsconfig.json`:
147
+
148
+ ```json
149
+ {
150
+ "include": ["src", "types"]
151
+ }
152
+ ```
153
+
154
+ ### Vue
155
+
156
+ ```vue
157
+ <script setup>
158
+ import { ref, onMounted, onUnmounted } from 'vue';
159
+ import { useRouter } from 'vue-router';
160
+ import { initTransitions, setDirection, setupPage, setupRouterOutlet } from '@capgo/capacitor-transitions/vue';
161
+ import '@capgo/capacitor-transitions';
162
+
163
+ // Initialize once
164
+ initTransitions({ platform: 'auto' });
165
+
166
+ const router = useRouter();
167
+ const outletRef = ref(null);
168
+ const pageRef = ref(null);
169
+ let cleanup;
170
+
171
+ onMounted(() => {
172
+ if (outletRef.value) {
173
+ setupRouterOutlet(outletRef.value, { platform: 'auto' });
174
+ }
175
+ if (pageRef.value) {
176
+ cleanup = setupPage(pageRef.value, {
177
+ onDidEnter: () => console.log('entered'),
178
+ });
179
+ }
180
+ });
181
+
182
+ onUnmounted(() => cleanup?.());
183
+
184
+ const goToDetails = (id) => {
185
+ setDirection('forward');
186
+ router.push(`/details/${id}`);
187
+ };
188
+ </script>
189
+
190
+ <template>
191
+ <cap-router-outlet ref="outletRef">
192
+ <cap-page ref="pageRef">
193
+ <cap-header slot="header">
194
+ <h1>Home</h1>
195
+ </cap-header>
196
+ <cap-content slot="content">
197
+ <button @click="goToDetails(1)">Go to Details</button>
198
+ </cap-content>
199
+ </cap-page>
200
+ </cap-router-outlet>
201
+ </template>
202
+ ```
203
+
204
+ ### Svelte
205
+
206
+ ```svelte
207
+ <script>
208
+ import { routerOutlet, page, setDirection } from '@capgo/capacitor-transitions/svelte'
209
+ import '@capgo/capacitor-transitions'
210
+
211
+ function navigate(to, direction = 'forward') {
212
+ setDirection(direction)
213
+ // Use your router's navigate function
214
+ }
215
+ </script>
216
+
217
+ <cap-router-outlet use:routerOutlet>
218
+ <cap-page use:page={{ onDidEnter: () => console.log('entered') }}>
219
+ <cap-header slot="header">
220
+ <h1>Home</h1>
221
+ </cap-header>
222
+ <cap-content slot="content">
223
+ <button on:click={() => navigate('/details/1')}>Go to Details</button>
224
+ </cap-content>
225
+ </cap-page>
226
+ </cap-router-outlet>
227
+ ```
228
+
229
+ ### Solid
230
+
231
+ ```tsx
232
+ import { onMount, onCleanup } from 'solid-js';
233
+ import { useNavigate } from '@solidjs/router';
234
+ import { initTransitions, setDirection, setupPage, setupRouterOutlet } from '@capgo/capacitor-transitions/solid';
235
+ import '@capgo/capacitor-transitions';
236
+
237
+ // Initialize once
238
+ initTransitions({ platform: 'auto' });
239
+
240
+ function HomePage() {
241
+ const navigate = useNavigate();
242
+ let pageRef;
243
+
244
+ onMount(() => {
245
+ if (pageRef) {
246
+ const cleanup = setupPage(pageRef, {
247
+ onDidEnter: () => console.log('entered'),
248
+ });
249
+ onCleanup(cleanup);
250
+ }
251
+ });
252
+
253
+ const goToDetails = (id) => {
254
+ setDirection('forward');
255
+ navigate(`/details/${id}`);
256
+ };
257
+
258
+ return (
259
+ <cap-page ref={pageRef}>
260
+ <cap-header slot="header">
261
+ <h1>Home</h1>
262
+ </cap-header>
263
+ <cap-content slot="content">
264
+ <button onClick={() => goToDetails(1)}>Go to Details</button>
265
+ </cap-content>
266
+ </cap-page>
267
+ );
268
+ }
269
+ ```
270
+
271
+ ### Angular
272
+
273
+ ```typescript
274
+ // app.component.ts
275
+ import { Component, CUSTOM_ELEMENTS_SCHEMA, ElementRef, ViewChild, AfterViewInit } from '@angular/core';
276
+ import '@capgo/capacitor-transitions';
277
+
278
+ @Component({
279
+ selector: 'app-root',
280
+ schemas: [CUSTOM_ELEMENTS_SCHEMA],
281
+ template: `
282
+ <cap-router-outlet #outlet platform="auto">
283
+ <router-outlet></router-outlet>
284
+ </cap-router-outlet>
285
+ `,
286
+ })
287
+ export class AppComponent {}
288
+
289
+ // home.component.ts
290
+ @Component({
291
+ selector: 'app-home',
292
+ schemas: [CUSTOM_ELEMENTS_SCHEMA],
293
+ template: `
294
+ <cap-page>
295
+ <cap-header slot="header">
296
+ <h1>Home</h1>
297
+ </cap-header>
298
+ <cap-content slot="content">
299
+ <button (click)="goToDetails(1)">Go to Details</button>
300
+ </cap-content>
301
+ </cap-page>
302
+ `,
303
+ })
304
+ export class HomeComponent {
305
+ constructor(private router: Router) {}
306
+
307
+ goToDetails(id: number) {
308
+ this.router.navigate(['/details', id]);
309
+ }
310
+ }
311
+ ```
312
+
313
+ ## Use With @capgo/native-navigation
314
+
315
+ Use `@capgo/native-navigation` when the native layer should own the navbar or tabbar, and use `@capgo/capacitor-transitions` for the WebView page content underneath that native chrome.
316
+
317
+ ```bash
318
+ npm install @capgo/capacitor-transitions @capgo/native-navigation
319
+ npx cap sync
320
+ ```
321
+
322
+ Configure native chrome first:
323
+
324
+ ```typescript
325
+ import { NativeNavigation } from '@capgo/native-navigation';
326
+
327
+ await NativeNavigation.configure({
328
+ contentInsetMode: 'css',
329
+ });
330
+
331
+ await NativeNavigation.setNavbar({
332
+ title: 'Inbox',
333
+ backButton: { visible: false },
334
+ });
335
+ ```
336
+
337
+ Keep the transition outlet focused on pages, not native bars:
338
+
339
+ ```html
340
+ <cap-router-outlet platform="auto" swipe-gesture="auto">
341
+ <cap-page>
342
+ <cap-content slot="content" fullscreen>
343
+ <main class="native-page">Inbox content</main>
344
+ </cap-content>
345
+ </cap-page>
346
+ </cap-router-outlet>
347
+ ```
348
+
349
+ ```css
350
+ .native-page {
351
+ padding-top: var(--cap-native-navigation-top);
352
+ padding-bottom: var(--cap-native-navigation-bottom);
353
+ }
354
+ ```
355
+
356
+ Drive both packages from the same router actions:
357
+
358
+ ```typescript
359
+ import { NativeNavigation } from '@capgo/native-navigation';
360
+ import { setDirection } from '@capgo/capacitor-transitions/react';
361
+ import { router } from './router';
362
+
363
+ await NativeNavigation.addListener('navbarBack', () => {
364
+ setDirection('back');
365
+ router.back();
366
+ });
367
+
368
+ async function openMessage(id: string) {
369
+ setDirection('forward');
370
+ router.push(`/message/${id}`);
371
+
372
+ await NativeNavigation.setNavbar({
373
+ title: 'Message',
374
+ backButton: { visible: true, title: 'Inbox' },
375
+ });
376
+ }
377
+ ```
378
+
379
+ Do not render the native top bar again as a moving `<cap-header>`. Native Navigation keeps the bar native; this package animates the web page body.
380
+
381
+ ## API Reference
382
+
383
+ ### Components
384
+
385
+ #### `<cap-router-outlet>`
386
+
387
+ Container for page transitions.
388
+
389
+ | Attribute | Type | Default | Description |
390
+ | --------------- | ------------------------------ | ---------------- | ---------------------------------------------------------------------------------- |
391
+ | `platform` | `'ios' \| 'android' \| 'auto'` | `'auto'` | Animation style |
392
+ | `duration` | `number` | Platform default | Animation duration in ms |
393
+ | `keep-in-dom` | `boolean` | `true` | Keep pages in DOM after navigating away |
394
+ | `max-cached` | `number` | `10` | Maximum pages to keep cached |
395
+ | `swipe-gesture` | `boolean \| 'auto'` | `'auto'` | Enable edge swipe-back gesture. `'auto'` enables only in native iOS Capacitor apps |
396
+
397
+ Methods:
398
+
399
+ - `push(element, config?)` - Navigate forward to new page
400
+ - `pop(config?)` - Navigate back
401
+ - `setRoot(element, config?)` - Replace navigation stack
402
+ - `setSwipeGesture(true | false | 'auto')` - Enable, disable, or auto-detect edge swipe-back gesture
403
+
404
+ `swipe-gesture="auto"` uses Capacitor's runtime helpers (`Capacitor.isNativePlatform()` and `Capacitor.getPlatform()`) and enables the gesture only for native iOS apps. Use `swipe-gesture="true"` to force it on any platform or `swipe-gesture="false"` to disable it.
405
+
406
+ #### `<cap-page>`
407
+
408
+ Page container with header/content/footer slots.
409
+
410
+ Events:
411
+
412
+ - `cap-will-enter` - Before page becomes visible
413
+ - `cap-did-enter` - After page becomes visible
414
+ - `cap-will-leave` - Before page leaves
415
+ - `cap-did-leave` - After page leaves
416
+
417
+ #### `<cap-header>`
418
+
419
+ Header container. Use with `slot="header"` inside `<cap-page>`.
420
+
421
+ #### `<cap-content>`
422
+
423
+ Main scrollable content area. Use with `slot="content"`.
424
+
425
+ | Attribute | Type | Default | Description |
426
+ | ------------ | --------- | ------- | ----------------------------- |
427
+ | `fullscreen` | `boolean` | `false` | Content scrolls behind header |
428
+ | `scroll-x` | `boolean` | `true` | Enable horizontal scroll |
429
+ | `scroll-y` | `boolean` | `true` | Enable vertical scroll |
430
+
431
+ #### `<cap-footer>`
432
+
433
+ Footer container. Use with `slot="footer"`.
434
+
435
+ ### Transition Directions
436
+
437
+ | Direction | Description |
438
+ | ----------- | -------------------------------------- |
439
+ | `'forward'` | Push animation (iOS: slide from right) |
440
+ | `'back'` | Pop animation (iOS: slide to right) |
441
+ | `'root'` | Replace animation (fade) |
442
+ | `'none'` | No animation |
443
+
444
+ ### Helper Functions
445
+
446
+ All framework bindings export these helper functions:
447
+
448
+ ```typescript
449
+ // Initialize the transition system
450
+ initTransitions({ platform: 'auto' });
451
+
452
+ // Set the direction for the next navigation
453
+ setDirection('forward' | 'back' | 'root' | 'none');
454
+
455
+ // Set up a router outlet element
456
+ setupRouterOutlet(element, options);
457
+
458
+ // Auto-enable the iOS edge swipe-back gesture in native Capacitor iOS apps
459
+ setupRouterOutlet(element, { swipeGesture: 'auto' });
460
+
461
+ // Force enable or disable from JavaScript
462
+ setupRouterOutlet(element, { swipeGesture: true });
463
+ setupRouterOutlet(element, { swipeGesture: false });
464
+
465
+ // Set up a page element with lifecycle callbacks (returns cleanup function)
466
+ setupPage(element, { onWillEnter, onDidEnter, onWillLeave, onDidLeave });
467
+
468
+ // Create a transition-aware navigate function
469
+ const transitionNavigate = createTransitionNavigate(navigate);
470
+ transitionNavigate('/path', 'forward');
471
+ ```
472
+
473
+ ### TransitionController
474
+
475
+ For advanced programmatic control:
476
+
477
+ ```typescript
478
+ import { createTransitionController } from '@capgo/capacitor-transitions';
479
+
480
+ const controller = createTransitionController({
481
+ platform: 'auto',
482
+ duration: 400,
483
+ useViewTransitions: true,
484
+ });
485
+
486
+ // Navigate
487
+ await controller.push(element, { direction: 'forward' });
488
+ await controller.pop({ direction: 'back' });
489
+ await controller.setRoot(element, { direction: 'root' });
490
+
491
+ // Lifecycle hooks
492
+ controller.registerLifecycle('page-id', {
493
+ onWillEnter: (event) => console.log('Will enter', event),
494
+ onDidEnter: (event) => console.log('Did enter', event),
495
+ onWillLeave: (event) => console.log('Will leave', event),
496
+ onDidLeave: (event) => console.log('Did leave', event),
497
+ });
498
+ ```
499
+
500
+ ## Browser Support
501
+
502
+ - Modern browsers with Web Animations API support
503
+ - Optional View Transitions API (Chrome 111+, Edge 111+, Safari 18+) support
504
+ - Graceful fallback for older browsers
505
+
506
+ ## Design Philosophy
507
+
508
+ This library is intentionally unopinionated about styling:
509
+
510
+ 1. **Minimal structural CSS** - You bring your own visual system
511
+ 2. **No design system** - Works with any UI library or custom styles
512
+ 3. **Just transitions** - Focus on smooth page navigation
513
+ 4. **Framework agnostic** - Use with React, Vue, Angular, Svelte, Solid, or vanilla JS
514
+
515
+ The goal is to provide Ionic-quality page transitions without Ionic's design system or framework lock-in.
516
+
517
+ ## Examples
518
+
519
+ See the `/examples` directory for complete examples:
520
+
521
+ - `react-app` - React with React Router
522
+ - `vue-app` - Vue 3 with Vue Router
523
+ - `angular-app` - Angular with Angular Router
524
+ - `svelte-app` - Svelte 5
525
+ - `solid-app` - Solid with Solid Router
526
+ - `tanstack-app` - React with TanStack Router
527
+
528
+ ### React Example
529
+
530
+ The React example demonstrates iOS-style page transitions with smooth animations:
531
+
532
+ <img src="./docs/react-transition-demo.webp" width="300" alt="React transition demo" />
533
+
534
+ <table>
535
+ <tr>
536
+ <td align="center">
537
+ <img
538
+ src="https://github.com/user-attachments/assets/c06a6c5a-c318-44ab-a57c-992ba9cfacd4"
539
+ width="300"
540
+ alt="React Home Page"
541
+ />
542
+ <br />
543
+ <strong>Home Page</strong>
544
+ </td>
545
+ <td align="center">
546
+ <img
547
+ src="https://github.com/user-attachments/assets/6fd808da-6352-4660-8c79-e32184332df8"
548
+ width="300"
549
+ alt="React Details Page"
550
+ />
551
+ <br />
552
+ <strong>Details Page</strong>
553
+ </td>
554
+ <td align="center">
555
+ <img
556
+ src="https://github.com/user-attachments/assets/8516dcf0-4189-40d9-a400-154b73c0135e"
557
+ width="300"
558
+ alt="React Nested Page"
559
+ />
560
+ <br />
561
+ <strong>Nested Page</strong>
562
+ </td>
563
+ </tr>
564
+ </table>
565
+
566
+ Features demonstrated:
567
+
568
+ - Forward navigation with slide-in animation
569
+ - Back navigation with slide-out animation
570
+ - Multi-level page stack
571
+ - Coordinated header/content/footer transitions
572
+
573
+ To run the React example:
574
+
575
+ ```bash
576
+ cd examples/react-app
577
+ npm install
578
+ npm run dev
579
+ ```
580
+
581
+ ## License
582
+
583
+ MIT