Skip to content

Commit 7442ba9

Browse files
Merge pull request #403 from rightones/master
2 parents acd89ea + 4273caf commit 7442ba9

File tree

4 files changed

+53
-6
lines changed

4 files changed

+53
-6
lines changed

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as React from 'react'
44
import { AssetWrapper } from './components/asset-wrapper'
55
import { Checkbox as DefaultCheckbox } from './components/checkbox'
66
import { Header } from './components/header'
7-
import { wrapNextImage, wrapNextLink } from './next'
7+
import { wrapNextImage, wrapNextLegacyImage, wrapNextLink } from './next'
88
import {
99
type MapImageUrlFn,
1010
type MapPageUrlFn,
@@ -205,8 +205,20 @@ export function NotionContextProvider({
205205
[themeComponents]
206206
)
207207

208-
if (wrappedThemeComponents.nextImage) {
208+
if (
209+
wrappedThemeComponents.nextImage &&
210+
wrappedThemeComponents.nextLegacyImage
211+
) {
212+
console.warn(
213+
'You should not pass both nextImage and nextLegacyImage. Only nextImage component will be used.'
214+
)
209215
wrappedThemeComponents.Image = wrapNextImage(themeComponents.nextImage)
216+
} else if (wrappedThemeComponents.nextImage) {
217+
wrappedThemeComponents.Image = wrapNextImage(themeComponents.nextImage)
218+
} else if (wrappedThemeComponents.nextLegacyImage) {
219+
wrappedThemeComponents.Image = wrapNextLegacyImage(
220+
themeComponents.nextLegacyImage
221+
)
210222
}
211223

212224
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
@@ -62,6 +62,7 @@ export interface NotionComponents {
6262

6363
// optional next.js-specific overrides
6464
nextImage?: any
65+
nextLegacyImage?: any
6566
nextLink?: any
6667
}
6768

readme.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,17 +273,17 @@ Another major factor for perf comes from images hosted by Notion. They're genera
273273

274274
`NotionRenderer` also supports lazy image loading with optional low quality image placeholder previews. You can see a demo of this in practice [on this page](https://react-notion-x-demo.transitivebullsh.it/3492bd6dbaf44fe7a5cac62c5d402f06) which is using [lqip-modern](https://github.com/transitive-bullshit/lqip-modern) to pre-generate placeholder images that are inspired by Medium.com's image loading.
275275

276-
If you're using Next.js, we recommend passing `next/image` and `next/link` to the renderer as follows:
276+
If you're using Next.js, we recommend passing `next/image` or `next/legacy/image`, and `next/link` to the renderer as follows:
277277

278278
```tsx
279-
import Image from 'next/image'
279+
import Image from 'next/image' // or import Image from 'next/legacy/image' if you use legacy Image
280280
import Link from 'next/link'
281281

282282
export default ({ recordMap }) => (
283283
<NotionRenderer
284284
recordMap={recordMap}
285285
components={{
286-
nextImage: Image,
286+
nextImage: Image, // or nextLegacyImage: LegacyImage,
287287
nextLink: Link
288288
}}
289289
/>
@@ -292,6 +292,8 @@ export default ({ recordMap }) => (
292292

293293
This wraps these next.js components in a compatability layer so `NotionRenderer` can use them the same as their non-next.js equivalents `<img>` and `<a>`.
294294

295+
Note that custom image component is currently only enabled with preview image or by turning on `forceCustomImages` of `NotionRenderer`.
296+
295297
## Related
296298

297299
- [Next.js Template](https://github.com/transitive-bullshit/nextjs-notion-starter-kit) - The easiest way to deploy a self-hosted Notion site with Next.js and Vercel.

0 commit comments

Comments
 (0)