@carbon/react
Version:
React components for the Carbon Design System
49 lines (47 loc) • 1.39 kB
JavaScript
/**
* Copyright IBM Corp. 2016, 2026
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
import React from "react";
import PropTypes from "prop-types";
import { jsx } from "react/jsx-runtime";
//#region src/components/Heading/index.tsx
/**
* Copyright IBM Corp. 2016, 2023
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
const HeadingContext = React.createContext(1);
const Section = React.forwardRef(function Section({ as: BaseComponent = "section", level: levelOverride, ...rest }, ref) {
const parentLevel = React.useContext(HeadingContext);
const level = levelOverride ?? parentLevel + 1;
const BaseComponentAsAny = BaseComponent;
return /* @__PURE__ */ jsx(HeadingContext.Provider, {
value: Math.min(level, 6),
children: /* @__PURE__ */ jsx(BaseComponentAsAny, {
ref,
...rest
})
});
});
Section.propTypes = {
as: PropTypes.elementType,
children: PropTypes.node,
className: PropTypes.string,
level: PropTypes.number
};
const Heading = React.forwardRef((props, ref) => {
return /* @__PURE__ */ jsx(`h${React.useContext(HeadingContext)}`, {
ref,
...props
});
});
Heading.propTypes = {
children: PropTypes.node,
className: PropTypes.string
};
//#endregion
export { Heading, Section };