使用 @capgo/capacitor-native-navigation
@capgo/capacitor-native-navigation 在单个全屏 Capacitor WebView 上渲染原生顶部导航、底部标签栏外壳和路由过渡外壳。Web 框架仍负责路由和内容,原生层负责应用外壳。
安装并同步
npm install @capgo/capacitor-native-navigation
npx cap sync
配置原生外壳
import { NativeNavigation } from '@capgo/capacitor-native-navigation';
await NativeNavigation.configure({
contentInsetMode: 'css',
animationDuration: 360,
colors: {
tint: '#0f172a',
inactiveTint: '#64748b',
},
});
渲染原生导航栏
await NativeNavigation.setNavbar({
title: 'Inbox',
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>',
},
},
],
});
在导航栏下方添加多级分段行
传入 segments 改为 setNavbar 在导航栏下方堆叠多行原生 segment,例如顶层是 scope 切换器,下方是 section 标签页。各行从上到下绘制,每一行都会为 navbar 和上报的 insets 增加 44pt (iOS) 或 48dp (Android)。iOS 使用 UISegmentedControl 以及玻璃胶囊;Android 使用 Material 分段按钮和 filter chips。
| iOS | Android |
|---|---|
![]() |
![]() |
const sections = {
account: ['General', 'Notifications', 'Password', 'Two-factor'],
organization: ['General', 'Members', 'Plans', 'Usage', 'Credits', 'Billing', 'Security', 'Webhooks'],
};
const selection = { scope: 'account', section: 'general' };
const renderNavbar = () =>
NativeNavigation.setNavbar({
title: 'Settings',
segments: [
{
id: 'scope',
selectedId: selection.scope,
items: [
{ id: 'account', title: 'Account' },
{ id: 'organization', title: 'Organization' },
],
},
{
id: 'section',
style: 'scroll', // scrolling chips; `auto` uses `segmented` for up to 4 items
selectedId: selection.section,
items: sections[selection.scope].map((title) => ({ id: title.toLowerCase(), title })),
},
],
});
await NativeNavigation.addListener('navbarSegmentSelect', async ({ groupId, id }) => {
if (groupId === 'scope') {
selection.scope = id;
selection.section = 'general'; // the second level depends on the first one
} else {
selection.section = id;
}
await renderNavbar();
});
await renderNavbar();
用户点击时,选中状态会原生切换,但每次 setNavbar 调用都会重新应用 selectedId 。请将选中的 ids 保留在 state 中,并在之后每次 setNavbar 调用时传入;若要取消选中,请发送上一次的 selectedId。Item 还接受 icon, accessibilityLabel,以及 enabled。传入 segments: [] 用于移除这些行。自 8.13.0 起可用。
渲染原生 Tabbar
await NativeNavigation.setTabbar({
selectedId: 'inbox',
labelVisibilityMode: 'selected',
icons: true,
colors: {
dynamic: true,
tint: '#0f172a',
inactiveTint: '#64748b',
},
tabs: [
{
id: 'inbox',
title: 'Inbox',
icon: {
svg: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M4 4h16v16H4z"/><path d="m4 13 4 4h8l4-4"/></svg>',
},
},
{
id: 'search',
title: 'Search',
icon: {
svg: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="11" cy="11" r="7"/><path d="m20 20-3-3"/></svg>',
},
},
],
});
将原生事件接入你的 Router
原生栏会发出 intent。路由跳转仍由你的 Router 执行:
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}`);
});
路由切换动画
用 transition transaction 包裹常规的 Web 路由更新:
const transition = await NativeNavigation.beginTransition({
direction: 'forward',
});
router.push('/message/42');
await router.ready?.();
await NativeNavigation.setNavbar({
title: 'Message',
backButton: { visible: true, title: 'Inbox' },
});
await NativeNavigation.finishTransition({
id: transition.id,
direction: 'forward',
});
添加缩放过渡动画
对从卡片、网格项或媒体预览打开的路由,使用 zoom helpers。
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,
});
}
用原生 insets 为内容添加内边距
当 contentInsetMode 为 css,该插件会为原生状态栏写入 CSS 变量:
.page {
padding-top: var(--cap-native-navigation-top);
padding-bottom: var(--cap-native-navigation-bottom);
}
图标选项
图标是原生描述符,而非 React 或 Vue 节点。若不想打包原生资源,请使用 SVG:
const icon = {
svg: '<svg viewBox="0 0 24 24"><path d="M3 10.5 12 3l9 7.5"/></svg>',
template: true,
ios: { sfSymbol: 'house.fill' },
android: { resource: 'ic_menu_view' },
};
内联 SVG 支持 path, line, polyline, polygon, circle,以及 rect,涵盖了 Lucide 和 Feather 等常用图标集。
与 @capgo/capacitor-transitions 结合
使用 Native Navigation 处理原生 navbar、tabbar、safe-area insets 和原生 Intent 事件。使用 @capgo/capacitor-transitions 用于原生 Chrome 下方的 WebView 页面栈。
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',
});
让 transition outlet 专注于页面,避免重复创建 Web 状态栏:
<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>
用同一套 router actions 驱动两个包:
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 普通页面 push 带动画,仅在共享元素或 zoom 路由中使用 Native Navigation 的 zoom 辅助方法。
完整参考
- GitHub: https://github.com/Cap-go/capacitor-native-navigation/
- 文档:/docs/plugins/native-navigation/
继续阅读:使用 @capgo/capacitor-native-navigation
如果你正在使用 使用 @capgo/capacitor-native-navigation 来规划原生媒体和界面行为,并将其与 @capgo/capacitor-native-navigation 有关 @capgo/capacitor-native-navigation 的实现细节, 快速入门 有关“入门指南”中的实现细节, 使用 @capgo/capacitor-live-activities 用于使用 @capgo/capacitor-live-activities 时的原生能力, @capgo/capacitor-live-activities 关于 @capgo/capacitor-live-activities 中的实现细节,以及 使用 @capgo/capacitor-video-player 有关“使用 @capgo/capacitor-video-player.”中的原生能力

