react-headings 3.3.3 → 3.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +25 -0
- package/dist/index.d.ts +19 -7
- package/dist/index.js +23 -13
- package/package.json +12 -12
package/README.md
CHANGED
|
@@ -159,6 +159,30 @@ function App() {
|
|
|
159
159
|
}
|
|
160
160
|
```
|
|
161
161
|
|
|
162
|
+
### Dialogs and portals
|
|
163
|
+
|
|
164
|
+
Content rendered elsewhere in the DOM (e.g. dialogs portalled to the end of `<body>`) can reset the heading hierarchy with the `level` prop.
|
|
165
|
+
Nested sections continue from that level.
|
|
166
|
+
|
|
167
|
+
```jsx
|
|
168
|
+
import React from "react";
|
|
169
|
+
import { Section, H } from "react-headings";
|
|
170
|
+
|
|
171
|
+
function App() {
|
|
172
|
+
return (
|
|
173
|
+
<Section component={<H>My H1</H>}>
|
|
174
|
+
<Section component={<H>My H2</H>}>
|
|
175
|
+
<Dialog>
|
|
176
|
+
<Section level={1} component={<H>My dialog title (H1)</H>}>
|
|
177
|
+
My dialog content
|
|
178
|
+
</Section>
|
|
179
|
+
</Dialog>
|
|
180
|
+
</Section>
|
|
181
|
+
</Section>
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
```
|
|
185
|
+
|
|
162
186
|
### Using component libraries
|
|
163
187
|
|
|
164
188
|
Leveraging `Component` and `level` from the context allows the use of component libraries.
|
|
@@ -218,6 +242,7 @@ Creates a new section (a heading and its level).
|
|
|
218
242
|
| ----------- | ------ | -------- | ------------------------------------------------------------------------------- |
|
|
219
243
|
| `component` | `node` | Yes | The heading component. Can be anything but best used in combination with `<H>`. |
|
|
220
244
|
| `children` | `node` | No | The content of the new level. |
|
|
245
|
+
| `level` | `1-6` | No | Renders `component` at this level instead of the current one. |
|
|
221
246
|
|
|
222
247
|
#### Example
|
|
223
248
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import React from "react";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
type Level = 1 | 2 | 3 | 4 | 5 | 6;
|
|
3
|
+
type Heading = `h${Level}`;
|
|
4
|
+
type LevelContextValue = {
|
|
5
5
|
level: Level;
|
|
6
6
|
Component: Heading;
|
|
7
7
|
};
|
|
@@ -9,21 +9,33 @@ declare type LevelContextValue = {
|
|
|
9
9
|
* Returns the current heading and level.
|
|
10
10
|
*/
|
|
11
11
|
export declare function useLevel(): LevelContextValue;
|
|
12
|
-
|
|
12
|
+
type HProps = React.DetailedHTMLProps<React.HTMLAttributes<HTMLHeadingElement>, HTMLHeadingElement> & {
|
|
13
13
|
render?: (context: LevelContextValue) => React.ReactElement;
|
|
14
14
|
};
|
|
15
15
|
/**
|
|
16
16
|
* Renders a dynamic HTML heading (h1, h2, etc.) or custom component according to the current level.
|
|
17
17
|
*/
|
|
18
|
-
export declare const H: React.ForwardRefExoticComponent<
|
|
19
|
-
|
|
18
|
+
export declare const H: React.ForwardRefExoticComponent<Omit<HProps, "ref"> & React.RefAttributes<HTMLHeadingElement>>;
|
|
19
|
+
type LevelProps = {
|
|
20
|
+
level?: Level;
|
|
21
|
+
children?: React.ReactNode;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Renders `children` 1 level down, or at `level` when provided.
|
|
25
|
+
* @param level The level to render `children` at
|
|
26
|
+
* @param children The children to render
|
|
27
|
+
*/
|
|
28
|
+
declare function Level({ level: desiredLevel, children }: LevelProps): JSX.Element;
|
|
29
|
+
type SectionProps = {
|
|
20
30
|
component: React.ReactNode;
|
|
21
31
|
children?: React.ReactNode;
|
|
32
|
+
level?: Level;
|
|
22
33
|
};
|
|
23
34
|
/**
|
|
24
35
|
* Renders `component` in the current level and `children` in the next level.
|
|
25
36
|
* @param component A component containing a heading
|
|
26
37
|
* @param children The children in the next level
|
|
38
|
+
* @param level A level to render `component` at instead of the current one (e.g. for dialogs and portals)
|
|
27
39
|
*/
|
|
28
|
-
export declare function Section({ component, children }: SectionProps): JSX.Element;
|
|
40
|
+
export declare function Section({ component, children, level, }: SectionProps): JSX.Element;
|
|
29
41
|
export {};
|
package/dist/index.js
CHANGED
|
@@ -38,33 +38,43 @@ function useLevel() {
|
|
|
38
38
|
return react_1.default.useContext(LevelContext);
|
|
39
39
|
}
|
|
40
40
|
exports.useLevel = useLevel;
|
|
41
|
-
|
|
41
|
+
/**
|
|
42
|
+
* Renders a dynamic HTML heading (h1, h2, etc.) or custom component according to the current level.
|
|
43
|
+
*/
|
|
44
|
+
exports.H = react_1.default.forwardRef(function H(_a, forwardedRef) {
|
|
42
45
|
var render = _a.render, props = __rest(_a, ["render"]);
|
|
43
46
|
var context = useLevel();
|
|
44
47
|
if (render) {
|
|
45
48
|
return render(context);
|
|
46
49
|
}
|
|
47
50
|
return react_1.default.createElement(context.Component, __assign({ ref: forwardedRef }, props));
|
|
48
|
-
}
|
|
51
|
+
});
|
|
49
52
|
/**
|
|
50
|
-
* Renders
|
|
53
|
+
* Renders `children` 1 level down, or at `level` when provided.
|
|
54
|
+
* @param level The level to render `children` at
|
|
55
|
+
* @param children The children to render
|
|
51
56
|
*/
|
|
52
|
-
|
|
57
|
+
function Level(_a) {
|
|
58
|
+
var desiredLevel = _a.level, children = _a.children;
|
|
59
|
+
var currentLevel = useLevel().level;
|
|
60
|
+
var level = desiredLevel !== null && desiredLevel !== void 0 ? desiredLevel : Math.min(currentLevel + 1, 6);
|
|
61
|
+
var value = {
|
|
62
|
+
level: level,
|
|
63
|
+
Component: "h".concat(level),
|
|
64
|
+
};
|
|
65
|
+
return (react_1.default.createElement(LevelContext.Provider, { value: value }, children));
|
|
66
|
+
}
|
|
53
67
|
/**
|
|
54
68
|
* Renders `component` in the current level and `children` in the next level.
|
|
55
69
|
* @param component A component containing a heading
|
|
56
70
|
* @param children The children in the next level
|
|
71
|
+
* @param level A level to render `component` at instead of the current one (e.g. for dialogs and portals)
|
|
57
72
|
*/
|
|
58
73
|
function Section(_a) {
|
|
59
|
-
var component = _a.component, children = _a.children;
|
|
60
|
-
var
|
|
61
|
-
var nextLevel = Math.min(level + 1, 6);
|
|
62
|
-
var value = {
|
|
63
|
-
level: nextLevel,
|
|
64
|
-
Component: "h" + nextLevel,
|
|
65
|
-
};
|
|
66
|
-
return (react_1.default.createElement(react_1.default.Fragment, null,
|
|
74
|
+
var component = _a.component, children = _a.children, level = _a.level;
|
|
75
|
+
var content = (react_1.default.createElement(react_1.default.Fragment, null,
|
|
67
76
|
component,
|
|
68
|
-
react_1.default.createElement(
|
|
77
|
+
react_1.default.createElement(Level, null, children)));
|
|
78
|
+
return level ? react_1.default.createElement(Level, { level: level }, content) : content;
|
|
69
79
|
}
|
|
70
80
|
exports.Section = Section;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-headings",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.4.0",
|
|
4
4
|
"description": "HTML headings with auto-incrementing levels for WCAG compliance.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -36,22 +36,22 @@
|
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
38
|
"@size-limit/preset-small-lib": "^7.0.8",
|
|
39
|
-
"@testing-library/react": "^13.0
|
|
40
|
-
"@types/react": "^18.
|
|
41
|
-
"@types/react-dom": "^18.
|
|
39
|
+
"@testing-library/react": "^13.4.0",
|
|
40
|
+
"@types/react": "^18.3.28",
|
|
41
|
+
"@types/react-dom": "^18.3.7",
|
|
42
42
|
"@typescript-eslint/eslint-plugin": "^4.33.0",
|
|
43
43
|
"@typescript-eslint/parser": "^4.33.0",
|
|
44
44
|
"@vitest/coverage-c8": "^0.30.1",
|
|
45
|
-
"conventional-changelog-conventionalcommits": "^
|
|
45
|
+
"conventional-changelog-conventionalcommits": "^8.0.0",
|
|
46
46
|
"eslint": "^7.32.0",
|
|
47
|
-
"eslint-config-prettier": "^8.
|
|
48
|
-
"jsdom": "^21.1.
|
|
49
|
-
"prettier": "^2.
|
|
50
|
-
"react": "^18.
|
|
51
|
-
"react-dom": "^18.
|
|
52
|
-
"semantic-release": "^
|
|
47
|
+
"eslint-config-prettier": "^8.10.2",
|
|
48
|
+
"jsdom": "^21.1.2",
|
|
49
|
+
"prettier": "^2.8.8",
|
|
50
|
+
"react": "^18.3.1",
|
|
51
|
+
"react-dom": "^18.3.1",
|
|
52
|
+
"semantic-release": "^25.0.3",
|
|
53
53
|
"size-limit": "^7.0.8",
|
|
54
|
-
"typescript": "^4.
|
|
54
|
+
"typescript": "^4.9.5",
|
|
55
55
|
"vitest": "^0.30.1"
|
|
56
56
|
}
|
|
57
57
|
}
|