Skip to content

Commit 79adff0

Browse files
authored
Merge pull request #2 from alexnault/next
2 parents 095c790 + 36d7999 commit 79adff0

File tree

8 files changed

+212
-149
lines changed

8 files changed

+212
-149
lines changed

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ name: CI
55

66
on:
77
pull_request:
8-
branches: [master]
8+
branches: [master, next]
99

1010
jobs:
1111
build-and-test:

.github/workflows/publish.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ name: Test and publish package
55

66
on:
77
push:
8-
branches: [master]
8+
branches: [master, next]
99

1010
jobs:
1111
test:

.releaserc.json

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
{
2+
"branches": [
3+
"master",
4+
{ "name": "next", "channel": "next", "prerelease": "next" }
5+
],
26
"plugins": [
37
[
48
"@semantic-release/commit-analyzer",

README.md

+74-54
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,32 @@
1-
<img src="https://github.com/alexnault/react-headings/raw/master/assets/react-headings.png" width="100%" alt="React Headings" />
1+
![React Headings Logo](https://github.com/alexnault/react-headings/raw/master/assets/react-headings.png)
22

33
# React Headings ![npm](https://img.shields.io/npm/v/react-headings?style=flat-square) ![size](https://img.shields.io/bundlephobia/minzip/react-headings?style=flat-square)
44

55
> Never worry about using the wrong heading level (`h1`, `h2`, etc.) in complex React apps!
66
77
React-headings maintains the current heading level and prevents skipping levels no matter your component structure, [as required by WCAG](https://www.w3.org/WAI/tutorials/page-structure/headings/).
88

9-
## Basic usage
9+
## Table of contents
1010

11-
```jsx
12-
import React from "react";
13-
import { H, Level } from "react-headings";
11+
- [Demos](#demos)
12+
- [Highlights](#highlights)
13+
- [Installation](#installation)
14+
- [Examples](#examples)
1415

15-
function ParentComponent() {
16-
return (
17-
<div>
18-
<H>My heading (hx)</H>
19-
<Level>
20-
<H>My subheading (hx+1)</H>
21-
<p>...</p>
22-
<H>My subheading (hx+1)</H>
23-
<Level>
24-
<H>My subsubheading (hx+2)</H>
25-
<p>...</p>
26-
<ChildComponent />
27-
</Level>
28-
</Level>
29-
</div>
30-
);
31-
}
16+
## Demos
3217

33-
function ChildComponent() {
34-
return (
35-
<div>
36-
<H>My heading (hy)</H>
37-
<Level>
38-
<H>My subheading (hy+1)</H>
39-
<p>...</p>
40-
</Level>
41-
</div>
42-
)
43-
}
44-
```
18+
- [Minimal](https://codesandbox.io/s/react-headings-minimal-4temt?file=/src/Demo.js)
19+
- [Custom component](https://codesandbox.io/s/react-headings-custom-component-l4bjb?file=/src/Demo.js)
20+
- [Advanced structure](https://codesandbox.io/s/react-headings-advanced-structure-uxk4p?file=/src/Demo.js)
4521

46-
## Features
22+
## Highlights
4723

48-
- Flexible (no component lock-in)
24+
- Flexible
4925
- Focused on developer experience
26+
- Fully tested
5027
- Typed with TypeScript
5128
- Works with component libraries (Material UI, etc.)
52-
- Fully tested
53-
- Zero dependencies
29+
- Supports server-side rendering
5430
- Tiny (<1 kB minified + gzipped)
5531
- Follows [semantic versioning](https://semver.org/)
5632

@@ -62,50 +38,94 @@ npm install react-headings
6238
yarn add react-headings
6339
```
6440

65-
## More examples
41+
## Examples
42+
43+
### Basic usage
44+
45+
```jsx
46+
import React from "react";
47+
import { H, Section } from "react-headings";
48+
49+
function ParentComponent() {
50+
return (
51+
<Section component={<H>My hx</H>}>
52+
<Section component={<H>My hx+1</H>}>
53+
<p>...</p>
54+
</Section>
55+
<Section component={<H>My hx+1</H>}>
56+
<ChildComponent />
57+
</Section>
58+
</Section>
59+
);
60+
}
61+
62+
function ChildComponent() {
63+
return (
64+
<Section component={<H>My hx+2</H>}>
65+
<p>...</p>
66+
</Section>
67+
);
68+
}
69+
```
6670

6771
### Custom component
6872

69-
`H` exposes a `render` prop to render a custom component based on the current level.
70-
Note: `render` as precedence over `children`.
73+
You can render custom headings anywhere by using either the `useLevel` hook or the `H` component.
74+
75+
- With the `useLevel` hook:
7176

7277
```jsx
7378
import React from "react";
74-
import { H, Level } from "react-headings";
75-
import { Typography } from "@material-ui/core";
79+
import { useLevel } from "react-headings";
7680

77-
function MyComponent() {
81+
function App() {
82+
const { Component, level } = useLevel();
83+
84+
return <Component>This is a h{level}</Component>;
85+
}
86+
```
87+
88+
- With the `H` component:
89+
90+
```jsx
91+
import React from "react";
92+
import { H } from "react-headings";
93+
94+
function App() {
7895
return (
7996
<H
8097
render={({ Component, level }) => (
81-
<Typography component={Component}>This is a h{level}</Typography>
98+
<Component>This is a h{level}</Component>
8299
)}
83100
/>
84101
);
85102
}
86103
```
87104

88-
### `useLevel` hook
105+
*Note: `render` as precedence over `children`.*
106+
107+
### Using component librairies
108+
109+
Here's an example with [Material UI](https://material-ui.com/api/typography/):
89110

90111
```jsx
91112
import React from "react";
92113
import { useLevel } from "react-headings";
114+
import { Typography } from "@material-ui/core";
93115

94-
function MyComponent() {
95-
const level = useLevel();
116+
function MyHeading(props) {
117+
const { Component } = useLevel();
96118

97-
return <div>Current level is {level}</div>;
119+
return <Typography component={Component} {...props} />;
98120
}
99121
```
100122

123+
Leveraging `Component` and `level` from the context should make implementing other librairies pretty straightforward.
124+
101125
## Changelog
102126

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

105129
## Contributing
106130

107-
Found a bug, have a question or looking to improve react-headings? Open an [issue](https://github.com/alexnault/react-headings/issues/new) or a [PR](https://github.com/alexnault/react-headings/fork)!
108-
109-
## License
110-
111-
This project is under the [MIT license](/LICENSE).
131+
Found a bug, have a question or looking to improve react-headings? Open an [issue](https://github.com/alexnault/react-headings/issues/new), start a [discussion](https://github.com/alexnault/react-headings/discussions/new) or submit a [PR](https://github.com/alexnault/react-headings/fork)!

assets/react-headings.png

-12.4 KB
Loading

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
"react": "^16.8.0 || ^17.0.0"
3232
},
3333
"devDependencies": {
34-
"@semantic-release/commit-analyzer": "^8.0.1",
3534
"@testing-library/react": "^11.2.2",
3635
"@types/jest": "^26.0.19",
3736
"@types/react": "^17.0.0",

0 commit comments

Comments
 (0)