React Router
To integrate Toolpad Core into a single-page app (with Vite, for example) using React Router, follow these steps.
Wrap all your pages in a ReactRouterAppProvider
In your router configuration (for example src/main.tsx
), use a shared component or element (for example src/App.tsx
) as a root layout route that wraps the whole application with the ReactRouterAppProvider
from @toolpad/core/react-router
.
You must use the <Outlet />
component from react-router
in this root layout element or component.
import * as React from 'react';
import * as ReactDOM from 'react-dom/client';
import { createBrowserRouter, RouterProvider } from 'react-router';
import App from './App';
import DashboardPage from './pages';
import OrdersPage from './pages/orders';
const router = createBrowserRouter([
{
Component: App, // root layout route
},
]);
ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<RouterProvider router={router} />
</React.StrictMode>,
);
import * as React from 'react';
import DashboardIcon from '@mui/icons-material/Dashboard';
import ShoppingCartIcon from '@mui/icons-material/ShoppingCart';
import { ReactRouterAppProvider } from '@toolpad/core/react-router';
import { Outlet } from 'react-router';
import type { Navigation } from '@toolpad/core';
const NAVIGATION: Navigation = [
{
kind: 'header',
title: 'Main items',
},
{
title: 'Dashboard',
icon: <DashboardIcon />,
},
{
segment: 'orders',
title: 'Orders',
icon: <ShoppingCartIcon />,
},
];
const BRANDING = {
title: 'My Toolpad Core App',
};
export default function App() {
return (
<ReactRouterAppProvider navigation={NAVIGATION} branding={BRANDING}>
<Outlet />
</ReactRouterAppProvider>
);
}
Create a dashboard layout
Create a layout file for your dashboard pages (for example src/layouts/dashboard.tsx
), to also be used as a layout route with the <Outlet />
component from react-router
:
import * as React from 'react';
import { Outlet } from 'react-router';
import { DashboardLayout } from '@toolpad/core/DashboardLayout';
import { PageContainer } from '@toolpad/core/PageContainer';
export default function Layout() {
return (
<DashboardLayout>
<PageContainer>
<Outlet />
</PageContainer>
</DashboardLayout>
);
}
The DashboardLayout
component provides a consistent layout for your dashboard pages, including a sidebar, navigation, and header. The PageContainer
component is used to wrap the page content, and provides breadcrumbs for navigation.
You can then add this layout component to your React Router configuration (for example src/main.tsx
), as a child of the root layout route created above.
import Layout from './layouts/dashboard';
//...
const router = createBrowserRouter([
{
Component: App, // root layout route
children: [
{
path: '/',
Component: Layout,
},
],
},
]);
//...
Create pages
Create a dashboard page (for example src/pages/index.tsx
) and an orders page (src/pages/orders.tsx
).
import * as React from 'react';
import Typography from '@mui/material/Typography';
export default function DashboardPage() {
return <Typography>Welcome to Toolpad!</Typography>;
}
import * as React from 'react';
import Typography from '@mui/material/Typography';
export default function OrdersPage() {
return <Typography>Welcome to the Toolpad orders!</Typography>;
}
You can then add these page components as routes to your React Router configuration (for example src/main.tsx
). By adding them as children of the layout route created above, they are automatically wrapped with that dashboard layout:
import DashboardPage from './pages';
import OrdersPage from './pages/orders';
//...
const router = createBrowserRouter([
{
Component: App, // root layout route
children: [
{
path: '/',
Component: Layout,
children: [
{
path: '',
Component: DashboardPage,
},
{
path: 'orders',
Component: OrdersPage,
},
],
},
],
},
]);
//...
That's it! You now have Toolpad Core integrated into your single-page app with React Router!
(Optional) Set up authentication
You can use the SignInPage
component to add authentication along with an external authentication provider of your choice. The following code demonstrates the code required to set up authentication with Firebase.
Define a SessionContext
to act as the mock authentication provider
import * as React from 'react';
export interface Session {
user: {
name?: string;
email?: string;
image?: string;
};
}
interface SessionContextType {
session: Session | null;
setSession: (session: Session) => void;
loading: boolean;
}
const SessionContext = React.createContext<SessionContextType>({
session: null,
setSession: () => {},
loading: true,
});
export default SessionContext;
export const useSession = () => React.useContext(SessionContext);
Add Firebase authentication
import { initializeApp } from 'firebase/app';
import { getAuth } from 'firebase/auth';
const app = initializeApp({
apiKey: import.meta.env.VITE_FIREBASE_API_KEY,
authDomain: import.meta.env.VITE_FIREBASE_AUTH_DOMAIN,
projectId: import.meta.env.VITE_FIREBASE_PROJECT_ID,
storageBucket: import.meta.env.VITE_FIREBASE_STORAGE_BUCKET,
messagingSenderId: import.meta.env.VITE_FIREBASE_MESSAGE_SENDER_ID,
appId: import.meta.env.VITE_FIREBASE_APP_ID,
});
export const firebaseAuth = getAuth(app);
export default app;
import {
GoogleAuthProvider,
GithubAuthProvider,
signInWithPopup,
setPersistence,
browserSessionPersistence,
signInWithEmailAndPassword,
signOut,
} from 'firebase/auth';
import { firebaseAuth } from './firebaseConfig';
const googleProvider = new GoogleAuthProvider();
const githubProvider = new GithubAuthProvider();
// Sign in with Google functionality
export const signInWithGoogle = async () => {
try {
return setPersistence(firebaseAuth, browserSessionPersistence).then(async () => {
const result = await signInWithPopup(firebaseAuth, googleProvider);
return {
success: true,
user: result.user,
error: null,
};
});
} catch (error: any) {
return {
success: false,
user: null,
error: error.message,
};
}
};
// Sign in with GitHub functionality
export const signInWithGithub = async () => {
try {
return setPersistence(firebaseAuth, browserSessionPersistence).then(async () => {
const result = await signInWithPopup(firebaseAuth, githubProvider);
return {
success: true,
user: result.user,
error: null,
};
});
} catch (error: any) {
return {
success: false,
user: null,
error: error.message,
};
}
};
// Sign in with email and password
export async function signInWithCredentials(email: string, password: string) {
try {
return setPersistence(firebaseAuth, browserSessionPersistence).then(async () => {
const userCredential = await signInWithEmailAndPassword(
firebaseAuth,
email,
password,
);
return {
success: true,
user: userCredential.user,
error: null,
};
});
} catch (error: any) {
return {
success: false,
user: null,
error: error.message || 'Failed to sign in with email/password',
};
}
}
// Sign out functionality
export const firebaseSignOut = async () => {
try {
await signOut(firebaseAuth);
return { success: true };
} catch (error: any) {
return {
success: false,
error: error.message,
};
}
};
// Auth state observer
export const onAuthStateChanged = (callback: (user: any) => void) => {
return firebaseAuth.onAuthStateChanged(callback);
};
Add authentication and session data to the AppProvider
import * as React from 'react';
import DashboardIcon from '@mui/icons-material/Dashboard';
import ShoppingCartIcon from '@mui/icons-material/ShoppingCart';
import { ReactRouterAppProvider } from '@toolpad/core/react-router';
import { Outlet, useNavigate } from 'react-router';
import type { Navigation, Session } from '@toolpad/core';
import {
firebaseSignOut,
signInWithGoogle,
onAuthStateChanged,
} from './firebase/auth';
import SessionContext, { type Session } from './SessionContext';
const NAVIGATION: Navigation = [
{
kind: 'header',
title: 'Main items',
},
{
title: 'Dashboard',
icon: <DashboardIcon />,
},
{
segment: 'orders',
title: 'Orders',
icon: <ShoppingCartIcon />,
},
];
const BRANDING = {
title: 'My Toolpad Core App',
};
const AUTHENTICATION: Authentication = {
signIn: signInWithGoogle,
signOut: firebaseSignOut,
};
export default function App() {
const [session, setSession] = React.useState<Session | null>(null);
const [loading, setLoading] = React.useState(true);
const sessionContextValue = React.useMemo(
() => ({
session,
setSession,
loading,
}),
[session, loading],
);
React.useEffect(() => {
// Returns an `unsubscribe` function to be called during teardown
const unsubscribe = onAuthStateChanged((user: User | null) => {
if (user) {
setSession({
user: {
name: user.displayName || '',
email: user.email || '',
image: user.photoURL || '',
},
});
} else {
setSession(null);
}
setLoading(false);
});
return () => unsubscribe();
}, []);
return (
<ReactRouterAppProvider
navigation={NAVIGATION}
branding={BRANDING}
session={session}
authentication={AUTHENTICATION}
>
<SessionContext.Provider value={sessionContextValue}>
<Outlet />
</SessionContext.Provider>
</ReactRouterAppProvider>
);
}
Protect routes inside the dashboard layout
import * as React from 'react';
import LinearProgress from '@mui/material/LinearProgress';
import { Outlet, Navigate, useLocation } from 'react-router';
import { DashboardLayout } from '@toolpad/core/DashboardLayout';
import { PageContainer } from '@toolpad/core/PageContainer';
import { useSession } from '../SessionContext';
export default function Layout() {
const { session, loading } = useSession();
const location = useLocation();
if (loading) {
return (
<div style={{ width: '100%' }}>
<LinearProgress />
</div>
);
}
if (!session) {
// Add the `callbackUrl` search parameter
const redirectTo = `/sign-in?callbackUrl=${encodeURIComponent(location.pathname)}`;
return <Navigate to={redirectTo} replace />;
}
return (
<DashboardLayout>
<PageContainer>
<Outlet />
</PageContainer>
</DashboardLayout>
);
}
You can protect any page or groups of pages through this mechanism.
Use the SignInPage
component to create a sign-in page
'use client';
import * as React from 'react';
import { SignInPage } from '@toolpad/core/SignInPage';
import LinearProgress from '@mui/material/LinearProgress';
import { Navigate, useNavigate } from 'react-router';
import { useSession, type Session } from '../SessionContext';
import {
signInWithGoogle,
signInWithGithub,
signInWithCredentials,
} from '../firebase/auth';
export default function SignIn() {
const { session, setSession, loading } = useSession();
const navigate = useNavigate();
if (loading) {
return <LinearProgress />;
}
if (session) {
return <Navigate to="/" />;
}
return (
<SignInPage
providers={[
{ id: 'google', name: 'Google' },
{ id: 'github', name: 'GitHub' },
{ id: 'credentials', name: 'Credentials' },
]}
signIn={async (provider, formData, callbackUrl) => {
let result;
try {
if (provider.id === 'google') {
result = await signInWithGoogle();
}
if (provider.id === 'github') {
result = await signInWithGithub();
}
if (provider.id === 'credentials') {
const email = formData?.get('email') as string;
const password = formData?.get('password') as string;
if (!email || !password) {
return { error: 'Email and password are required' };
}
result = await signInWithCredentials(email, password);
}
if (result?.success && result?.user) {
// Convert Firebase user to Session format
const userSession: Session = {
user: {
name: result.user.displayName || '',
email: result.user.email || '',
image: result.user.photoURL || '',
},
};
setSession(userSession);
navigate(callbackUrl || '/', { replace: true });
return {};
}
return { error: result?.error || 'Failed to sign in' };
} catch (error) {
return {
error: error instanceof Error ? error.message : 'An error occurred',
};
}
}}
/>
);
}
Add the sign in page to the router
import * as React from 'react';
import * as ReactDOM from 'react-dom/client';
import { createBrowserRouter, RouterProvider } from 'react-router';
import App from './App';
import Layout from './layouts/dashboard';
import DashboardPage from './pages';
import OrdersPage from './pages/orders';
import SignInPage from './pages/signIn';
const router = createBrowserRouter([
{
Component: App,
children: [
{
path: '/',
Component: Layout,
children: [
{
path: '/',
Component: DashboardPage,
},
{
path: '/orders',
Component: OrdersPage,
},
],
},
{
path: '/sign-in',
Component: SignInPage,
},
],
},
]);
ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<RouterProvider router={router} />
</React.StrictMode>,
);