Shared layout components for ecommerce micro-frontends - provides Navbar, Footer, and Layout with intelligent navigation for standalone and MFE modes
npm install @ecommerce-platform/shared-layoutShared layout components for ecommerce micro-frontends. Provides Navbar, Footer, and Layout components with intelligent navigation that works in both standalone and MFE (Module Federation) modes.
- Intelligent Navigation: Automatically detects standalone vs MFE mode and redirects users appropriately
- Host Fallback: In standalone mode, tries to navigate to host app first, then falls back to standalone MFE if host is unavailable
- Consistent UI: Same layout and styling across all micro-frontends
- Flexible Props: Accepts external data (basket, categories) for maximum flexibility
``bash`
npm install @ecommerce-platform/shared-layout
`tsx
import { Layout, configureNavigation } from '@ecommerce-platform/shared-layout';
import { BrowserRouter, Routes, Route } from 'react-router-dom';
// Configure navigation (optional, defaults work for most cases)
configureNavigation({
hostUrl: 'http://localhost:4200',
currentAppName: 'store', // e.g., 'store', 'checkout', 'account'
standalonePorts: {
store: 4201,
checkout: 4202,
account: 4203,
},
});
function App() {
return (
);
}
`
`tsx
import { Layout } from '@ecommerce-platform/shared-layout';
import { useBasket } from './services/basket';
function App() {
const { items, itemCount, isLoading } = useBasket();
// Map your basket items to CartItem format
const cartItems = items.map(item => ({
id: item.productId,
name: item.productName,
price: item.price,
quantity: item.quantity,
image: item.imageFile || '',
}));
return (
basketCount={itemCount}
cartItems={cartItems}
isLoading={isLoading}
onRemoveCartItem={(id) => {
// Handle remove cart item
}}
>
);
}
`
`tsx
import { Layout } from '@ecommerce-platform/shared-layout';
import { useCategories } from './services/categories';
function App() {
const { categories, isLoading } = useCategories();
return (
categories={categories.map(cat => ({
id: cat.id,
name: cat.name,
icon: cat.icon,
path: cat.path,
}))}
categoriesLoading={isLoading}
>
);
}
`
The package automatically detects if it's running in standalone mode by checking:
1. If window.__MFE_HOST__ is defined (set by host when loading MFE)
2. If the current port matches a standalone MFE port
- In MFE mode (loaded by host): Uses React Router navigation normally
- In standalone mode:
1. First tries to navigate to host app (e.g., http://localhost:4200/checkout)http://localhost:4202/checkout
2. If host is unavailable, falls back to standalone MFE (e.g., )
`tsx
import { useNavigate } from '@ecommerce-platform/shared-layout';
function MyComponent() {
const navigate = useNavigate('store'); // Pass app name for fallback
const handleClick = () => {
// Will try host first, then fallback to standalone if needed
navigate('/checkout');
};
return ;
}
`
The package includes a complete Ant Design theme configuration that matches the host app:
`tsx
import { themeConfig } from '@ecommerce-platform/shared-layout';
import { ConfigProvider } from 'antd';
function App() {
return (
);
}
`
The theme includes:
- Brand colors (primary gradient: #667eea to #764ba2)
- Typography (Poppins font family)
- Spacing, shadows, and border radius
- Component-specific styles (Button, Input, Card, Layout, Typography)
Main layout component that wraps your application.
Props:
- appName?: string - Current app name for navigation fallbackbasketCount?: number
- - Number of items in basketcartItems?: CartItem[]
- - Cart items for previewisLoading?: boolean
- - Loading state for cart dataonRemoveCartItem?: (id: string) => void
- - Callback when removing itemcategories?: Array<{id: string; name: string; icon?: string; path: string}>
- - Categories for dropdowncategoriesLoading?: boolean
- - Loading state for categorieshideNavbar?: boolean
- - Hide navbar (default: false)hideFooter?: boolean
- - Hide footer (default: false)
Standalone navbar component.
Props: Same as Layout (except hideNavbar and hideFooter)
Standalone footer component.
Props:
- appName?: string - Current app name for navigation fallback
Configure navigation settings.
Config:
- hostUrl?: string - Host app URL (default: http://localhost:4200)currentAppName?: string
- - Current MFE app namestandalonePorts?: Record
- - Map of app names to ports
Custom navigation hook that handles redirects intelligently.
Check if running in standalone mode.
`tsx
// store/src/app.tsx
import { Layout, configureNavigation } from '@ecommerce-platform/shared-layout';
configureNavigation({
currentAppName: 'store',
});
export function App() {
return (
);
}
`
`tsx
// checkout/src/app.tsx
import { Layout, configureNavigation } from '@ecommerce-platform/shared-layout';
configureNavigation({
currentAppName: 'checkout',
});
export function App() {
return (
);
}
``
MIT