Saltar al contenido principal
Volver a plugins
@capgo/navegación nativa
Tutoriales
por github.com/Cap-go

Navegación nativa

Renderizar barras de navegación nativas, pestañas y conchas de transición sobre una pantalla completa de Capacitor WebView

Guía

Tutorial sobre Navegación Nativa

Usando @capgo/native-navigation

@capgo/native-navigation renderiza navegación superior nativa, barra de tabs inferior y cápsulas de transición de ruta sobre una pantalla completa de WebView Capacitor. Su framework web sigue controlando las rutas y el contenido, mientras que la nativa se encarga de la estructura de la aplicación.

Instalar y sincronizar

bun add @capgo/native-navigation
bunx cap sync

Configurar la estructura nativa

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

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

Renderizar una barra de navegación nativa

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

Renderizar una barra de tabs nativa

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

Conectar eventos nativos a su router

Las barras nativas emiten intención. Su router sigue realizando el cambio de ruta:

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

Cambiar animaciones de ruta

Utilice una transacción de transición alrededor de la actualización de ruta web normal:

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

Añadir contenido con rellenos nativos

Cuando contentInsetMode es css, el plugin escribe variables CSS para las barras nativas:

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

Opciones de iconos

Los iconos son descripciones nativas, no nodos de React o Vue. Utilice SVG cuando no desee empaquetar activos nativos:

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 línea admite path, line, polyline, polygon, circle, y rect, que cubre conjuntos de iconos comunes como Lucide y Feather.

Referencia Completa