# Chapitre 2 : Navigation
## Navbar
La barre de navigation supérieure. Affiche le titre et des actions.
### Syntaxe de base
```javascript
import { Navbar, UpperNavbarItem } from '@cap-rel/smartcommon';
import { FiMenu, FiUser } from 'react-icons/fi';
function MyPage() {
return (
<>
{/* Contenu */}
>
);
}
```
### Props Navbar
^ Prop ^ Type ^ Description ^
| title | string | Titre principal |
| subtitle | string | Sous-titre optionnel |
| showBack | boolean | Afficher le bouton retour |
| onBack | function | Callback du bouton retour |
### Props UpperNavbarItem
^ Prop ^ Type ^ Description ^
| icon | Component | Icône react-icons |
| onClick | function | Action au clic |
| badge | number | Compteur de notification |
## Sidebar
Menu latéral avec navigation.
### Syntaxe
```javascript
import { useState } from 'react';
import { Sidebar, Button } from '@cap-rel/smartcommon';
import { useNavigation, useGlobalStates, useApi } from '@cap-rel/smartcommon';
import { FiHome, FiList, FiSettings, FiLogOut } from 'react-icons/fi';
function MainLayout({ children }) {
const [isSidebarOpen, setIsSidebarOpen] = useState(false);
const navigate = useNavigation();
const api = useApi();
const [session] = useGlobalStates('session');
const menuItems = [
{ icon: FiHome, label: 'Accueil', path: '/' },
{ icon: FiList, label: 'Produits', path: '/products' },
{ icon: FiSettings, label: 'Paramètres', path: '/settings' }
];
const handleLogout = async () => {
await api.logout();
navigate('/login');
};
return (
<>
setIsSidebarOpen(false)}
>
{/* En-tête utilisateur */}
{session?.user?.name}
{session?.user?.email}
{/* Menu */}
{/* Déconnexion */}
{children}
>
);
}
```
### Props
^ Prop ^ Type ^ Description ^
| isOpen | boolean | Contrôle l'affichage |
| onClose | function | Callback à la fermeture |
| position | string | 'left' ou 'right' |
## Tabbar
Barre d'onglets en bas de l'écran (navigation mobile).
### Syntaxe
```javascript
import { Tabbar, TabbarItem } from '@cap-rel/smartcommon';
import { useNavigation } from '@cap-rel/smartcommon';
import { useLocation } from 'react-router-dom';
import { FiHome, FiSearch, FiShoppingCart, FiUser } from 'react-icons/fi';
function AppTabbar() {
const navigate = useNavigation();
const location = useLocation();
const tabs = [
{ icon: FiHome, label: 'Accueil', path: '/' },
{ icon: FiSearch, label: 'Recherche', path: '/search' },
{ icon: FiShoppingCart, label: 'Panier', path: '/cart', badge: 3 },
{ icon: FiUser, label: 'Profil', path: '/profile' }
];
return (
{tabs.map(tab => (
navigate(tab.path)}
/>
))}
);
}
```
### Props TabbarItem
^ Prop ^ Type ^ Description ^
| icon | Component | Icône react-icons |
| label | string | Texte sous l'icône |
| active | boolean | État sélectionné |
| badge | number | Compteur notification |
| onClick | function | Action au clic |
## useNavigation
Hook pour la navigation programmatique.
### Syntaxe
```javascript
import { useNavigation } from '@cap-rel/smartcommon';
function MyComponent() {
const navigate = useNavigation();
return (
{/* Navigation vers une route */}
{/* Navigation avec paramètre */}
{/* Retour arrière */}
{/* Remplacement (pas d'historique) */}
);
}
```
## Exemple complet : Layout avec navigation
```javascript
// components/layouts/MainLayout/index.jsx
import { useState } from 'react';
import { useLocation } from 'react-router-dom';
import {
Navbar,
Sidebar,
Tabbar,
TabbarItem,
UpperNavbarItem
} from '@cap-rel/smartcommon';
import { useNavigation, useGlobalStates, useApi } from '@cap-rel/smartcommon';
import {
FiMenu,
FiHome,
FiList,
FiPlus,
FiSettings,
FiLogOut
} from 'react-icons/fi';
export const MainLayout = ({ children, title }) => {
const [sidebarOpen, setSidebarOpen] = useState(false);
const navigate = useNavigation();
const location = useLocation();
const api = useApi();
const [session] = useGlobalStates('session');
const handleLogout = async () => {
await api.logout();
navigate('/login');
};
return (
{/* Navbar */}
setSidebarOpen(true)}
/>
{/* Sidebar */}
setSidebarOpen(false)}
>
{/* Contenu principal */}
{children}
{/* Tabbar */}
navigate('/')}
/>
navigate('/products')}
/>
navigate('/products/new')}
/>
navigate('/settings')}
/>
);
};
// Composant helper
const SidebarLink = ({ icon: Icon, label, onClick }) => (
);
```
### Utilisation du layout
```javascript
// components/pages/private/ProductsPage/index.jsx
import { MainLayout } from '../../../layouts/MainLayout';
import { Block, List, ListItem } from '@cap-rel/smartcommon';
export const ProductsPage = () => {
const products = [...];
return (
{products.map(p => (
))}
);
};
```
## Points clés à retenir
1. **Navbar** = barre supérieure avec titre et actions
2. **Sidebar** = menu latéral coulissant
3. **Tabbar** = navigation par onglets en bas
4. **useNavigation** = navigation programmatique
5. Combiner ces composants dans un **Layout réutilisable**
[[:15_training:module6-smartcommon-composants:mise-en-page|← Chapitre précédent]] | [[:15_training:module6-smartcommon-composants:start|Retour au module]] | [[:15_training:module6-smartcommon-composants:formulaires|Chapitre suivant : Formulaires →]]