@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 バーの下にネイティブセグメントの行をスタックするには、例えば上部にスコープスイッチャー、その下にセクションタブを配置する場合などです。行は上から下へ描画され、各行がナビゲーションバーとレポートされるインセットに44pt (iOS) または48dp (Android) を加算します。iOSでは UISegmentedControl とガラス調のピルを使用します。AndroidではMaterialのセグメントボタンとフィルターチップを使用します。
| 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 を再適用します。選択したIDはステートに保持し、その後の各 setNavbar 呼び出しに渡してください。選択を拒否するには、以前の selectedIdを送信してください。アイテムは次も受け付けます icon, accessibilityLabelや enabled. を渡します segments: [] 行を削除します。8.13.0 以降で利用可能です。
ネイティブのタブバーを表示
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>',
},
},
],
});
ネイティブイベントをルーターに接続
ネイティブバーはインテントを発行します。ルート変更は引き続きルーターが実行します:
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}`);
});
ルート変更のアニメーション
通常のWebルート更新を transition transaction で囲みます:
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',
});
ズームトランジションを追加
カード、グリッドアイテム、またはメディアプレビューから開くルートには、ズームヘルパーを使用してください。
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,
});
}
ネイティブインセットでコンテンツをパディング
次の場合: 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と組み合わせることで
ネイティブのnavbar、tabbar、セーフエリアのインセット、ネイティブのintentイベントには Native Navigation を使用します。また、 @capgo/capacitor-transitions ネイティブのクロームの下にある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>
同じルーターアクションで両方のパッケージを動かします:
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}`);
});
ルート変更ごとにアニメーションレイヤーは1つにしましょう。 @capgo/capacitor-transitions 通常のページプッシュはアニメーションさせ、Native Navigation のズームヘルパーは共有要素やズームのルートだけに使います。
リファレンス全文
- 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. の使い方」のネイティブ機能については

