跳过主要内容
返回插件
@capgo/capacitor-transitions
教程
由 github.com/Cap-go

过渡

添加Ionic风格页面过渡和iOS边缘滑动返回手势, 无需Ionic UI

演示

WebP 动画演示

一个 React 路由流,展示 iOS 风格的 Capacitor 页面过渡作为一个 WebP 动画演示

源资产
前进和后退导航在一个 Capacitor 风格的 React 页面过渡演示
React 过渡流程

指南

关于过渡的教程

使用 @capgo/capacitor-transitions

@capgo/capacitor-transitions 在不采用 Ionic UI 的情况下,添加 Ionic 风格的路由过渡到 Capacitor 应用。它在 web 层运行,保留您的现有路由,并且可以在原生 Capacitor iOS 中自动启用 iOS 边缘滑动返回手势。

安装

npm install @capgo/capacitor-transitions

由于该包不添加原生插件 code,所以没有原生同步步骤。

注册元素

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 示例

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}>{/* routes */}</cap-router-outlet>;
}

export function InboxPage() {
  const navigate = useNavigate();
  const pageRef = useRef<HTMLElement>(null);

  useEffect(() => {
    if (!pageRef.current) return;
    return setupPage(pageRef.current);
  }, []);

  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 还报告说 "does not exist on",请添加以下文件: cap-router-outlet 对于 Vite、Create React App 和大多数 webpack React 应用,仅将该文件放在 "下即可。对于 Next.js 或自定义 TypeScript 设置,请确保它被包含在 "中: JSX.IntrinsicElements启用滑动返回

// src/capgo-transitions.d.ts
import '@capgo/capacitor-transitions/react';

使用 "启用该手势,只有当 __CAPGO_KEEP_0__ 报告本地 iOS 运行时时: src/ 您还可以从 JavaScript 强制启用: tsconfig.json:

{
  "include": ["src", "src/capgo-transitions.d.ts"]
}

手势在过渡期间跟随手指,然后在用户释放时完成或取消。添加 "到按钮、滑块或抽屉上,应不启动边缘手势:

使用本机导航 swipe-gesture="auto" to enable the gesture only when Capacitor reports a native iOS runtime:

<cap-router-outlet swipe-gesture="auto"></cap-router-outlet>

For Next.js or custom TypeScript setups, make sure it is included by

const outlet = document.querySelector('cap-router-outlet');

outlet?.setSwipeGesture(true);
outlet?.setSwipeGesture(false);
outlet?.setSwipeGesture('auto');

The React subpath includes JSX typings for the custom elements. If TypeScript still reports that data-swipe-gesture-ignore does not exist on

Add

安装 @capgo/native-navigation 当原生层应该拥有导航栏或标签栏时:

npm install @capgo/native-navigation
npx cap sync

配置原生浏览器,然后保持 WebView 页面的过渡效果:

import { NativeNavigation } from '@capgo/native-navigation';
import { setDirection } from '@capgo/capacitor-transitions/react';
import { router } from './router';

await NativeNavigation.configure({
  contentInsetMode: 'css',
});

await NativeNavigation.setNavbar({
  title: 'Inbox',
  backButton: { visible: false },
});

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-content 为动画页面内容和原生导航 CSS 变量进行 insets:

<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);
}

不要将原生导航栏作为移动的 Web 头重复。 @capgo/native-navigation 保持原生栏; @capgo/capacitor-transitions 在它下面动画页面内容。

全局参考

继续使用@capgo/capacitor-transitions

如果您正在使用 使用@capgo/capacitor-transitions 如果您正在使用@__CAPGO_KEEP_0__/__CAPGO_KEEP_1__-transitions 连接@capgo/capacitor-transitions 查看@capgo/capacitor-transitions 开始 查看开始 使用@capgo/capacitor-live-activities 查看@capgo/capacitor-live-activities 查看@capgo/capacitor-live-activities 查看@capgo/capacitor-live-activities 使用 @capgo/capacitor-视频播放器 为 @capgo/capacitor-视频播放器 的原生能力