开始
复制包含安装步骤和本插件的完整 Markdown 指南的设置提示。
Set up this Capacitor plugin in the project.
Use the package manager already used by the project.
Install these package(s): `@capgo/capacitor-transitions`
Run the required Capacitor sync/update step after installation.
Read this markdown guide for the full setup steps: https://raw.githubusercontent.com/Cap-go/website/refs/heads/main/apps/docs/src/content/docs/docs/plugins/transitions/getting-started.mdx
Use that guide for platform-specific steps, native file edits, permissions, config changes, imports, and usage setup.
If that guide references other docs pages, read them too.
安装
标题为“安装”You can use our AI-Assisted Setup to install the plugin. Add the Capgo skills to your AI tool using the following command:
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins然后使用以下提示:
Use the `capacitor-plugins` skill from `Cap-go/capgo-skills` to install the `@capgo/capacitor-transitions` plugin in my project.如果您更喜欢手动设置,请按照以下命令安装插件并遵循以下平台特定的说明:
-
安装包
终端窗口 npm install @capgo/capacitor-transitions -
注册 Web 组件
import '@capgo/capacitor-transitions'; -
包装路由页面
<cap-router-outlet platform="auto" swipe-gesture="auto"><cap-page><cap-header slot="header"><h1>Inbox</h1></cap-header><cap-content slot="content"><button>Open message</button></cap-content><cap-footer slot="footer"><nav>Tabs</nav></cap-footer></cap-page></cap-router-outlet> -
在路由器改变路由之前设置方向
import { setDirection } from '@capgo/capacitor-transitions/react';setDirection('forward');router.push('/message/42');setDirection('back');router.back();
React设置
名为“React设置”的部分import { useEffect, useRef } from 'react';import { useNavigate } from 'react-router-dom';import { initTransitions, setDirection, setupPage, setupRouterOutlet } from '@capgo/capacitor-transitions/react';import '@capgo/capacitor-transitions';
initTransitions({ platform: 'auto' });
export function AppShell() { const outletRef = useRef<HTMLElement>(null);
useEffect(() => { if (!outletRef.current) return;
setupRouterOutlet(outletRef.current, { platform: 'auto', swipeGesture: 'auto', }); }, []);
return ( <cap-router-outlet ref={outletRef}> {/* Your router renders cap-page children here. */} </cap-router-outlet> );}
export function InboxPage() { const navigate = useNavigate(); const pageRef = useRef<HTMLElement>(null);
useEffect(() => { if (!pageRef.current) return;
return setupPage(pageRef.current, { onDidEnter: () => console.log('Inbox visible'), }); }, []);
return ( <cap-page ref={pageRef}> <cap-header slot="header"> <h1>Inbox</h1> </cap-header>
<cap-content slot="content"> <button onClick={() => { setDirection('forward'); navigate('/message/42'); }} > Open message </button> </cap-content> </cap-page> );}React JSX TypeScript
名为“React JSX TypeScript”的部分从中导入 @capgo/capacitor-transitions/react 包括JSX类型定义的 cap-router-outlet, cap-page, cap-header, cap-content,和 cap-footer. 在大多数 React 项目中,导入使自定义元素在 TSX 中自动有效。
如果 TypeScript 还报告错误, Property 'cap-router-outlet' does not exist on type 'JSX.IntrinsicElements'添加一个项目声明文件:
import '@capgo/capacitor-transitions/react';对于 Vite、Create React App 和大多数 webpack React 应用程序,将该文件放在 src/ 中即可。对于 Next.js,放在 src/ 或项目根目录,并确保 tsconfig.json 包含它:
{ "include": ["src", "src/capgo-transitions.d.ts"]}对于自定义 TypeScript 或 webpack 的设置,使用单独的 types/ 文件夹,包含该文件夹:
{ "include": ["src", "types"]}滑动返回
标题:滑动返回从标记中启用或禁用 iOS 边缘手势:
<cap-router-outlet swipe-gesture="auto"></cap-router-outlet><cap-router-outlet swipe-gesture="true"></cap-router-outlet><cap-router-outlet swipe-gesture="false"></cap-router-outlet>或从 JavaScript 中:
const outlet = document.querySelector('cap-router-outlet');
outlet?.setSwipeGesture('auto');outlet?.setSwipeGesture(true);outlet?.setSwipeGesture(false);auto 仅在 Capacitor 报告本机 iOS 运行时时启用手势。在手势期间,页面转换跟随手指。当用户释放时,转换要么完成并要求浏览器历史记录向后移动,要么取消并恢复当前页面。
要防止元素开始手势,请添加 data-swipe-gesture-ignore:
<button data-swipe-gesture-ignore>Open drawer</button>使用原生导航
标题:使用原生导航使用 @capgo/capacitor-transitions 与 @capgo/native-navigation 当原生应该拥有顶部和底部导航栏,而网页内容保持Ionic样式的页面动画时
-
安装并同步原生导航包:
终端窗口 npm install @capgo/native-navigationnpx cap sync -
配置原生浏览器:
import { NativeNavigation } from '@capgo/native-navigation';await NativeNavigation.configure({contentInsetMode: 'css',});await NativeNavigation.setNavbar({title: 'Inbox',backButton: { visible: false },}); -
只让网页过渡出口负责页面:
<cap-router-outlet platform="auto" swipe-gesture="auto"><cap-page><cap-content slot="content" fullscreen><main class="native-page">Inbox content</main></cap-content></cap-page></cap-router-outlet>.native-page {padding-top: var(--cap-native-navigation-top);padding-bottom: var(--cap-native-navigation-bottom);} -
从同一个路由事件驱动两种系统:
import { NativeNavigation } from '@capgo/native-navigation';import { setDirection } from '@capgo/capacitor-transitions/react';import { router } from './router';await NativeNavigation.addListener('navbarBack', () => {setDirection('back');router.back();});async function openMessage(id: string) {setDirection('forward');router.push(`/message/${id}`);await NativeNavigation.setNavbar({title: 'Message',backButton: { visible: true, title: 'Inbox' },});}
不再将原生顶栏渲染为移动 <cap-header>. Let @capgo/native-navigation 保留原生顶栏并使用 @capgo/capacitor-transitions 为 WebView 页面内容
组件
标题:组件<cap-router-outlet>
标题:<cap-router-outlet>| 属性 | 类型 | 默认值 | 描述 |
|---|---|---|---|
platform | 'ios' | 'android' | 'auto' | 'auto' | 动画风格 |
duration | number | 平台默认 | 动画持续时间(毫秒) |
keep-in-dom | boolean | true | 保持不活跃页面在 DOM 中 |
max-cached | number | 10 | 最大缓存页面 |
swipe-gesture | boolean | 'auto' | 'auto' | 启用、禁用或检测 iOS 边缘手势 |
方法:
push(element, config?)pop(config?)setRoot(element, config?)setSwipeGesture(true | false | 'auto')
<cap-page>
标题:<cap-page>包裹一个页面并发射生命周期事件:
cap-will-entercap-did-entercap-will-leavecap-did-leave
<cap-content>
标题:<cap-content>| 属性 | 类型 | 默认 | 描述 |
|---|---|---|---|
fullscreen | boolean | false | 让内容滚动在头部后面 |
scroll-x | boolean | true | 启用水平滚动 |
scroll-y | boolean | true | 启用垂直滚动 |
框架辅助工具
标题:框架辅助工具框架入口点暴露相同的核心辅助工具:
import { initTransitions, setDirection, setupPage, setupRouterOutlet } from '@capgo/capacitor-transitions/react';
initTransitions({ platform: 'auto' });setDirection('forward');setupRouterOutlet(element, { platform: 'auto', swipeGesture: 'auto' });setupPage(element, { onWillEnter, onDidEnter, onWillLeave, onDidLeave });可用入口点:
@capgo/capacitor-transitions/react@capgo/capacitor-transitions/vue@capgo/capacitor-transitions/angular@capgo/capacitor-transitions/svelte@capgo/capacitor-transitions/solid
继续从开始指南
标题:继续从开始指南如果您正在使用 入门 以规划迁移和企业运营,连接它 使用@capgo/capacitor-转换 使用@capgo/capacitor-转换 Capgo企业 Capgo企业 Ionic企业插件替代品 __CAPGO_KEEP_0__替代品 Capgo替代品 Capgo咨询 Capgo 为产品工作流程在Capgo咨询中.