Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/wild-frogs-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphcms/rich-text-react-renderer': patch
---

docs: add examples for next image and gatsby link
107 changes: 91 additions & 16 deletions packages/react-renderer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ const content = {
],
};

const App = () => {
function App() {
return <RichText content={content} />;
};
}
```

The content from the example above will render:
Expand All @@ -55,7 +55,11 @@ By default, the elements won't have any styling, despite the `IFrame`, which we
```tsx
import { RichText } from '@graphcms/rich-text-react-renderer';

const App = () => {
const content = {
/* ... */
};

function App() {
return (
<div>
<RichText
Expand All @@ -67,7 +71,7 @@ const App = () => {
/>
</div>
);
};
}
```

Below you can check the full list of elements you can customize, alongside the props available for each of them.
Expand Down Expand Up @@ -157,20 +161,10 @@ import Link from 'next/link';
import { RichText } from '@graphcms/rich-text-react-renderer';

const content = {
children: [
{
type: 'paragraph',
children: [
{
bold: true,
text: 'Hello World!',
},
],
},
],
/* ... */
};

const App = () => {
function App() {
return (
<RichText
content={content}
Expand Down Expand Up @@ -198,9 +192,90 @@ const App = () => {
}}
/>
);
}
```

#### Next.js Image component

```js
import Image from 'next/image';
import { RichText } from '@graphcms/rich-text-react-renderer';

const content = {
/* ... */
};

function App() {
return (
<RichText
content={content}
renderers={{
img: ({ src, altText, height, width }) => (
<Image
src={src}
alt={altText}
height={height}
width={width}
objectFit="cover"
/>
),
}}
/>
);
}
```

Since the images are in the GraphCMS CDN, you need to specify our domain in the `next.config.js` file. For more information, check [this guide](https://nextjs.org/docs/basic-features/image-optimization#domains).

```js
module.exports = {
images: {
domains: ['media.graphcms.com'],
},
};
```

#### Gatsby Link component

```js
import { Link } from 'gatsby';
import { RichText } from '@graphcms/rich-text-react-renderer';

const content = {
/* ... */
};

function App() {
return (
<RichText
content={content}
renderers={{
a: ({ children, openInNewTab, href, rel, ...rest }) => {
if (href.match(/^https?:\/\/|^\/\//i)) {
return (
<a
href={href}
target={openInNewTab ? '_blank' : '_self'}
rel={rel || 'noopener noreferrer'}
{...rest}
>
{children}
</a>
);
}

return (
<Link to={href} {...rest}>
{children}
</Link>
);
},
}}
/>
);
}
```

## 📝 License

Licensed under the MIT License.
Expand Down