Never worry about using the wrong heading level (
h1
,h2
, etc.) in complex React apps!
React-headings maintains the proper hierarchy of headings for improved accessibility and SEO, no matter the component structure, while you keep full control of what's rendered.
References:
- Flexible
- Focused on developer experience
- Fully tested
- Typed with TypeScript
- Works with component libraries (Material UI, etc.)
- Supports server-side rendering
- Under 1 kB minified & gzipped
- Follows semantic versioning
npm install react-headings
# or
yarn add react-headings
import React from "react";
import { H, Section } from "react-headings";
import MyIcon from "./MyIcon";
function ParentComponent() {
return (
<Section
component={
<div>
<MyIcon />
<H>My hx</H>
</div>
}
>
<Section component={<H>My hx+1</H>}>
<p>...</p>
</Section>
<Section component={<H>My hx+1</H>}>
<ChildComponent />
</Section>
</Section>
);
}
function ChildComponent() {
return (
<Section component={<H>My hx+2</H>}>
<p>...</p>
</Section>
);
}
You can render custom headings anywhere by using either the useLevel
hook or the H
component.
- With the
useLevel
hook:
import React from "react";
import { useLevel } from "react-headings";
function App() {
const { Component, level } = useLevel();
return <Component>This is a h{level}</Component>;
}
- With the
H
component:
import React from "react";
import { H } from "react-headings";
function App() {
return (
<H
render={({ Component, level }) => (
<Component>This is a h{level}</Component>
)}
/>
);
}
Here's an example with Material UI:
import React from "react";
import { useLevel } from "react-headings";
import { Typography } from "@material-ui/core";
function MyHeading(props) {
const { Component } = useLevel();
return <Typography component={Component} {...props} />;
}
Leveraging Component
and level
from the context should make implementing other librairies pretty straightforward.
Renders a <h1>
, <h2>
, <h3>
, <h4>
, <h5>
or <h6>
depending on the current level.
Name | Type | Required | Description |
---|---|---|---|
render |
function |
No | Override with a custom heading. Has precedence over children . |
children |
node |
No | The content of the heading. Usually the title. |
Any other props will be passed to the heading element.
import React from "react";
import { H } from "react-headings";
function Example1() {
return <H>This is my title</H>;
}
function Example2() {
return (
<H render={({ level, Component }) => <Component>My h{level}</Component>} />
);
}
Creates a new section (a heading and its level).
Name | Type | Required | Description |
---|---|---|---|
component |
node |
Yes | The heading component. Can be anything but best used in combination with <H> . |
children |
node |
No | The content of the new level. |
import React from "react";
import { Section, H } from "react-headings";
function Example1() {
return (
<Section component={<H>This is my title</H>}>This is my content</Section>
);
}
function Example2() {
return (
<Section
component={
<div>
<div>
<H>This is my title</H>
</div>
</div>
}
>
This is my content
</Section>
);
}
Returns an object containing the current level
and current Component
.
None
Name | Type | Description |
---|---|---|
level |
1 | 2 | 3 | 4 | 5 | 6 |
The current level. |
Component |
"h1" | "h2" | "h3" | "h4" | "h5" | "h6" |
The current component. Same as level. |
import React from "react";
import { useLevel } from "react-headings";
function Example(props) {
const { level, Component } = useLevel();
return <Component {...props}>This is a h{level}</Component>;
}
For a list of changes and releases, see the changelog.
Found a bug, have a question or looking to improve react-headings? Open an issue, start a discussion or submit a PR!