Skip to content

Commit db15000

Browse files
authored
docs(): Add API docs (#4)
1 parent 990870d commit db15000

File tree

1 file changed

+109
-8
lines changed

1 file changed

+109
-8
lines changed

README.md

+109-8
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ References:
1717
- [Highlights](#highlights)
1818
- [Installation](#installation)
1919
- [Examples](#examples)
20+
- [API](#api)
2021
- [Changelog](#changelog)
2122
- [Contributing](#contributing)
2223

@@ -56,12 +57,14 @@ import MyIcon from "./MyIcon";
5657

5758
function ParentComponent() {
5859
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+
>
6568
<Section component={<H>My hx+1</H>}>
6669
<p>...</p>
6770
</Section>
@@ -115,8 +118,6 @@ function App() {
115118
}
116119
```
117120

118-
*Note: `render` as precedence over `children`.*
119-
120121
### Using component librairies
121122

122123
Here's an example with [Material UI](https://material-ui.com/api/typography/):
@@ -135,6 +136,106 @@ function MyHeading(props) {
135136

136137
Leveraging `Component` and `level` from the context should make implementing other librairies pretty straightforward.
137138

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+
138239
## Changelog
139240

140241
For a list of changes and releases, see the [changelog](https://github.com/alexnault/react-headings/releases).

0 commit comments

Comments
 (0)