Skip to content

Commit 19db752

Browse files
authored
docs: Improve readme examples (#12)
1 parent e51b706 commit 19db752

File tree

1 file changed

+66
-37
lines changed

1 file changed

+66
-37
lines changed

README.md

Lines changed: 66 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ References:
2929

3030
## Highlights
3131

32-
- Flexible
33-
- Focused on developer experience
34-
- Fully tested
35-
- Typed with TypeScript
36-
- Works with component libraries (Material UI, etc.)
32+
- Improves SEO and accessibility
3733
- Supports server-side rendering
3834
- Under 1 kB minified & gzipped
35+
- Typed with TypeScript
36+
- Fully tested
37+
- Works with any CSS solutions (Tailwind, CSS-in-JS, etc.)
38+
- Plays nicely with component librairies (Material UI, etc.)
3939
- Follows [semantic versioning](https://semver.org/)
4040

4141
## Installation
@@ -53,73 +53,102 @@ yarn add react-headings
5353
```jsx
5454
import React from "react";
5555
import { H, Section } from "react-headings";
56-
import MyIcon from "./MyIcon";
5756

58-
function ParentComponent() {
57+
function App() {
5958
return (
60-
<Section
61-
component={
62-
<div>
63-
<MyIcon />
64-
<H>My hx</H>
65-
</div>
66-
}
67-
>
59+
<Section component={<H>My hx</H>}>
60+
<div>...</div>
61+
<div>...</div>
62+
<div>...</div>
6863
<Section component={<H>My hx+1</H>}>
69-
<p>...</p>
64+
<div>...</div>
65+
<div>...</div>
66+
<div>...</div>
7067
</Section>
68+
</Section>
69+
);
70+
}
71+
```
72+
73+
### Advanced structure
74+
75+
Child components inherit the current level of their parent:
76+
77+
```jsx
78+
import React from "react";
79+
import { H, Section } from "react-headings";
80+
81+
function ParentComponent() {
82+
return (
83+
<Section component={<H>My hx</H>}>
7184
<Section component={<H>My hx+1</H>}>
72-
<ChildComponent />
85+
<Section component={<H>My hx+2</H>}>
86+
<ChildComponent />
87+
</Section>
7388
</Section>
7489
</Section>
7590
);
7691
}
7792

7893
function ChildComponent() {
7994
return (
80-
<Section component={<H>My hx+2</H>}>
81-
<p>...</p>
95+
<Section component={<H>My hy</H>}>
96+
{/* The following heading would be a <h5> in the current context */}
97+
<Section component={<H>My hy+1</H>}>
98+
<p>...</p>
99+
</Section>
82100
</Section>
83101
);
84102
}
85103
```
86104

87-
### Custom component
88-
89-
You can render custom headings anywhere by using either the `useLevel` hook or the `H` component.
105+
### Styling
90106

91-
- With the `useLevel` hook:
107+
A heading can be styled like any ordinary `<hx>` element since it accepts all the same props:
92108

93109
```jsx
94110
import React from "react";
95-
import { useLevel } from "react-headings";
111+
import { H, Section } from "react-headings";
96112

97113
function App() {
98-
const { Component, level } = useLevel();
99-
100-
return <Component>This is a h{level}</Component>;
114+
return (
115+
<Section component={<H className="my-class">My hx</H>}>
116+
...
117+
</Section>
118+
);
101119
}
102120
```
103121

104-
- With the `H` component:
122+
### Custom heading
123+
124+
A heading can be as complex as we want:
105125

106126
```jsx
107127
import React from "react";
108-
import { H } from "react-headings";
128+
import { H, Section } from "react-headings";
129+
import MyIcon from "./MyIcon";
109130

110131
function App() {
111132
return (
112-
<H
113-
render={({ Component, level }) => (
114-
<Component>This is a h{level}</Component>
115-
)}
116-
/>
133+
<Section
134+
component={
135+
<div className="my-div">
136+
<MyIcon className="my-icon" />
137+
<H className="my-heading">My hx</H>
138+
</div>
139+
}
140+
>
141+
<div>...</div>
142+
<div>...</div>
143+
<div>...</div>
144+
</Section>
117145
);
118146
}
119147
```
120148

121149
### Using component librairies
122150

151+
Leveraging `Component` and `level` from the context allows the use of component librairies.
123152
Here's an example with [Material UI](https://material-ui.com/api/typography/):
124153

125154
```jsx
@@ -134,8 +163,6 @@ function MyHeading(props) {
134163
}
135164
```
136165

137-
Leveraging `Component` and `level` from the context should make implementing other librairies pretty straightforward.
138-
139166
## API
140167

141168
### `<H>` component
@@ -187,7 +214,9 @@ import { Section, H } from "react-headings";
187214

188215
function Example1() {
189216
return (
190-
<Section component={<H>This is my title</H>}>This is my content</Section>
217+
<Section component={<H>This is my title</H>}>
218+
This is my content
219+
</Section>
191220
);
192221
}
193222

0 commit comments

Comments
 (0)