@open-pioneer/react-utils 4.0.0-dev.20250527091545 → 4.0.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/CHANGELOG.md +29 -2
- package/TitledSection.d.ts +23 -8
- package/TitledSection.js +11 -8
- package/TitledSection.js.map +1 -1
- package/package.json +4 -4
package/CHANGELOG.md
CHANGED
|
@@ -1,14 +1,41 @@
|
|
|
1
1
|
# @open-pioneer/react-utils
|
|
2
2
|
|
|
3
|
-
## 4.0.0
|
|
3
|
+
## 4.0.0
|
|
4
4
|
|
|
5
5
|
### Major Changes
|
|
6
6
|
|
|
7
7
|
- 9f074d8: Update to Chakra 3.x
|
|
8
8
|
|
|
9
|
+
### Minor Changes
|
|
10
|
+
|
|
11
|
+
- a5177fc: Allow using `SectionHeading` in the children of a `TitledSection` instead of the `title` prop.
|
|
12
|
+
This is more flexible and more readable compared to complex components in the `title` prop.
|
|
13
|
+
|
|
14
|
+
For example:
|
|
15
|
+
|
|
16
|
+
```jsx
|
|
17
|
+
<TitledSection>
|
|
18
|
+
<SectionHeading>Top level heading (H1)</SectionHeading>
|
|
19
|
+
.. Some Content ..
|
|
20
|
+
<TitledSection>
|
|
21
|
+
<Box backgroundColor="green.500">
|
|
22
|
+
<SectionHeading>Other Heading (H2)</SectionHeading>
|
|
23
|
+
</Box>
|
|
24
|
+
.. More Content ..
|
|
25
|
+
</TitledSection>
|
|
26
|
+
</TitledSection>
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Note that you should be using exactly one `<SectionHeading />` for each `<TitledSection />`.
|
|
30
|
+
|
|
9
31
|
### Patch Changes
|
|
10
32
|
|
|
11
|
-
-
|
|
33
|
+
- 9a2967c: Remove the prop `substituteHeadingLevel` from the `TitledSectionProps` interface.
|
|
34
|
+
The property was mistakenly documented, but was never implemented.
|
|
35
|
+
You can use the `<ConfigureTitledSection />` component instead.
|
|
36
|
+
- 434bd04: Bump dependencies.
|
|
37
|
+
- Updated dependencies [434bd04]
|
|
38
|
+
- @open-pioneer/core@4.0.0
|
|
12
39
|
|
|
13
40
|
## 3.1.0
|
|
14
41
|
|
package/TitledSection.d.ts
CHANGED
|
@@ -19,17 +19,12 @@ export interface TitledSectionProps {
|
|
|
19
19
|
* Children are rendered without any modifications.
|
|
20
20
|
*/
|
|
21
21
|
children?: ReactNode | undefined;
|
|
22
|
-
/**
|
|
23
|
-
* Allows to substitute the heading level that is used up from this section.
|
|
24
|
-
* This should only be used to configure the initial level
|
|
25
|
-
* or if it is actually intended to adjust the DOM structure.
|
|
26
|
-
*/
|
|
27
|
-
substituteHeadingLevel?: HeadingLevel | undefined;
|
|
28
22
|
}
|
|
29
23
|
/**
|
|
30
24
|
* Automatically manages the level of html headings such as `h1` and `h2`.
|
|
31
25
|
*
|
|
32
|
-
* Use this component's `title` property
|
|
26
|
+
* Use this component's `title` property or `<SectionHeading />` components in the children
|
|
27
|
+
* of this component instead of manual heading tags.
|
|
33
28
|
* Nested sections will automatically use the correct level for their current location.
|
|
34
29
|
*
|
|
35
30
|
* Titles are rendered as [Chakra Headings](https://chakra-ui.com/docs/components/heading) by default.
|
|
@@ -53,6 +48,26 @@ export interface TitledSectionProps {
|
|
|
53
48
|
* When using using a string for the `title` prop, you can specify the `sectionHeadingProps` to
|
|
54
49
|
* customize the properties of the automatically generated heading component.
|
|
55
50
|
*
|
|
51
|
+
* **Example:** custom section headings as children
|
|
52
|
+
*
|
|
53
|
+
* ```jsx
|
|
54
|
+
* <TitledSection>
|
|
55
|
+
* <SectionHeading>Top level heading (H1)</SectionHeading>
|
|
56
|
+
*
|
|
57
|
+
* .. Some Content ..
|
|
58
|
+
*
|
|
59
|
+
* <TitledSection>
|
|
60
|
+
* <SectionHeading>Other Heading (H2)</SectionHeading>
|
|
61
|
+
*
|
|
62
|
+
* .. More Content ..
|
|
63
|
+
* </TitledSection>
|
|
64
|
+
*
|
|
65
|
+
* </TitledSection>
|
|
66
|
+
* ```
|
|
67
|
+
*
|
|
68
|
+
* > NOTE: You should use exactly one `<SectionHeading />` for each `<TitledSection />`.
|
|
69
|
+
* > The SectionHeading can appear anywhere, for example wrapped in other layout components.
|
|
70
|
+
*
|
|
56
71
|
* **Example:** custom title rendering
|
|
57
72
|
*
|
|
58
73
|
* You can also use completely custom components for the title.
|
|
@@ -134,6 +149,6 @@ export type HeadingLevel = 1 | 2 | 3 | 4 | 5 | 6;
|
|
|
134
149
|
/**
|
|
135
150
|
* Returns the current heading level.
|
|
136
151
|
*
|
|
137
|
-
* This hook should be used in the `title` property of a {@link TitledSection}.
|
|
152
|
+
* This hook should be used in the `title` property of a {@link TitledSection} or in that component's children.
|
|
138
153
|
*/
|
|
139
154
|
export declare function useHeadingLevel(): HeadingLevel;
|
package/TitledSection.js
CHANGED
|
@@ -1,17 +1,16 @@
|
|
|
1
|
-
import { jsx, jsxs
|
|
1
|
+
import { jsx, jsxs } from 'react/jsx-runtime';
|
|
2
2
|
import { Heading } from '@chakra-ui/react';
|
|
3
3
|
import { createContext, useContext } from 'react';
|
|
4
4
|
|
|
5
|
-
const
|
|
6
|
-
const LevelContext = createContext(DEFAULT_LEVEL);
|
|
5
|
+
const LevelContext = createContext(void 0);
|
|
7
6
|
LevelContext.displayName = "LevelContext";
|
|
8
7
|
function TitledSection(props) {
|
|
9
8
|
const { title, sectionHeadingProps, children } = props;
|
|
10
|
-
const currentLevel = useContext(LevelContext);
|
|
9
|
+
const currentLevel = useContext(LevelContext) ?? 0;
|
|
11
10
|
const heading = typeof title === "string" ? /* @__PURE__ */ jsx(SectionHeading, { ...sectionHeadingProps, children: title }) : title;
|
|
12
|
-
return /* @__PURE__ */ jsxs(
|
|
11
|
+
return /* @__PURE__ */ jsxs(LevelContext, { value: currentLevel + 1, children: [
|
|
13
12
|
heading,
|
|
14
|
-
|
|
13
|
+
children
|
|
15
14
|
] });
|
|
16
15
|
}
|
|
17
16
|
function SectionHeading(props) {
|
|
@@ -21,11 +20,15 @@ function SectionHeading(props) {
|
|
|
21
20
|
return /* @__PURE__ */ jsx(Heading, { as: tag, ref, ...rest, children });
|
|
22
21
|
}
|
|
23
22
|
const ConfigureTitledSection = (props) => {
|
|
24
|
-
|
|
23
|
+
const level = props.level - 1;
|
|
24
|
+
return /* @__PURE__ */ jsx(LevelContext, { value: level, children: props.children });
|
|
25
25
|
};
|
|
26
26
|
function useHeadingLevel() {
|
|
27
27
|
const level = useContext(LevelContext);
|
|
28
|
-
|
|
28
|
+
if (level == null) {
|
|
29
|
+
throw new Error("useHeadingLevel() must be used within a <TitledSection />");
|
|
30
|
+
}
|
|
31
|
+
return Math.max(1, Math.min(level, 6));
|
|
29
32
|
}
|
|
30
33
|
function getHeadingTag(level) {
|
|
31
34
|
return `h${level}`;
|
package/TitledSection.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TitledSection.js","sources":["TitledSection.tsx"],"sourcesContent":["// SPDX-FileCopyrightText: 2023-2025 Open Pioneer project (https://github.com/open-pioneer)\n// SPDX-License-Identifier: Apache-2.0\nimport { Heading, HeadingProps } from \"@chakra-ui/react\";\nimport { FC, ReactNode, RefAttributes, createContext, useContext } from \"react\";\n\nconst
|
|
1
|
+
{"version":3,"file":"TitledSection.js","sources":["TitledSection.tsx"],"sourcesContent":["// SPDX-FileCopyrightText: 2023-2025 Open Pioneer project (https://github.com/open-pioneer)\n// SPDX-License-Identifier: Apache-2.0\nimport { Heading, HeadingProps } from \"@chakra-ui/react\";\nimport { FC, ReactNode, RefAttributes, createContext, useContext } from \"react\";\n\nconst LevelContext = createContext<number | undefined>(undefined);\nLevelContext.displayName = \"LevelContext\";\n\n/** Properties of the {@link TitledSection} component. */\nexport interface TitledSectionProps {\n /**\n * The title component.\n *\n * Strings are automatically wrapped in a {@link SectionHeading}.\n * More complex nodes should use the {@link SectionHeading} internally.\n */\n title?: string | ReactNode | undefined;\n\n /**\n * Properties for the automatically generated section heading.\n *\n * NOTE: This is only applied if `title` is a string.\n */\n sectionHeadingProps?: SectionHeadingProps | undefined;\n\n /**\n * Children are rendered without any modifications.\n */\n children?: ReactNode | undefined;\n}\n\n/**\n * Automatically manages the level of html headings such as `h1` and `h2`.\n *\n * Use this component's `title` property or `<SectionHeading />` components in the children\n * of this component instead of manual heading tags.\n * Nested sections will automatically use the correct level for their current location.\n *\n * Titles are rendered as [Chakra Headings](https://chakra-ui.com/docs/components/heading) by default.\n *\n * **Example:** Simple string headings (1, 2 and 3 will be rendered as h1, h2 and h3):\n *\n * ```jsx\n * <TitledSection title=\"1\">\n * Top level content\n *\n * <TitledSection title=\"2\" sectionHeadingProps={...}>\n * Nested content\n *\n * <TitledSection title=\"3\">\n * Deeply nested content\n * </TitledSection>\n * </TitledSection>\n * </TitledSection>\n * ```\n *\n * When using using a string for the `title` prop, you can specify the `sectionHeadingProps` to\n * customize the properties of the automatically generated heading component.\n *\n * **Example:** custom section headings as children\n *\n * ```jsx\n * <TitledSection>\n * <SectionHeading>Top level heading (H1)</SectionHeading>\n *\n * .. Some Content ..\n *\n * <TitledSection>\n * <SectionHeading>Other Heading (H2)</SectionHeading>\n *\n * .. More Content ..\n * </TitledSection>\n *\n * </TitledSection>\n * ```\n *\n * > NOTE: You should use exactly one `<SectionHeading />` for each `<TitledSection />`.\n * > The SectionHeading can appear anywhere, for example wrapped in other layout components.\n *\n * **Example:** custom title rendering\n *\n * You can also use completely custom components for the title.\n * In this case, you should ensure that you use the `SectionHeading` component within the `title` in some place.\n * The section heading does not need to be the immediate child, you can wrap it any way you want.\n *\n * ```jsx\n * <TitledSection\n * title={\n * <SectionHeading size=\"4xl\">Heading</SectionHeading>\n * }\n * >\n * Content\n * </TitledSection>\n * ```\n */\nexport function TitledSection(props: TitledSectionProps): ReactNode {\n const { title, sectionHeadingProps, children } = props;\n const currentLevel = useContext(LevelContext) ?? 0;\n const heading =\n typeof title === \"string\" ? (\n <SectionHeading {...sectionHeadingProps}>{title}</SectionHeading>\n ) : (\n title\n );\n\n return (\n <LevelContext value={currentLevel + 1}>\n {heading}\n {children}\n </LevelContext>\n );\n}\n\n/**\n * Properties supported by the {@link SectionHeading} component.\n *\n * All chakra properties are forwarded to the [Heading](https://chakra-ui.com/docs/components/heading) component.\n */\nexport interface SectionHeadingProps extends HeadingProps, RefAttributes<HTMLHeadingElement> {\n children?: ReactNode | undefined;\n}\n\n/**\n * Renders an appropriate heading tag for the current heading level (`h1` through `h6`).\n * This component should only be used as part of the `title` prop of the {@link TitledSection}.\n *\n * Headings are rendered as [Chakra Headings](https://chakra-ui.com/docs/components/heading).\n *\n * Heading levels are managed by nesting {@link TitledSection}.\n */\nexport function SectionHeading(props: SectionHeadingProps): ReactNode {\n const { children, ref, ...rest } = props;\n const level = useHeadingLevel();\n const tag = getHeadingTag(level);\n return (\n <Heading as={tag} ref={ref} {...rest}>\n {children}\n </Heading>\n );\n}\n\n/**\n * Properties for the {@link ConfigureTitledSection} component.\n */\nexport interface ConfigureTitledSectionProps {\n level: HeadingLevel;\n children?: ReactNode;\n}\n\n/**\n * Overrides the heading level for all children.\n *\n * TitledSection components nested in `children` will start with the configured level.\n * Nested TitledSections will continue to use the next appropriate heading level.\n *\n * Example: Changing the global heading level for your application\n *\n * If your application is embedded into another site, it should often not use the `h1` tag\n * but start with a higher heading level instead. To achieve that, simply wrap your application\n * with `<ConfigureTitledSectionProps>`. No other code changes are necessary:\n *\n * ```jsx\n * <ConfigureTitledSection level={2}>\n * <TheRestOfYourApplication />\n * </ConfigureTitledSection>\n * ```\n *\n * Example: Embedding a widget\n *\n * Given a `<Widget />` (that we cannot easily change) that internally uses `TitledSection`,\n * this is how we can embed it with a custom heading level.\n * This can be appropriate in some circumstances, for example when the react component tree does not match the DOM\n * tree (portals etc.).\n *\n * ```jsx\n * <ConfigureTitledSection level={5}>\n * <Widget />\n * </ConfigureTitledSection>\n * ```\n *\n * The headings used by `Widget` will start with `h5`.\n *\n */\nexport const ConfigureTitledSection: FC<ConfigureTitledSectionProps> = (props) => {\n const level = props.level - 1; // TitledSection increments by 1.\n return <LevelContext value={level}>{props.children}</LevelContext>;\n};\n\n/** The level of a html heading. */\nexport type HeadingLevel = 1 | 2 | 3 | 4 | 5 | 6;\n\n/**\n * Returns the current heading level.\n *\n * This hook should be used in the `title` property of a {@link TitledSection} or in that component's children.\n */\nexport function useHeadingLevel(): HeadingLevel {\n const level = useContext(LevelContext);\n if (level == null) {\n throw new Error(\"useHeadingLevel() must be used within a <TitledSection />\");\n }\n return Math.max(1, Math.min(level, 6)) as HeadingLevel;\n}\n\nfunction getHeadingTag(level: HeadingLevel): `h${HeadingLevel}` {\n return `h${level}`;\n}\n"],"names":[],"mappings":";;;;AAKA,MAAM,YAAA,GAAe,cAAkC,MAAS,CAAA;AAChE,YAAA,CAAa,WAAc,GAAA,cAAA;AAyFpB,SAAS,cAAc,KAAsC,EAAA;AAChE,EAAA,MAAM,EAAE,KAAA,EAAO,mBAAqB,EAAA,QAAA,EAAa,GAAA,KAAA;AACjD,EAAM,MAAA,YAAA,GAAe,UAAW,CAAA,YAAY,CAAK,IAAA,CAAA;AACjD,EAAM,MAAA,OAAA,GACF,OAAO,KAAU,KAAA,QAAA,uBACZ,cAAgB,EAAA,EAAA,GAAG,mBAAsB,EAAA,QAAA,EAAA,KAAA,EAAM,CAEhD,GAAA,KAAA;AAGR,EAAA,uBACK,IAAA,CAAA,YAAA,EAAA,EAAa,KAAO,EAAA,YAAA,GAAe,CAC/B,EAAA,QAAA,EAAA;AAAA,IAAA,OAAA;AAAA,IACA;AAAA,GACL,EAAA,CAAA;AAER;AAmBO,SAAS,eAAe,KAAuC,EAAA;AAClE,EAAA,MAAM,EAAE,QAAA,EAAU,GAAK,EAAA,GAAG,MAAS,GAAA,KAAA;AACnC,EAAA,MAAM,QAAQ,eAAgB,EAAA;AAC9B,EAAM,MAAA,GAAA,GAAM,cAAc,KAAK,CAAA;AAC/B,EAAA,2BACK,OAAQ,EAAA,EAAA,EAAA,EAAI,KAAK,GAAW,EAAA,GAAG,MAC3B,QACL,EAAA,CAAA;AAER;AA4Ca,MAAA,sBAAA,GAA0D,CAAC,KAAU,KAAA;AAC9E,EAAM,MAAA,KAAA,GAAQ,MAAM,KAAQ,GAAA,CAAA;AAC5B,EAAA,uBAAQ,GAAA,CAAA,YAAA,EAAA,EAAa,KAAO,EAAA,KAAA,EAAQ,gBAAM,QAAS,EAAA,CAAA;AACvD;AAUO,SAAS,eAAgC,GAAA;AAC5C,EAAM,MAAA,KAAA,GAAQ,WAAW,YAAY,CAAA;AACrC,EAAA,IAAI,SAAS,IAAM,EAAA;AACf,IAAM,MAAA,IAAI,MAAM,2DAA2D,CAAA;AAAA;AAE/E,EAAA,OAAO,KAAK,GAAI,CAAA,CAAA,EAAG,KAAK,GAAI,CAAA,KAAA,EAAO,CAAC,CAAC,CAAA;AACzC;AAEA,SAAS,cAAc,KAAyC,EAAA;AAC5D,EAAA,OAAO,IAAI,KAAK,CAAA,CAAA;AACpB;;;;"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"type": "module",
|
|
3
3
|
"name": "@open-pioneer/react-utils",
|
|
4
|
-
"version": "4.0.0
|
|
4
|
+
"version": "4.0.0",
|
|
5
5
|
"description": "Contains common react utilities used by the open pioneer trails project",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"open-pioneer-trails"
|
|
@@ -14,10 +14,10 @@
|
|
|
14
14
|
"directory": "src/packages/react-utils"
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@chakra-ui/react": "^3.19.
|
|
18
|
-
"classnames": "^2.
|
|
17
|
+
"@chakra-ui/react": "^3.19.2",
|
|
18
|
+
"classnames": "^2.5.1",
|
|
19
19
|
"react": "^19.1.0",
|
|
20
|
-
"@open-pioneer/core": "4.0.0
|
|
20
|
+
"@open-pioneer/core": "^4.0.0"
|
|
21
21
|
},
|
|
22
22
|
"exports": {
|
|
23
23
|
"./package.json": "./package.json",
|