@carbon/react
Version:
React components for the Carbon Design System
64 lines (60 loc) • 1.84 kB
JavaScript
/**
* 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.
*/
import { extends as _extends } from '../../_virtual/_rollupPluginBabelHelpers.js';
import PropTypes from 'prop-types';
import React from 'react';
const HeadingContext = /*#__PURE__*/React.createContext(1);
const Section = /*#__PURE__*/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__*/React.createElement(HeadingContext.Provider, {
value: Math.min(level, 6)
}, /*#__PURE__*/React.createElement(BaseComponentAsAny, _extends({
ref: ref
}, rest)));
});
Section.propTypes = {
/**
* Provide an alternative tag or component to use instead of the default
* <section> element
*/
as: PropTypes.elementType,
/**
* Specify the content that will be placed in the component
*/
children: PropTypes.node,
/**
* Specify a class name for the outermost node of the component
*/
className: PropTypes.string,
/**
* Overrides the level of the section
*/
level: PropTypes.number
};
const Heading = /*#__PURE__*/React.forwardRef(function Heading(props, ref) {
const HeadingIntrinsic = `h${React.useContext(HeadingContext)}`;
return /*#__PURE__*/React.createElement(HeadingIntrinsic, _extends({
ref: ref
}, props));
});
Heading.propTypes = {
/**
* Specify the content that will be placed in the component
*/
children: PropTypes.node,
/**
* Specify a class name for the outermost node of the component
*/
className: PropTypes.string
};
export { Heading, Section };