ConfigProvider.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __export = (target, all) => {
  6. for (var name2 in all)
  7. __defProp(target, name2, { get: all[name2], enumerable: true });
  8. };
  9. var __copyProps = (to, from, except, desc) => {
  10. if (from && typeof from === "object" || typeof from === "function") {
  11. for (let key of __getOwnPropNames(from))
  12. if (!__hasOwnProp.call(to, key) && key !== except)
  13. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  14. }
  15. return to;
  16. };
  17. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  18. var stdin_exports = {};
  19. __export(stdin_exports, {
  20. CONFIG_PROVIDER_KEY: () => CONFIG_PROVIDER_KEY,
  21. configProviderProps: () => configProviderProps,
  22. default: () => stdin_default
  23. });
  24. module.exports = __toCommonJS(stdin_exports);
  25. var import_vue = require("vue");
  26. var import_vue2 = require("vue");
  27. var import_utils = require("../utils");
  28. var import_use_global_z_index = require("../composables/use-global-z-index");
  29. const [name, bem] = (0, import_utils.createNamespace)("config-provider");
  30. const CONFIG_PROVIDER_KEY = Symbol(name);
  31. const configProviderProps = {
  32. tag: (0, import_utils.makeStringProp)("div"),
  33. theme: (0, import_utils.makeStringProp)("light"),
  34. zIndex: Number,
  35. themeVars: Object,
  36. themeVarsDark: Object,
  37. themeVarsLight: Object,
  38. iconPrefix: String
  39. };
  40. function mapThemeVarsToCSSVars(themeVars) {
  41. const cssVars = {};
  42. Object.keys(themeVars).forEach((key) => {
  43. cssVars[`--van-${(0, import_utils.kebabCase)(key)}`] = themeVars[key];
  44. });
  45. return cssVars;
  46. }
  47. var stdin_default = (0, import_vue2.defineComponent)({
  48. name,
  49. props: configProviderProps,
  50. setup(props, {
  51. slots
  52. }) {
  53. const style = (0, import_vue2.computed)(() => mapThemeVarsToCSSVars((0, import_utils.extend)({}, props.themeVars, props.theme === "dark" ? props.themeVarsDark : props.themeVarsLight)));
  54. if (import_utils.inBrowser) {
  55. const addTheme = () => {
  56. document.documentElement.classList.add(`van-theme-${props.theme}`);
  57. };
  58. const removeTheme = (theme = props.theme) => {
  59. document.documentElement.classList.remove(`van-theme-${theme}`);
  60. };
  61. (0, import_vue2.watch)(() => props.theme, (newVal, oldVal) => {
  62. if (oldVal) {
  63. removeTheme(oldVal);
  64. }
  65. addTheme();
  66. }, {
  67. immediate: true
  68. });
  69. (0, import_vue2.onActivated)(addTheme);
  70. (0, import_vue2.onDeactivated)(removeTheme);
  71. (0, import_vue2.onBeforeUnmount)(removeTheme);
  72. }
  73. (0, import_vue2.provide)(CONFIG_PROVIDER_KEY, props);
  74. (0, import_vue2.watchEffect)(() => {
  75. if (props.zIndex !== void 0) {
  76. (0, import_use_global_z_index.setGlobalZIndex)(props.zIndex);
  77. }
  78. });
  79. return () => (0, import_vue.createVNode)(props.tag, {
  80. "class": bem(),
  81. "style": style.value
  82. }, {
  83. default: () => {
  84. var _a;
  85. return [(_a = slots.default) == null ? void 0 : _a.call(slots)];
  86. }
  87. });
  88. }
  89. });