UNPKG

2.64 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = ClassNameConfigurator;
7exports.useClassNamesOverride = useClassNamesOverride;
8var React = _interopRequireWildcard(require("react"));
9var _jsxRuntime = require("react/jsx-runtime");
10function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
11function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
12const defaultContextValue = {
13 disableDefaultClasses: false
14};
15const ClassNameConfiguratorContext = /*#__PURE__*/React.createContext(defaultContextValue);
16/**
17 * @ignore - internal hook.
18 *
19 * Wraps the `generateUtilityClass` function and controls how the classes are generated.
20 * Currently it only affects whether the classes are applied or not.
21 *
22 * @returns Function to be called with the `generateUtilityClass` function specific to a component to generate the classes.
23 */
24function useClassNamesOverride(generateUtilityClass) {
25 const {
26 disableDefaultClasses
27 } = React.useContext(ClassNameConfiguratorContext);
28 return slot => {
29 if (disableDefaultClasses) {
30 return '';
31 }
32 return generateUtilityClass(slot);
33 };
34}
35
36/**
37 * Allows to configure the components within to not apply any built-in classes.
38 */
39function ClassNameConfigurator(props) {
40 const {
41 disableDefaultClasses,
42 children
43 } = props;
44 const contextValue = React.useMemo(() => ({
45 disableDefaultClasses: disableDefaultClasses != null ? disableDefaultClasses : false
46 }), [disableDefaultClasses]);
47 return /*#__PURE__*/(0, _jsxRuntime.jsx)(ClassNameConfiguratorContext.Provider, {
48 value: contextValue,
49 children: children
50 });
51}
\No newline at end of file