@mskcc/carbon-react
Version:
Carbon react components for the MSKCC DSM
71 lines (67 loc) • 2.24 kB
JavaScript
/**
* MSKCC 2021, 2024
*/
import { extends as _extends } from '../../_virtual/_rollupPluginBabelHelpers.js';
import React__default from 'react';
import PropTypes from 'prop-types';
import cx from 'classnames';
import { usePrefix } from '../../internal/usePrefix.js';
import { LayerContext } from './LayerContext.js';
import { levels, MIN_LEVEL, MAX_LEVEL } from './LayerLevel.js';
/**
* A custom hook that will return information about the current layer. A common
* field to pull from this is the `level` for the layer that the component that
* calls this hook is currently in
*/
function useLayer() {
const level = React__default.useContext(LayerContext);
return {
level
};
}
const LayerRenderFunction = /*#__PURE__*/React__default.forwardRef(function Layer(_ref, ref) {
let {
as = 'div',
className: customClassName,
children,
level: overrideLevel,
...rest
} = _ref;
const contextLevel = React__default.useContext(LayerContext);
const level = overrideLevel ?? contextLevel;
const prefix = usePrefix();
const className = cx(`${prefix}--layer-${levels[level]}`, customClassName);
// The level should be between MIN_LEVEL and MAX_LEVEL
const value = Math.max(MIN_LEVEL, Math.min(level + 1, MAX_LEVEL));
const BaseComponent = as;
return /*#__PURE__*/React__default.createElement(LayerContext.Provider, {
value: value
}, /*#__PURE__*/React__default.createElement(BaseComponent, _extends({
ref: ref
}, rest, {
className: className
}), children));
});
LayerRenderFunction.displayName = 'Layer';
LayerRenderFunction.propTypes = {
/**
* Specify a custom component or element to be rendered as the top-level
* element in the component
*/
as: PropTypes.oneOfType([PropTypes.func, PropTypes.string, PropTypes.elementType]),
/**
* Provide child elements to be rendered inside of `Theme`
*/
children: PropTypes.node,
/**
* Provide a custom class name to be used on the outermost element rendered by
* the component
*/
className: PropTypes.string,
/**
* Specify the layer level and override any existing levels based on hierarchy
*/
level: PropTypes.oneOf([0, 1, 2])
};
const Layer = LayerRenderFunction;
export { Layer, useLayer };