Skip to content

Commit e15f227

Browse files
authored
doc: add usage guide to readme (#4)
* chore: add usage guide to readme * Fix code examples errors
1 parent 8627a24 commit e15f227

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

README.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,93 @@ yarn add @strapi/blocks-react-renderer react react-dom
1313
```sh
1414
npm install @strapi/blocks-react-renderer react react-dom
1515
```
16+
17+
## Basic usage
18+
19+
After fetching your Strapi content, you can use the BlocksRenderer component to render the data from a blocks attribute. Pass the array of blocks coming from your Strapi API to the `content` prop:
20+
21+
```jsx
22+
import { BlocksRenderer } from '@strapi/blocks-react-renderer';
23+
24+
// Content should come from your Strapi API
25+
const content = [
26+
{
27+
type: 'paragraph',
28+
children: [{ type: 'text', text: 'A simple paragraph' }],
29+
},
30+
];
31+
32+
const App = () => {
33+
return <BlocksRenderer content={content} />;
34+
};
35+
```
36+
37+
## Custom components
38+
39+
You can provide your own React components to the renderer, both for blocks and modifier. They will be merged with the default components, so you can override only the ones you need.
40+
41+
- **Blocks** are full-width elements, usually at the root of the content. The available options are:
42+
- paragraph
43+
- heading
44+
- list
45+
- quote
46+
- code
47+
- image
48+
- link
49+
- **Modifiers** are inline elements, used to change the appearance of fragments of text within a block. The available options are:
50+
- bold
51+
- italic
52+
- underline
53+
- strikethrough
54+
- code
55+
56+
To provide your own components, pass an object to the `blocks` and `modifiers` props of the renderer. For each type, the value should be a React component that will receive the props of the block or modifier. Make sure to always render the children, so that the nested blocks and modifiers are rendered as well.
57+
58+
```jsx
59+
import { BlocksRenderer } from '@strapi/blocks-react-renderer';
60+
61+
// Content should come from your Strapi API
62+
const content = [
63+
{
64+
type: 'paragraph',
65+
children: [{ type: 'text', text: 'A simple paragraph' }],
66+
},
67+
];
68+
69+
const App = () => {
70+
return (
71+
<BlocksRenderer
72+
content={content}
73+
blocks={{
74+
// You can use the default components to set class names...
75+
paragraph: ({ children }) => <p className="text-neutral900 max-w-prose">{children}</p>,
76+
// ...or point to a design system
77+
heading: ({ children, level }) => {
78+
switch (level) {
79+
case 1:
80+
return <Typography variant="h1">{children}</h1>
81+
case 2:
82+
return <Typography variant="h2">{children}</h2>
83+
case 3:
84+
return <Typography variant="h3">{children}</h3>
85+
case 4:
86+
return <Typography variant="h4">{children}</h4>
87+
case 5:
88+
return <Typography variant="h5">{children}</h5>
89+
case 6:
90+
return <Typography variant="h6">{children}</h6>
91+
default:
92+
return <Typography variant="h1">{children}</h1>
93+
}
94+
},
95+
// For links, you may want to use the component from your router or framework
96+
link: ({ children, url }) => <Link to={url}>{children}</Link>,
97+
}}
98+
modifiers={{
99+
bold: ({ children }) => <strong>{children}</strong>,
100+
italic: ({ children }) => <span className="italic">{children}</span>,
101+
}}
102+
/>
103+
);
104+
};
105+
```

0 commit comments

Comments
 (0)