UNPKG

@carbon/react

Version:

React components for the Carbon Design System

69 lines (64 loc) 2.17 kB
/** * 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. */ 'use strict'; var _rollupPluginBabelHelpers = require('../../_virtual/_rollupPluginBabelHelpers.js'); var PropTypes = require('prop-types'); var React = require('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; // eslint-disable-next-line @typescript-eslint/no-explicit-any -- https://github.com/carbon-design-system/carbon/issues/20452 const BaseComponentAsAny = BaseComponent; return /*#__PURE__*/React.createElement(HeadingContext.Provider, { value: Math.min(level, 6) }, /*#__PURE__*/React.createElement(BaseComponentAsAny, _rollupPluginBabelHelpers.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 }; // eslint-disable-next-line react/display-name -- https://github.com/carbon-design-system/carbon/issues/20452 const Heading = /*#__PURE__*/React.forwardRef((props, ref) => { const HeadingIntrinsic = `h${React.useContext(HeadingContext)}`; return /*#__PURE__*/React.createElement(HeadingIntrinsic, _rollupPluginBabelHelpers.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 }; exports.Heading = Heading; exports.Section = Section;