-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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
[docs] Add <Link>
and <Image>
docs and examples on Next.js integration page
#45404
Comments
For Next.js Link I currently have integration like this: import NextLink from 'next/link'
const LinkBehaviour = forwardRef<
HTMLAnchorElement,
ComponentProps<typeof NextLink>
>(function LinkBehaviour(props, ref) {
return <NextLink ref={ref} {...props} />
})
export const theme = createTheme({
components: {
MuiLink: {
defaultProps: {
component: LinkBehaviour,
},
},
}) I am not sure it will work with Button's href prop though. I also am not sure how to integrate <Avatar>
<Image src="/profile.jpg" alt="Profile" width={40} height={40} />
</Avatar> |
We have example repos for Next.js integration, e.g. https://github.com/mui/material-ui/tree/master/examples/material-ui-nextjs-ts Example usage of Link/NextLink is here: https://github.com/mui/material-ui/blob/master/examples/material-ui-nextjs-ts/src/app/page.tsx#L25-L27
AFAIK Next.js Image is just a component, Material UI doesn't have one, so there isn't anything to "integrate" like Link/NextLink |
It passes it as component in one place, which mean there is huge chance that somebody will forget to change Link or Button with href to it. The issue with Link can be solved with theme. But I wonder if Button with href would render MUI Link despite Next.js defined one in theme or would it render Next.js Link. It can be beneficial to cover this in docs for example as a lot of people would just use Next.js integration guide. I personally didn't even know the example repo existed
I mean to have the mui components use Image from next.js instead of normal img component. So, for example: <Avatar alt="Remy Sharp" src="/static/images/avatar/1.jpg" /> Would actually render Next Image instead of default img element. Is this possible with Mui? |
For Avatar, the image can be replaced with NextImage with the import Image from 'next/image'
<Avatar slots={{
img: Image
}} /> We're still standardizing For Link, it's kind of documented all over the place:
I agree that it's probably better to centralize all the Next.js/Link related info on the dedicated Next.js integration page |
<Link>
and <Image>
Integration with MUI Components<Link>
and <Image>
docs and examples on Next.js integration page
@mj12albert, indeed it works. Though, it is a bit more tricky. Next So, for example, this code works but throws ts error that img and Image types not match: <Avatar
src={company.logoUrl ? company.logoUrl : undefined}
slots={{
img: Image,
}}
slotProps={{
img: {
width: 164,
height: 164,
},
}}
sx={{ width: 164, height: 164 }}
/> I assume there we can do the type augmentation, but I then I get an typescript error that As a temporary workaround I created // AppAvatar.tsx
import type { AvatarProps } from '@mui/material'
// eslint-disable-next-line no-restricted-imports
import { Avatar } from '@mui/material'
import Image from 'next/image'
export interface AppAvatarProps {
src?: string
alt: string
width?: number
height?: number
}
export function AppAvatar<
RootComponent extends React.ElementType = 'div',
AdditionalProps = object,
>({
src,
alt,
width = 40,
height = 40,
sx,
...props
}: AppAvatarProps & AvatarProps<RootComponent, AdditionalProps>) {
return (
<Avatar
sx={{
width: width,
height: height,
...sx,
}}
{...props}
>
{src && <Image src={src} width={width} height={height} alt={alt} />}
</Avatar>
)
} // eslint.config.mjs
'no-restricted-imports': [
2,
...
{
name: '@mui/material',
importNames: ['Avatar'],
message: 'Please use `@/shared/ui/AppAvatar` instead.',
},
{
name: '@mui/material/Avatar',
message: 'Please use `@/shared/ui/AppAvatar` instead.',
},
] |
I see ~ in that case I would suggest opening a separate issue for better integrating Avatar with Next.js Image using the |
Related page
https://mui.com/material-ui/integrations/nextjs/
Kind of issue
Missing information
Issue description
Next.js has its own routing and image optimization mechanisms, but I don't see docs mentioning it. Should we add it?
Context
Building Next.js app with MUI
Search keywords: NextLink NextImage Next Link Next Image
The text was updated successfully, but these errors were encountered: