Langsung ke Konten

Pemula

GitHub

Anda dapat menggunakan Pengaturan Bantu AI untuk menginstal plugin. Tambahkan Capgo kemampuan ke alat AI Anda menggunakan perintah berikut:

Tampilan jendela terminal
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins

Lalu gunakan prompt berikut:

Use the `capacitor-plugins` skill from `Cap-go/capgo-skills` to install the `@capgo/capacitor-native-navigation` plugin in my project.

Jika Anda lebih suka Pengaturan Manual, instal plugin dengan menjalankan perintah-perintah berikut dan ikuti instruksi spesifik platform di bawah ini:

  1. Instal paket

    Jendela terminal
    npm i @capgo/capacitor-native-navigation
  2. Sinkronkan proyek native

    Jendela terminal
    npx cap sync
  3. Konfigurasi chrome native

    import { NativeNavigation } from '@capgo/capacitor-native-navigation';
    await NativeNavigation.configure({
    contentInsetMode: 'css',
    animationDuration: 360,
    colors: {
    tint: '#0f172a',
    inactiveTint: '#64748b',
    },
    });
  4. Tampilkan navbar native

    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. Tampilkan tabbar native

    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. Menangani event-intent native

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

Transisi native adalah transaksi sekeliling perubahan jalur JavaScript normal Anda:

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

Pakai bantuan zoom untuk alur kartu-ke-detail atau media-preview. Berikan elemen yang diklik sebelum perubahan konten router Anda, lalu selesaikan setelah halaman detail siap.

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

Pakai dengan @capgo/capacitor-transisi

Judul bagian “Pakai dengan @capgo/capacitor-transisi”

Gunakan Native Navigation untuk navbar native, tabbar, insets area aman, dan event intent native. Gunakan @capgo/capacitor-transitions untuk stack halaman WebView di bawah chrome native.

Jendela Terminal
npm install @capgo/capacitor-native-navigation @capgo/capacitor-transitions
npx cap sync

Mulai kedua paket sekali:

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

Tetapkan cap-router-outlet Terfokus pada halaman, bukan bar web duplikat:

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

Antar kedua paket dari aksi router yang sama:

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

Pilih satu layer animasi per perubahan rute. Biarkan @capgo/capacitor-transitions animasi push halaman normal, dan gunakan bantuan Native Navigation hanya untuk rute elemen bersama atau rute zoom.

Dengan contentInsetMode: 'css', plugin ini menulis dimensi bar native ke document.documentElement.

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

Variabel tersedia:

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

Ikon harus dapat di serialisasi karena antarmuka native yang menampilkannya. Anda dapat menggunakan SVG lintas platform, SVG spesifik platform, SF Symbols, gambar iOS yang dibundel, sumber daya drawable Android, atau gambar Android yang dibundel.

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',
},
};

SVG inline mendukung subset ikon yang difokuskan oleh set ikon umum seperti Lucide dan Feather: path, line, polyline, polygon, circledan rectGambar SVG dirender sebagai gambar template secara default, sehingga warna tinta asli dapat mengubahnya.

Paket ini dapat mendaftarkan elemen-elemen kustom untuk pengaturan deklaratif yang tidak bergantung pada framework:

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 mengrenderkan UINavigationBar dan UITabBar; iOS 26+ menggunakan Liquid Glass bar perilaku sistem.
  • Android mengrenderkan toolbar AppCompat dan navigasi bawah Material.
  • Jatuh kembali ke fallback web tidak menggambar bar native. Ini memantulkan acara dan variabel inset untuk pengembangan browser.
  • The plugin keeps one full-screen Capacitor WebView. Native owns the frame, bars, safe-area reporting, and transition shell.

Jika Anda menggunakan Getting Started untuk merencanakan media native dan perilaku antarmuka, hubungkannya dengan Gunakan @capgo/capacitor-native-navigation untuk kemampuan asli dalam Menggunakan @capgo/capacitor-native-navigation, Menggunakan @capgo/capacitor-live-activities untuk kemampuan asli dalam Menggunakan @capgo/capacitor-live-activities, @capgo/capacitor-live-activities untuk detail implementasi dalam @capgo/capacitor-live-activities, Menggunakan @capgo/capacitor-video-player untuk kemampuan asli dalam Menggunakan @capgo/capacitor-video-player, dan @capgo/capacitor-video-player untuk detail implementasi dalam @capgo/capacitor-video-player.