@carbon/ibm-products
Version:
Carbon for IBM Products
82 lines (75 loc) • 2.84 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.
*/
;
var _rollupPluginBabelHelpers = require('../../_virtual/_rollupPluginBabelHelpers.js');
var React = require('react');
var icons = require('@carbon/react/icons');
var index = require('../../_virtual/index.js');
var cx = require('classnames');
var devtools = require('../../global/js/utils/devtools.js');
var settings = require('../../settings.js');
var uuidv4 = require('../../global/js/utils/uuidv4.js');
var _ChevronDown;
// The block part of our conventional BEM class names (blockClass__E--M).
const blockClass = `${settings.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.
*/
exports.NonLinearReading = /*#__PURE__*/React.forwardRef((_ref, ref) => {
let {
children,
className,
definition,
theme = 'light',
...rest
} = _ref;
const [isOpen, setOpen] = React.useState(false);
const contentId = React.useRef(uuidv4.default()).current;
const handleToggle = () => {
setOpen(prevState => !prevState);
};
return /*#__PURE__*/React.createElement("span", _rollupPluginBabelHelpers.extends({}, rest, {
className: cx(blockClass, `${blockClass}__${theme}`, className),
ref: ref
}, devtools.getDevtoolsProps(componentName)), ' ', /*#__PURE__*/React.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.createElement(icons.ChevronDown, {
size: 16
}))), ' ', /*#__PURE__*/React.createElement("span", {
id: contentId,
className: `${blockClass}__body`,
hidden: !isOpen
}, isOpen && definition), ' ');
});
// Return a placeholder if not released and not enabled by feature flag
exports.NonLinearReading = settings.pkg.checkComponentEnabled(exports.NonLinearReading, componentName);
exports.NonLinearReading.displayName = componentName;
exports.NonLinearReading.propTypes = {
/**
* The keyword of the component appears as a pill.
*/
children: index.default.node.isRequired,
/**
* Provide an optional class to be applied to the containing node.
*/
className: index.default.string,
/**
* The content that appears when the keyword is toggled open.
*/
definition: index.default.node.isRequired,
/**
* Determines the theme of the component.
*/
theme: index.default.oneOf(['light', 'dark'])
};