Zum Hauptinhalt springen
Zurück zu Plugins
@capgo/native-navigation
Tutorial
von github.com/Cap-go

Native Navigation

Rendern Sie native Navigationsleisten, Tabellen und Übergangshüllen über einem vollbildigen Capacitor WebView

Richtlinie

Tutorial zur nativen Navigation

Mit @capgo/native-navigation

@capgo/native-navigation renders native top Navigation, bottom Tab Chrome und Routenübergangshüllen über eine einzelne volle Bildschirm Capacitor WebView. Ihre Webframework besitzt noch immer Routen und Inhalte, während native die App-Frame besitzt.

Installieren und synchronisieren

bun add @capgo/native-navigation
bunx cap sync

Konfigurieren Sie das native Frame

import { NativeNavigation } from '@capgo/native-navigation';

await NativeNavigation.configure({
  contentInsetMode: 'css',
  animationDuration: 360,
  colors: {
    tint: '#0f172a',
    inactiveTint: '#64748b',
  },
});

Ein nativer Navbar rendern

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

Ein nativer Tabbar rendern

await NativeNavigation.setTabbar({
  selectedId: 'inbox',
  labels: true,
  icons: true,
  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>',
      },
    },
  ],
});

Verbinden Sie native Ereignisse mit Ihrem Router

Native Bars senden Absicht. Ihr Router führt noch immer die Routenänderung durch:

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

Animate route Änderungen

Verwenden Sie eine Übergangstransaktion um Ihre normale Web-Route-Update zu umgeben:

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

Füllen Sie Inhalte mit nativen Einstellungen

Wenn contentInsetMode ist css, schreibt das Plugin CSS-Variablen für die nativen Balken:

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

Iconauswahl

Icons sind native Beschreibungen, nicht React- oder Vue-Node. Verwenden Sie SVG, wenn Sie nicht native Assets in Ihrem Bundle haben möchten:

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

Inline SVG unterstützt path, line, polyline, polygon, circle, und rect, die gängigen Icon-Sets wie Lucide und Feather abdeckt.

Vollständige Referenz