Passer au contenu principal
Retour aux plugins
@capgo/navigation native
Tutoriel
par github.com/Cap-go

Navigation native

Rendre les barres de navigation native, les onglets et les coques de transition sur une fenêtre WebView plein écran Capacitor

Guide

Tutoriel sur la navigation native

Utilisation de @capgo/native-navigation

@capgo/native-navigation affiche une navigation native en haut, une barre d'onglets en bas, et des coquilles de transition de route sur une seule écran Capacitor WebView. Votre framework web possède toujours les routes et le contenu, tandis que la nativité possède la structure de l'application.

Installer et synchroniser

bun add @capgo/native-navigation
bunx cap sync

Configurer la structure native

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

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

Afficher un navbar natif

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

Afficher un tabbar natif

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

Connecter les événements natifs à votre routeur

Les barres natives émettent une intention. Votre routeur effectue toujours la mise à jour de la route :

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

Animer les changements de route

Utilisez une transaction de transition autour de la mise à jour normale de la route 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',
});

Faites correspondre le contenu avec les insets natives

Lorsque contentInsetMode est css, le plugin écrit les variables CSS pour les barres natives :

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

Choix d'icônes

Les icônes sont descripteurs natives, pas des nœuds React ou Vue. Utilisez SVG lorsque vous ne voulez pas charger les actifs natifs :

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 en ligne prend en charge path, line, polyline, polygon, circle, et rect, qui couvre les ensembles d'icônes courants comme Lucide et Feather.

Référence complète