-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve AppBar Customization #8681
Merged
+1,017
−321
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6515dd1
Improve AppBar features and documentation
fzaninotto 74334bb
Improve AppBar Doc
fzaninotto 88bddc3
Fix missing TitlePortal in JSDoc
fzaninotto 789858b
Fix tests
fzaninotto 662f57e
Apply suggestions from code review
fzaninotto 6a4e716
Add images
fzaninotto dcf5ab7
Typo fixes
fzaninotto 8c4b129
Fix outdated usage of AppBar in the doc
fzaninotto 88a7276
Add doc for Title component
fzaninotto 6e9ac1a
Fix configurable title outline
fzaninotto 299a325
Fix typo
fzaninotto 222d205
Add screencast for configurable mode
fzaninotto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
--- | ||
layout: default | ||
title: "The Title Component" | ||
--- | ||
|
||
# `<Title>` | ||
|
||
Set the page title (the text displayed in the app bar) from within a react-admin component. | ||
|
||
 | ||
|
||
## Usage | ||
|
||
Use `<Title>` from anywhere in the page to set the page title. | ||
|
||
```jsx | ||
import { Title } from 'react-admin'; | ||
|
||
const CustomPage = () => ( | ||
<> | ||
<Title title="My Custom Page" /> | ||
<div>Content</div> | ||
</> | ||
); | ||
``` | ||
|
||
`<Title>` uses a [React Portal](https://reactjs.org/docs/portals.html) 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>`. | ||
|
||
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. | ||
|
||
```jsx | ||
import { List } from 'react-admin'; | ||
|
||
const PostList = () => ( | ||
<List title="All posts"> | ||
... | ||
</List> | ||
); | ||
``` | ||
|
||
## Props | ||
|
||
| Prop | Required | Type | Default | Description | | ||
| ------------------- | -------- | --------------------- | -------- | --------------------------------------------------------------------------- | | ||
| `title` | Optional | `string|ReactElement` | - | What to display in the central part of the app bar | | ||
| `defaultTitle` | Optional | `string` | `''` | What to display in the central part of the app bar when `title` is not set | | ||
| `preferenceKey` | Optional | `string` | ``${pathname}.title`` | The key to use in the user preferences to store a custom title | | ||
|
||
## `title` | ||
|
||
The `title` prop can be a string or a React element. | ||
|
||
If it's a string, it will be passed to [the `translate` function](./useTranslate.md), so you can use a title or a message id. | ||
|
||
```jsx | ||
import { Title } from 'react-admin'; | ||
|
||
const CustomPage = () => ( | ||
<> | ||
<Title title="my.custom.page.title" /> | ||
<div>Content</div> | ||
</> | ||
); | ||
``` | ||
|
||
If it's a React element, it will be rendered as is. If the element contains some text, it's your responsibliity to translate it. | ||
|
||
```jsx | ||
import { Title } from 'react-admin'; | ||
import ArticleIcon from '@mui/icons-material/Article'; | ||
|
||
const ArticlePage = () => ( | ||
<> | ||
<Title title={ | ||
<> | ||
<ArticleIcon /> | ||
My Custom Page | ||
</> | ||
} /> | ||
<div>My Custom Content</div> | ||
</> | ||
); | ||
``` | ||
|
||
## `defaultTitle` | ||
|
||
It often happens that the title is empty while the component fetches the data to display. To avoid a flicker, you can pass a default title to the `<Title>` component. | ||
|
||
```jsx | ||
import { Title, useGetOne } from 'react-admin'; | ||
import ArticleIcon from '@mui/icons-material/Article'; | ||
|
||
const ArticlePage = ({ id }) => { | ||
const { data, loading } = useGetOne('articles', { id }); | ||
return ( | ||
<> | ||
<Title | ||
title={data && <><ArticleIcon />{data.title}</>} | ||
defaultTitle={<ArticleIcon />} | ||
/> | ||
{!loading && <div>{data.body}</div>} | ||
</> | ||
); | ||
}; | ||
``` | ||
|
||
## `preferenceKey` | ||
|
||
In [Configurable mode](./AppBar.md#configurable), users can customize the page title via the inspector. To avoid conflicts, the `<Title>` component uses a preference key based on the current pathname. For example, the `<Title>` component in the `posts` list page will use the `posts.title` preference key. | ||
|
||
If you want to use a custom preference key, pass it to the `<Title>` component. | ||
|
||
```jsx | ||
import { Title } from 'react-admin'; | ||
|
||
const CustomPage = () => ( | ||
<> | ||
<Title title="My Custom Page" preferenceKey="my.custom.page.title" /> | ||
<div>Content</div> | ||
</> | ||
); | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
300 changes: 300 additions & 0 deletions
300
packages/ra-ui-materialui/src/layout/AppBar.stories.tsx
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import * as React from 'react'; | ||
import { Typography } from '@mui/material'; | ||
|
||
export const TitlePortal = ({ className }: { className?: string }) => ( | ||
<Typography | ||
flex="1" | ||
textOverflow="ellipsis" | ||
whiteSpace="nowrap" | ||
overflow="hidden" | ||
variant="h6" | ||
color="inherit" | ||
id="react-admin-title" | ||
className={className} | ||
/> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Found a typo, I'm not sure if this is an issue
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, can you open a PR to fix it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure!