react-headings 3.3.4 → 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 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
@@ -16,14 +16,26 @@ type HProps = React.DetailedHTMLProps<React.HTMLAttributes<HTMLHeadingElement>,
16
16
  * Renders a dynamic HTML heading (h1, h2, etc.) or custom component according to the current level.
17
17
  */
18
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;
19
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
- function InternalH(_a, forwardedRef) {
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 a dynamic HTML heading (h1, h2, etc.) or custom component according to the current level.
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
- exports.H = react_1.default.forwardRef(InternalH);
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 level = useLevel().level;
61
- var nextLevel = Math.min(level + 1, 6);
62
- var value = {
63
- level: nextLevel,
64
- Component: "h".concat(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(LevelContext.Provider, { value: value }, children)));
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.4",
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",