This repository has been archived by the owner on Jan 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set title of header using Portal (#177)
* Set title using Portal * cleanup
- Loading branch information
Showing
7 changed files
with
96 additions
and
27 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
{ | ||
"home": { | ||
"title": "Homepage" | ||
}, | ||
"header": { | ||
"myProjects": "My Projects", | ||
"myCourses": "My Courses", | ||
|
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 |
---|---|---|
@@ -1,4 +1,7 @@ | ||
{ | ||
"home": { | ||
"title": "Homepagina" | ||
}, | ||
"header": { | ||
"myProjects": "Mijn Projecten", | ||
"myCourses": "Mijn Vakken", | ||
|
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,14 @@ | ||
export const PageTitle = ({ title, defaultTitle, className }: {title:string,defaultTitle:string,className:string}) => { | ||
|
||
return ( | ||
<span className={className}> | ||
{!title ? ( | ||
<span>{defaultTitle}</span> | ||
) : typeof title === 'string' ? ( | ||
<span>{title}</span> | ||
) : ( | ||
title | ||
)} | ||
</span> | ||
); | ||
}; |
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,51 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { createPortal } from 'react-dom'; | ||
import PropTypes from 'prop-types'; | ||
|
||
export const Title = (props: TitleProps) => { | ||
const { defaultTitle, title } = props; | ||
const [container, setContainer] = useState(() => | ||
typeof document !== 'undefined' | ||
? document.getElementById('react-admin-title') | ||
: null | ||
); | ||
|
||
// on first mount, we don't have the container yet, so we wait for it | ||
useEffect(() => { | ||
setContainer(container => { | ||
const isInTheDom = | ||
typeof document !== 'undefined' && | ||
document.body.contains(container); | ||
if (container && isInTheDom) return container; | ||
return typeof document !== 'undefined' | ||
? document.getElementById('react-admin-title') | ||
: null; | ||
}); | ||
}, []); | ||
|
||
if (!container) return null; | ||
|
||
return createPortal( | ||
<>{title || defaultTitle}</>, | ||
container | ||
); | ||
}; | ||
|
||
export const TitlePropType = PropTypes.oneOfType([ | ||
PropTypes.string, | ||
PropTypes.element, | ||
]); | ||
|
||
Title.propTypes = { | ||
defaultTitle: PropTypes.string, | ||
className: PropTypes.string, | ||
record: PropTypes.any, | ||
title: TitlePropType, | ||
}; | ||
|
||
export interface TitleProps { | ||
className?: string; | ||
defaultTitle?: string; | ||
title?: string; | ||
preferenceKey?: string; | ||
} |
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,14 @@ | ||
import { Typography, TypographyProps } from '@mui/material'; | ||
|
||
export const TitlePortal = (props: TypographyProps) => ( | ||
<Typography | ||
flex="1" | ||
textOverflow="ellipsis" | ||
whiteSpace="nowrap" | ||
overflow="hidden" | ||
variant="h6" | ||
color="inherit" | ||
id="react-admin-title" | ||
{...props} | ||
/> | ||
); |
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 |
---|---|---|
@@ -1,12 +1,16 @@ | ||
|
||
import { useTranslation } from "react-i18next"; | ||
import { Title } from "../../components/Header/Title"; | ||
/** | ||
* This component is the home page component that will be rendered when on the index route. | ||
* @returns - The home page component | ||
*/ | ||
export default function Home() { | ||
const { t } = useTranslation("translation", { keyPrefix: "home" }); | ||
return ( | ||
<div> | ||
<h1></h1> | ||
</div> | ||
<> | ||
<Title title={t('title')} /> | ||
<div> | ||
</div> | ||
</> | ||
); | ||
} |