Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ This can be done via the `branding` prop in the [AppProvider](https://mui.com/to
{{"demo": "DashboardLayoutBranding.js", "height": 400, "iframe": true}}

:::info
Optionally, a specific `branding` can also be passed as a prop to the `DashboardLayout` component itself, which properties take precedence over any properties of the `branding` prop passed to the `AppProvider`.

You may also override the default branding by passing in your own component to the `appTitle` slot, as shown in the [Slots section](#slots).
:::

Expand All @@ -46,6 +48,10 @@ The `navigation` prop in the [AppProvider](https://mui.com/toolpad/core/react-ap

The flexibility in composing and ordering these different elements allows for a great variety of navigation structures to fit your use case.

:::info
Optionally, a specific `navigation` can also be passed as a prop to the `DashboardLayout` component itself, which takes complete precedence over any `navigation` prop passed to the `AppProvider`.
:::

### Navigation links

Navigation links can be placed in the sidebar as items with the format:
Expand Down
7 changes: 7 additions & 0 deletions docs/pages/toolpad/core/api/dashboard-layout.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
"defaultSidebarCollapsed": { "type": { "name": "bool" }, "default": "false" },
"disableCollapsibleSidebar": { "type": { "name": "bool" }, "default": "false" },
"hideNavigation": { "type": { "name": "bool" }, "default": "false" },
"navigation": {
"type": {
"name": "arrayOf",
"description": "Array&lt;{ action?: node, children?: Array&lt;object<br>&#124;&nbsp;{ kind: 'header', title: string }<br>&#124;&nbsp;{ kind: 'divider' }&gt;, icon?: node, kind?: 'page', pattern?: string, segment?: string, title?: string }<br>&#124;&nbsp;{ kind: 'header', title: string }<br>&#124;&nbsp;{ kind: 'divider' }&gt;"
},
"default": "[]"
},
"sidebarExpandedWidth": {
"type": { "name": "union", "description": "number<br>&#124;&nbsp;string" },
"default": "320"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"hideNavigation": {
"description": "Whether the navigation bar and menu icon should be hidden"
},
"navigation": { "description": "Navigation definition for the dashboard." },
"sidebarExpandedWidth": { "description": "Width of the sidebar when expanded." },
"slotProps": { "description": "The props used for each slot inside." },
"slots": { "description": "The components used for each slot inside." },
Expand Down
60 changes: 51 additions & 9 deletions packages/toolpad-core/src/DashboardLayout/DashboardLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { DashboardSidebarSubNavigation } from './DashboardSidebarSubNavigation';
import { ToolbarActions } from './ToolbarActions';
import { AppTitle, AppTitleProps } from './AppTitle';
import { getDrawerSxTransitionMixin, getDrawerWidthTransitionMixin } from './utils';
import type { Branding } from '../AppProvider';
import type { Branding, Navigation } from '../AppProvider';

const AppBar = styled(MuiAppBar)(({ theme }) => ({
borderWidth: 0,
Expand Down Expand Up @@ -74,6 +74,11 @@ export interface DashboardLayoutProps {
* @default null
*/
branding?: Branding | null;
/**
* Navigation definition for the dashboard.
* @default []
*/
navigation?: Navigation;
/**
* Whether the sidebar should not be collapsible to a mini variant in desktop and tablet viewports.
* @default false
Expand Down Expand Up @@ -124,6 +129,7 @@ function DashboardLayout(props: DashboardLayoutProps) {
const {
children,
branding: brandingProp,
navigation: navigationProp,
disableCollapsibleSidebar = false,
defaultSidebarCollapsed = false,
hideNavigation = false,
Expand All @@ -139,6 +145,9 @@ function DashboardLayout(props: DashboardLayoutProps) {
const navigationContext = React.useContext(NavigationContext);
const appWindowContext = React.useContext(WindowContext);

const branding = { ...brandingContext, ...brandingProp };
const navigation = navigationProp ?? navigationContext;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's important to document that the navigation override in the DashboardLayout completely replaces the one passed in the parent AppProvider, whereas the branding prop is merged with the parent branding


const [isDesktopNavigationExpanded, setIsDesktopNavigationExpanded] =
React.useState(!defaultSidebarCollapsed);
const [isMobileNavigationExpanded, setIsMobileNavigationExpanded] = React.useState(false);
Expand Down Expand Up @@ -208,10 +217,10 @@ function DashboardLayout(props: DashboardLayoutProps) {

// If useEffect was used, the reset would also happen on the client render after SSR which we don't need
React.useMemo(() => {
if (navigationContext) {
if (navigation) {
selectedItemIdRef.current = '';
}
}, [navigationContext]);
}, [navigation]);

const isDesktopMini = !disableCollapsibleSidebar && !isDesktopNavigationExpanded;
const isMobileMini = !disableCollapsibleSidebar && !isMobileNavigationExpanded;
Expand Down Expand Up @@ -247,8 +256,6 @@ function DashboardLayout(props: DashboardLayoutProps) {
const ToolbarAccountSlot = slots?.toolbarAccount ?? Account;
const SidebarFooterSlot = slots?.sidebarFooter ?? null;

const appTitleBrandingProp = { ...brandingContext, ...brandingProp };

const getDrawerContent = React.useCallback(
(isMini: boolean, viewport: 'phone' | 'tablet' | 'desktop') => (
<React.Fragment>
Expand All @@ -262,14 +269,14 @@ function DashboardLayout(props: DashboardLayoutProps) {
flexDirection: 'column',
justifyContent: 'space-between',
overflow: 'auto',
pt: navigationContext[0]?.kind === 'header' && !isMini ? 0 : 2,
pt: navigation[0]?.kind === 'header' && !isMini ? 0 : 2,
...(hasDrawerTransitions
? getDrawerSxTransitionMixin(isNavigationFullyExpanded, 'padding')
: {}),
}}
>
<DashboardSidebarSubNavigation
subNavigation={navigationContext}
subNavigation={navigation}
onLinkClick={handleNavigationLinkClick}
isMini={isMini}
isFullyExpanded={isNavigationFullyExpanded}
Expand All @@ -287,7 +294,7 @@ function DashboardLayout(props: DashboardLayoutProps) {
handleNavigationLinkClick,
hasDrawerTransitions,
isNavigationFullyExpanded,
navigationContext,
navigation,
slotProps?.sidebarFooter,
],
);
Expand Down Expand Up @@ -368,7 +375,7 @@ function DashboardLayout(props: DashboardLayoutProps) {
* 2. Branding prop passed to the `DashboardLayout`
* 3. Branding prop passed to the `AppProvider`
*/
<AppTitle branding={appTitleBrandingProp} {...slotProps?.appTitle} />
<AppTitle branding={branding} {...slotProps?.appTitle} />
)}
</Stack>
<Stack direction="row" alignItems="center" spacing={1} sx={{ marginLeft: 'auto' }}>
Expand Down Expand Up @@ -483,6 +490,41 @@ DashboardLayout.propTypes /* remove-proptypes */ = {
* @default false
*/
hideNavigation: PropTypes.bool,
/**
* Navigation definition for the dashboard.
* @default []
*/
navigation: PropTypes.arrayOf(
PropTypes.oneOfType([
PropTypes.shape({
action: PropTypes.node,
children: PropTypes.arrayOf(
PropTypes.oneOfType([
PropTypes.object,
PropTypes.shape({
kind: PropTypes.oneOf(['header']).isRequired,
title: PropTypes.string.isRequired,
}),
PropTypes.shape({
kind: PropTypes.oneOf(['divider']).isRequired,
}),
]).isRequired,
),
icon: PropTypes.node,
kind: PropTypes.oneOf(['page']),
pattern: PropTypes.string,
segment: PropTypes.string,
title: PropTypes.string,
}),
PropTypes.shape({
kind: PropTypes.oneOf(['header']).isRequired,
title: PropTypes.string.isRequired,
}),
PropTypes.shape({
kind: PropTypes.oneOf(['divider']).isRequired,
}),
]).isRequired,
),
/**
* Width of the sidebar when expanded.
* @default 320
Expand Down