Skip to content

Commit ca51ba9

Browse files
committed
Add Next.js 13 new Image component support
1 parent 16d1870 commit ca51ba9

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

packages/react-notion-x/src/context.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { ExtendedRecordMap } from 'notion-types'
55
import { AssetWrapper } from './components/asset-wrapper'
66
import { Checkbox as DefaultCheckbox } from './components/checkbox'
77
import { Header } from './components/header'
8-
import { wrapNextImage, wrapNextLink } from './next'
8+
import { wrapNextImage, wrapNextLegacyImage, wrapNextLink } from './next'
99
import {
1010
MapImageUrlFn,
1111
MapPageUrlFn,
@@ -199,8 +199,20 @@ export const NotionContextProvider: React.FC<PartialNotionContext> = ({
199199
[themeComponents]
200200
)
201201

202-
if (wrappedThemeComponents.nextImage) {
202+
if (
203+
wrappedThemeComponents.nextImage &&
204+
wrappedThemeComponents.nextLegacyImage
205+
) {
206+
console.warn(
207+
'You should not pass both nextImage and nextLegacyImage. Only nextImage component will be used.'
208+
)
203209
wrappedThemeComponents.Image = wrapNextImage(themeComponents.nextImage)
210+
} else if (wrappedThemeComponents.nextImage) {
211+
wrappedThemeComponents.Image = wrapNextImage(themeComponents.nextImage)
212+
} else if (wrappedThemeComponents.nextLegacyImage) {
213+
wrappedThemeComponents.Image = wrapNextLegacyImage(
214+
themeComponents.nextLegacyImage
215+
)
204216
}
205217

206218
if (wrappedThemeComponents.nextLink) {

packages/react-notion-x/src/next.tsx

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,38 @@ export const wrapNextImage = (NextImage: any): React.FC<any> => {
1010
width,
1111
height,
1212

13+
className,
14+
15+
fill,
16+
17+
...rest
18+
}) {
19+
if (fill === 'undefined') {
20+
fill = !(width && height)
21+
}
22+
23+
return (
24+
<NextImage
25+
className={className}
26+
src={src}
27+
alt={alt}
28+
width={!fill && width && height ? width : undefined}
29+
height={!fill && width && height ? height : undefined}
30+
fill={fill}
31+
{...rest}
32+
/>
33+
)
34+
}, isEqual)
35+
}
36+
37+
export const wrapNextLegacyImage = (NextLegacyImage: any): React.FC<any> => {
38+
return React.memo(function ReactNotionXNextLegacyImage({
39+
src,
40+
alt,
41+
42+
width,
43+
height,
44+
1345
className,
1446
style,
1547

@@ -22,7 +54,7 @@ export const wrapNextImage = (NextImage: any): React.FC<any> => {
2254
}
2355

2456
return (
25-
<NextImage
57+
<NextLegacyImage
2658
className={className}
2759
src={src}
2860
alt={alt}

packages/react-notion-x/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export interface NotionComponents {
5959

6060
// optional next.js-specific overrides
6161
nextImage?: any
62+
nextLegacyImage?: any
6263
nextLink?: any
6364
}
6465

0 commit comments

Comments
 (0)