@carbon/ibm-products
Version:
Carbon for IBM Products
82 lines (75 loc) • 2.8 kB
JavaScript
/**
* Copyright IBM Corp. 2020, 2025
*
* 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 React__default, { useState, useRef } from 'react';
import { ChevronDown } from '@carbon/react/icons';
import PropTypes from '../../_virtual/index.js';
import cx from 'classnames';
import { getDevtoolsProps } from '../../global/js/utils/devtools.js';
import { pkg } from '../../settings.js';
import uuidv4 from '../../global/js/utils/uuidv4.js';
var _ChevronDown;
// The block part of our conventional BEM class names (blockClass__E--M).
const blockClass = `${pkg.prefix}--non-linear-reading`;
const componentName = 'NonLinearReading';
/**
* Use non-linear reading when space is limited to share a
* brief, at-a-glance, summary of a concept that may require
* more explanation for some users.
*/
let NonLinearReading = /*#__PURE__*/React__default.forwardRef((_ref, ref) => {
let {
children,
className,
definition,
theme = 'light',
...rest
} = _ref;
const [isOpen, setOpen] = useState(false);
const contentId = useRef(uuidv4()).current;
const handleToggle = () => {
setOpen(prevState => !prevState);
};
return /*#__PURE__*/React__default.createElement("span", _extends({}, rest, {
className: cx(blockClass, `${blockClass}__${theme}`, className),
ref: ref
}, getDevtoolsProps(componentName)), ' ', /*#__PURE__*/React__default.createElement("button", {
type: "button",
"aria-controls": contentId,
"aria-expanded": isOpen,
className: cx(`${blockClass}__keyword`, [isOpen ? [`${blockClass}__keyword-open`] : [`${blockClass}__keyword-closed`]]),
onClick: handleToggle
}, children, _ChevronDown || (_ChevronDown = /*#__PURE__*/React__default.createElement(ChevronDown, {
size: 16
}))), ' ', /*#__PURE__*/React__default.createElement("span", {
id: contentId,
className: `${blockClass}__body`,
hidden: !isOpen
}, isOpen && definition), ' ');
});
// Return a placeholder if not released and not enabled by feature flag
NonLinearReading = pkg.checkComponentEnabled(NonLinearReading, componentName);
NonLinearReading.displayName = componentName;
NonLinearReading.propTypes = {
/**
* The keyword of the component appears as a pill.
*/
children: PropTypes.node.isRequired,
/**
* Provide an optional class to be applied to the containing node.
*/
className: PropTypes.string,
/**
* The content that appears when the keyword is toggled open.
*/
definition: PropTypes.node.isRequired,
/**
* Determines the theme of the component.
*/
theme: PropTypes.oneOf(['light', 'dark'])
};
export { NonLinearReading };