Skip to content

Commit 3f31239

Browse files
authoredAug 10, 2023
Merge pull request #9171 from marmelab/title-portal-props
Add ability to customize the page title style
2 parents dac7eed + 84d2536 commit 3f31239

File tree

5 files changed

+47
-6
lines changed

5 files changed

+47
-6
lines changed
 

‎docs/AppBar.md

+11
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,17 @@ export const MyAppBar = () => (
101101
![App bar with a settings button](./img/AppBar-children.png)
102102

103103
**Tip**: Whats the `<TitlePortal>`? It's a placeholder for the page title, that components in the page can fill using [the `<Title>` component](./Title.md). `<Title>` uses a [React Portal](https://react.dev/reference/react-dom/createPortal) under the hood. `<TitlePortal>` takes all the available space in the app bar, so it "pushes" the following children to the right.
104+
105+
**Tip**: `<TitlePortal>` renders a [Material-ui `<Typography>`](https://mui.com/material-ui/react-typography/) element that you can customize by passing your own props:
106+
107+
```jsx
108+
export const MyAppBar = () => (
109+
<AppBar>
110+
<TitlePortal variant="body2" component="h3" />
111+
<SettingsButton />
112+
</AppBar>
113+
);
114+
```
104115

105116
If you omit `<TitlePortal>`, `<AppBar>` will no longer display the page title. This can be done on purpose, e.g. if you want to render something completely different in the AppBar, like a company logo and a search engine:
106117

‎docs/Layout.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export const MyLayout = props => <Layout {...props} appBar={MyAppBar} />;
4040

4141
Then pass this custom layout to the `<Admin>` component:
4242

43-
Instead of the default layout, you can use your own component as the admin layout. Just use the layout prop of the <Admin> component:
43+
Instead of the default layout, you can use your own component as the admin layout. Just use the layout prop of the `<Admin>` component:
4444

4545
```jsx
4646
// in src/App.js
@@ -98,7 +98,7 @@ export const MyLayout = (props) => <Layout {...props} appBar={MyAppBar} />;
9898

9999
You can use [react-admin's `<AppBar>` component](./AppBar.md) as a base for your custom app bar, or the component of your choice.
100100

101-
By default, react-admin's `<AppBar>` displays the page title. You can override this default by passing children to `<AppBar>` - they will replace the default title. And if you still want to include the page title, make sure you include an element with id `react-admin-title` in the top bar (this uses [React Portals](https://react.dev/reference/react-dom/createPortal)).
101+
By default, react-admin's `<AppBar>` displays the page title. You can override this default by passing children to `<AppBar>` - they will replace the default title. And if you still want to include the page title defined by each page, make sure you include the `<TitlePortal>` element (which uses [React Portals](https://react.dev/reference/react-dom/createPortal)).
102102

103103
Here is a custom app bar component extending `<AppBar>` to include a company logo in the center of the page header:
104104

‎docs/Title.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const CustomPage = () => (
2424
);
2525
```
2626

27-
`<Title>` uses a [React Portal](https://react.dev/reference/react-dom/createPortal) to render the title outside of the current component. It works because the default [ `<AppBar>`](./AppBar.md) component contains a placeholder for the title called `<TitlePortal>`.
27+
`<Title>` uses a [React Portal](https://react.dev/reference/react-dom/createPortal) to render the title outside of the current component. It works because the default [`<AppBar>`](./AppBar.md) component contains a placeholder for the title called `<TitlePortal>`.
2828

2929
CRUD page components ([`<List>`](./List.md), [`<Edit>`](./Edit.md), [`<Create>`](./Create.md), [`<Show>`](./Show.md)) already use a `<Title>` component. To set the page title for these components, use the `title` prop.
3030

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import * as React from 'react';
2+
import { MemoryRouter } from 'react-router-dom';
3+
4+
import { TitlePortal } from './TitlePortal';
5+
import { Title } from './Title';
6+
7+
export default {
8+
title: 'ra-ui-materialui/layout/TitlePortal',
9+
};
10+
11+
export const Basic = () => (
12+
<MemoryRouter>
13+
<TitlePortal />
14+
<Title title="Hello, world" />
15+
</MemoryRouter>
16+
);
17+
18+
export const Props = () => (
19+
<MemoryRouter>
20+
<TitlePortal variant="body1" />
21+
<Title title="Hello, world" />
22+
</MemoryRouter>
23+
);
24+
25+
export const Sx = () => (
26+
<MemoryRouter>
27+
<TitlePortal sx={{ color: 'primary.main' }} />
28+
<Title title="Hello, world" />
29+
</MemoryRouter>
30+
);
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as React from 'react';
2-
import { Typography } from '@mui/material';
2+
import { Typography, TypographyProps } from '@mui/material';
33

4-
export const TitlePortal = ({ className }: { className?: string }) => (
4+
export const TitlePortal = (props: TypographyProps) => (
55
<Typography
66
flex="1"
77
textOverflow="ellipsis"
@@ -10,6 +10,6 @@ export const TitlePortal = ({ className }: { className?: string }) => (
1010
variant="h6"
1111
color="inherit"
1212
id="react-admin-title"
13-
className={className}
13+
{...props}
1414
/>
1515
);

0 commit comments

Comments
 (0)
Please sign in to comment.