useTheme.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = useTheme;
  7. var _useMemo = _interopRequireDefault(require("rc-util/lib/hooks/useMemo"));
  8. var _isEqual = _interopRequireDefault(require("rc-util/lib/isEqual"));
  9. var _warning = require("../../_util/warning");
  10. var _internal = require("../../theme/internal");
  11. var _useThemeKey = _interopRequireDefault(require("./useThemeKey"));
  12. function useTheme(theme, parentTheme, config) {
  13. var _a, _b;
  14. const warning = (0, _warning.devUseWarning)('ConfigProvider');
  15. const themeConfig = theme || {};
  16. const parentThemeConfig = themeConfig.inherit === false || !parentTheme ? Object.assign(Object.assign({}, _internal.defaultConfig), {
  17. hashed: (_a = parentTheme === null || parentTheme === void 0 ? void 0 : parentTheme.hashed) !== null && _a !== void 0 ? _a : _internal.defaultConfig.hashed,
  18. cssVar: parentTheme === null || parentTheme === void 0 ? void 0 : parentTheme.cssVar
  19. }) : parentTheme;
  20. const themeKey = (0, _useThemeKey.default)();
  21. if (process.env.NODE_ENV !== 'production') {
  22. const cssVarEnabled = themeConfig.cssVar || parentThemeConfig.cssVar;
  23. const validKey = !!(typeof themeConfig.cssVar === 'object' && ((_b = themeConfig.cssVar) === null || _b === void 0 ? void 0 : _b.key) || themeKey);
  24. process.env.NODE_ENV !== "production" ? warning(!cssVarEnabled || validKey, 'breaking', 'Missing key in `cssVar` config. Please upgrade to React 18 or set `cssVar.key` manually in each ConfigProvider inside `cssVar` enabled ConfigProvider.') : void 0;
  25. }
  26. return (0, _useMemo.default)(() => {
  27. var _a, _b;
  28. if (!theme) {
  29. return parentTheme;
  30. }
  31. // Override
  32. const mergedComponents = Object.assign({}, parentThemeConfig.components);
  33. Object.keys(theme.components || {}).forEach(componentName => {
  34. mergedComponents[componentName] = Object.assign(Object.assign({}, mergedComponents[componentName]), theme.components[componentName]);
  35. });
  36. const cssVarKey = `css-var-${themeKey.replace(/:/g, '')}`;
  37. const mergedCssVar = ((_a = themeConfig.cssVar) !== null && _a !== void 0 ? _a : parentThemeConfig.cssVar) && Object.assign(Object.assign(Object.assign({
  38. prefix: config === null || config === void 0 ? void 0 : config.prefixCls
  39. }, typeof parentThemeConfig.cssVar === 'object' ? parentThemeConfig.cssVar : {}), typeof themeConfig.cssVar === 'object' ? themeConfig.cssVar : {}), {
  40. key: typeof themeConfig.cssVar === 'object' && ((_b = themeConfig.cssVar) === null || _b === void 0 ? void 0 : _b.key) || cssVarKey
  41. });
  42. // Base token
  43. return Object.assign(Object.assign(Object.assign({}, parentThemeConfig), themeConfig), {
  44. token: Object.assign(Object.assign({}, parentThemeConfig.token), themeConfig.token),
  45. components: mergedComponents,
  46. cssVar: mergedCssVar
  47. });
  48. }, [themeConfig, parentThemeConfig], (prev, next) => prev.some((prevTheme, index) => {
  49. const nextTheme = next[index];
  50. return !(0, _isEqual.default)(prevTheme, nextTheme, true);
  51. }));
  52. }