Skip to content

Commit ddfaff2

Browse files
fix: no-img-element lint warnings by using next/image
1 parent 40ea071 commit ddfaff2

2 files changed

Lines changed: 52 additions & 17 deletions

File tree

src/components/Layout/HomeContent.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import CodeBlock from 'components/MDX/CodeBlock';
3434
import {ExternalLink} from 'components/ExternalLink';
3535
import sidebarBlog from '../../sidebarBlog.json';
3636
import * as React from 'react';
37+
import uwuLogo from '../../../public/images/uwu.png';
3738
import Image from 'next/image';
3839

3940
function Section({children, background = null}) {
@@ -509,11 +510,11 @@ export function HomeContent() {
509510

510511
<div className="mt-20 px-5 lg:px-0 mb-6 max-w-4xl text-center text-opacity-80">
511512
<div className="uwu-visible flex justify-center">
512-
<img
513+
<Image
513514
alt="logo by @sawaratsuki1004"
514515
title="logo by @sawaratsuki1004"
515516
className="uwu-visible mb-10 lg:mb-8 h-24 lg:h-32"
516-
src="/images/uwu.png"
517+
src={uwuLogo}
517518
/>
518519
</div>
519520
<Logo className="uwu-hidden text-brand dark:text-brand-dark w-24 lg:w-28 mb-10 lg:mb-8 mt-12 h-auto mx-auto self-start" />
@@ -789,24 +790,26 @@ const CommunityImages = memo(function CommunityImages({isLazy}) {
789790
<div
790791
key={i}
791792
className={cn(
792-
`group flex justify-center px-5 min-w-[50%] lg:min-w-[25%] rounded-2xl`
793+
'group flex justify-center px-5 min-w-[50%] lg:min-w-[25%] rounded-2xl'
793794
)}>
794795
<div
795796
className={cn(
796-
'h-auto rounded-2xl before:rounded-2xl before:absolute before:pointer-events-none before:inset-0 before:transition-opacity before:-z-1 before:shadow-lg lg:before:shadow-2xl before:opacity-0 before:group-hover:opacity-100 transition-transform ease-in-out duration-300',
797+
'h-auto rounded-2xl before:rounded-2xl before:absolute before:pointer-events-none before:inset-0 before:transition-opacity before:-z-1 before:shadow-lg lg:before:shadow-2xl before:opacity-0 before:group-hover:opacity-100 transition-transform ease-in-out duration-300',
797798
i % 2 === 0
798799
? 'rotate-2 group-hover:rotate-[-1deg] group-hover:scale-110'
799800
: 'group-hover:rotate-1 group-hover:scale-110 rotate-[-2deg]'
800801
)}>
801802
<div
802803
className={cn(
803-
'overflow-clip relative before:absolute before:inset-0 before:pointer-events-none before:-translate-x-full group-hover:before:animate-[shimmer_1s_forwards] before:bg-gradient-to-r before:from-transparent before:via-white/10 before:to-transparent transition-transform ease-in-out duration-300'
804+
'overflow-clip relative aspect-[4/3] before:absolute before:inset-0 before:pointer-events-none before:-translate-x-full group-hover:before:animate-[shimmer_1s_forwards] before:bg-gradient-to-r before:from-transparent before:via-white/10 before:to-transparent transition-transform ease-in-out duration-300'
804805
)}>
805-
<img
806+
<Image
806807
loading={isLazy ? 'lazy' : 'eager'}
807808
src={src}
808809
alt={alt}
809-
className="aspect-[4/3] h-full w-full flex object-cover rounded-2xl bg-gray-10 dark:bg-gray-80"
810+
fill
811+
sizes="(min-width: 1024px) 25vw, 50vw"
812+
className="object-cover rounded-2xl bg-gray-10 dark:bg-gray-80"
810813
/>
811814
</div>
812815
</div>
@@ -1564,7 +1567,7 @@ function Cover({background, children}) {
15641567
<div className="absolute inset-0 px-4 py-2 flex items-end bg-gradient-to-t from-black/40 via-black/0">
15651568
{children}
15661569
</div>
1567-
<img
1570+
<Image
15681571
src={background}
15691572
width={500}
15701573
height={263}
@@ -1641,10 +1644,12 @@ function Thumbnail({video}) {
16411644
<>
16421645
<div className="transition-opacity mt-2.5 -space-x-2 flex flex-row w-full justify-center">
16431646
{image.speakers.map((src, i) => (
1644-
<img
1647+
<Image
16451648
key={i}
16461649
className="h-8 w-8 border-2 shadow-md border-gray-70 object-cover rounded-full"
16471650
src={src}
1651+
width={32}
1652+
height={32}
16481653
alt=""
16491654
/>
16501655
))}

src/components/MDX/MDXComponents.tsx

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {Children, useContext, useMemo} from 'react';
1313
import * as React from 'react';
1414
import cn from 'classnames';
1515
import type {HTMLAttributes} from 'react';
16+
import NextImage from 'next/image';
1617

1718
import CodeBlock from './CodeBlock';
1819
import {CodeDiagram} from './CodeDiagram';
@@ -297,6 +298,21 @@ const IllustrationContext = React.createContext<{
297298
isInBlock: false,
298299
});
299300

301+
function toNumber(value: unknown, fallback: number) {
302+
if (typeof value === 'number' && Number.isFinite(value)) {
303+
return value;
304+
}
305+
306+
if (typeof value === 'string') {
307+
const parsed = Number(value);
308+
if (Number.isFinite(parsed) && parsed > 0) {
309+
return parsed;
310+
}
311+
}
312+
313+
return fallback;
314+
}
315+
300316
function Illustration({
301317
caption,
302318
src,
@@ -315,10 +331,12 @@ function Illustration({
315331
return (
316332
<div className="relative group before:absolute before:-inset-y-16 before:inset-x-0 my-16 mx-0 2xl:mx-auto max-w-4xl 2xl:max-w-6xl">
317333
<figure className="my-8 flex justify-center">
318-
<img
334+
<NextImage
319335
src={src}
320336
alt={alt}
321-
style={{maxHeight: 300}}
337+
width={1200}
338+
height={630}
339+
style={{maxHeight: 300, width: 'auto', height: 'auto'}}
322340
className="rounded-lg"
323341
/>
324342
{caption ? (
@@ -351,11 +369,13 @@ function IllustrationBlock({
351369
const images = imageInfos.map((info, index) => (
352370
<figure key={index}>
353371
<div className="bg-white rounded-lg p-4 flex-1 flex xl:p-6 justify-center items-center my-4">
354-
<img
372+
<NextImage
355373
className="text-primary"
356374
src={info.src}
357375
alt={info.alt}
358-
height={info.height}
376+
width={toNumber(info.width, 1200)}
377+
height={toNumber(info.height, 630)}
378+
style={{height: 'auto'}}
359379
/>
360380
</div>
361381
{info.caption ? (
@@ -485,9 +505,19 @@ function YouTubeIframe(props: any) {
485505
);
486506
}
487507

488-
function Image(props: any) {
489-
const {alt, ...rest} = props;
490-
return <img alt={alt} className="max-w-[calc(min(700px,100%))]" {...rest} />;
508+
function MdxImage(props: any) {
509+
const {alt = '', width, height, className, style, ...rest} = props;
510+
511+
return (
512+
<NextImage
513+
alt={alt}
514+
width={toNumber(width, 1200)}
515+
height={toNumber(height, 630)}
516+
className={cn('max-w-[calc(min(700px,100%))]', className)}
517+
style={{height: 'auto', ...style}}
518+
{...rest}
519+
/>
520+
);
491521
}
492522

493523
export const MDXComponents = {
@@ -504,7 +534,7 @@ export const MDXComponents = {
504534
h5: H5,
505535
hr: Divider,
506536
a: Link,
507-
img: Image,
537+
img: MdxImage,
508538
BlogCard,
509539
code: InlineCode,
510540
pre: CodeBlock,

0 commit comments

Comments
 (0)