跳过内容

Getting Started

GitHub

您可以使用我们的 AI 助手设置来安装插件。使用以下命令将 Capgo 技能添加到您的 AI 工具中:

终端窗口
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-native-navigation` plugin in my project.

如果您prefer Manual Setup,安装插件并运行以下命令,然后按照以下平台特定的说明进行操作:

  1. 安装包

    终端窗口
    npm i @capgo/capacitor-native-navigation
  2. 同步本地项目

    终端窗口
    npx cap sync
  3. 配置本地Chrome

    import { NativeNavigation } from '@capgo/capacitor-native-navigation';
    await NativeNavigation.configure({
    contentInsetMode: 'css',
    animationDuration: 360,
    colors: {
    tint: '#0f172a',
    inactiveTint: '#64748b',
    },
    });
  4. 渲染本地导航栏

    await NativeNavigation.setNavbar({
    title: 'Home',
    subtitle: 'Native chrome',
    transparent: true,
    backButton: { visible: false },
    rightItems: [
    {
    id: 'compose',
    title: 'Compose',
    icon: {
    svg: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M12 20h9"/><path d="M16.5 3.5a2.12 2.12 0 0 1 3 3L7 19l-4 1 1-4Z"/></svg>',
    },
    },
    ],
    });
  5. 渲染本地标签栏

    await NativeNavigation.setTabbar({
    selectedId: 'home',
    labelVisibilityMode: 'selected',
    icons: true,
    colors: {
    dynamic: true,
    tint: '#0f172a',
    inactiveTint: '#64748b',
    },
    tabs: [
    {
    id: 'home',
    title: 'Home',
    icon: {
    svg: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M3 10.5 12 3l9 7.5"/><path d="M5 10v10h14V10"/></svg>',
    },
    },
    {
    id: 'settings',
    title: 'Settings',
    badge: '2',
    icon: {
    svg: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="3"/><path d="M12 2v3M12 19v3M2 12h3M19 12h3"/></svg>',
    },
    },
    ],
    });
  6. 处理本机意图事件

    await NativeNavigation.addListener('navbarBack', () => {
    router.back();
    });
    await NativeNavigation.addListener('navbarItemTap', ({ id }) => {
    if (id === 'compose') router.push('/compose');
    });
    await NativeNavigation.addListener('tabSelect', ({ id }) => {
    router.push(`/${id}`);
    });

本机过渡是对正常 JavaScript 路由更改的交易:

const transition = await NativeNavigation.beginTransition({
direction: 'forward',
});
router.push('/detail');
await router.ready?.();
await NativeNavigation.setNavbar({
title: 'Detail',
backButton: { visible: true, title: 'Back' },
});
await NativeNavigation.finishTransition({
id: transition.id,
direction: 'forward',
});

使用缩放助手进行卡片到详细信息或媒体预览流程。将点击元素传递给路由器更改内容之前,然后在详细页面准备好后完成。

import { beginZoomTransition, finishZoomTransition } from '@capgo/capacitor-native-navigation';
const card = document.querySelector('[data-message-card]');
if (card) {
const transition = await beginZoomTransition(card, { cornerRadius: 18 });
router.push('/message/42');
await router.ready?.();
await NativeNavigation.setNavbar({
title: 'Message',
backButton: { visible: true, title: 'Inbox' },
});
await finishZoomTransition(undefined, {
id: transition.id,
cornerRadius: 18,
});
}

与 @capgo/capacitor-transitions 使用

标题:与 @capgo/capacitor-transitions 使用

使用原生导航来使用原生navbar、tabbar、安全区域 insets、和原生意图事件。使用 @capgo/capacitor-transitions 为WebView页面堆栈在原生chrome下

终端窗口
npm install @capgo/capacitor-native-navigation @capgo/capacitor-transitions
npx cap sync

初始化两次包:

import { NativeNavigation } from '@capgo/capacitor-native-navigation';
import '@capgo/capacitor-transitions';
import { initTransitions, setupRouterOutlet, setDirection } from '@capgo/capacitor-transitions/react';
initTransitions({ platform: 'auto' });
const outlet = document.querySelector('cap-router-outlet');
if (outlet) {
setupRouterOutlet(outlet, { platform: 'auto', swipeGesture: 'auto' });
}
await NativeNavigation.configure({
contentInsetMode: 'css',
});

__CAPGO_KEEP_0__ cap-router-outlet 保持

<cap-router-outlet platform="auto" swipe-gesture="auto">
<cap-page>
<cap-content slot="content" fullscreen>
<main class="page">Inbox content</main>
</cap-content>
</cap-page>
</cap-router-outlet>

复制到剪贴板

async function openMessage(id: string) {
setDirection('forward');
await router.push(`/messages/${id}`);
await NativeNavigation.setNavbar({
title: 'Message',
backButton: { visible: true, title: 'Inbox' },
});
}
await NativeNavigation.addListener('navbarBack', () => {
setDirection('back');
router.back();
});
await NativeNavigation.addListener('tabSelect', ({ id }) => {
setDirection('root');
router.push(`/${id}`);
});

复制到剪贴板 @capgo/capacitor-transitions animate normal page pushes, and use Native Navigation’s zoom helpers only for shared-element or zoom routes.

使用 contentInsetMode: 'css'With document.documentElement.

.page {
padding-top: var(--cap-native-navigation-top);
padding-bottom: var(--cap-native-navigation-bottom);
}

Copy to clipboard

  • --cap-native-navigation-top
  • --cap-native-navigation-right
  • --cap-native-navigation-bottom
  • --cap-native-navigation-left
  • --cap-native-navbar-height
  • --cap-native-tabbar-height

Available variables:

Icon descriptors

Section titled “Icon descriptors”

const icon = {
svg: '<svg viewBox="0 0 24 24"><path d="M3 10.5 12 3l9 7.5"/></svg>',
width: 24,
height: 24,
template: true,
src: 'fallback_asset_name',
ios: {
svg: '<svg viewBox="0 0 24 24"><path d="M3 10.5 12 3l9 7.5"/></svg>',
sfSymbol: 'house.fill',
image: 'BundledAssetName',
},
android: {
svg: '<svg viewBox="0 0 24 24"><path d="M3 10.5 12 3l9 7.5"/></svg>',
resource: 'ic_menu_view',
image: 'bundled_drawable_name',
},
};

Copy to clipboard path, line, polyline, polygon, circle, 和 rect. SVG icons 是通过模板图像渲染的,如此可以让原生颜色重绘它们。

该包可以为框架无关的声明式设置注册自定义元素:

import { defineNativeNavigationElements } from '@capgo/capacitor-native-navigation';
defineNativeNavigationElements();
<cap-native-navigation-provider enabled="true" content-inset-mode="css"></cap-native-navigation-provider>
<cap-native-navbar
title="Home"
transparent
right-items='[{"id":"compose","title":"Compose","icon":{"svg":"<svg viewBox=\"0 0 24 24\"><path d=\"M12 20h9\"/></svg>"}}]'
></cap-native-navbar>
<cap-native-tabbar
selected-id="home"
tabs='[{"id":"home","title":"Home","icon":{"ios":{"sfSymbol":"house.fill"}}}]'
></cap-native-tabbar>
  • iOS渲染 UINavigationBarUITabBar; iOS 26+ 使用系统 Liquid Glass 条件栏行为。
  • Android渲染一个 AppCompat 工具栏和 Material 底部导航。
  • Web fallback 不绘制原生栏。它反射事件和浏览器开发的 inset 变量。
  • 插件保留一个全屏 Capacitor WebView。原生拥有框架、栏、安全区域报告和过渡壳。

从 Getting Started 继续

标题:从 Getting Started 继续

如果您正在使用 Getting Started 来规划原生媒体和界面行为,连接它与 使用 @capgo/capacitor-native-navigation 为 @capgo/capacitor 原生导航使用 使用 @capgo/capacitor-live-activities 为 @capgo/capacitor-live-activities 原生能力使用 @capgo/capacitor-live-activities 在 @capgo/capacitor-live-activities 中实现细节 使用 @capgo/capacitor-video-player 为 @capgo/capacitor-video-player 原生能力使用 @capgo/capacitor-video-player 在 @capgo/capacitor-video-player 中实现细节