@mskcc/carbon-react
Version:
Carbon react components for the MSKCC DSM
99 lines (94 loc) • 2.8 kB
JavaScript
/**
* MSKCC 2021, 2024
*/
import { extends as _extends } from '../../_virtual/_rollupPluginBabelHelpers.js';
import cx from 'classnames';
import PropTypes from 'prop-types';
import React__default, { useMemo } from 'react';
import { LayerContext } from '../Layer/LayerContext.js';
const ThemeContext = /*#__PURE__*/React__default.createContext({
theme: 'light'
});
function GlobalTheme(_ref) {
let {
children,
theme
} = _ref;
const value = useMemo(() => {
document.documentElement.classList.toggle('msk-theme--light', theme === 'light');
document.documentElement.classList.toggle('msk-theme--dark', theme === 'dark');
return {
theme
};
}, [theme]);
return /*#__PURE__*/React__default.createElement(ThemeContext.Provider, {
value: value
}, children);
}
GlobalTheme.propTypes = {
/**
* Provide child elements to be rendered inside of `GlobalTheme`, this is
* typically the root of your app
*/
children: PropTypes.node,
/**
* Specify the global theme for your app
*/
// theme: PropTypes.oneOf(['white', 'g10', 'g90', 'g100']),
theme: PropTypes.oneOf(['light', 'dark'])
};
/**
* Specify the theme to be applied to a page, or a region in a page
*/
function Theme(_ref2) {
let {
as: BaseComponent = 'div',
className: customClassName,
theme,
...rest
} = _ref2;
const className = cx(customClassName, 'msk-theme--parent');
const value = useMemo(() => {
document.documentElement.classList.toggle('msk-theme--light', theme === 'light');
document.documentElement.classList.toggle('msk-theme--dark', theme === 'dark');
return {
theme
};
}, [theme]);
const BaseComponentAsAny = BaseComponent;
return /*#__PURE__*/React__default.createElement(ThemeContext.Provider, {
value: value
}, /*#__PURE__*/React__default.createElement(LayerContext.Provider, {
value: 1
}, /*#__PURE__*/React__default.createElement(BaseComponentAsAny, _extends({}, rest, {
className: className
}))));
}
Theme.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 theme
*/
// theme: PropTypes.oneOf(['white', 'g10', 'g90', 'g100']),
theme: PropTypes.oneOf(['light', 'dark'])
};
/**
* Get access to the current theme
*/
function useTheme() {
return React__default.useContext(ThemeContext);
}
export { GlobalTheme, Theme, ThemeContext, useTheme };