@@ -17,6 +17,7 @@ References:
17
17
- [ Highlights] ( #highlights )
18
18
- [ Installation] ( #installation )
19
19
- [ Examples] ( #examples )
20
+ - [ API] ( #api )
20
21
- [ Changelog] ( #changelog )
21
22
- [ Contributing] ( #contributing )
22
23
@@ -56,12 +57,14 @@ import MyIcon from "./MyIcon";
56
57
57
58
function ParentComponent () {
58
59
return (
59
- < Section component= {
60
- < div>
61
- < MyIcon / >
62
- < H > My hx< / H >
63
- < / div>
64
- }>
60
+ < Section
61
+ component= {
62
+ < div>
63
+ < MyIcon / >
64
+ < H > My hx< / H >
65
+ < / div>
66
+ }
67
+ >
65
68
< Section component= {< H > My hx+ 1 < / H > }>
66
69
< p> ... < / p>
67
70
< / Section>
@@ -115,8 +118,6 @@ function App() {
115
118
}
116
119
```
117
120
118
- * Note: ` render ` as precedence over ` children ` .*
119
-
120
121
### Using component librairies
121
122
122
123
Here's an example with [ Material UI] ( https://material-ui.com/api/typography/ ) :
@@ -135,6 +136,106 @@ function MyHeading(props) {
135
136
136
137
Leveraging ` Component ` and ` level ` from the context should make implementing other librairies pretty straightforward.
137
138
139
+ ## API
140
+
141
+ ### ` <H> ` component
142
+
143
+ Renders a ` <h1> ` , ` <h2> ` , ` <h3> ` , ` <h4> ` , ` <h5> ` or ` <h6> ` depending on the current level.
144
+
145
+ #### Props
146
+
147
+ | Name | Type | Required | Description |
148
+ | ---------- | ---------- | -------- | --------------------------------------------------------------- |
149
+ | ` render ` | ` function ` | No | Override with a custom heading. Has precedence over ` children ` . |
150
+ | ` children ` | ` node ` | No | The content of the heading. Usually the title. |
151
+
152
+ Any other props will be passed to the heading element.
153
+
154
+ #### Example
155
+
156
+ ``` jsx
157
+ import React from " react" ;
158
+ import { H } from " react-headings" ;
159
+
160
+ function Example1 () {
161
+ return < H > This is my title< / H > ;
162
+ }
163
+
164
+ function Example2 () {
165
+ return (
166
+ < H render= {({ level, Component }) => < Component> My h{level}< / Component> } / >
167
+ );
168
+ }
169
+ ```
170
+
171
+ ### ` <Section> ` component
172
+
173
+ Creates a new section (a heading and its level).
174
+
175
+ #### Props
176
+
177
+ | Name | Type | Required | Description |
178
+ | ----------- | ------ | -------- | ------------------------------------------------------------------------------- |
179
+ | ` component ` | ` node ` | Yes | The heading component. Can be anything but best used in combination with ` <H> ` . |
180
+ | ` children ` | ` node ` | No | The content of the new level. |
181
+
182
+ #### Example
183
+
184
+ ``` jsx
185
+ import React from " react" ;
186
+ import { Section , H } from " react-headings" ;
187
+
188
+ function Example1 () {
189
+ return (
190
+ < Section component= {< H > This is my title< / H > }> This is my content< / Section>
191
+ );
192
+ }
193
+
194
+ function Example2 () {
195
+ return (
196
+ < Section
197
+ component= {
198
+ < div>
199
+ < div>
200
+ < H > This is my title< / H >
201
+ < / div>
202
+ < / div>
203
+ }
204
+ >
205
+ This is my content
206
+ < / Section>
207
+ );
208
+ }
209
+ ```
210
+
211
+ ### ` useLevel ` hook
212
+
213
+ Returns an object containing the current ` level ` and current ` Component ` .
214
+
215
+ #### Arguments
216
+
217
+ None
218
+
219
+ #### Returns
220
+
221
+ | Name | Type | Description |
222
+ | ----------- | -------------------------------------------------------- | ------------------------------------- |
223
+ | ` level ` | ` 1 ` \| ` 2 ` \| ` 3 ` \| ` 4 ` \| ` 5 ` \| ` 6 ` | The current level. |
224
+ | ` Component ` | ` "h1" ` \| ` "h2" ` \| ` "h3" ` \| ` "h4" ` \| ` "h5" ` \| ` "h6" ` | The current component. Same as level. |
225
+
226
+ #### Example
227
+
228
+ ``` jsx
229
+ import React from " react" ;
230
+ import { useLevel } from " react-headings" ;
231
+
232
+ function Example (props ) {
233
+ const { level , Component } = useLevel ();
234
+
235
+ return < Component {... props}> This is a h{level}< / Component> ;
236
+ }
237
+ ```
238
+
138
239
## Changelog
139
240
140
241
For a list of changes and releases, see the [ changelog] ( https://github.com/alexnault/react-headings/releases ) .
0 commit comments